Le jeton de rafraîchissement est une chaîne opaque de 256 bits, jamais un JWT. Il doit être révocable, donc sa ligne en base existe de toute façon, et le JWT n'ajouterait qu'un second chemin de signature. Surtout, la séparation d'avec le jeton d'accès devient structurelle : un JWT ne figure dans aucune ligne, une chaîne opaque échoue au décodage. La confusion refresh-vers-accès, qui transforme une fenêtre de 15 minutes en fenêtre de 7 jours, est impossible même si quelqu'un oublie le test. Seule l'empreinte SHA-256 est stockée. Pas d'Argon2 : l'entrée fait 256 bits de CSPRNG, aucun dictionnaire ne l'atteint, et une KDF coûterait 17 ms à chaque rafraîchissement. La rotation ne protège de rien par elle-même : elle rend la réutilisation détectable, et c'est la détection qui termine le vol. Un jeton déjà tourné révoque donc toute sa famille et laisse une trace dans `audit_log` ; un jeton expiré, lui, ne révoque rien, ce n'est pas une preuve de compromission. Les deux cas ont leur test. La revendication est une seule instruction SQL avec RETURNING. Un SELECT puis un UPDATE laisseraient une fenêtre où deux onglets réussissent la même rotation ; le test d'intégration le prouve, ce qui est indémontrable sur un double. `expires_at` est absolu et hérité du prédécesseur : s'il glissait, la promesse de sept jours serait fictive. Corrige au passage un défaut trouvé par un test : une `HTTPException` construit sa propre réponse, donc l'effacement du cookie posé sur la `Response` injectée était perdu. Un navigateur gardait un cookie mort après une détection de réutilisation.
209 lines
6.8 KiB
Python
209 lines
6.8 KiB
Python
from collections.abc import Iterator
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
from httpx import AsyncClient
|
|
|
|
from app.api.deps import get_auth_service
|
|
from app.core.principal import Principal
|
|
from app.core.roles import AccountKind, Role
|
|
from app.services.auth import (
|
|
AuthenticatedSession,
|
|
InvalidCredentialsError,
|
|
RateLimitedError,
|
|
SessionRejectedError,
|
|
)
|
|
|
|
IDENTIFIANTS = {"email": "operateur@enervision.fr", "password": "un-mot-de-passe-valide"}
|
|
|
|
PRINCIPAL = Principal(
|
|
id=uuid4(),
|
|
email="operateur@enervision.fr",
|
|
role=Role.OPERATEUR,
|
|
kind=AccountKind.HUMAIN,
|
|
must_change_password=False,
|
|
)
|
|
|
|
|
|
class FauxService:
|
|
def __init__(self, erreur: Exception | None = None) -> None:
|
|
self._erreur = erreur
|
|
|
|
async def refresh(self, **_: object) -> AuthenticatedSession:
|
|
return await self.authenticate()
|
|
|
|
async def logout(self, **_: object) -> None:
|
|
return None
|
|
|
|
async def authenticate(self, **_: object) -> AuthenticatedSession:
|
|
if self._erreur is not None:
|
|
raise self._erreur
|
|
return AuthenticatedSession(
|
|
principal=PRINCIPAL,
|
|
access_token="un.jeton.factice",
|
|
expires_in=900,
|
|
refresh_secret="un-secret-opaque",
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_auth_service(app: FastAPI) -> Iterator[list[Exception | None]]:
|
|
programme: list[Exception | None] = [None]
|
|
app.dependency_overrides[get_auth_service] = lambda: FauxService(programme[0])
|
|
yield programme
|
|
app.dependency_overrides.pop(get_auth_service, None)
|
|
|
|
|
|
async def test_login_returns_the_token_and_the_principal_when_credentials_match(
|
|
fake_auth_service: list[Exception | None], client: AsyncClient
|
|
) -> None:
|
|
response = await client.post("/api/v1/auth/login", json=IDENTIFIANTS)
|
|
|
|
assert response.status_code == 200
|
|
corps = response.json()
|
|
assert corps["access_token"] == "un.jeton.factice"
|
|
assert corps["token_type"] == "bearer"
|
|
assert corps["principal"]["role"] == "operateur"
|
|
|
|
|
|
async def test_login_forbids_intermediaries_from_caching_the_response(
|
|
fake_auth_service: list[Exception | None], client: AsyncClient
|
|
) -> None:
|
|
response = await client.post("/api/v1/auth/login", json=IDENTIFIANTS)
|
|
|
|
assert response.headers["cache-control"] == "no-store"
|
|
|
|
|
|
async def test_login_never_reveals_which_half_of_the_credentials_was_wrong(
|
|
fake_auth_service: list[Exception | None], client: AsyncClient
|
|
) -> None:
|
|
fake_auth_service[0] = InvalidCredentialsError("Identifiants invalides")
|
|
|
|
response = await client.post("/api/v1/auth/login", json=IDENTIFIANTS)
|
|
|
|
assert response.status_code == 401
|
|
assert response.json() == {"detail": "Identifiants invalides"}
|
|
|
|
|
|
async def test_login_returns_429_with_a_retry_after_when_the_rate_limit_is_reached(
|
|
fake_auth_service: list[Exception | None], client: AsyncClient
|
|
) -> None:
|
|
fake_auth_service[0] = RateLimitedError(900)
|
|
|
|
response = await client.post("/api/v1/auth/login", json=IDENTIFIANTS)
|
|
|
|
assert response.status_code == 429
|
|
assert response.headers["retry-after"] == "900"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"corps",
|
|
[
|
|
{"email": "pas-une-adresse", "password": "un-mot-de-passe-valide"},
|
|
{"email": "operateur@enervision.fr"},
|
|
{"email": "operateur@enervision.fr", "password": "x" * 129},
|
|
],
|
|
ids=["adresse_invalide", "mot_de_passe_absent", "mot_de_passe_trop_long"],
|
|
)
|
|
async def test_login_rejects_a_malformed_body_without_echoing_the_password(
|
|
fake_auth_service: list[Exception | None], client: AsyncClient, corps: dict[str, str]
|
|
) -> None:
|
|
response = await client.post("/api/v1/auth/login", json=corps)
|
|
|
|
assert response.status_code == 422
|
|
assert "un-mot-de-passe-valide" not in response.text
|
|
assert "x" * 129 not in response.text
|
|
|
|
|
|
async def test_login_posts_an_http_only_refresh_cookie_scoped_to_the_auth_routes(
|
|
fake_auth_service: list[Exception | None], client: AsyncClient
|
|
) -> None:
|
|
response = await client.post("/api/v1/auth/login", json=IDENTIFIANTS)
|
|
|
|
depose = response.headers["set-cookie"]
|
|
assert depose.startswith("ev_refresh=un-secret-opaque")
|
|
assert "HttpOnly" in depose
|
|
assert "SameSite=strict" in depose
|
|
assert "Path=/api/v1/auth" in depose
|
|
|
|
|
|
async def test_login_keeps_the_refresh_secret_out_of_the_response_body(
|
|
fake_auth_service: list[Exception | None], client: AsyncClient
|
|
) -> None:
|
|
response = await client.post("/api/v1/auth/login", json=IDENTIFIANTS)
|
|
|
|
assert "un-secret-opaque" not in response.text
|
|
|
|
|
|
async def test_refresh_returns_401_when_no_cookie_is_presented(
|
|
fake_auth_service: list[Exception | None], client: AsyncClient
|
|
) -> None:
|
|
response = await client.post("/api/v1/auth/refresh")
|
|
|
|
assert response.status_code == 401
|
|
|
|
|
|
async def test_refresh_rotates_the_cookie_when_the_session_is_still_valid(
|
|
fake_auth_service: list[Exception | None], client: AsyncClient
|
|
) -> None:
|
|
client.cookies.set("ev_refresh", "un-secret-opaque")
|
|
|
|
response = await client.post("/api/v1/auth/refresh")
|
|
|
|
assert response.status_code == 200
|
|
assert "ev_refresh=" in response.headers["set-cookie"]
|
|
|
|
|
|
async def test_refresh_clears_the_cookie_when_the_session_is_rejected(
|
|
fake_auth_service: list[Exception | None], client: AsyncClient
|
|
) -> None:
|
|
fake_auth_service[0] = SessionRejectedError("Session révoquée")
|
|
client.cookies.set("ev_refresh", "un-secret-rejoue")
|
|
|
|
response = await client.post("/api/v1/auth/refresh")
|
|
|
|
assert response.status_code == 401
|
|
assert 'ev_refresh=""' in response.headers["set-cookie"]
|
|
assert "Path=/api/v1/auth" in response.headers["set-cookie"]
|
|
|
|
|
|
async def test_logout_answers_204_and_clears_the_cookie(
|
|
fake_auth_service: list[Exception | None], client: AsyncClient
|
|
) -> None:
|
|
client.cookies.set("ev_refresh", "un-secret-opaque")
|
|
|
|
response = await client.post("/api/v1/auth/logout")
|
|
|
|
assert response.status_code == 204
|
|
assert 'ev_refresh=""' in response.headers["set-cookie"]
|
|
|
|
|
|
async def test_logout_stays_idempotent_without_a_cookie(
|
|
fake_auth_service: list[Exception | None], client: AsyncClient
|
|
) -> None:
|
|
response = await client.post("/api/v1/auth/logout")
|
|
|
|
assert response.status_code == 204
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"chemin",
|
|
["/api/v1/auth/refresh", "/api/v1/auth/logout"],
|
|
ids=["rotation", "deconnexion"],
|
|
)
|
|
async def test_a_cookie_bearing_route_refuses_a_foreign_origin(
|
|
fake_auth_service: list[Exception | None], client: AsyncClient, chemin: str
|
|
) -> None:
|
|
response = await client.post(chemin, headers={"Origin": "https://malveillant.example"})
|
|
|
|
assert response.status_code == 403
|
|
|
|
|
|
async def test_a_cookie_bearing_route_accepts_a_request_without_origin(
|
|
fake_auth_service: list[Exception | None], client: AsyncClient
|
|
) -> None:
|
|
response = await client.post("/api/v1/auth/logout")
|
|
|
|
assert response.status_code != 403
|