-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauthenticator.py
58 lines (51 loc) · 1.78 KB
/
authenticator.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import json
import hashlib
USER_DB = "users.json"
class Authenticator:
@staticmethod
def load_users():
if os.path.exists(USER_DB):
with open(USER_DB, "r") as f:
users = json.load(f)
for user in users.values():
if "phone" not in user:
user["phone"] = ""
return users
return {}
@staticmethod
def save_users(users):
with open(USER_DB, "w") as f:
json.dump(users, f, indent=4)
@staticmethod
def authenticate_user(username, password):
users = Authenticator.load_users()
if username in users:
hashed_input_password = hashlib.sha256(password.encode()).hexdigest()
return hashed_input_password == users[username]["password"]
return False
@staticmethod
def register_user(username, password, phone):
users = Authenticator.load_users()
if username in users:
return False
hashed_password = hashlib.sha256(password.encode()).hexdigest()
users[username] = {"password": hashed_password, "phone": phone}
Authenticator.save_users(users)
return True
@staticmethod
def update_password(username, new_password):
users = Authenticator.load_users()
if username in users:
hashed_password = hashlib.sha256(new_password.encode()).hexdigest()
users[username]["password"] = hashed_password
Authenticator.save_users(users)
return True
return False
@staticmethod
def get_username_by_phone(phone):
users = Authenticator.load_users()
for username, details in users.items():
if details.get("phone") == phone:
return username
return None