Spaces:
Sleeping
Sleeping
File size: 874 Bytes
b7a913f | 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 | from fastapi import Security, HTTPException, status
from fastapi.security.api_key import APIKeyHeader
import config
API_KEY_HEADER = APIKeyHeader(name="Authorization", auto_error=False)
def verify_api_key(api_key_header: str = Security(API_KEY_HEADER)):
# If no key is set in configurations, allow anonymous calls
if not config.BENCHMARK_API_KEY:
return
if not api_key_header:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="API Key is required."
)
# Extract Bearer token if present
token = api_key_header
if token.lower().startswith("bearer "):
token = token[7:].strip()
if token != config.BENCHMARK_API_KEY:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Invalid API Key."
)
|