From eadd8fe5c33173055b65c1deba1de2c9395f98d7 Mon Sep 17 00:00:00 2001 From: Juan Garces Date: Sat, 22 Aug 2026 09:28:26 -0500 Subject: [PATCH 1/3] fix(auth): send PKCE on the OIDC authorization request Authlib only derives a code_verifier when `code_challenge_method` is set on the client, so the OIDC client registered in `decorators/auth.py` sent no `code_challenge` at all. Against a provider that mandates PKCE the user authenticates successfully and is then redirected back without a `code`, which surfaces as a generic login failure with nothing useful in the logs. Set `code_challenge_method: "S256"` in `client_kwargs`. Providers that do not support PKCE ignore the extra parameters (OIDC Core 3.1.2.1), so this is backwards-compatible, and OAuth 2.1 requires PKCE of every client. Co-Authored-By: Claude Opus 5 (1M context) --- backend/decorators/auth.py | 9 +++++++ backend/tests/decorators/__init__.py | 0 backend/tests/decorators/test_auth.py | 35 +++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 backend/tests/decorators/__init__.py create mode 100644 backend/tests/decorators/test_auth.py diff --git a/backend/decorators/auth.py b/backend/decorators/auth.py index 08e03aaba..d4da80967 100644 --- a/backend/decorators/auth.py +++ b/backend/decorators/auth.py @@ -65,6 +65,15 @@ oauth.register( client_kwargs={ "scope": f"openid profile email {OIDC_CLAIM_ROLES}".strip(), "verify": OIDC_TLS_CACERTFILE, + # PKCE (RFC 7636). Authlib only derives a code_verifier when + # code_challenge_method is set, so without this the authorization + # request carries no code_challenge at all. Providers that mandate + # PKCE then refuse to issue the code: the user authenticates fine and + # is redirected back without a `code`, surfacing as a generic login + # failure. Providers that don't support PKCE ignore the extra + # parameters (OIDC Core 3.1.2.1), so sending it unconditionally is + # backwards-compatible -- and OAuth 2.1 requires it of every client. + "code_challenge_method": "S256", }, ) diff --git a/backend/tests/decorators/__init__.py b/backend/tests/decorators/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/backend/tests/decorators/test_auth.py b/backend/tests/decorators/test_auth.py new file mode 100644 index 000000000..d97c16de8 --- /dev/null +++ b/backend/tests/decorators/test_auth.py @@ -0,0 +1,35 @@ +from urllib.parse import parse_qs, urlparse + +from authlib.integrations.httpx_client import AsyncOAuth2Client + +from decorators.auth import oauth + + +def test_oidc_client_is_configured_for_pkce(): + """The OIDC client must ask Authlib for PKCE. + + Authlib only derives a code_verifier when code_challenge_method is set on + the client, so this kwarg is what makes the whole flow PKCE-capable. + """ + assert oauth.openid.client_kwargs["code_challenge_method"] == "S256" + + +def test_authorization_url_carries_a_code_challenge(): + """The configured method must actually yield PKCE parameters. + + Guards the end of the chain: providers that mandate PKCE reject an + authorization request with no code_challenge, so asserting the kwarg + alone would not prove the request is well-formed. + """ + client = AsyncOAuth2Client( + client_id="romm", + code_challenge_method=oauth.openid.client_kwargs["code_challenge_method"], + ) + + url, _state = client.create_authorization_url( + "https://idp.example.com/authorize", code_verifier="a" * 48 + ) + + params = parse_qs(urlparse(url).query) + assert params["code_challenge_method"] == ["S256"] + assert params["code_challenge"] From 316734b3fb4411f5e7f73144e193219fc59be378 Mon Sep 17 00:00:00 2001 From: Juan Garces Date: Sat, 22 Aug 2026 13:08:07 -0500 Subject: [PATCH 2/3] style(auth): trim the PKCE comment and docstrings to repo convention CLAUDE.md caps a comment at one or two lines and a docstring at one sentence. The rationale that was inline (the failure mode, the backwards-compatibility argument, the OAuth 2.1 reference) belongs in the PR body, so what stays is the one fact the code cannot convey: Authlib derives no code_verifier unless code_challenge_method is set. Co-Authored-By: Claude Opus 5 (1M context) --- backend/decorators/auth.py | 10 ++-------- backend/tests/decorators/test_auth.py | 13 ++----------- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/backend/decorators/auth.py b/backend/decorators/auth.py index d4da80967..d525b7c7c 100644 --- a/backend/decorators/auth.py +++ b/backend/decorators/auth.py @@ -65,14 +65,8 @@ oauth.register( client_kwargs={ "scope": f"openid profile email {OIDC_CLAIM_ROLES}".strip(), "verify": OIDC_TLS_CACERTFILE, - # PKCE (RFC 7636). Authlib only derives a code_verifier when - # code_challenge_method is set, so without this the authorization - # request carries no code_challenge at all. Providers that mandate - # PKCE then refuse to issue the code: the user authenticates fine and - # is redirected back without a `code`, surfacing as a generic login - # failure. Providers that don't support PKCE ignore the extra - # parameters (OIDC Core 3.1.2.1), so sending it unconditionally is - # backwards-compatible -- and OAuth 2.1 requires it of every client. + # Authlib only derives a code_verifier when code_challenge_method is set, + # so providers that mandate PKCE refuse the code without it. "code_challenge_method": "S256", }, ) diff --git a/backend/tests/decorators/test_auth.py b/backend/tests/decorators/test_auth.py index d97c16de8..164c9cb0b 100644 --- a/backend/tests/decorators/test_auth.py +++ b/backend/tests/decorators/test_auth.py @@ -6,21 +6,12 @@ from decorators.auth import oauth def test_oidc_client_is_configured_for_pkce(): - """The OIDC client must ask Authlib for PKCE. - - Authlib only derives a code_verifier when code_challenge_method is set on - the client, so this kwarg is what makes the whole flow PKCE-capable. - """ + """Authlib only derives a code_verifier when this kwarg is set.""" assert oauth.openid.client_kwargs["code_challenge_method"] == "S256" def test_authorization_url_carries_a_code_challenge(): - """The configured method must actually yield PKCE parameters. - - Guards the end of the chain: providers that mandate PKCE reject an - authorization request with no code_challenge, so asserting the kwarg - alone would not prove the request is well-formed. - """ + """The configured method must reach the authorization URL itself.""" client = AsyncOAuth2Client( client_id="romm", code_challenge_method=oauth.openid.client_kwargs["code_challenge_method"], From 6efcd0a1e70306bc67f53aa7eb4586d524e90145 Mon Sep 17 00:00:00 2001 From: Juan Garces Date: Sat, 22 Aug 2026 15:14:57 -0500 Subject: [PATCH 3/3] test(auth): exercise the real OIDC client instead of a stand-in The previous test built its own AsyncOAuth2Client and passed an explicit code_verifier, so it asserted Authlib's behaviour rather than RomM's and passed with or without the fix. Drive oauth.openid.authorize_redirect (what /api/login/openid calls) and assert the challenge on the redirect matches the verifier saved to the session, which is the pair the callback needs to redeem the code. --- backend/tests/decorators/test_auth.py | 42 +++++++++++++++++++-------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/backend/tests/decorators/test_auth.py b/backend/tests/decorators/test_auth.py index 164c9cb0b..2c6db97c8 100644 --- a/backend/tests/decorators/test_auth.py +++ b/backend/tests/decorators/test_auth.py @@ -1,26 +1,44 @@ +import hashlib +from base64 import urlsafe_b64encode +from types import SimpleNamespace from urllib.parse import parse_qs, urlparse -from authlib.integrations.httpx_client import AsyncOAuth2Client +import pytest from decorators.auth import oauth +def _s256(verifier: str) -> str: + digest = hashlib.sha256(verifier.encode("ascii")).digest() + return urlsafe_b64encode(digest).rstrip(b"=").decode("ascii") + + def test_oidc_client_is_configured_for_pkce(): """Authlib only derives a code_verifier when this kwarg is set.""" assert oauth.openid.client_kwargs["code_challenge_method"] == "S256" -def test_authorization_url_carries_a_code_challenge(): - """The configured method must reach the authorization URL itself.""" - client = AsyncOAuth2Client( - client_id="romm", - code_challenge_method=oauth.openid.client_kwargs["code_challenge_method"], +@pytest.mark.asyncio +async def test_authorize_redirect_sends_a_challenge_matching_the_stored_verifier( + monkeypatch, +): + """The callback can only redeem the code if these two halves agree.""" + monkeypatch.setitem( + oauth.openid.server_metadata, + "authorization_endpoint", + "https://idp.example.com/authorize", + ) + monkeypatch.setitem(oauth.openid.server_metadata, "_loaded_at", 0.0) + request = SimpleNamespace(session={}) + + response = await oauth.openid.authorize_redirect( + request, "https://romm.example.com/api/oauth/openid" ) - url, _state = client.create_authorization_url( - "https://idp.example.com/authorize", code_verifier="a" * 48 - ) - - params = parse_qs(urlparse(url).query) + params = parse_qs(urlparse(response.headers["location"]).query) assert params["code_challenge_method"] == ["S256"] - assert params["code_challenge"] + + state_data = await oauth.openid.framework.get_state_data( + request.session, params["state"][0] + ) + assert params["code_challenge"] == [_s256(state_data["code_verifier"])]