interstellar_ai/py/db.py

161 lines
6.1 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-25 11:21:05 +02:00
import os
import pycouchdb
2024-09-20 11:15:42 +02:00
class DB:
def __init__(self):
# Initialize the database dictionary to store user data
2024-09-20 11:15:42 +02:00
self.database = {}
2024-09-25 09:45:04 +02:00
def ensure_username(self, data):
# Ensure a username can be retrieved either from username or email
2024-10-07 15:01:06 +02:00
if "username" in data:
return data.get("username")
elif "email" in data:
for index, entry in self.database.items():
if entry.get("email") == data.get("email"):
2024-09-25 11:21:05 +02:00
return index
2024-09-20 11:15:42 +02:00
2024-09-20 11:34:21 +02:00
@staticmethod
def hash_password(password):
# Hash the password with a salt for secure storage
salt = "your_secret_salt" # Consider using a secure random salt
2024-09-20 11:15:42 +02:00
hashed_password = hashlib.sha256((password + salt).encode()).hexdigest()
return hashed_password
2024-09-20 11:34:21 +02:00
def add_user(self, data):
# Add a new user to the database if username is unique
2024-10-07 15:01:06 +02:00
username = data.get("username")
password = data.get("password")
email = data.get("email")
hashed_password = self.hash_password(password) # Hash the password
2024-10-07 15:01:06 +02:00
user_data = {
"hashed_password": hashed_password,
"email": email,
"settings": None,
"history": None,
}
2024-09-25 09:45:04 +02:00
if username not in self.database:
self.database[username] = user_data
self.save_database() # Save changes to the database
2024-09-25 09:45:04 +02:00
return True
return False # User already exists
2024-09-25 09:45:04 +02:00
def delete_user(self, data):
# Delete a user from the database if credentials are valid
2024-09-25 09:45:04 +02:00
username = self.ensure_username(data)
if not self.check_credentials(data):
return False # Invalid credentials
2024-09-25 09:45:04 +02:00
del self.database[username] # Remove user from database
self.save_database() # Save changes
2024-09-25 09:45:04 +02:00
return True
2024-09-20 11:15:42 +02:00
2024-09-20 11:34:21 +02:00
def update_password(self, data):
# Update the user's password if credentials are valid
2024-09-25 09:45:04 +02:00
username = self.ensure_username(data)
2024-10-07 15:01:06 +02:00
new_password = data.get("new_password")
2024-09-20 11:34:21 +02:00
if not self.check_credentials(data):
return False # Invalid credentials
2024-09-20 11:15:42 +02:00
hashed_new_password = self.hash_password(new_password) # Hash the new password
2024-09-20 11:15:42 +02:00
self.database[username].update({"hashed_password": hashed_new_password})
self.save_database() # Save changes
2024-09-20 11:15:42 +02:00
return True
2024-09-20 11:34:21 +02:00
def check_credentials(self, data):
# Verify if provided credentials match stored data
2024-09-25 09:45:04 +02:00
username = self.ensure_username(data)
2024-10-07 15:01:06 +02:00
password = data.get("password")
2024-09-20 11:15:42 +02:00
if username not in self.database:
return False # User not found
2024-09-20 11:15:42 +02:00
stored_hashed_password = self.database[username]["hashed_password"]
2024-09-20 11:34:21 +02:00
entered_hashed_password = self.hash_password(password)
return stored_hashed_password == entered_hashed_password # Check hashed password
2024-09-20 11:15:42 +02:00
2024-10-02 14:14:20 +02:00
def change_settings(self, data):
# Change user settings if credentials are valid
2024-10-02 14:14:20 +02:00
username = self.ensure_username(data)
if not self.check_credentials(data):
return False # Invalid credentials
2024-10-02 14:14:20 +02:00
self.database[username]["settings"] = data.get("data") # Update settings
self.save_database() # Save changes
2024-10-02 14:14:20 +02:00
return True
def get_settings(self, data):
# Retrieve user settings if credentials are valid
2024-10-02 14:14:20 +02:00
username = self.ensure_username(data)
if not self.check_credentials(data):
return None # Invalid credentials
2024-10-02 14:14:20 +02:00
send_back = self.database[username].get("settings") # Get settings
2024-10-02 14:14:20 +02:00
return send_back
def change_history(self, data):
# Change user history if credentials are valid
2024-10-02 14:14:20 +02:00
username = self.ensure_username(data)
if not self.check_credentials(data):
return False # Invalid credentials
2024-10-02 14:14:20 +02:00
self.database[username]["history"] = data.get("data") # Update history
self.save_database() # Save changes
2024-10-02 14:14:20 +02:00
return True
def get_history(self, data):
# Retrieve user history if credentials are valid
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):
return None # Invalid credentials
2024-09-20 11:15:42 +02:00
send_back = self.database[username].get("history") # Get history
2024-09-20 11:34:21 +02:00
return send_back
2024-10-01 15:34:43 +02:00
def get_email(self, data):
# Retrieve user email if credentials are valid
2024-10-01 15:34:43 +02:00
username = self.ensure_username(data)
if not self.check_credentials(data):
return None # Invalid credentials
2024-10-01 15:34:43 +02:00
send_back = self.database[username].get("email") # Get email
2024-10-01 15:34:43 +02:00
return send_back
def get_name(self, data):
# Retrieve username if credentials are valid
2024-10-01 15:34:43 +02:00
if not self.check_credentials(data):
return None # Invalid credentials
2024-10-01 15:34:43 +02:00
send_back = self.ensure_username(data) # Get username
2024-10-01 15:34:43 +02:00
return send_back
2024-09-20 16:06:40 +02:00
def save_database(self):
# Save the database to the specified storage (CouchDB or JSON file)
2024-10-07 15:01:06 +02:00
if os.environ.get("PRODUCTION") == "YES":
2024-09-25 11:21:05 +02:00
server = pycouchdb.Server("http://admin:admin@localhost:5984/")
db = server.database("interstellar_ai")
db.save(self.database) # Save to CouchDB
2024-09-25 11:21:05 +02:00
else:
2024-10-07 15:01:06 +02:00
with open("database.json", "w") as file:
json.dump(self.database, file) # Save to JSON file
2024-09-20 16:06:40 +02:00
def load_database(self):
# Load the database from the specified storage (CouchDB or JSON file)
2024-10-07 15:01:06 +02:00
if os.environ.get("PRODUCTION") == "YES":
2024-09-25 11:21:05 +02:00
server = pycouchdb.Server("http://admin:admin@localhost:5984/")
db = server.database("interstellar_ai")
if db:
self.database = db # Load from CouchDB
2024-09-25 11:21:05 +02:00
else:
server.create("interstellar_ai") # Create database if it doesn't exist
2024-09-25 11:21:05 +02:00
db = server.database("interstellar_ai")
db.save(self.database) # Save initial empty database
2024-09-25 11:21:05 +02:00
else:
try:
2024-10-07 15:01:06 +02:00
with open("database.json", "r") as file:
self.database = json.load(file) # Load from JSON file
2024-09-25 11:21:05 +02:00
except FileNotFoundError:
pass # File not found, do nothing