Spaces:
Sleeping
Sleeping
Download src/api/middleware/auth.py from AliAskariFace/backend-todo-app: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/spaces/AliAskariFace/backend-todo-app/resolve/main/src/api/middleware/auth.py
- Command line
-
hf download hf://spaces/AliAskariFace/backend-todo-app/src/api/middleware/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/AliAskariFace/backend-todo-app/resolve/main/src/api/middleware/auth.py
1.52 kB
| from fastapi import HTTPException, status, Request | |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | |
| import jwt | |
| from jwt import PyJWTError | |
| from typing import Optional | |
| import os | |
| from ..utils.jwt import decode_jwt_token | |
| security = HTTPBearer() | |
| async def jwt_middleware(request: Request, credentials: HTTPAuthorizationCredentials = security): | |
| """ | |
| JWT verification middleware to validate incoming requests. | |
| Args: | |
| request: The incoming request object | |
| credentials: The authorization credentials from the request | |
| Raises: | |
| HTTPException: If the token is missing, invalid, or expired | |
| """ | |
| try: | |
| # Extract token from credentials | |
| token = credentials.credentials | |
| # Decode and verify the JWT token | |
| payload = decode_jwt_token(token) | |
| # Store the user information in the request state for later use | |
| request.state.user_id = payload.get("sub") | |
| request.state.user_email = payload.get("email", "") | |
| return payload | |
| except jwt.ExpiredSignatureError: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Token has expired" | |
| ) | |
| except PyJWTError: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Invalid token" | |
| ) | |
| except Exception as e: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail=f"Authentication error: {str(e)}" | |
| ) |