Refactor import paths and enhance logging in various modules; add user table creation in start.py
This commit is contained in:
@@ -11,3 +11,4 @@ venv/
|
|||||||
update.ps1
|
update.ps1
|
||||||
venv/pyvenv.cfg
|
venv/pyvenv.cfg
|
||||||
venv/*
|
venv/*
|
||||||
|
venv/pyvenv.cfg
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from fastapi import APIRouter, HTTPException, Depends, Header
|
from fastapi import APIRouter, HTTPException, Depends, Header
|
||||||
import logging
|
import logging
|
||||||
import db
|
import db
|
||||||
import api_security as auth
|
import auth.api_security as auth
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from api import tokens
|
from api import tokens
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
from fastapi import APIRouter, HTTPException, Depends, Header
|
from fastapi import APIRouter, HTTPException, Depends, Header
|
||||||
import logging
|
import logging
|
||||||
import db
|
import db
|
||||||
import api_security as auth
|
import auth.api_security as auth
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
@@ -2,7 +2,7 @@ import logging
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import api_security as auth
|
import auth.api_security as auth
|
||||||
import time
|
import time
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from dotenv import load_dotenv
|
|||||||
import db
|
import db
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import api_security as auth
|
import auth.api_security as auth
|
||||||
|
|
||||||
#Logging
|
#Logging
|
||||||
|
|
||||||
@@ -44,8 +44,15 @@ def new_table(name):
|
|||||||
db.connection.commit()
|
db.connection.commit()
|
||||||
logging.info("Table 'luggage' created")
|
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("tokens")
|
||||||
new_table("luggage")
|
new_table("luggage")
|
||||||
|
new_table("user")
|
||||||
print(auth.admin_token())
|
print(auth.admin_token())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user