Add token auth utility functions

This commit is contained in:
2025-05-10 08:44:45 +02:00
parent 3f5d9ea545
commit e28276634a

View File

@ -0,0 +1,17 @@
"""Auth helpers."""
import bcrypt
def hash_token(token: str) -> str:
"""Hash a token."""
pwbytes = token.encode("utf-8")
salt = bcrypt.gensalt()
hashed_bytes = bcrypt.hashpw(password=pwbytes, salt=salt)
return hashed_bytes.decode()
def verify_token(token: str, stored_hash: str) -> bool:
"""Verify token."""
token_bytes = token.encode("utf-8")
stored_bytes = stored_hash.encode("utf-8")
return bcrypt.checkpw(token_bytes, stored_bytes)