Download src/security.py from valent-p/vibra: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/spaces/valent-p/vibra/resolve/main/src/security.py
- Command line
-
hf download hf://spaces/valent-p/vibra/src/security.py
-
curl -L -o security.py https://huggingface.co/spaces/valent-p/vibra/resolve/main/src/security.py
1.28 kB
| # src/security.py | |
| from datetime import datetime, timedelta, timezone | |
| import jwt | |
| from pwdlib import PasswordHash | |
| from pwdlib.hashers.argon2 import Argon2Hasher | |
| from .config import settings | |
| # Initialize password hashing using the modern Argon2 standard | |
| password_hash = PasswordHash((Argon2Hasher(),)) | |
| def hash_password(password: str) -> str: | |
| return password_hash.hash(password) | |
| def verify_password(plain_password: str, hashed_password: str) -> bool: | |
| try: | |
| return password_hash.verify(plain_password, hashed_password) | |
| except Exception: | |
| return False | |
| def create_access_token(data: dict, expires_delta: timedelta | None = None) -> str: | |
| to_encode = data.copy() | |
| if expires_delta: | |
| expire = datetime.now(timezone.utc) + expires_delta | |
| else: | |
| expire = datetime.now(timezone.utc) + timedelta( | |
| minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES | |
| ) | |
| to_encode.update({"exp": expire}) | |
| return jwt.encode( | |
| to_encode, settings.JWT_SECRET_KEY, algorithm=settings.JWT_ALGORITHM | |
| ) | |
| def decode_access_token(token: str) -> dict | None: | |
| try: | |
| return jwt.decode( | |
| token, settings.JWT_SECRET_KEY, algorithms=[settings.JWT_ALGORITHM] | |
| ) | |
| except jwt.PyJWTError: | |
| return None | |