Blorg-Backend/securityHandler.py

51 lines
1.4 KiB
Python

import typing
import argon2
import psycopg2
import dbHandler
debug: bool = True
passwordHasher = argon2.PasswordHasher()
def debugPrint(msg: str) -> None:
if debug:
print("(SECURITY HANDLER) PRINT: " + msg)
def hashPassword(password: str) -> str:
return passwordHasher.hash(password)
def verifyPassword(password: str, hash: str) -> bool:
try:
if passwordHasher.verify(hash, password):
return True
else:
return False
except:
return False
def verifyRehash(hash: str) -> bool:
try:
if passwordHasher.check_needs_rehash(hash):
return True
else:
return False
except:
return False
def handlePassword(dbConnection: psycopg2.extensions.connection, password: str, userID: int) -> bool:
hash = dbHandler.getFieldByID(dbConnection, "users", userID, "passwordhash")
debugPrint("Now verifying password against hash for user ID " + userid + "...")
if verifyPassword(password, hash):
debugPrint("(USER ID) " + userID + " Password verification success!")
if verifyRehash(hash):
debugPrint("(USER ID) " + userID + " Hash needs to be rehashed! Will now rehash...")
return True
else:
debugPrint("(USER ID) " + userID + " Password verification failure!")
return False
hashed: str = hashPassword("testing")
print(verifyPassword("testing", hashed))
print(verifyRehash(hashed))