2026-05-19 12:53:31 +01:00
|
|
|
import secrets
|
2026-03-26 10:58:03 +00:00
|
|
|
from typing import Annotated
|
|
|
|
|
|
|
|
|
|
from fastapi import Depends, Header, HTTPException
|
|
|
|
|
from starlette import status
|
|
|
|
|
|
|
|
|
|
from src.config import settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def api_key(host: str = Header(), authorization: str | None = Header(None)) -> bool:
|
|
|
|
|
if host.lower().strip() != settings.API_DOMAIN.strip():
|
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
try:
|
2026-05-19 12:53:31 +01:00
|
|
|
if secrets.compare_digest(authorization.split()[1], settings.API_KEY):
|
2026-03-26 10:58:03 +00:00
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
except AttributeError, TypeError, IndexError:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ApiKey = Annotated[bool, Depends(api_key)]
|