File size: 441 Bytes
c1e4ce4
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
from fastapi import Header, HTTPException

from .config import settings


def require_api_key(x_api_key: str | None = Header(default=None, alias="X-API-Key")) -> None:
    # If backend_api_key is not set, auth is disabled (useful for local dev)
    if not settings.backend_api_key:
        return

    if not x_api_key or x_api_key != settings.backend_api_key:
        raise HTTPException(status_code=401, detail="Unauthorized")