Class AuthenticationManager
java.lang.Object
g1701_1800.s1797_design_authentication_manager.AuthenticationManager
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>[“AuthenticationManager”, “<code>renew</code>”, “generate”, “<code>countUnexpiredTokens</code>”, “generate”, “<code>renew</code>”, “<code>renew</code>”, “<code>countUnexpiredTokens</code>”]</p>
<p>[[5], [“aaa”, 1], [“aaa”, 2], [6], [“bbb”, 7], [“aaa”, 8], [“bbb”, 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>(“aaa”, 1); // No token exists with tokenId “aaa” at time 1, so nothing happens.</p>
<p>authenticationManager.generate(“aaa”, 2); // Generates a new token with tokenId “aaa” at time 2.</p>
<p>authenticationManager.<code>countUnexpiredTokens</code>(6); // The token with tokenId “aaa” is the only unexpired one at time 6, so return 1.</p>
<p>authenticationManager.generate(“bbb”, 7); // Generates a new token with tokenId “bbb” at time 7.</p>
<p>authenticationManager.<code>renew</code>(“aaa”, 8); // The token with tokenId “aaa” 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>(“bbb”, 10); // The token with tokenId “bbb” 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 “bbb” expires at time 15, and the token with tokenId “aaa” 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 Summary
Constructors -
Method Summary
-
Constructor Details
-
AuthenticationManager
public AuthenticationManager(int timeToLive)
-
-
Method Details
-
generate
-
renew
-
countUnexpiredTokens
public int countUnexpiredTokens(int currentTime)
-