-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthenticationManager.cpp
More file actions
41 lines (37 loc) · 1.25 KB
/
AuthenticationManager.cpp
File metadata and controls
41 lines (37 loc) · 1.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
class AuthenticationManager {
public:
AuthenticationManager(int timeToLive): ttl(timeToLive){
}
void generate(string tokenId, int currentTime) {
l.push_back(tokenId);
m[tokenId] = std::make_pair(currentTime + ttl, std::prev(l.end()));
}
void renew(string tokenId, int currentTime) {
removeExpired(currentTime);
if (m.find(tokenId) != m.end() && m[tokenId].first > currentTime) {
l.erase(m[tokenId].second);
l.push_back(tokenId);
m[tokenId] = std::make_pair(currentTime + ttl, std::prev(l.end()));
}
}
int countUnexpiredTokens(int currentTime) {
removeExpired(currentTime);
return l.size();
}
void removeExpired(int currentTime) {
while (!l.empty() && m[l.front()].first <= currentTime) {
l.pop_front();
}
}
private:
unordered_map<string, pair<int, std::list<string>::iterator>> m;
list<string> l;
int ttl;
};
/**
* 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);
*/