-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzt.py
More file actions
46 lines (39 loc) · 1.58 KB
/
Copy pathzt.py
File metadata and controls
46 lines (39 loc) · 1.58 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
42
43
44
45
46
import bcrypt
from pathlib import Path
class PasswordTreatment():
@staticmethod
def hash_password(password: str) -> str:
if not isinstance(password, str):
raise TypeError("Password must be a string")
try:
hashed_password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
return hashed_password
except Exception as e:
raise ValueError(f"Error hashing password: {e}")
@staticmethod
def verify_password(password: str, hashed: str) -> bool:
if not isinstance(password, str) or not isinstance(hashed, str):
raise TypeError("Both password and hashed password must be strings")
try:
return bcrypt.checkpw(password.encode("utf-8"), hashed.encode("utf-8"))
except Exception as e:
raise ValueError(f"Error verifying password: {e}")
class FilePath():
@staticmethod
def get_abs_path(path_str: str) -> str:
abs_path = Path(path_str).expanduser().resolve()
return str(abs_path)
class APIFunction():
@staticmethod
def check_session_token(token: str) -> bool:
session_info_path = "./_db/session_token"
session_info_abs_path = FilePath.get_abs_path(session_info_path)
if not Path(session_info_abs_path).is_file():
return False
with open(session_info_abs_path, "r", encoding="utf-8") as f:
saved_token = f.read().strip()
return token == saved_token
@staticmethod
def get_random_gid() -> str:
import uuid
return str(uuid.uuid4())