INNER CODE UNIT · Python
require_api_key
georgekhananaev/google-reviews-scraper-pro · api_server.py:33
async def require_api_key(request: Request, key: Optional[str] = Security(_api_key_header)):
"""Authenticate via DB-managed API keys. Open access when no keys exist."""
api_key_db = getattr(request.app.state, "api_key_db", None)
# DB keys required when any active key exists
if api_key_db and api_key_db.has_active_keys():
if not key:
raise HTTPException(status_code=401, detail="Missing API key")
info = api_key_db.verify_key(key)
if not info:
raise HTTPException(status_code=401, detail="Invalid or revoked API key")
request.state.api_key_info = info
return
# No keys configured — open access
request.state.api_key_info = None