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