31 lines
921 B
Python
31 lines
921 B
Python
import hashlib
|
|
|
|
from tables import *
|
|
from sql import *
|
|
|
|
def hash_password(password: str, salt: int = None):
|
|
password = f"{password}{salt}"
|
|
return hashlib.sha256(password.encode()).hexdigest()
|
|
|
|
def get_user_by_email(email: str, session) -> User | None:
|
|
return session.query(User).filter(User.email == email).first()
|
|
|
|
def get_user_by_username(username: str, session) -> User | None:
|
|
return session.query(User).filter(User.username == username).first()
|
|
|
|
def validate_password(password: str, hash: str, salt: int) -> bool:
|
|
hashed_pass = hash_password(password, salt)
|
|
if hash == hashed_pass:
|
|
return True
|
|
|
|
return False
|
|
|
|
def authenticate_user(username: str, password: str, session: SessionDep):
|
|
user = get_user_by_username(username, session)
|
|
if not user:
|
|
return False
|
|
|
|
if not validate_password(password, user.password, user.salt):
|
|
return False
|
|
|
|
return user |