From e28276634a46d7634e282aca648771b191da34c7 Mon Sep 17 00:00:00 2001 From: Allan Eising Date: Sat, 10 May 2025 08:44:45 +0200 Subject: [PATCH] Add token auth utility functions --- .../src/sshecret_backend/auth.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 packages/sshecret-backend/src/sshecret_backend/auth.py diff --git a/packages/sshecret-backend/src/sshecret_backend/auth.py b/packages/sshecret-backend/src/sshecret_backend/auth.py new file mode 100644 index 0000000..c6a94cd --- /dev/null +++ b/packages/sshecret-backend/src/sshecret_backend/auth.py @@ -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)