Spaces:
Runtime error
Runtime error
Download server/auth.py from TheAbyss99/abyss-api: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/TheAbyss99/abyss-api/resolve/main/server/auth.py
- Command line
-
hf download hf://spaces/TheAbyss99/abyss-api/server/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/TheAbyss99/abyss-api/resolve/main/server/auth.py
1.03 kB
| import os | |
| from fastapi import HTTPException, Security | |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | |
| _security = HTTPBearer(auto_error=False) | |
| def _load_keys() -> set: | |
| keys = set() | |
| raw = os.environ.get("ABYSS_API_KEYS", "") | |
| for k in raw.split(","): | |
| k = k.strip() | |
| if k: | |
| keys.add(k) | |
| keys_file = os.path.join(os.path.dirname(__file__), "..", "keys.txt") | |
| if os.path.exists(keys_file): | |
| with open(keys_file) as f: | |
| for line in f: | |
| line = line.strip() | |
| if line and not line.startswith("#"): | |
| keys.add(line) | |
| return keys | |
| VALID_KEYS = _load_keys() | |
| def verify_key( | |
| creds: HTTPAuthorizationCredentials = Security(_security) | |
| ) -> str: | |
| if not VALID_KEYS: | |
| return "dev" | |
| if creds is None or creds.credentials not in VALID_KEYS: | |
| raise HTTPException(status_code=401, detail="Invalid or missing API key") | |
| return creds.credentials |