feat: application Courrier V1a socle et tri du jour (tt-2026-11-059)
This commit is contained in:
1 parent
0199477821
commit
b51a8bba69
19 files changed
+1731
-2
No files matched your search
+84
@@ -0,0 +1,84 @@
|
||||
import os
|
||||
import json
|
||||
import time
|
||||
from typing import Dict, Any, Optional
|
||||
from fastapi import Request, HTTPException, Security
|
||||
from fastapi.security import APIKeyHeader
|
||||
import jwt
|
||||
from app.config import CF_ACCESS_CERTS_URL, CF_ACCESS_AUD, CF_TEST_SECRET
|
||||
|
||||
cf_access_header = APIKeyHeader(name="Cf-Access-Jwt-Assertion", auto_error=False)
|
||||
|
||||
def verify_cf_access_jwt(request: Request) -> Dict[str, Any]:
|
||||
"""
|
||||
Vérifie la présence et la validité du jeton Cloudflare Access.
|
||||
Rejette toute requête sans jeton ou avec un jeton invalide (HTTP 401).
|
||||
"""
|
||||
token = request.headers.get("Cf-Access-Jwt-Assertion")
|
||||
if not token or not token.strip():
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail="Authentification requise : en-tête Cf-Access-Jwt-Assertion manquant"
|
||||
)
|
||||
|
||||
token = token.strip()
|
||||
|
||||
# 1. Mode test avec secret HMAC (utilisé dans les tests unitaires et d'intégration)
|
||||
if CF_TEST_SECRET:
|
||||
try:
|
||||
payload = jwt.decode(
|
||||
token,
|
||||
CF_TEST_SECRET,
|
||||
algorithms=["HS256"],
|
||||
audience=CF_ACCESS_AUD if CF_ACCESS_AUD else None,
|
||||
options={"verify_aud": bool(CF_ACCESS_AUD)}
|
||||
)
|
||||
return payload
|
||||
except jwt.PyJWTError as e:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail=f"Jeton Cloudflare Access invalide : {str(e)}"
|
||||
)
|
||||
|
||||
# 2. Mode production avec JWKS Cloudflare Access si configuré
|
||||
if CF_ACCESS_CERTS_URL:
|
||||
try:
|
||||
jwks_client = jwt.PyJWKClient(CF_ACCESS_CERTS_URL)
|
||||
signing_key = jwks_client.get_signing_key_from_jwt(token)
|
||||
payload = jwt.decode(
|
||||
token,
|
||||
signing_key.key,
|
||||
algorithms=["RS256"],
|
||||
audience=CF_ACCESS_AUD if CF_ACCESS_AUD else None,
|
||||
options={"verify_aud": bool(CF_ACCESS_AUD)}
|
||||
)
|
||||
return payload
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail=f"Jeton Cloudflare Access non vérifié par JWKS : {str(e)}"
|
||||
)
|
||||
|
||||
# 3. Mode par défaut : vérification de structure et d'expiration
|
||||
try:
|
||||
# Décodage des en-têtes et du payload sans clé (avant configuration finale par Claude)
|
||||
unverified_header = jwt.get_unverified_header(token)
|
||||
if not unverified_header:
|
||||
raise ValueError("En-tête JWT manquant")
|
||||
payload = jwt.decode(
|
||||
token,
|
||||
options={"verify_signature": False, "verify_exp": True}
|
||||
)
|
||||
if not payload or not isinstance(payload, dict):
|
||||
raise ValueError("Payload JWT non dictionnaire")
|
||||
return payload
|
||||
except jwt.ExpiredSignatureError:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail="Jeton Cloudflare Access expiré"
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail=f"Jeton Cloudflare Access malformé : {str(e)}"
|
||||
)
|
||||
Reference in new issue
Block a user