Spaces:
Sleeping
Sleeping
Download src/api/auth/dependencies.py from HAK-16/sorted-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.85 kB
-
https://huggingface.co/spaces/HAK-16/sorted-backend/resolve/main/src/api/auth/dependencies.py
- Command line
-
hf download hf://spaces/HAK-16/sorted-backend/src/api/auth/dependencies.py
-
curl -L -o dependencies.py https://huggingface.co/spaces/HAK-16/sorted-backend/resolve/main/src/api/auth/dependencies.py
1.85 kB
| """ | |
| Authentication dependencies for FastAPI route handlers. | |
| Provides dependency injection for extracting authenticated user identity | |
| from JWT tokens in request headers. | |
| """ | |
| from fastapi import Depends, HTTPException, status | |
| from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer | |
| from jwt import ExpiredSignatureError, InvalidTokenError | |
| from src.api.auth.jwt import TokenPayload, verify_token | |
| _bearer_scheme = HTTPBearer(auto_error=False) | |
| async def get_current_user( | |
| credentials: HTTPAuthorizationCredentials | None = Depends(_bearer_scheme), | |
| ) -> TokenPayload: | |
| """ | |
| Extract and verify user identity from JWT Bearer token. | |
| Args: | |
| credentials: Bearer token from Authorization header. | |
| Returns: | |
| TokenPayload with the authenticated user's id and email. | |
| Raises: | |
| HTTPException 401: If token is missing, expired, or invalid. | |
| """ | |
| if credentials is None: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Authentication required", | |
| ) | |
| try: | |
| return verify_token(credentials.credentials) | |
| except ExpiredSignatureError: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Token has expired", | |
| ) | |
| except InvalidTokenError as e: | |
| import logging | |
| logging.getLogger(__name__).error(f"JWT verification failed: {e}") | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Invalid authentication token", | |
| ) | |
| except Exception as e: | |
| import logging | |
| logging.getLogger(__name__).error(f"Unexpected auth error: {type(e).__name__}: {e}") | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Authentication failed", | |
| ) | |