Download auth/utils.py from kevinkal/magic: direct link, hf CLI and curl.
- Browser
- Download file 961 Bytes
-
https://huggingface.co/spaces/kevinkal/magic/resolve/main/auth/utils.py
- Command line
-
hf download hf://spaces/kevinkal/magic/auth/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/kevinkal/magic/resolve/main/auth/utils.py
961 Bytes
| from fastapi import HTTPException, Depends | |
| from fastapi.security import APIKeyQuery | |
| from typing import Annotated, Optional | |
| import os | |
| import uuid | |
| AUTH_TOKEN = os.getenv("AUTH_TOKEN") | |
| api_key_query = APIKeyQuery(name="token", auto_error=False) | |
| async def verify_token(token: Annotated[str, Optional[str], Depends(api_key_query)]): | |
| if not AUTH_TOKEN: | |
| raise HTTPException( | |
| status_code=500, | |
| detail="AUTH_TOKEN not configured on server" | |
| ) | |
| if not token: | |
| raise HTTPException( | |
| status_code=401, | |
| detail="Token is required" | |
| ) | |
| try: | |
| uuid.UUID(token) | |
| except ValueError: | |
| raise HTTPException( | |
| status_code=400, | |
| detail="Invalid token format. Token must be a valid UUID" | |
| ) | |
| if token != AUTH_TOKEN: | |
| raise HTTPException( | |
| status_code=401, | |
| detail="Invalid token" | |
| ) | |
| return token |