21 lines
665 B
Python
21 lines
665 B
Python
import secrets
|
|
from typing import Annotated
|
|
|
|
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import APIKeyHeader
|
|
|
|
from app.config import Settings, get_settings
|
|
|
|
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
|
|
|
|
|
|
async def require_api_key(
|
|
supplied_key: Annotated[str | None, Depends(api_key_header)],
|
|
current_settings: Annotated[Settings, Depends(get_settings)],
|
|
) -> None:
|
|
if supplied_key is None or not secrets.compare_digest(supplied_key, current_settings.api_key):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Invalid or missing API key",
|
|
)
|