23 lines
806 B
Python
23 lines
806 B
Python
"""
|
||
Uwierzytelnianie przez klucz API przesyłany w nagłówku X-API-Key.
|
||
Klucz jest wspólny dla PHP i Pythona – przechowuj go w .env.
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
from fastapi import Security, HTTPException, status
|
||
from fastapi.security.api_key import APIKeyHeader
|
||
|
||
from .config import settings
|
||
|
||
_api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
|
||
|
||
|
||
async def require_api_key(api_key: str | None = Security(_api_key_header)) -> str:
|
||
"""Dependency: weryfikuje klucz API. Rzuca 403 jeśli klucz jest nieprawidłowy."""
|
||
if not api_key or api_key != settings.api_key:
|
||
raise HTTPException(
|
||
status_code=status.HTTP_403_FORBIDDEN,
|
||
detail="Nieprawidłowy lub brakujący klucz API (X-API-Key)",
|
||
)
|
||
return api_key
|