Download app/core/security.py from nvikou/corrective-rag-data-assistant: direct link, hf CLI and curl.
- Browser
- Download file 1.54 kB
-
https://huggingface.co/spaces/nvikou/corrective-rag-data-assistant/resolve/main/app/core/security.py
- Command line
-
hf download hf://spaces/nvikou/corrective-rag-data-assistant/app/core/security.py
-
curl -L -o security.py https://huggingface.co/spaces/nvikou/corrective-rag-data-assistant/resolve/main/app/core/security.py
1.54 kB
| """Sécurité minimale de l'API : clé API sur l'en-tête X-API-Key. | |
| Volontairement simple (pas d'OAuth) car ce projet est une démo/portfolio | |
| déployée sur un Space Hugging Face public ; adapter selon le contexte réel | |
| (JWT, SSO, etc.) avant tout usage en production avec des données sensibles. | |
| """ | |
| from __future__ import annotations | |
| from fastapi import Header, HTTPException, status | |
| from app.core.config import get_settings | |
| async def verify_api_key(x_api_key: str | None = Header(default=None)) -> None: | |
| """Sur Space public, la clé est optionnelle sauf si explicitement requise.""" | |
| settings = get_settings() | |
| key = (settings.app_api_key or "").strip() | |
| # Démo publique : pas de clé, ou valeur placeholder → accès libre. | |
| if ( | |
| not key | |
| or key in {"change-me-please", "disabled", "none", "public"} | |
| or not settings.require_api_key | |
| ): | |
| return | |
| if x_api_key != key: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Clé API invalide ou manquante (en-tête X-API-Key).", | |
| ) | |
| MAX_QUESTION_LENGTH = 1000 | |
| def sanitize_question(question: str) -> str: | |
| question = question.strip() | |
| if not question: | |
| raise HTTPException(status_code=422, detail="La question ne peut pas être vide.") | |
| if len(question) > MAX_QUESTION_LENGTH: | |
| raise HTTPException( | |
| status_code=422, | |
| detail=f"Question trop longue (max {MAX_QUESTION_LENGTH} caractères).", | |
| ) | |
| return question | |