Refactor database interactions to use SQLAlchemy ORM; update user and token management in api_security.py and user.py; add models for Items, Tokens, and User

This commit is contained in:
Mabe
2026-09-23 19:54:20 -07:00
parent c0108fd1e1
commit cfe8a0e22d
6 changed files with 132 additions and 163 deletions
+15 -34
View File
@@ -7,6 +7,7 @@ import logging
from argon2 import PasswordHasher
import random
import string
from models import Tokens
#Logging
@@ -23,17 +24,7 @@ try:
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)
session = db.session
def generate_token(duration):
@@ -43,11 +34,9 @@ def generate_token(duration):
hash = ph.hash(token)
end_date = enddate(duration)
time = datetime.now()
cursor = db.connection.cursor()
executed = "INSERT INTO tokens (tokens, time, end_date) VALUES (?, ?, ?)"
executed_data = (hash, time, end_date)
cursor.execute(executed, executed_data)
db.connection.commit()
new_item = Tokens(tokens=hash, time=time, end_date=end_date)
session.add(new_item)
session.commit()
logging.info(f"Token till {end_date} created")
return token
@@ -68,10 +57,8 @@ def enddate(duration):
def check_token(token):
ph = PasswordHasher()
cursor = db.connection.cursor()
valid = False
cursor.execute("SELECT tokens FROM tokens")
hashes = [row[0] for row in cursor.fetchall()]
hashes = [row[0] for row in session.query(Tokens.tokens).all()]
for hash in hashes:
try:
valid = ph.verify(hash, token)
@@ -84,13 +71,12 @@ def check_token(token):
def check_validaty():
cursor = db.connection.cursor()
cursor.execute("SELECT end_date FROM tokens")
end_dates = [row[0] for row in cursor.fetchall()]
end_dates = [row[0] for row in session.query(Tokens.end_date).all()]
for end_date in end_dates:
end_date_dt = datetime.strptime(end_date, "%Y-%m-%d %H:%M:%S")
if datetime.now() > end_date_dt:
cursor.execute("DELETE FROM tokens WHERE end_date = ?", (end_date,))
session.query(Tokens).filter_by(end_date=end_date).delete()
session.commit()
def admin_token():
@@ -99,11 +85,9 @@ def admin_token():
hash = ph.hash(token)
time = datetime.now()
end_date = enddate("forever")
cursor = db.connection.cursor()
executed = "INSERT INTO tokens (tokens, time, end_date) VALUES (?, ?, ?)"
executed_data = (hash, time, end_date)
cursor.execute(executed, executed_data)
db.connection.commit()
new_item = Tokens(tokens=hash, time=time, end_date=end_date)
session.add(new_item)
session.commit()
logging.info(f"Token till {end_date} created")
return token
@@ -113,8 +97,7 @@ def check_admin_token(token):
if len(token) == 64:
ph = PasswordHasher()
cursor = db.connection.cursor()
cursor.execute("SELECT tokens FROM tokens")
hashes = [row[0] for row in cursor.fetchall()]
hashes = [row[0] for row in session.query(Tokens.tokens).all()]
for hash in hashes:
try:
valid = ph.verify(hash, token)
@@ -127,14 +110,12 @@ def check_admin_token(token):
def delete_token(token):
if not len(token) == 64:
ph = PasswordHasher()
cursor = db.connection.cursor()
cursor.execute("SELECT tokens FROM tokens")
hashes = [row[0] for row in cursor.fetchall()]
hashes = [row[0] for row in session.query(Tokens.tokens).all()]
for hash in hashes:
try:
valid = ph.verify(hash, token)
if valid == True:
cursor.execute("DELETE FROM tokens WHERE tokens = ?", (hash,))
session.query(Tokens).filter_by(tokens=hash).delete()
db.connection.commit()
logging.critical("token deleted")
except Exception as e: