-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign_authentication_manager.py
39 lines (24 loc) · 1.79 KB
/
design_authentication_manager.py
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
'''
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.
'''
class AuthenticationManager:
def __init__(self, timeToLive: int):
self.main = dict()
self.ttl = timeToLive
def generate(self, tokenId: str, currentTime: int) -> None:
self.main[tokenId] = self.ttl + currentTime
def renew(self, tokenId: str, currentTime: int) -> None:
if tokenId in self.main.keys() and self.main[tokenId] > currentTime:
self.main[tokenId] = currentTime + self.ttl
def countUnexpiredTokens(self, currentTime: int) -> int:
count = 0
for _,v in self.main.items():
if v > currentTime:
count+=1
return count