diff --git a/src/auth_playground/oauth.py b/src/auth_playground/oauth.py index 310577c..9885fcd 100644 --- a/src/auth_playground/oauth.py +++ b/src/auth_playground/oauth.py @@ -520,7 +520,9 @@ def password(): def read_id_token(token): """Return the claims of the ID token, or flash why it cannot be read. - The password grant sends no nonce, so the ID token has none to check. + No nonce is checked. The password grant sends none. Per OIDC Core ยง12.2, a + refreshed ID token should have none, and Authlib does not compare it with + the original ID token. """ try: return auth_playground.oauth.default.parse_id_token(token, nonce=None) @@ -666,6 +668,8 @@ def refresh(): store_token(new_token, "refresh_token", requested_scope=original_scope) flash(_("Token successfully refreshed"), "success") + if new_token.get("id_token"): + session["user"] = read_id_token(new_token) except AuthlibBaseError as exc: flash( _("An error happened during token refresh: {error}").format( diff --git a/src/auth_playground/translations/messages.pot b/src/auth_playground/translations/messages.pot index 5f1adf3..6fe86fd 100644 --- a/src/auth_playground/translations/messages.pot +++ b/src/auth_playground/translations/messages.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PROJECT VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2026-11-12 09:30+0100\n" +"POT-Creation-Date: 2026-11-12 09:54+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -93,8 +93,8 @@ msgstr "" #: src/auth_playground/endpoints.py:199 src/auth_playground/oauth.py:135 #: src/auth_playground/oauth.py:294 src/auth_playground/oauth.py:368 -#: src/auth_playground/oauth.py:464 src/auth_playground/oauth.py:586 -#: src/auth_playground/oauth.py:651 src/auth_playground/oauth.py:696 +#: src/auth_playground/oauth.py:464 src/auth_playground/oauth.py:588 +#: src/auth_playground/oauth.py:653 src/auth_playground/oauth.py:700 msgid "Invalid request" msgstr "" @@ -628,7 +628,7 @@ msgstr "" msgid "The client obtained tokens." msgstr "" -#: src/auth_playground/oauth.py:511 src/auth_playground/oauth.py:577 +#: src/auth_playground/oauth.py:511 src/auth_playground/oauth.py:579 msgid "You have been successfully logged in." msgstr "" @@ -637,71 +637,71 @@ msgstr "" msgid "Tokens obtained for {username}." msgstr "" -#: src/auth_playground/oauth.py:529 +#: src/auth_playground/oauth.py:531 #, python-brace-format msgid "The signing keys of the provider could not be fetched: {error}" msgstr "" -#: src/auth_playground/oauth.py:535 src/auth_playground/oauth.py:558 +#: src/auth_playground/oauth.py:537 src/auth_playground/oauth.py:560 #, python-brace-format msgid "The ID token is not valid: {error}" msgstr "" -#: src/auth_playground/oauth.py:551 src/auth_playground/oauth.py:562 +#: src/auth_playground/oauth.py:553 src/auth_playground/oauth.py:564 #, python-brace-format msgid "An error happened during login: {error}" msgstr "" -#: src/auth_playground/oauth.py:590 +#: src/auth_playground/oauth.py:592 msgid "The tokens were forgotten" msgstr "" -#: src/auth_playground/oauth.py:592 +#: src/auth_playground/oauth.py:594 msgid "You have been logged out" msgstr "" -#: src/auth_playground/oauth.py:642 +#: src/auth_playground/oauth.py:644 msgid "You have been logged out from the server" msgstr "" -#: src/auth_playground/oauth.py:656 +#: src/auth_playground/oauth.py:658 msgid "No refresh token available" msgstr "" -#: src/auth_playground/oauth.py:668 +#: src/auth_playground/oauth.py:670 msgid "Token successfully refreshed" msgstr "" -#: src/auth_playground/oauth.py:671 +#: src/auth_playground/oauth.py:675 #, python-brace-format msgid "An error happened during token refresh: {error}" msgstr "" -#: src/auth_playground/oauth.py:700 +#: src/auth_playground/oauth.py:704 msgid "No access token available" msgstr "" -#: src/auth_playground/oauth.py:704 +#: src/auth_playground/oauth.py:708 msgid "These tokens are not tied to a user." msgstr "" -#: src/auth_playground/oauth.py:709 +#: src/auth_playground/oauth.py:713 msgid "The provider has no UserInfo endpoint." msgstr "" -#: src/auth_playground/oauth.py:719 +#: src/auth_playground/oauth.py:723 msgid "The response is not JSON." msgstr "" -#: src/auth_playground/oauth.py:723 +#: src/auth_playground/oauth.py:727 msgid "The response is not a JSON object." msgstr "" -#: src/auth_playground/oauth.py:730 +#: src/auth_playground/oauth.py:734 msgid "User info received" msgstr "" -#: src/auth_playground/oauth.py:734 +#: src/auth_playground/oauth.py:738 #, python-brace-format msgid "An error happened during the UserInfo request: {error}" msgstr "" diff --git a/tests/test_authentication.py b/tests/test_authentication.py index 4d92194..870c14f 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -316,3 +316,82 @@ def test_refresh_token_error_without_description(test_client, csrf_token): ) assert "An error happened during token refresh: invalid_grant" in res.text + + +def sign_in_and_get_refresh_form(iam_server, iam_client, user, testapp): + """Sign the user in, then return the refresh form and the tokens.""" + iam_server.login(user) + iam_server.consent(user, iam_client) + res = testapp.get("/authorize") + res = iam_server.test_client.get(res.location) + testapp.get(res.location) + with testapp.session_transaction() as sess: + token = dict(sess["token"]) + return testapp.get("/en/tokens/current").forms["refresh-form"], token + + +def refresh_response(token): + """Answer the refresh request with ``token``. + + The provider of the tests sends no ID token in refresh responses. + """ + return patch( + "authlib.integrations.base_client.sync_app.OAuth2Mixin.fetch_access_token", + return_value=token, + ) + + +def test_refresh_reads_the_new_id_token(iam_server, iam_client, user, testapp): + """Test the claims shown are those of the ID token of the refresh response.""" + form, token = sign_in_and_get_refresh_form(iam_server, iam_client, user, testapp) + with testapp.session_transaction() as sess: + sess["user"] = {"sub": "old"} + + with refresh_response(token): + res = form.submit("refresh").maybe_follow() + + assert "Token successfully refreshed" in res.text + assert "The ID token is not valid" not in res.text + with testapp.session_transaction() as sess: + assert sess["user"]["sub"] == user.user_name + + +def test_refresh_with_an_invalid_id_token(iam_server, iam_client, user, testapp): + """Test an invalid ID token in the refresh response shows a warning. + + The new tokens are kept, but the user claims are not trusted anymore. + """ + form, token = sign_in_and_get_refresh_form(iam_server, iam_client, user, testapp) + new_token = {**token, "access_token": "new"} + + with ( + refresh_response(new_token), + patch( + "auth_playground.oauth_client.PlaygroundOAuth2App.parse_id_token", + side_effect=InvalidClaimError("iss"), + ), + ): + res = form.submit("refresh").maybe_follow() + + assert "Token successfully refreshed" in res.text + assert "The ID token is not valid:" in res.text + with testapp.session_transaction() as sess: + assert sess["token"]["access_token"] == "new" + assert sess["user"] is None + + +def test_refresh_without_id_token_keeps_the_user(test_client, csrf_token): + """Test a refresh response without ID token keeps the claims of the previous one.""" + with test_client.session_transaction() as sess: + sess["token"] = {"access_token": "a", "refresh_token": "r", "id_token": "i"} + sess["user"] = {"sub": "testuser"} + + with patch( + "authlib.integrations.base_client.sync_app.OAuth2Mixin.fetch_access_token", + return_value={"access_token": "b", "token_type": "Bearer"}, + ): + test_client.post("/refresh", data={"csrf_token": csrf_token(test_client)}) + + with test_client.session_transaction() as sess: + assert sess["user"] == {"sub": "testuser"} + assert sess["token"]["id_token"] == "i"