AI_Detector / src /auth.py
Tany Nguyen
Init repo
1182571
raw
history blame contribute delete
749 Bytes
from fastapi import Security, HTTPException, status
from fastapi.security import APIKeyHeader
from .config import Config
API_KEY_NAME = "X-API-Key"
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
def get_api_key(api_key_header: str = Security(api_key_header)):
expected_api_key = Config.API_KEY
# If no API key is set in env, fail safe
if not expected_api_key:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Server API Key not configured"
)
if api_key_header == expected_api_key:
return api_key_header
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Could not validate credentials"
)