Class AuthenticationManager

java.lang.Object
g1701_1800.s1797_design_authentication_manager.AuthenticationManager

public class AuthenticationManager extends Object
1797 - Design Authentication Manager.<p>Medium</p> <p>There is an authentication system that works with authentication tokens. For each session, the user will receive a new authentication token that will expire <code>timeToLive</code> seconds after the <code>currentTime</code>. If the token is renewed, the expiry time will be <strong>extended</strong> to expire <code>timeToLive</code> seconds after the (potentially different) <code>currentTime</code>.</p> <p>Implement the <code>AuthenticationManager</code> class:</p> <ul> <li><code>AuthenticationManager(int timeToLive)</code> constructs the <code>AuthenticationManager</code> and sets the <code>timeToLive</code>.</li> <li><code>generate(string tokenId, int currentTime)</code> generates a new token with the given <code>tokenId</code> at the given <code>currentTime</code> in seconds.</li> <li><code>renew(string tokenId, int currentTime)</code> renews the <strong>unexpired</strong> token with the given <code>tokenId</code> at the given <code>currentTime</code> in seconds. If there are no unexpired tokens with the given <code>tokenId</code>, the request is ignored, and nothing happens.</li> <li><code>countUnexpiredTokens(int currentTime)</code> returns the number of <strong>unexpired</strong> tokens at the given currentTime.</li> </ul> <p>Note that if a token expires at time <code>t</code>, and another action happens on time <code>t</code> (<code>renew</code> or <code>countUnexpiredTokens</code>), the expiration takes place <strong>before</strong> the other actions.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/02/25/copy-of-pc68_q2.png" alt="" /></p> <p><strong>Input</strong></p> <p>[&ldquo;AuthenticationManager&rdquo;, &ldquo;<code>renew</code>&rdquo;, &ldquo;generate&rdquo;, &ldquo;<code>countUnexpiredTokens</code>&rdquo;, &ldquo;generate&rdquo;, &ldquo;<code>renew</code>&rdquo;, &ldquo;<code>renew</code>&rdquo;, &ldquo;<code>countUnexpiredTokens</code>&rdquo;]</p> <p>[[5], [&ldquo;aaa&rdquo;, 1], [&ldquo;aaa&rdquo;, 2], [6], [&ldquo;bbb&rdquo;, 7], [&ldquo;aaa&rdquo;, 8], [&ldquo;bbb&rdquo;, 10], [15]]</p> <p><strong>Output:</strong> [null, null, null, 1, null, null, null, 0]</p> <p><strong>Explanation:</strong></p> <p>AuthenticationManager authenticationManager = new AuthenticationManager(5); // Constructs the AuthenticationManager with <code>timeToLive</code> = 5 seconds.</p> <p>authenticationManager.<code>renew</code>(&ldquo;aaa&rdquo;, 1); // No token exists with tokenId &ldquo;aaa&rdquo; at time 1, so nothing happens.</p> <p>authenticationManager.generate(&ldquo;aaa&rdquo;, 2); // Generates a new token with tokenId &ldquo;aaa&rdquo; at time 2.</p> <p>authenticationManager.<code>countUnexpiredTokens</code>(6); // The token with tokenId &ldquo;aaa&rdquo; is the only unexpired one at time 6, so return 1.</p> <p>authenticationManager.generate(&ldquo;bbb&rdquo;, 7); // Generates a new token with tokenId &ldquo;bbb&rdquo; at time 7.</p> <p>authenticationManager.<code>renew</code>(&ldquo;aaa&rdquo;, 8); // The token with tokenId &ldquo;aaa&rdquo; expired at time 7, and 8 >= 7, so at time 8 the <code>renew</code> request is ignored, and nothing happens.</p> <p>authenticationManager.<code>renew</code>(&ldquo;bbb&rdquo;, 10); // The token with tokenId &ldquo;bbb&rdquo; is unexpired at time 10, so the <code>renew</code> request is fulfilled and now the token will expire at time 15.</p> <p>authenticationManager.<code>countUnexpiredTokens</code>(15); // The token with tokenId &ldquo;bbb&rdquo; expires at time 15, and the token with tokenId &ldquo;aaa&rdquo; expired at time 7, so currently no token is unexpired, so return 0.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= timeToLive <= 10<sup>8</sup></code></li> <li><code>1 <= currentTime <= 10<sup>8</sup></code></li> <li><code>1 <= tokenId.length <= 5</code></li> <li><code>tokenId</code> consists only of lowercase letters.</li> <li>All calls to <code>generate</code> will contain unique values of <code>tokenId</code>.</li> <li>The values of <code>currentTime</code> across all the function calls will be <strong>strictly increasing</strong>.</li> <li>At most <code>2000</code> calls will be made to all functions combined.</li> </ul>
  • Constructor Details

    • AuthenticationManager

      public AuthenticationManager(int timeToLive)
  • Method Details

    • generate

      public void generate(String tokenId, int currentTime)
    • renew

      public void renew(String tokenId, int currentTime)
    • countUnexpiredTokens

      public int countUnexpiredTokens(int currentTime)