diff --git a/src/auth_playground/oauth.py b/src/auth_playground/oauth.py index 9885fcd..949000f 100644 --- a/src/auth_playground/oauth.py +++ b/src/auth_playground/oauth.py @@ -633,12 +633,31 @@ def logout(): @bp.route("/logout_callback") +@server_config_needed def logout_callback(): - """Handle callback after server-side logout.""" + """Handle callback after server-side logout. + + A response with a missing or unknown state may be forged, so the session is kept. + """ try: auth_playground.oauth.default.validate_logout_response() - except OAuthError: - pass + except OAuthError as exc: + error = exc.description or exc.error + if not session.get("token"): + flash( + _("The logout response is not valid: {error}.").format(error=error), + "error", + ) + return redirect(url_for("routes.tokens")) + + flash( + _( + "The logout response is not valid: {error}. " + "You are still logged in here." + ).format(error=error), + "error", + ) + return redirect(url_for("routes.tokens", tab="logout")) clear_user_session() flash(_("You have been logged out from the server"), "success") diff --git a/src/auth_playground/translations/messages.pot b/src/auth_playground/translations/messages.pot index 6fe86fd..740fdc9 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:54+0100\n" +"POT-Creation-Date: 2026-11-12 14:44+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -94,7 +94,7 @@ 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:588 -#: src/auth_playground/oauth.py:653 src/auth_playground/oauth.py:700 +#: src/auth_playground/oauth.py:672 src/auth_playground/oauth.py:719 msgid "Invalid request" msgstr "" @@ -660,48 +660,58 @@ msgstr "" msgid "You have been logged out" msgstr "" -#: src/auth_playground/oauth.py:644 +#: src/auth_playground/oauth.py:648 +#, python-brace-format +msgid "The logout response is not valid: {error}." +msgstr "" + +#: src/auth_playground/oauth.py:655 +#, python-brace-format +msgid "The logout response is not valid: {error}. You are still logged in here." +msgstr "" + +#: src/auth_playground/oauth.py:663 msgid "You have been logged out from the server" msgstr "" -#: src/auth_playground/oauth.py:658 +#: src/auth_playground/oauth.py:677 msgid "No refresh token available" msgstr "" -#: src/auth_playground/oauth.py:670 +#: src/auth_playground/oauth.py:689 msgid "Token successfully refreshed" msgstr "" -#: src/auth_playground/oauth.py:675 +#: src/auth_playground/oauth.py:694 #, python-brace-format msgid "An error happened during token refresh: {error}" msgstr "" -#: src/auth_playground/oauth.py:704 +#: src/auth_playground/oauth.py:723 msgid "No access token available" msgstr "" -#: src/auth_playground/oauth.py:708 +#: src/auth_playground/oauth.py:727 msgid "These tokens are not tied to a user." msgstr "" -#: src/auth_playground/oauth.py:713 +#: src/auth_playground/oauth.py:732 msgid "The provider has no UserInfo endpoint." msgstr "" -#: src/auth_playground/oauth.py:723 +#: src/auth_playground/oauth.py:742 msgid "The response is not JSON." msgstr "" -#: src/auth_playground/oauth.py:727 +#: src/auth_playground/oauth.py:746 msgid "The response is not a JSON object." msgstr "" -#: src/auth_playground/oauth.py:734 +#: src/auth_playground/oauth.py:753 msgid "User info received" msgstr "" -#: src/auth_playground/oauth.py:738 +#: src/auth_playground/oauth.py:757 #, 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 870c14f..ef333b4 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -1,5 +1,9 @@ +import html from unittest.mock import patch +from urllib.parse import parse_qs +from urllib.parse import urlsplit +import pytest import requests from authlib.integrations.base_client import OAuthError from joserfc.errors import InvalidClaimError @@ -132,18 +136,62 @@ def test_logout_redirects_to_end_session(iam_server, iam_client, user, test_clie assert "end" in res.location.lower() -def test_logout_callback_clears_session(test_client): - """Test that logout callback clears user and token from session.""" +def test_logout_callback_clears_session(iam_server, iam_client, user, test_client): + """Test the logout callback with the state of the logout request clears the session.""" + iam_server.login(user) + iam_server.consent(user, iam_client) + res = test_client.get("/authorize") + res = iam_server.test_client.get(res.location) + test_client.get(res.location) + res = test_client.get("/logout") + state = parse_qs(urlsplit(res.location).query)["state"][0] + + res = test_client.get(f"/logout_callback?state={state}", follow_redirects=True) + + assert b"You have been logged out from the server" in res.data + with test_client.session_transaction() as sess: + assert "user" not in sess + assert "token" not in sess + + +@pytest.mark.parametrize( + "query,error", + [ + ("", 'Missing "state" parameter'), + ("?state=other", 'Invalid "state" parameter'), + ], +) +def test_logout_callback_keeps_session_without_valid_state(test_client, query, error): + """Test a logout response without the state of the logout request is refused.""" with test_client.session_transaction() as sess: sess["user"] = {"sub": "testuser"} sess["token"] = {"id_token": "test_token"} - res = test_client.get("/logout_callback") - assert res.status_code == 302 + res = test_client.get(f"/logout_callback{query}", follow_redirects=True) + text = html.unescape(res.text) + assert f"The logout response is not valid: {error}." in text + assert "You are still logged in here." in text with test_client.session_transaction() as sess: - assert "user" not in sess - assert "token" not in sess + assert sess["user"] == {"sub": "testuser"} + assert sess["token"] == {"id_token": "test_token"} + + +def test_logout_callback_without_session(test_client): + """Test a logout response replayed after the logout does not claim a session.""" + res = test_client.get("/logout_callback?state=consumed", follow_redirects=True) + + text = html.unescape(res.text) + assert 'The logout response is not valid: Invalid "state" parameter.' in text + assert "You are still logged in here." not in text + + +def test_logout_callback_without_client(unconfigured_test_client): + """Test a logout response asks for the configuration when there is no client.""" + res = unconfigured_test_client.get("/logout_callback?state=state") + + assert res.status_code == 302 + assert "/server" in res.location def test_logout_local_clears_session_without_contacting_provider(testapp):