Compare commits

..

No commits in common. "a3299c973fe57cfb5c7afe18c5b1058e3e224f2f" and "87559ee61ed7610e80df179a021831f22486cd9d" have entirely different histories.

2 changed files with 13 additions and 29 deletions

View file

@ -41,15 +41,9 @@ class API:
@self.app.route('/interstellar/api/db', methods=['POST']) @self.app.route('/interstellar/api/db', methods=['POST'])
def db_manipulate(): def db_manipulate():
action = request.args.get('action') action = request.args.get('action')
data = request.args.get('data')
if action == "create_account": if action == "create_account":
self.db.add_user(data) print("ahh")
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') ssl_context = ('cert.pem', 'key.pem')
self.app.run(debug=True, host='0.0.0.0', port=5000, ssl_context=ssl_context) self.app.run(debug=True, host='0.0.0.0', port=5000, ssl_context=ssl_context)

View file

@ -6,46 +6,36 @@ class DB:
def __init__(self): def __init__(self):
self.database = {} self.database = {}
@staticmethod def _hash_password(self, password: str) -> str:
def hash_password(password):
salt = "your_secret_salt" salt = "your_secret_salt"
hashed_password = hashlib.sha256((password + salt).encode()).hexdigest() hashed_password = hashlib.sha256((password + salt).encode()).hexdigest()
return hashed_password return hashed_password
def add_user(self, data): def add_user(self, username: str, password: str) -> None:
username = data.get['username'] hashed_password = self._hash_password(password)
password = data.get['password']
hashed_password = self.hash_password(password)
user_data = {"hashed_password": hashed_password} user_data = {"hashed_password": hashed_password}
self.database[username] = user_data self.database[username] = user_data
def update_password(self, data): def update_password(self, username: str, old_password: str, new_password: str) -> bool:
username = data.get['username'] if not self.check_credentials(username, old_password):
old_password = data.get['old_password']
new_password = data.get['new_password']
if not self.check_credentials(data):
return False 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}) self.database[username].update({"hashed_password": hashed_new_password})
return True return True
def check_credentials(self, data): def check_credentials(self, username: str, password: str) -> bool:
username = data.get['username']
password = data.get['password']
if username not in self.database: if username not in self.database:
return False return False
stored_hashed_password = self.database[username]["hashed_password"] 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 return stored_hashed_password == entered_hashed_password
def get_additional_info(self, data): def get_additional_info(self, username: str, password: str) -> dict | None:
username = data.get['username'] if not self.check_credentials(username, password):
password = data.get['password']
if not self.check_credentials(data):
return None return None
send_back = self.database[username] send_back = self.database[username]
del send_back['hashed_password'] del send_back['hashed_password']
return send_back return send_back