"""API dependencies for authentication and database access.""" from typing import Annotated from uuid import UUID from fastapi import Depends, HTTPException, status from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer from sqlalchemy.ext.asyncio import AsyncSession from src.database import get_session from src.utils.security import decode_access_token # Bearer token security scheme security = HTTPBearer() async def get_current_user_id( credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)], ) -> UUID: """Extract and validate current user from JWT token.""" token = credentials.credentials payload = decode_access_token(token) if payload is None: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid or expired token", headers={"WWW-Authenticate": "Bearer"}, ) try: user_id = UUID(payload["sub"]) except (KeyError, ValueError): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token payload", headers={"WWW-Authenticate": "Bearer"}, ) return user_id # Type aliases for dependency injection SessionDep = Annotated[AsyncSession, Depends(get_session)] CurrentUserDep = Annotated[UUID, Depends(get_current_user_id)]