From 115fb1d38d6bad87e58a16580377f8a88a67ee1f Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Fri, 20 Sep 2024 11:34:21 +0200 Subject: [PATCH 1/2] Database Backend --- py/api.py | 9 ++++++++- py/db.py | 32 +++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/py/api.py b/py/api.py index a5d2a97..f9e5ca2 100644 --- a/py/api.py +++ b/py/api.py @@ -41,8 +41,15 @@ class API: @self.app.route('/interstellar/api/db', methods=['POST']) def db_manipulate(): action = request.args.get('action') + data = request.args.get('data') if action == "create_account": - print("ahh") + self.db.add_user(data) + if action == "change_password": + self.db.update_password(data) + if action == "get_data": + self.db.get_additional_info(data) + if action == "check_credentials": + self.db.check_credentials(data) ssl_context = ('cert.pem', 'key.pem') diff --git a/py/db.py b/py/db.py index 5116a93..37c9ca0 100644 --- a/py/db.py +++ b/py/db.py @@ -6,36 +6,46 @@ class DB: def __init__(self): self.database = {} - def _hash_password(self, password: str) -> str: + @staticmethod + def hash_password(password): salt = "your_secret_salt" hashed_password = hashlib.sha256((password + salt).encode()).hexdigest() return hashed_password - def add_user(self, username: str, password: str) -> None: - hashed_password = self._hash_password(password) + def add_user(self, data): + username = data.get['username'] + password = data.get['password'] + hashed_password = self.hash_password(password) user_data = {"hashed_password": hashed_password} self.database[username] = user_data - def update_password(self, username: str, old_password: str, new_password: str) -> bool: - if not self.check_credentials(username, old_password): + 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): return False - hashed_new_password = self._hash_password(new_password) + hashed_new_password = self.hash_password(new_password) self.database[username].update({"hashed_password": hashed_new_password}) return True - def check_credentials(self, username: str, password: str) -> bool: + def check_credentials(self, data): + username = data.get['username'] + password = data.get['password'] if username not in self.database: return False stored_hashed_password = self.database[username]["hashed_password"] - entered_hashed_password = self._hash_password(password) + entered_hashed_password = self.hash_password(password) return stored_hashed_password == entered_hashed_password - def get_additional_info(self, username: str, password: str) -> dict | None: - if not self.check_credentials(username, password): + def get_additional_info(self, data): + username = data.get['username'] + password = data.get['password'] + if not self.check_credentials(data): return None send_back = self.database[username] del send_back['hashed_password'] - return send_back \ No newline at end of file + return send_back From 13c47b9743157ed3fb7e609584dffba201ef5abe Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Fri, 20 Sep 2024 11:35:13 +0200 Subject: [PATCH 2/2] whoops --- py/api.py | 1 - 1 file changed, 1 deletion(-) diff --git a/py/api.py b/py/api.py index f9e5ca2..1e1f785 100644 --- a/py/api.py +++ b/py/api.py @@ -51,7 +51,6 @@ class API: if action == "check_credentials": self.db.check_credentials(data) - ssl_context = ('cert.pem', 'key.pem') self.app.run(debug=True, host='0.0.0.0', port=5000, ssl_context=ssl_context)