1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402"""Unit tests for ``OIDCAuthProvider`` against a respx-mocked IdP.
Covers the OIDC contract surface: PKCE round-trip, state tampering,
expired ID token, ``aud`` mismatch, ``iss`` mismatch, nonce mismatch,
JWKS rotation handled, and provider-id binding on the tx cookie.
"""
from __future__ import annotations
import time
from typing import Any
import httpx
import pytest
import respx
from joserfc import jwt
from joserfc.jwk import KeySet, RSAKey
from pydantic import SecretStr
from src.auth.providers.oidc_provider import (
OIDCAuthProvider,
OIDCError,
OIDCProviderConfig,
)
ISSUER = "https://idp.example/"
DISCOVERY_URL = f"{ISSUER}.well-known/openid-configuration"
AUTHZ = f"{ISSUER}authorize"
TOKEN_URL = f"{ISSUER}token"
JWKS_URL = f"{ISSUER}jwks"
CLIENT_ID = "client-abc"
REDIRECT_URI = "http://app.local/api/v1/auth/callback/idp"
def _discovery_doc() -> dict[str, str]:
return {
"issuer": ISSUER,
"authorization_endpoint": AUTHZ,
"token_endpoint": TOKEN_URL,
"jwks_uri": JWKS_URL,
}
def _build_keypair() -> tuple[RSAKey, dict[str, Any]]:
"""Generate an RSA signing key + the matching JWKS the IdP would publish."""
key = RSAKey.generate_key(2048, parameters={"alg": "RS256", "use": "sig"})
jwks = KeySet([key]).as_dict()
return key, jwks
def _id_token(
key: RSAKey,
*,
iss: str = ISSUER,
aud: str | list[str] = CLIENT_ID,
nonce: str | None = "n-test",
sub: str = "user-123",
email: str | None = "alice@example.test",
name: str | None = "Alice",
exp_offset_seconds: int = 600,
) -> str:
now = int(time.time())
claims: dict[str, Any] = {
"iss": iss,
"aud": aud,
"sub": sub,
"iat": now,
"exp": now + exp_offset_seconds,
}
if nonce is not None:
claims["nonce"] = nonce
if email is not None:
claims["email"] = email
if name is not None:
claims["name"] = name
return jwt.encode({"alg": "RS256", "kid": key.kid}, claims, key)
def _make_provider() -> OIDCAuthProvider:
config = OIDCProviderConfig(
id="idp",
display_name="Sign in with IdP",
discovery_url=DISCOVERY_URL,
client_id=CLIENT_ID,
client_secret=SecretStr("client-secret-shh"),
)
return OIDCAuthProvider(config, session_ttl_seconds=3600)
@pytest.fixture
def mock_idp() -> Any:
key, jwks = _build_keypair()
with respx.mock(assert_all_called=False) as respx_mock:
respx_mock.get(DISCOVERY_URL).mock(
return_value=httpx.Response(200, json=_discovery_doc())
)
respx_mock.get(JWKS_URL).mock(return_value=httpx.Response(200, json=jwks))
respx_mock.post(TOKEN_URL).mock(
return_value=httpx.Response(
200,
json={
"access_token": "ignored",
"id_token": _id_token(key),
"token_type": "Bearer",
"expires_in": 3600,
},
)
)
# Stash the signing key on the mock so individual tests can mint
# alternative tokens (expired / wrong-aud / etc.) and override
# the token route.
respx_mock.signing_key = key # type: ignore[attr-defined]
respx_mock.jwks = jwks # type: ignore[attr-defined]
yield respx_mock
@pytest.mark.asyncio
@pytest.mark.unit
async def test_begin_authorization_builds_valid_pkce_url(mock_idp: Any) -> None:
provider = _make_provider()
auth_url, tx = await provider.begin_authorization(
redirect_uri=REDIRECT_URI, next_path="/somewhere"
)
assert auth_url.startswith(f"{AUTHZ}?")
# State, nonce, verifier all present and look like ≥32-char tokens.
assert len(tx["state"]) >= 32
assert len(tx["nonce"]) >= 32
assert len(tx["verifier"]) >= 64
assert tx["next_path"] == "/somewhere"
assert tx["provider_id"] == "idp"
# PKCE challenge is in the auth URL, not the verifier.
assert tx["verifier"] not in auth_url
assert "code_challenge=" in auth_url
assert "code_challenge_method=S256" in auth_url
@pytest.mark.asyncio
@pytest.mark.unit
async def test_complete_authorization_happy_path(mock_idp: Any) -> None:
provider = _make_provider()
_, tx = await provider.begin_authorization(redirect_uri=REDIRECT_URI, next_path="/")
# Re-mint the id_token so its nonce matches the freshly minted tx.
mock_idp.post(TOKEN_URL).mock(
return_value=httpx.Response(
200,
json={
"access_token": "ignored",
"id_token": _id_token(mock_idp.signing_key, nonce=tx["nonce"]),
"token_type": "Bearer",
},
)
)
result = await provider.complete_authorization(
code="real-code",
state_from_query=tx["state"],
tx_payload=tx,
redirect_uri=REDIRECT_URI,
)
assert result.identity.sub == "user-123"
assert result.identity.email == "alice@example.test"
assert result.identity.name == "Alice"
assert result.identity.provider_id == "idp"
assert result.next_path == "/"
@pytest.mark.asyncio
@pytest.mark.unit
async def test_state_tampering_rejected(mock_idp: Any) -> None:
provider = _make_provider()
_, tx = await provider.begin_authorization(redirect_uri=REDIRECT_URI, next_path="/")
with pytest.raises(OIDCError, match="state mismatch"):
await provider.complete_authorization(
code="real-code",
state_from_query="not-the-state",
tx_payload=tx,
redirect_uri=REDIRECT_URI,
)
@pytest.mark.asyncio
@pytest.mark.unit
async def test_provider_id_binding_rejected(mock_idp: Any) -> None:
"""A tx cookie minted for one provider must not be replayed at another."""
provider = _make_provider()
_, tx = await provider.begin_authorization(redirect_uri=REDIRECT_URI, next_path="/")
tx["provider_id"] = "different-provider"
with pytest.raises(OIDCError, match="tx cookie does not belong"):
await provider.complete_authorization(
code="real-code",
state_from_query=tx["state"],
tx_payload=tx,
redirect_uri=REDIRECT_URI,
)
@pytest.mark.asyncio
@pytest.mark.unit
async def test_nonce_mismatch_rejected(mock_idp: Any) -> None:
provider = _make_provider()
_, tx = await provider.begin_authorization(redirect_uri=REDIRECT_URI, next_path="/")
# IdP returns a token with the wrong nonce.
mock_idp.post(TOKEN_URL).mock(
return_value=httpx.Response(
200,
json={
"access_token": "x",
"id_token": _id_token(mock_idp.signing_key, nonce="some-other-nonce"),
},
)
)
with pytest.raises(OIDCError, match="nonce mismatch"):
await provider.complete_authorization(
code="real-code",
state_from_query=tx["state"],
tx_payload=tx,
redirect_uri=REDIRECT_URI,
)
@pytest.mark.asyncio
@pytest.mark.unit
async def test_aud_mismatch_rejected(mock_idp: Any) -> None:
provider = _make_provider()
_, tx = await provider.begin_authorization(redirect_uri=REDIRECT_URI, next_path="/")
mock_idp.post(TOKEN_URL).mock(
return_value=httpx.Response(
200,
json={
"access_token": "x",
"id_token": _id_token(
mock_idp.signing_key, aud="some-other-client", nonce=tx["nonce"]
),
},
)
)
with pytest.raises(OIDCError, match="aud"):
await provider.complete_authorization(
code="real-code",
state_from_query=tx["state"],
tx_payload=tx,
redirect_uri=REDIRECT_URI,
)
@pytest.mark.asyncio
@pytest.mark.unit
async def test_iss_mismatch_rejected(mock_idp: Any) -> None:
provider = _make_provider()
_, tx = await provider.begin_authorization(redirect_uri=REDIRECT_URI, next_path="/")
mock_idp.post(TOKEN_URL).mock(
return_value=httpx.Response(
200,
json={
"access_token": "x",
"id_token": _id_token(
mock_idp.signing_key,
iss="https://impostor.example/",
nonce=tx["nonce"],
),
},
)
)
with pytest.raises(OIDCError, match="issuer"):
await provider.complete_authorization(
code="real-code",
state_from_query=tx["state"],
tx_payload=tx,
redirect_uri=REDIRECT_URI,
)
@pytest.mark.asyncio
@pytest.mark.unit
async def test_expired_id_token_rejected(mock_idp: Any) -> None:
provider = _make_provider()
_, tx = await provider.begin_authorization(redirect_uri=REDIRECT_URI, next_path="/")
mock_idp.post(TOKEN_URL).mock(
return_value=httpx.Response(
200,
json={
"access_token": "x",
"id_token": _id_token(
mock_idp.signing_key,
nonce=tx["nonce"],
exp_offset_seconds=-3600,
),
},
)
)
with pytest.raises(OIDCError, match="expired"):
await provider.complete_authorization(
code="real-code",
state_from_query=tx["state"],
tx_payload=tx,
redirect_uri=REDIRECT_URI,
)
@pytest.mark.asyncio
@pytest.mark.unit
async def test_aud_as_list_with_client_id_accepted(mock_idp: Any) -> None:
"""``aud`` may be an array; we accept as long as our client_id is in it."""
provider = _make_provider()
_, tx = await provider.begin_authorization(redirect_uri=REDIRECT_URI, next_path="/")
mock_idp.post(TOKEN_URL).mock(
return_value=httpx.Response(
200,
json={
"access_token": "x",
"id_token": _id_token(
mock_idp.signing_key,
aud=[CLIENT_ID, "another-resource-server"],
nonce=tx["nonce"],
),
},
)
)
result = await provider.complete_authorization(
code="real-code",
state_from_query=tx["state"],
tx_payload=tx,
redirect_uri=REDIRECT_URI,
)
assert result.identity.sub == "user-123"
@pytest.mark.asyncio
@pytest.mark.unit
async def test_token_endpoint_error_surfaces(mock_idp: Any) -> None:
provider = _make_provider()
_, tx = await provider.begin_authorization(redirect_uri=REDIRECT_URI, next_path="/")
mock_idp.post(TOKEN_URL).mock(
return_value=httpx.Response(
400, json={"error": "invalid_grant", "error_description": "bad code"}
)
)
with pytest.raises(OIDCError, match="token exchange failed"):
await provider.complete_authorization(
code="real-code",
state_from_query=tx["state"],
tx_payload=tx,
redirect_uri=REDIRECT_URI,
)
@pytest.mark.asyncio
@pytest.mark.unit
async def test_jwks_rotation_handled(mock_idp: Any) -> None:
"""If the IdP rotated keys between our cache fill and the id_token,
the provider must refresh JWKS once and retry validation. Fixes a
common quiet-day-long-uptime failure mode.
"""
provider = _make_provider()
_, tx = await provider.begin_authorization(redirect_uri=REDIRECT_URI, next_path="/")
# Force the provider to cache the *original* JWKS first (touched
# during begin_authorization's discovery fetch). Now simulate
# rotation: a new key signs the id_token, and JWKS_URL serves it.
new_key, new_jwks = _build_keypair()
mock_idp.get(JWKS_URL).mock(return_value=httpx.Response(200, json=new_jwks))
mock_idp.post(TOKEN_URL).mock(
return_value=httpx.Response(
200,
json={
"access_token": "x",
"id_token": _id_token(new_key, nonce=tx["nonce"]),
},
)
)
# Provider has the OLD jwks cached; validation will fail, force-refresh
# JWKS, retry, and succeed.
result = await provider.complete_authorization(
code="real-code",
state_from_query=tx["state"],
tx_payload=tx,
redirect_uri=REDIRECT_URI,
)
assert result.identity.sub == "user-123"
@pytest.mark.asyncio
@pytest.mark.unit
async def test_login_url_returns_backend_kickoff() -> None:
provider = _make_provider()
url = await provider.login_url(request=None, next_path="/back/to/here")
assert url.startswith("/api/v1/auth/login/idp")
assert "next=/back/to/here" in url
@pytest.mark.asyncio
@pytest.mark.unit
async def test_handle_callback_protocol_method_raises() -> None:
"""OIDC routes call ``complete_authorization`` directly; the Protocol's
``handle_callback`` is only meaningful for shapes that don't need
cookie state, and would silently misuse the OIDC flow if invoked.
"""
provider = _make_provider()
with pytest.raises(NotImplementedError):
await provider.handle_callback(request=None)