-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesignAuthenticationManager.py
More file actions
41 lines (31 loc) · 1.2 KB
/
designAuthenticationManager.py
File metadata and controls
41 lines (31 loc) · 1.2 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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/design-authentication-manager/
# Author: Miao Zhang
# Date: 2021-06-08
class AuthenticationManager:
def __init__(self, timeToLive: int):
self.tokens = {}
self.ttl = timeToLive
def generate(self, tokenId: str, currentTime: int) -> None:
self.clear(currentTime)
self.tokens[tokenId] = currentTime + self.ttl
def renew(self, tokenId: str, currentTime: int) -> None:
self.clear(currentTime)
if tokenId not in self.tokens: return
self.tokens[tokenId] = currentTime + self.ttl
def countUnexpiredTokens(self, currentTime: int) -> int:
self.clear(currentTime)
return len(self.tokens)
def clear(self, currentTime: int) -> None:
ids = []
for id, t in self.tokens.items():
if t <= currentTime:
ids.append(id)
for i in ids:
self.tokens.pop(i)
# Your AuthenticationManager object will be instantiated and called as such:
# obj = AuthenticationManager(timeToLive)
# obj.generate(tokenId,currentTime)
# obj.renew(tokenId,currentTime)
# param_3 = obj.countUnexpiredTokens(currentTime)