"""Authentication and password utilities.""" from datetime import datetime, timedelta, timezone from typing import Any from jose import jwt from passlib.context import CryptContext from backend.core.config import get_settings # PBKDF2 avoids bcrypt's 72-byte password limit and external backend quirks. pwd_context = CryptContext(schemes=["pbkdf2_sha256"], deprecated="auto") ALGORITHM = "HS256" def verify_password(plain_password: str, hashed_password: str) -> bool: """Verify a plaintext password against a hash.""" return pwd_context.verify(plain_password, hashed_password) def get_password_hash(password: str) -> str: """Hash a password for storage.""" return pwd_context.hash(password) def create_access_token(subject: str, expires_delta: timedelta | None = None) -> str: """Create a signed JWT access token.""" settings = get_settings() expire = datetime.now(timezone.utc) + ( expires_delta or timedelta(minutes=settings.access_token_expire_minutes) ) to_encode: dict[str, Any] = {"sub": subject, "exp": expire} return jwt.encode(to_encode, settings.secret_key, algorithm=ALGORITHM)