"""Security utilities for authentication and authorization.""" from datetime import datetime, timedelta from typing import Any from jose import jwt from passlib.context import CryptContext from app.core.config import settings pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") def verify_password(plain_password: str, hashed_password: str) -> bool: """Verify a password against a hash.""" result: bool = pwd_context.verify(plain_password, hashed_password) return result def get_password_hash(password: str) -> str: """Generate password hash.""" hashed: str = pwd_context.hash(password) return hashed def create_access_token(subject: str | int, expires_delta: timedelta | None = None) -> str: """Create JWT access token.""" if expires_delta: expire = datetime.utcnow() + expires_delta else: expire = datetime.utcnow() + timedelta( minutes=settings.access_token_expire_minutes ) to_encode: dict[str, Any] = {"exp": expire, "sub": str(subject)} encoded_jwt: str = jwt.encode(to_encode, settings.secret_key, algorithm=settings.algorithm) return encoded_jwt