Add token auth utility functions
This commit is contained in:
17
packages/sshecret-backend/src/sshecret_backend/auth.py
Normal file
17
packages/sshecret-backend/src/sshecret_backend/auth.py
Normal 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)
|
||||||
Reference in New Issue
Block a user