-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathDesign Authentication Manager.cpp
44 lines (40 loc) · 1.16 KB
/
Design Authentication Manager.cpp
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
class AuthenticationManager {
int ttl = 0;
unordered_map<string, int> tokens;
public:
AuthenticationManager(int timeToLive) {
ttl = timeToLive;
}
void generate(string tokenId, int currentTime) {
tokens[tokenId] = currentTime + ttl;
}
void renew(string tokenId, int currentTime) {
auto it = tokens.find(tokenId);
if (it == tokens.end()) {
return;
}
if (it->second <= currentTime) {
tokens.erase(it);
} else {
it->second = currentTime + ttl;
}
}
int countUnexpiredTokens(int currentTime) {
auto it = tokens.begin();
while (it != tokens.end()) {
if (it->second <= currentTime) {
it = tokens.erase(it);
} else {
it++;
}
}
return tokens.size();
}
};
/**
* Your AuthenticationManager object will be instantiated and called as such:
* AuthenticationManager* obj = new AuthenticationManager(timeToLive);
* obj->generate(tokenId,currentTime);
* obj->renew(tokenId,currentTime);
* int param_3 = obj->countUnexpiredTokens(currentTime);
*/