Spaces:
Sleeping
Sleeping
Download security.py from areen09/test_app_1: direct link, hf CLI and curl.
- Browser
- Download file 920 Bytes
-
https://huggingface.co/spaces/areen09/test_app_1/resolve/main/security.py
- Command line
-
hf download hf://spaces/areen09/test_app_1/security.py
-
curl -L -o security.py https://huggingface.co/spaces/areen09/test_app_1/resolve/main/security.py
920 Bytes
| from fastapi import Depends, HTTPException, status | |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | |
| from config import settings | |
| # Instantiate the bearer scheme | |
| bearer_scheme = HTTPBearer() | |
| def verify_token(credentials: HTTPAuthorizationCredentials = Depends(bearer_scheme)): | |
| """ | |
| Dependency function to verify the bearer token. | |
| """ | |
| if not credentials or credentials.scheme!= "Bearer": | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Invalid authentication scheme.", | |
| ) | |
| # The token string is in the 'credentials' attribute, not 'token'. | |
| if credentials.credentials!= settings.API_SECRET_TOKEN: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Invalid token.", | |
| headers={"WWW-Authenticate": "Bearer"}, | |
| ) | |
| return True |