from datetime import datetime, timedelta, timezone from typing import Any import bcrypt from jose import jwt from app.core.config import settings def hash_password(password: str) -> str: return bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8") def verify_password(plain_password: str, hashed_password: str) -> bool: return bcrypt.checkpw(plain_password.encode("utf-8"), hashed_password.encode("utf-8")) def create_access_token(subject: Any, session_version: int = 0) -> str: expires_delta = timedelta(minutes=settings.access_token_expire_minutes) expire = datetime.now(timezone.utc) + expires_delta to_encode = { "exp": expire, "sub": str(subject), "sv": session_version, "iss": settings.jwt_issuer, "aud": settings.jwt_audience, } return jwt.encode(to_encode, settings.secret_key, algorithm=settings.algorithm) def decode_access_token(token: str) -> dict[str, Any]: return jwt.decode( token, settings.secret_key, algorithms=[settings.algorithm], issuer=settings.jwt_issuer, audience=settings.jwt_audience, )