-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1797-design-authentication-manager.js
More file actions
68 lines (64 loc) · 2.25 KB
/
1797-design-authentication-manager.js
File metadata and controls
68 lines (64 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* 1797. Design Authentication Manager
* https://leetcode.com/problems/design-authentication-manager/
* Difficulty: Medium
*
* There is an authentication system that works with authentication tokens. For each session,
* the user will receive a new authentication token that will expire timeToLive seconds after
* the currentTime. If the token is renewed, the expiry time will be extended to expire
* timeToLive seconds after the (potentially different) currentTime.
*
* Implement the AuthenticationManager class:
* - AuthenticationManager(int timeToLive) constructs the AuthenticationManager and sets the
* timeToLive.
* - generate(string tokenId, int currentTime) generates a new token with the given tokenId
* at the given currentTime in seconds.
* - renew(string tokenId, int currentTime) renews the unexpired token with the given tokenId
* at the given currentTime in seconds. If there are no unexpired tokens with the given
* tokenId, the request is ignored, and nothing happens.
* - countUnexpiredTokens(int currentTime) returns the number of unexpired tokens at the
* given currentTime.
*
* Note that if a token expires at time t, and another action happens on time t (renew or
* countUnexpiredTokens), the expiration takes place before the other actions.
*/
/**
* @param {number} timeToLive
*/
var AuthenticationManager = function(timeToLive) {
this.timeToLive = timeToLive;
this.tokens = new Map();
};
/**
* @param {string} tokenId
* @param {number} currentTime
* @return {void}
*/
AuthenticationManager.prototype.generate = function(tokenId, currentTime) {
this.tokens.set(tokenId, currentTime + this.timeToLive);
};
/**
* @param {string} tokenId
* @param {number} currentTime
* @return {void}
*/
AuthenticationManager.prototype.renew = function(tokenId, currentTime) {
if (this.tokens.has(tokenId) && this.tokens.get(tokenId) > currentTime) {
this.tokens.set(tokenId, currentTime + this.timeToLive);
}
};
/**
* @param {number} currentTime
* @return {number}
*/
AuthenticationManager.prototype.countUnexpiredTokens = function(currentTime) {
let count = 0;
for (const [tokenId, expiry] of this.tokens) {
if (expiry > currentTime) {
count++;
} else {
this.tokens.delete(tokenId);
}
}
return count;
};