18 lines
430 B
Python
18 lines
430 B
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from fastapi import Header, HTTPException
|
|
|
|
|
|
API_KEY_ENV = "API_KEY"
|
|
|
|
|
|
def require_api_key(x_api_key: str | None = Header(default=None)) -> None:
|
|
expected = os.getenv(API_KEY_ENV, "")
|
|
if not expected:
|
|
# No API key configured → allow
|
|
return
|
|
if not x_api_key or x_api_key != expected:
|
|
raise HTTPException(status_code=401, detail="invalid_api_key")
|
|
|