diff --git a/py/api.py b/py/api.py index b25e4d4..a5d2a97 100644 --- a/py/api.py +++ b/py/api.py @@ -2,6 +2,7 @@ from flask import Flask, request, jsonify from flask_cors import CORS import secrets from ai import AI +from db import DB class API: @@ -9,6 +10,7 @@ class API: self.app = Flask(__name__) self.ai_response = {} self.ai = AI() + self.db = DB() CORS(self.app) def run(self): @@ -36,6 +38,13 @@ class API: return jsonify({'status': 401, 'error': 'Invalid access token'}) return jsonify({'status': 200, 'response': self.ai_response[data]}) + @self.app.route('/interstellar/api/db', methods=['POST']) + def db_manipulate(): + action = request.args.get('action') + if action == "create_account": + print("ahh") + + ssl_context = ('cert.pem', 'key.pem') self.app.run(debug=True, host='0.0.0.0', port=5000, ssl_context=ssl_context) diff --git a/py/db.py b/py/db.py index e69de29..5116a93 100644 --- a/py/db.py +++ b/py/db.py @@ -0,0 +1,41 @@ +import json +import hashlib + + +class DB: + def __init__(self): + self.database = {} + + def _hash_password(self, password: str) -> str: + 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) + 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): + return False + + 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: + if username not in self.database: + return False + + stored_hashed_password = self.database[username]["hashed_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): + return None + + send_back = self.database[username] + del send_back['hashed_password'] + return send_back \ No newline at end of file