Spaces:
Sleeping
Sleeping
| """ | |
| Authentication middleware for JWT token verification. | |
| """ | |
| from fastapi import Depends, HTTPException, status, Request | |
| from fastapi.security import HTTPBearer | |
| from fastapi.security.http import HTTPAuthorizationCredentials | |
| from sqlmodel import Session | |
| from src.core.security import decode_access_token | |
| from src.db.session import get_session | |
| from src.services.user_service import UserService | |
| from src.models.user import User | |
| security = HTTPBearer(auto_error=False) | |
| def get_current_user( | |
| request: Request, | |
| credentials: HTTPAuthorizationCredentials = Depends(security), | |
| session: Session = Depends(get_session) | |
| ) -> User: | |
| """ | |
| Dependency to get current authenticated user from JWT token. | |
| Args: | |
| request: FastAPI request object | |
| credentials: HTTP Bearer credentials containing JWT token | |
| session: Database session | |
| Returns: | |
| User: Authenticated user | |
| Raises: | |
| HTTPException: If token is invalid or user not found | |
| """ | |
| # Skip authentication for OPTIONS requests (CORS preflight) | |
| if request.method == "OPTIONS": | |
| return None | |
| if credentials is None: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Not authenticated", | |
| headers={"WWW-Authenticate": "Bearer"}, | |
| ) | |
| token = credentials.credentials | |
| payload = decode_access_token(token) | |
| if payload is None: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Invalid authentication token", | |
| headers={"WWW-Authenticate": "Bearer"}, | |
| ) | |
| user_id: int = payload.get("sub") | |
| if user_id is None: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Invalid authentication token", | |
| headers={"WWW-Authenticate": "Bearer"}, | |
| ) | |
| user = UserService.get_user_by_id(session, user_id) | |
| if user is None: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="User not found", | |
| headers={"WWW-Authenticate": "Bearer"}, | |
| ) | |
| if not user.is_active: | |
| raise HTTPException( | |
| status_code=status.HTTP_403_FORBIDDEN, | |
| detail="Inactive user" | |
| ) | |
| return user | |