from typing import Annotated from fastapi import Depends, HTTPException, status from fastapi.security import OAuth2PasswordBearer from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select from app.core.database import get_db from app.core.security import decode_token from app.models.user import User oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/auth/login") async def get_current_user( token: Annotated[str, Depends(oauth2_scheme)], db: Annotated[AsyncSession, Depends(get_db)], ) -> User: credentials_exception = HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="인증 정보가 유효하지 않습니다", headers={"WWW-Authenticate": "Bearer"}, ) payload = decode_token(token) if not payload or payload.get("type") != "access": raise credentials_exception user_id: str = payload.get("sub") if not user_id: raise credentials_exception result = await db.execute(select(User).where(User.id == user_id, User.is_active == True)) user = result.scalar_one_or_none() if not user: raise credentials_exception return user async def get_current_active_user( current_user: Annotated[User, Depends(get_current_user)], ) -> User: if not current_user.is_active: raise HTTPException(status_code=400, detail="비활성 계정입니다") return current_user async def require_admin( current_user: Annotated[User, Depends(get_current_active_user)], ) -> User: from app.models.user import UserRole if current_user.role != UserRole.ADMIN: raise HTTPException(status_code=403, detail="관리자 권한이 필요합니다") return current_user # Convenience type aliases CurrentUser = Annotated[User, Depends(get_current_active_user)] DB = Annotated[AsyncSession, Depends(get_db)]