1
0
Fork 0
mirror of https://github.com/dancojocaru2000/foxbank.git synced 2025-02-23 16:49:35 +02:00
foxbank/server/foxbank_server/ram_db.py

36 lines
982 B
Python
Raw Normal View History

2021-12-06 01:34:00 +02:00
from datetime import datetime, timedelta
from uuid import uuid4
USED_TOKENS = set()
LOGGED_IN_USERS: dict[str, (int, datetime)] = {}
def login_user(user_id: int) -> str:
'''
Creates token for user
'''
token = str(uuid4())
while token in USED_TOKENS:
token = str(uuid4())
if len(USED_TOKENS) > 10_000_000:
USED_TOKENS.clear()
2021-12-06 02:04:13 +02:00
for token in LOGGED_IN_USERS:
USED_TOKENS.add(token)
2021-12-06 01:34:00 +02:00
USED_TOKENS.add(token)
LOGGED_IN_USERS[token] = user_id, datetime.now()
return token
2021-12-06 02:04:13 +02:00
def logout_user(token: str):
if token in LOGGED_IN_USERS:
del LOGGED_IN_USERS[token]
2021-12-06 01:34:00 +02:00
def get_user(token: str) -> int | None:
if token not in LOGGED_IN_USERS:
return None
user_id, login_date = LOGGED_IN_USERS[token]
time_since_login: timedelta = datetime.now() - login_date
if time_since_login.total_seconds() > (60 * 30): # 30 mins
del LOGGED_IN_USERS[token]
return None
return user_id