From f5919b2845f2448a8d10049019fd24c54560cdd7 Mon Sep 17 00:00:00 2001 From: Mabe Date: Sat, 19 Sep 2026 13:13:03 -0700 Subject: [PATCH] Refactor import paths and enhance logging in various modules; add user table creation in start.py --- .gitignore | 3 +- api/db.py | 2 +- api/tokens.py | 2 +- api_security.py => auth/api_security.py | 0 auth/user.py | 70 +++++++++++++++++++++++++ main.py | 2 +- start.py | 9 +++- 7 files changed, 83 insertions(+), 5 deletions(-) rename api_security.py => auth/api_security.py (100%) create mode 100644 auth/user.py diff --git a/.gitignore b/.gitignore index 8129ea8..68bccb6 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ update.sh venv/ update.ps1 venv/pyvenv.cfg -venv/* \ No newline at end of file +venv/* +venv/pyvenv.cfg diff --git a/api/db.py b/api/db.py index 3acdbd4..c106656 100644 --- a/api/db.py +++ b/api/db.py @@ -1,7 +1,7 @@ from fastapi import APIRouter, HTTPException, Depends, Header import logging import db -import api_security as auth +import auth.api_security as auth from pydantic import BaseModel from api import tokens diff --git a/api/tokens.py b/api/tokens.py index faf2cd5..0888065 100644 --- a/api/tokens.py +++ b/api/tokens.py @@ -1,7 +1,7 @@ from fastapi import APIRouter, HTTPException, Depends, Header import logging import db -import api_security as auth +import auth.api_security as auth from pydantic import BaseModel diff --git a/api_security.py b/auth/api_security.py similarity index 100% rename from api_security.py rename to auth/api_security.py diff --git a/auth/user.py b/auth/user.py new file mode 100644 index 0000000..65c7e06 --- /dev/null +++ b/auth/user.py @@ -0,0 +1,70 @@ +from argon2 import PasswordHasher +import db +import mariadb +from dotenv import load_dotenv +import logging +import os + +#Logging + +logging.basicConfig( + filename='auth/user.log', # Log to a file + level=logging.DEBUG, # Log everything (DEBUG and above) + format='%(asctime)s %(levelname)s %(message)s' +) + +logging.info('Application started') +try: + logging.debug('Debug info here') +except Exception as e: + logging.exception('An error occurred') + +load_dotenv() +db_name = os.getenv("DB_NAME") +db_psswd = os.getenv("DB_PASSWORD") +db_user = os.getenv("DB_USER") +db_port = os.getenv("DB_PORT") or "3306" +db_host = os.getenv("DB_HOST") +if db_host == None: + logging.error("no hostname provided") + os._exit(1) + +db.connect(db_user, db_psswd, db_host, int(db_port), db_name) + + +def create_user(username, password): + user = username + passwd = PasswordHasher().hash(password) + cursor = db.connection.cursor() + executed = "INSERT INTO user (username, password) VALUES (?, ?)" + executed_data = (user, passwd) + cursor.execute(executed, executed_data) + db.connection.commit() + +def check_login(username, password): + cursor = db.connection.cursor() + hash_passwd = cursor.execute("SELECT password FROM user WHERE username = ?", (username,)) + valid = False + try: + valid = PasswordHasher().verify(hash_passwd, password) + if valid == True: + return valid + except Exception as e: + logging.error(e) + return False + +def delete_user(username, password): + cursor = db.connection.cursor() + hash_password = cursor.execute("SELECT password FROM user WHERE username = ?", (username,))# + valid = False + try: + valid = PasswordHasher().verify(hash_password, password) + except Exception as e: + logging.error(e) + if valid == True: + try: + cursor.execute("DELETE FROM user WHERE username = ?", (username,)) + db.connection.commit() + logging.info(f"User {username} was successfully deleted") + except Exception as e: + logging.error(e) diff --git a/main.py b/main.py index e0495a5..f231d79 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ import logging import subprocess import sys import os -import api_security as auth +import auth.api_security as auth import time try: diff --git a/start.py b/start.py index cf57cb4..77e6e86 100644 --- a/start.py +++ b/start.py @@ -2,7 +2,7 @@ from dotenv import load_dotenv import db import os import logging -import api_security as auth +import auth.api_security as auth #Logging @@ -44,8 +44,15 @@ def new_table(name): db.connection.commit() logging.info("Table 'luggage' created") + if name == "user": + executed = "CREATE TABLE user (username VARCHAR(50) PRIMARY KEY, password VARCHAR(255))" + cursor.execute(executed) + db.connection.commit() + logging.info("Table 'user' created") + new_table("tokens") new_table("luggage") +new_table("user") print(auth.admin_token())