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." )