interstellar_ai/py/db.py

96 lines
2.9 KiB
Python
Raw Normal View History

2024-09-20 11:15:42 +02:00
import hashlib
2024-09-20 16:06:40 +02:00
import json
2024-09-20 11:15:42 +02:00
class DB:
def __init__(self):
self.database = {}
2024-09-25 09:45:04 +02:00
self.emails = []
def ensure_username(self, data):
if hasattr(data, 'username'):
return data.get['username']
elif hasattr(data, 'email'):
return self.emails[data.get['username']]
2024-09-20 11:15:42 +02:00
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']
2024-09-25 09:45:04 +02:00
email = data.get['email']
2024-09-20 11:34:21 +02:00
hashed_password = self.hash_password(password)
2024-09-20 11:15:42 +02:00
user_data = {"hashed_password": hashed_password}
2024-09-25 09:45:04 +02:00
if username not in self.database:
self.database[username] = user_data
self.emails[email] = username
return True
return False
def delete_user(self, data):
username = self.ensure_username(data)
data = data.get['data']
if not self.check_credentials(data):
return False
del self.database[username]
for i in self.emails:
if i == username:
del i
self.save_database()
return True
2024-09-20 11:15:42 +02:00
2024-09-20 15:26:18 +02:00
def change_data(self, data):
2024-09-25 09:45:04 +02:00
username = self.ensure_username(data)
2024-09-20 15:26:18 +02:00
data = data.get['data']
if not self.check_credentials(data):
return False
self.database[username]['data'] = data
2024-09-20 16:06:40 +02:00
self.save_database()
2024-09-20 15:26:18 +02:00
return True
2024-09-20 11:34:21 +02:00
def update_password(self, data):
2024-09-25 09:45:04 +02:00
username = self.ensure_username(data)
2024-09-20 11:34:21 +02:00
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})
2024-09-20 16:06:40 +02:00
self.save_database()
2024-09-20 11:15:42 +02:00
return True
2024-09-20 11:34:21 +02:00
def check_credentials(self, data):
2024-09-25 09:45:04 +02:00
username = self.ensure_username(data)
2024-09-20 11:34:21 +02:00
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 15:26:18 +02:00
def get_data(self, data):
2024-09-25 09:45:04 +02:00
username = self.ensure_username(data)
2024-09-20 11:34:21 +02:00
if not self.check_credentials(data):
2024-09-20 11:15:42 +02:00
return None
2024-09-20 15:26:18 +02:00
send_back = self.database[username].get['data']
2024-09-20 11:34:21 +02:00
return send_back
2024-09-20 16:06:40 +02:00
def save_database(self):
with open("database.json", 'w') as file:
json.dump(self.database, file)
def load_database(self):
try:
with open("database.json", 'r') as file:
self.database = json.load(file)
except FileNotFoundError:
pass