Spaces:
Runtime error
Runtime error
File size: 1,034 Bytes
cc15101 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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 |