Files
post-moderation/ai-moderation/app/core/security.py
2026-10-20 00:37:43 +03:00

19 lines
659 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import hmac
from fastapi import HTTPException, Security
from fastapi.security import APIKeyHeader
from app.config.settings import settings
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
def verify_api_key(api_key: str | None = Security(api_key_header)) -> None:
"""Проверка общего секрета с бэкендом. Выключена, пока API_KEY не задан."""
if settings.API_KEY is None:
return
expected = settings.API_KEY.get_secret_value().encode()
if api_key is None or not hmac.compare_digest(api_key.encode(), expected):
raise HTTPException(status_code=401, detail="Invalid API key")