-
Notifications
You must be signed in to change notification settings - Fork 0
/
design-authentication-manager.js
53 lines (48 loc) · 1.29 KB
/
design-authentication-manager.js
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
/**
* @param {number} timeToLive
*/
var AuthenticationManager = function(timeToLive) {
this.timeToLive = timeToLive;
this.map = new Map(); // tokenId -> liveTime (latest)
};
/**
* @param {string} tokenId
* @param {number} currentTime
* @return {void}
*/
AuthenticationManager.prototype.generate = function(tokenId, currentTime) {
this.map.set(tokenId, currentTime + this.timeToLive);
};
/**
* @param {string} tokenId
* @param {number} currentTime
* @return {void}
*/
AuthenticationManager.prototype.renew = function(tokenId, currentTime) {
const liveTime = this.map.get(tokenId);
if (this.map.has(tokenId) && liveTime > currentTime) {
this.map.set(tokenId, currentTime + this.timeToLive);
}
};
/**
* @param {number} currentTime
* @return {number}
*/
AuthenticationManager.prototype.countUnexpiredTokens = function(currentTime) {
let count = 0;
this.map.forEach((item) => {
if (item > currentTime) {
count++;
} else {
this.map.delete(key); // 移除过期的 token
}
});
return count;
};
/**
* Your AuthenticationManager object will be instantiated and called as such:
* var obj = new AuthenticationManager(timeToLive)
* obj.generate(tokenId,currentTime)
* obj.renew(tokenId,currentTime)
* var param_3 = obj.countUnexpiredTokens(currentTime)
*/