Spaces:
Sleeping
Sleeping
Download src/middleware/auth.py from umarwaqar/full-stack-todo: direct link, hf CLI and curl.
- Browser
- Download file 1.04 kB
-
https://huggingface.co/spaces/umarwaqar/full-stack-todo/resolve/main/src/middleware/auth.py
- Command line
-
hf download hf://spaces/umarwaqar/full-stack-todo/src/middleware/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/umarwaqar/full-stack-todo/resolve/main/src/middleware/auth.py
1.04 kB
| from fastapi import HTTPException, status | |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | |
| from jose import JWTError, jwt | |
| from functools import wraps | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # JWT Configuration | |
| SECRET_KEY = os.getenv("JWT_SECRET", "your-super-secret-jwt-key-change-in-production") | |
| ALGORITHM = "HS256" | |
| security = HTTPBearer() | |
| def verify_token(token: str): | |
| """ | |
| Verify and decode a JWT token | |
| """ | |
| try: | |
| payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) | |
| user_id: int = payload.get("sub") | |
| if user_id is None: | |
| raise HTTPException(status_code=401, detail="Could not validate credentials") | |
| return user_id | |
| except JWTError: | |
| raise HTTPException(status_code=401, detail="Could not validate credentials") | |
| def get_current_user(credentials: HTTPAuthorizationCredentials = security): | |
| """ | |
| Dependency to get current authenticated user | |
| """ | |
| user_id = verify_token(credentials.credentials) | |
| return user_id |