Compare commits

..
2 Commits
7 changed files with 82 additions and 5 deletions
+2 -1
View File
@@ -10,4 +10,5 @@ update.sh
venv/
update.ps1
venv/pyvenv.cfg
venv/*
venv/*
venv/pyvenv.cfg
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+69
View File
@@ -0,0 +1,69 @@
from argon2 import PasswordHasher
import db
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)
+1 -1
View File
@@ -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:
+8 -1
View File
@@ -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())