2024-09-20 11:15:42 +02:00
|
|
|
import json
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
|
|
|
|
class DB:
|
|
|
|
def __init__(self):
|
|
|
|
self.database = {}
|
|
|
|
|
2024-09-20 11:34:21 +02:00
|
|
|
@staticmethod
|
|
|
|
def hash_password(password):
|
2024-09-20 11:15:42 +02:00
|
|
|
salt = "your_secret_salt"
|
|
|
|
hashed_password = hashlib.sha256((password + salt).encode()).hexdigest()
|
|
|
|
return hashed_password
|
|
|
|
|
2024-09-20 11:34:21 +02:00
|
|
|
def add_user(self, data):
|
|
|
|
username = data.get['username']
|
|
|
|
password = data.get['password']
|
|
|
|
hashed_password = self.hash_password(password)
|
2024-09-20 11:15:42 +02:00
|
|
|
user_data = {"hashed_password": hashed_password}
|
|
|
|
self.database[username] = user_data
|
|
|
|
|
2024-09-20 11:34:21 +02:00
|
|
|
def update_password(self, data):
|
|
|
|
username = data.get['username']
|
|
|
|
old_password = data.get['old_password']
|
|
|
|
new_password = data.get['new_password']
|
|
|
|
if not self.check_credentials(data):
|
2024-09-20 11:15:42 +02:00
|
|
|
return False
|
|
|
|
|
2024-09-20 11:34:21 +02:00
|
|
|
hashed_new_password = self.hash_password(new_password)
|
2024-09-20 11:15:42 +02:00
|
|
|
self.database[username].update({"hashed_password": hashed_new_password})
|
|
|
|
return True
|
|
|
|
|
2024-09-20 11:34:21 +02:00
|
|
|
def check_credentials(self, data):
|
|
|
|
username = data.get['username']
|
|
|
|
password = data.get['password']
|
2024-09-20 11:15:42 +02:00
|
|
|
if username not in self.database:
|
|
|
|
return False
|
|
|
|
|
|
|
|
stored_hashed_password = self.database[username]["hashed_password"]
|
2024-09-20 11:34:21 +02:00
|
|
|
entered_hashed_password = self.hash_password(password)
|
2024-09-20 11:15:42 +02:00
|
|
|
return stored_hashed_password == entered_hashed_password
|
|
|
|
|
2024-09-20 11:34:21 +02:00
|
|
|
def get_additional_info(self, data):
|
|
|
|
username = data.get['username']
|
|
|
|
password = data.get['password']
|
|
|
|
if not self.check_credentials(data):
|
2024-09-20 11:15:42 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
send_back = self.database[username]
|
|
|
|
del send_back['hashed_password']
|
2024-09-20 11:34:21 +02:00
|
|
|
return send_back
|