49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
import typing
|
|
import argon2
|
|
import psycopg2
|
|
|
|
import dbHandler
|
|
import userHandler
|
|
|
|
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 handlePasswordVerification(dbConnection: psycopg2.extensions.connection, password: str, userID: int) -> bool:
|
|
hash = userHandler.getHashValueByUserID(dbConnection, userID)
|
|
userIDstr = str(userID)
|
|
debugPrint("Now verifying password against hash for user ID " + userIDstr + "...")
|
|
if verifyPassword(password, hash):
|
|
debugPrint("(USER ID " + userIDstr + ") Password verification success!")
|
|
if verifyRehash(hash):
|
|
debugPrint("(USER ID " + userIDstr + ") Hash needs to be rehashed! Will now rehash...")
|
|
return True
|
|
else:
|
|
debugPrint("(USER ID " + userIDstr + ") Password verification failure!")
|
|
return False
|