Spaces:
Running
Running
| import os | |
| from fastapi import HTTPException, Security, status | |
| from fastapi.security import APIKeyHeader | |
| API_KEY_NAME = "X-API-Key" | |
| api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False) | |
| EXPECTED_API_KEY = os.getenv("BACKEND_API_KEY") | |
| async def verify_api_key(api_key: str = Security(api_key_header)): | |
| if not EXPECTED_API_KEY: | |
| raise HTTPException( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| detail="Błąd serwera: Klucz API nie został skonfigurowany." | |
| ) | |
| if api_key != EXPECTED_API_KEY: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Brak autoryzacji: Nieprawidłowy lub brakujący klucz API." | |
| ) | |
| return api_key |