diff --git a/src/auth_playground/forms.py b/src/auth_playground/forms.py index 810dad1..adb10a2 100644 --- a/src/auth_playground/forms.py +++ b/src/auth_playground/forms.py @@ -183,6 +183,12 @@ class RefreshTokenForm(BaseForm): refresh = SubmitField(_("Renew tokens")) +class UserInfoForm(BaseForm): + """Form to call the UserInfo endpoint with the access token.""" + + userinfo = SubmitField(_("Get user info")) + + def is_loopback_address(hostname): """Tell whether the hostname is a loopback IP address.""" try: diff --git a/src/auth_playground/oauth.py b/src/auth_playground/oauth.py index d6be45c..310577c 100644 --- a/src/auth_playground/oauth.py +++ b/src/auth_playground/oauth.py @@ -46,6 +46,7 @@ from auth_playground.forms import PasswordForm from auth_playground.forms import RefreshTokenForm from auth_playground.forms import UnregisterClientForm +from auth_playground.forms import UserInfoForm from auth_playground.grants import tokens_grant from auth_playground.pages import render_tokens_page from auth_playground.session import server_metadata @@ -93,9 +94,19 @@ def get_response_error(exc: requests.RequestException) -> str | None: return error if isinstance(error, str) else None +def www_authenticate(exc: requests.RequestException) -> str | None: + """Get the WWW-Authenticate header of an error response. + + Per RFC 6750 §3, a protected resource gives its error there. + """ + if exc.response is None: + return None + return exc.response.headers.get("WWW-Authenticate") + + def forget_tokens(): """Forget the user and the tokens.""" - for key in ("user", "token", "token_info"): + for key in ("user", "token", "token_info", "userinfo"): session.pop(key, None) @@ -400,6 +411,7 @@ def store_token(token, grant_type, requested_scope=None, pkce=None): """ old_token = session.get("token", {}) old_info = session.get("token_info", {}) + session.pop("userinfo", None) session["token"] = { "access_token": token.get("access_token"), "refresh_token": token.get("refresh_token") or old_token.get("refresh_token"), @@ -663,3 +675,63 @@ def refresh(): ) return redirect(url_for("routes.tokens")) + + +def bearer_token(): + """Return the access token alone, to send it to the provider as it is. + + With an expiry date or a refresh token, Authlib could refuse the token + before sending it, or refresh it without the playground knowing. + """ + token = session.get("token", {}) + return {key: token[key] for key in ("access_token", "token_type") if token.get(key)} + + +@bp.route("/userinfo", methods=["POST"]) +@server_config_needed +def userinfo(): + """Call the UserInfo endpoint with the access token.""" + form = UserInfoForm() + if not form.validate_on_submit(): + flash(_("Invalid request"), "error") + return redirect(url_for("routes.tokens")) + + if not session.get("token", {}).get("access_token"): + flash(_("No access token available"), "error") + return redirect(url_for("routes.tokens")) + + if not tokens_grant().user: + flash(_("These tokens are not tied to a user."), "error") + return redirect(url_for("routes.tokens")) + + endpoint = server_metadata().get("userinfo_endpoint") + if not endpoint: + flash(_("The provider has no UserInfo endpoint."), "error") + return redirect(url_for("routes.tokens")) + + try: + claims = auth_playground.oauth.default.userinfo( + token=bearer_token(), timeout=10 + ) + except AuthlibBaseError as exc: + error = exc.description or exc.error + except requests.JSONDecodeError: + error = _("The response is not JSON.") + except requests.RequestException as exc: + error = get_response_error(exc) or www_authenticate(exc) or exc + except (TypeError, ValueError): + error = _("The response is not a JSON object.") + else: + session["userinfo"] = { + "endpoint": endpoint, + "received_at": int(time.time()), + "claims": dict(claims), + } + flash(_("User info received"), "success") + return redirect(url_for("routes.tokens")) + + flash( + _("An error happened during the UserInfo request: {error}").format(error=error), + "error", + ) + return redirect(url_for("routes.tokens")) diff --git a/src/auth_playground/pages.py b/src/auth_playground/pages.py index a55ea85..8c0212b 100644 --- a/src/auth_playground/pages.py +++ b/src/auth_playground/pages.py @@ -15,8 +15,10 @@ from auth_playground.forms import LogoutParamsForm from auth_playground.forms import PasswordForm from auth_playground.forms import RefreshTokenForm +from auth_playground.forms import UserInfoForm from auth_playground.grants import get_grant from auth_playground.grants import tokens_grant +from auth_playground.session import server_metadata GRANT_FORMS = { "authorization_code": AuthorizationParamsForm, @@ -52,6 +54,8 @@ def render_tokens_page(tab, grant=None, grant_form=None): "tokens.html", tab=tab, refresh_form=RefreshTokenForm(), + userinfo_form=UserInfoForm(), + userinfo_endpoint=server_metadata().get("userinfo_endpoint"), grant=grant, tokens_grant=tokens_grant(), public_client=oauth_config.get("token_endpoint_auth_method") == PUBLIC_METHOD, diff --git a/src/auth_playground/templates/_tokens.html b/src/auth_playground/templates/_tokens.html index a504f9f..3ef1a15 100644 --- a/src/auth_playground/templates/_tokens.html +++ b/src/auth_playground/templates/_tokens.html @@ -201,6 +201,17 @@

{% trans %}Token metadata{% endtrans %}

{% endif %} + {% if session.userinfo %} +
+

{% trans %}UserInfo response{% endtrans %}

+ +
{{ session.userinfo.claims|tojson(indent=2) }}
+
+ {% endif %} +
{% if session.token and session.token.access_token %}
diff --git a/src/auth_playground/templates/tokens.html b/src/auth_playground/templates/tokens.html index 66380fd..44bfafe 100644 --- a/src/auth_playground/templates/tokens.html +++ b/src/auth_playground/templates/tokens.html @@ -24,6 +24,11 @@

{% trans %}Silent refresh — auth_time and acr stay unchanged.{% endtrans %}

{% endif %} {% endif %} + {% if userinfo_endpoint and tokens_grant.user and session.token.access_token %} + {% call render_form(userinfo_form, url_for('oauth.userinfo'), id="userinfo-form") %} + {{ userinfo_form.userinfo() }} + {% endcall %} + {% endif %} {% include "_tokens.html" %} {% if not tokens_grant.provider_session %} {% call render_form(logout_local_form, url_for('oauth.logout_local'), id="forget-tokens-form") %} diff --git a/src/auth_playground/translations/messages.pot b/src/auth_playground/translations/messages.pot index 96a010f..5f1adf3 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-07 08:52+0100\n" +"POT-Creation-Date: 2026-11-12 09:30+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -91,10 +91,10 @@ msgstr "" msgid "Server metadata successfully loaded" msgstr "" -#: src/auth_playground/endpoints.py:199 src/auth_playground/oauth.py:124 -#: src/auth_playground/oauth.py:283 src/auth_playground/oauth.py:357 -#: src/auth_playground/oauth.py:452 src/auth_playground/oauth.py:574 -#: src/auth_playground/oauth.py:639 +#: 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 msgid "Invalid request" msgstr "" @@ -138,365 +138,369 @@ msgstr "" msgid "Renew tokens" msgstr "" -#: src/auth_playground/forms.py:199 +#: src/auth_playground/forms.py:189 +msgid "Get user info" +msgstr "" + +#: src/auth_playground/forms.py:205 msgid "URL must start with http:// or https://" msgstr "" -#: src/auth_playground/forms.py:212 +#: src/auth_playground/forms.py:218 msgid "" "HTTP is only allowed for localhost or in debug/testing mode. Use HTTPS in" " production." msgstr "" -#: src/auth_playground/forms.py:221 +#: src/auth_playground/forms.py:227 msgid "Provider URL:" msgstr "" -#: src/auth_playground/forms.py:223 +#: src/auth_playground/forms.py:229 msgid "Provider URL is required" msgstr "" -#: src/auth_playground/forms.py:232 +#: src/auth_playground/forms.py:238 msgid "Continue" msgstr "" -#: src/auth_playground/forms.py:239 +#: src/auth_playground/forms.py:245 #, python-brace-format msgid "{value} (not advertised)" msgstr "" -#: src/auth_playground/forms.py:339 +#: src/auth_playground/forms.py:345 msgid "Client key:" msgstr "" -#: src/auth_playground/forms.py:341 +#: src/auth_playground/forms.py:347 msgid "Generate a key" msgstr "" -#: src/auth_playground/forms.py:342 +#: src/auth_playground/forms.py:348 msgid "Import a key" msgstr "" -#: src/auth_playground/forms.py:346 +#: src/auth_playground/forms.py:352 msgid "The client signs its token requests with this key." msgstr "" -#: src/auth_playground/forms.py:349 +#: src/auth_playground/forms.py:355 msgid "Private key:" msgstr "" -#: src/auth_playground/forms.py:352 +#: src/auth_playground/forms.py:358 msgid "PEM or JWK: RSA, EC or Ed25519. Kept in your session, never shown again." msgstr "" -#: src/auth_playground/forms.py:362 +#: src/auth_playground/forms.py:368 msgid "Key ID (kid):" msgstr "" -#: src/auth_playground/forms.py:365 +#: src/auth_playground/forms.py:371 msgid "" "Sent in the assertion header. Leave empty to use the key thumbprint (RFC " "7638)." msgstr "" -#: src/auth_playground/forms.py:370 +#: src/auth_playground/forms.py:376 msgid "Signing algorithm:" msgstr "" -#: src/auth_playground/forms.py:373 +#: src/auth_playground/forms.py:379 msgid "From token_endpoint_auth_signing_alg_values_supported." msgstr "" -#: src/auth_playground/forms.py:376 +#: src/auth_playground/forms.py:382 msgid "Generate another key" msgstr "" -#: src/auth_playground/forms.py:463 +#: src/auth_playground/forms.py:469 msgid "Paste the private key of the client." msgstr "" -#: src/auth_playground/forms.py:474 src/auth_playground/forms.py:872 +#: src/auth_playground/forms.py:480 src/auth_playground/forms.py:878 #: src/auth_playground/templates/configure_client.html:43 msgid "Client ID:" msgstr "" -#: src/auth_playground/forms.py:476 +#: src/auth_playground/forms.py:482 msgid "Client ID is required" msgstr "" -#: src/auth_playground/forms.py:480 +#: src/auth_playground/forms.py:486 msgid "Client ID must be between 1 and 255 characters" msgstr "" -#: src/auth_playground/forms.py:486 src/auth_playground/forms.py:544 +#: src/auth_playground/forms.py:492 src/auth_playground/forms.py:550 msgid "Token endpoint authentication:" msgstr "" -#: src/auth_playground/forms.py:489 +#: src/auth_playground/forms.py:495 msgid "How the client proves its identity to the token endpoint." msgstr "" -#: src/auth_playground/forms.py:492 +#: src/auth_playground/forms.py:498 msgid "Client secret:" msgstr "" -#: src/auth_playground/forms.py:493 +#: src/auth_playground/forms.py:499 msgid "Leave empty for a public client." msgstr "" -#: src/auth_playground/forms.py:504 +#: src/auth_playground/forms.py:510 msgid "Complete configuration" msgstr "" -#: src/auth_playground/forms.py:522 +#: src/auth_playground/forms.py:528 msgid "This authentication method needs a client secret." msgstr "" -#: src/auth_playground/forms.py:528 +#: src/auth_playground/forms.py:534 msgid "A public client has no secret. Leave the secret empty." msgstr "" -#: src/auth_playground/forms.py:536 +#: src/auth_playground/forms.py:542 msgid "Client type:" msgstr "" -#: src/auth_playground/forms.py:538 +#: src/auth_playground/forms.py:544 msgid "Confidential, with a secret or a key" msgstr "" -#: src/auth_playground/forms.py:539 +#: src/auth_playground/forms.py:545 msgid "Public, without a secret, with PKCE" msgstr "" -#: src/auth_playground/forms.py:547 +#: src/auth_playground/forms.py:553 msgid "A public client uses none." msgstr "" -#: src/auth_playground/forms.py:560 +#: src/auth_playground/forms.py:566 msgid "Public key sent to the provider:" msgstr "" -#: src/auth_playground/forms.py:562 +#: src/auth_playground/forms.py:568 msgid "jwks: the key goes in the registration request" msgstr "" -#: src/auth_playground/forms.py:566 +#: src/auth_playground/forms.py:572 msgid "" "jwks_uri: the provider downloads the key from the playground, which must " "be reachable from the provider" msgstr "" -#: src/auth_playground/forms.py:575 +#: src/auth_playground/forms.py:581 msgid "Grant types to register:" msgstr "" -#: src/auth_playground/forms.py:582 +#: src/auth_playground/forms.py:588 msgid "Initial access token:" msgstr "" -#: src/auth_playground/forms.py:585 +#: src/auth_playground/forms.py:591 msgid "Leave empty if not required" msgstr "" -#: src/auth_playground/forms.py:589 +#: src/auth_playground/forms.py:595 msgid "Register client" msgstr "" -#: src/auth_playground/forms.py:606 +#: src/auth_playground/forms.py:612 msgid "Choose at least one grant type." msgstr "" -#: src/auth_playground/forms.py:612 +#: src/auth_playground/forms.py:618 msgid "Change provider" msgstr "" -#: src/auth_playground/forms.py:618 +#: src/auth_playground/forms.py:624 msgid "Unregister client" msgstr "" -#: src/auth_playground/forms.py:624 +#: src/auth_playground/forms.py:630 msgid "Log out" msgstr "" -#: src/auth_playground/forms.py:631 +#: src/auth_playground/forms.py:637 msgid "Disabled" msgstr "" -#: src/auth_playground/forms.py:682 +#: src/auth_playground/forms.py:688 msgid "Scopes:" msgstr "" -#: src/auth_playground/forms.py:685 +#: src/auth_playground/forms.py:691 msgid "Select the scopes to request from the authorization server" msgstr "" -#: src/auth_playground/forms.py:704 +#: src/auth_playground/forms.py:710 msgid "Other scopes:" msgstr "" -#: src/auth_playground/forms.py:705 +#: src/auth_playground/forms.py:711 msgid "Space-separated, sent as they are." msgstr "" -#: src/auth_playground/forms.py:708 +#: src/auth_playground/forms.py:714 msgid "Extra parameters:" msgstr "" -#: src/auth_playground/forms.py:709 +#: src/auth_playground/forms.py:715 msgid "One name=value per line, added to the request as they are." msgstr "" -#: src/auth_playground/forms.py:722 +#: src/auth_playground/forms.py:728 #, python-brace-format msgid "" "Each line must be name=value, with a parameter the playground does not " "set itself: {errors}" msgstr "" -#: src/auth_playground/forms.py:734 +#: src/auth_playground/forms.py:740 #: src/auth_playground/templates/_tokens.html:14 msgid "PKCE:" msgstr "" -#: src/auth_playground/forms.py:738 +#: src/auth_playground/forms.py:744 msgid "Sends a code_challenge here, and the code_verifier with the token request." msgstr "" -#: src/auth_playground/forms.py:743 +#: src/auth_playground/forms.py:749 msgid "Prompt:" msgstr "" -#: src/auth_playground/forms.py:745 +#: src/auth_playground/forms.py:751 msgid "Control the authentication UI behavior" msgstr "" -#: src/auth_playground/forms.py:748 src/auth_playground/forms.py:884 +#: src/auth_playground/forms.py:754 src/auth_playground/forms.py:890 msgid "UI Locale:" msgstr "" -#: src/auth_playground/forms.py:751 +#: src/auth_playground/forms.py:757 msgid "Preferred language for the authentication UI" msgstr "" -#: src/auth_playground/forms.py:754 +#: src/auth_playground/forms.py:760 msgid "Max age (seconds):" msgstr "" -#: src/auth_playground/forms.py:757 +#: src/auth_playground/forms.py:763 msgid "" "Maximum authentication age. 0 forces re-authentication and refreshes " "auth_time." msgstr "" -#: src/auth_playground/forms.py:762 +#: src/auth_playground/forms.py:768 msgid "ACR values:" msgstr "" -#: src/auth_playground/forms.py:764 +#: src/auth_playground/forms.py:770 msgid "" "Requested authentication context classes, space-separated (step-up " "authentication)." msgstr "" -#: src/auth_playground/forms.py:768 +#: src/auth_playground/forms.py:774 msgid "Login hint:" msgstr "" -#: src/auth_playground/forms.py:769 +#: src/auth_playground/forms.py:775 msgid "Identifier to pre-fill on the provider's login screen." msgstr "" -#: src/auth_playground/forms.py:772 +#: src/auth_playground/forms.py:778 msgid "Claims (JSON):" msgstr "" -#: src/auth_playground/forms.py:774 +#: src/auth_playground/forms.py:780 msgid "Specific claims to request, as an OIDC claims JSON object (advanced)." msgstr "" -#: src/auth_playground/forms.py:778 +#: src/auth_playground/forms.py:784 msgid "Authorization request" msgstr "" -#: src/auth_playground/forms.py:780 +#: src/auth_playground/forms.py:786 msgid "Sign in" msgstr "" -#: src/auth_playground/forms.py:792 +#: src/auth_playground/forms.py:798 msgid "Claims must be a JSON object." msgstr "" -#: src/auth_playground/forms.py:803 src/auth_playground/forms.py:827 +#: src/auth_playground/forms.py:809 src/auth_playground/forms.py:833 msgid "Token request" msgstr "" -#: src/auth_playground/forms.py:805 src/auth_playground/forms.py:829 +#: src/auth_playground/forms.py:811 src/auth_playground/forms.py:835 msgid "Request tokens" msgstr "" -#: src/auth_playground/forms.py:815 +#: src/auth_playground/forms.py:821 msgid "Username:" msgstr "" -#: src/auth_playground/forms.py:820 +#: src/auth_playground/forms.py:826 msgid "Password:" msgstr "" -#: src/auth_playground/forms.py:836 +#: src/auth_playground/forms.py:842 msgid "deprecated" msgstr "" -#: src/auth_playground/forms.py:838 +#: src/auth_playground/forms.py:844 msgid "not advertised" msgstr "" -#: src/auth_playground/forms.py:841 +#: src/auth_playground/forms.py:847 #, python-brace-format msgid "{label} ({marks})" msgstr "" -#: src/auth_playground/forms.py:860 +#: src/auth_playground/forms.py:866 #: src/auth_playground/templates/_tokens.html:6 msgid "Grant type:" msgstr "" -#: src/auth_playground/forms.py:861 +#: src/auth_playground/forms.py:867 msgid "Choose" msgstr "" -#: src/auth_playground/forms.py:868 +#: src/auth_playground/forms.py:874 msgid "ID Token Hint:" msgstr "" -#: src/auth_playground/forms.py:869 +#: src/auth_playground/forms.py:875 msgid "ID Token previously issued, used to identify the user session" msgstr "" -#: src/auth_playground/forms.py:873 +#: src/auth_playground/forms.py:879 msgid "OAuth client identifier" msgstr "" -#: src/auth_playground/forms.py:876 +#: src/auth_playground/forms.py:882 msgid "Post-Logout Redirect URI:" msgstr "" -#: src/auth_playground/forms.py:877 +#: src/auth_playground/forms.py:883 msgid "URI to redirect to after logout" msgstr "" -#: src/auth_playground/forms.py:880 +#: src/auth_playground/forms.py:886 msgid "Logout Hint:" msgstr "" -#: src/auth_playground/forms.py:881 +#: src/auth_playground/forms.py:887 msgid "Hint about the user to log out" msgstr "" -#: src/auth_playground/forms.py:886 +#: src/auth_playground/forms.py:892 msgid "Preferred language for the logout UI" msgstr "" -#: src/auth_playground/forms.py:888 +#: src/auth_playground/forms.py:894 msgid "Server log out" msgstr "" @@ -539,28 +543,28 @@ msgid "" "to the token endpoint. The provider never sees the user sign in." msgstr "" -#: src/auth_playground/oauth.py:129 +#: src/auth_playground/oauth.py:140 msgid "Dynamic client registration not supported" msgstr "" -#: src/auth_playground/oauth.py:185 +#: src/auth_playground/oauth.py:196 msgid "Invalid JSON response from registration endpoint" msgstr "" -#: src/auth_playground/oauth.py:191 +#: src/auth_playground/oauth.py:202 #, python-brace-format msgid "Dynamic client registration failed: {error}" msgstr "" -#: src/auth_playground/oauth.py:195 +#: src/auth_playground/oauth.py:206 msgid "Dynamic client registration failed" msgstr "" -#: src/auth_playground/oauth.py:199 +#: src/auth_playground/oauth.py:210 msgid "Invalid response from registration endpoint" msgstr "" -#: src/auth_playground/oauth.py:206 +#: src/auth_playground/oauth.py:217 #, python-brace-format msgid "" "The server registered the client with the {auth_method} authentication " @@ -568,7 +572,7 @@ msgid "" "registered on the server." msgstr "" -#: src/auth_playground/oauth.py:218 +#: src/auth_playground/oauth.py:229 #, python-brace-format msgid "" "The server registered the client without a client secret, but its " @@ -576,103 +580,132 @@ msgid "" "registered on the server." msgstr "" -#: src/auth_playground/oauth.py:229 +#: src/auth_playground/oauth.py:240 msgid "" "The server registered the client with the private_key_jwt authentication " "method, but the playground has no key for it. The client is still " "registered on the server." msgstr "" -#: src/auth_playground/oauth.py:244 +#: src/auth_playground/oauth.py:255 #, python-brace-format msgid "" "The server registered the client with the {alg} signing algorithm, which " "its key cannot use. The client is still registered on the server." msgstr "" -#: src/auth_playground/oauth.py:270 +#: src/auth_playground/oauth.py:281 msgid "Client successfully registered!" msgstr "" -#: src/auth_playground/oauth.py:290 +#: src/auth_playground/oauth.py:301 msgid "Client registration management credentials not found" msgstr "" -#: src/auth_playground/oauth.py:304 +#: src/auth_playground/oauth.py:315 #, python-brace-format msgid "Client unregistration failed: {error}" msgstr "" -#: src/auth_playground/oauth.py:308 +#: src/auth_playground/oauth.py:319 msgid "Client unregistration failed" msgstr "" -#: src/auth_playground/oauth.py:316 +#: src/auth_playground/oauth.py:327 msgid "Client successfully unregistered" msgstr "" -#: src/auth_playground/oauth.py:428 +#: src/auth_playground/oauth.py:440 msgid "The provider has no token endpoint." msgstr "" -#: src/auth_playground/oauth.py:445 +#: src/auth_playground/oauth.py:457 #, python-brace-format msgid "The token request failed: {error}" msgstr "" -#: src/auth_playground/oauth.py:472 +#: src/auth_playground/oauth.py:484 msgid "The client obtained tokens." msgstr "" -#: src/auth_playground/oauth.py:499 src/auth_playground/oauth.py:565 +#: src/auth_playground/oauth.py:511 src/auth_playground/oauth.py:577 msgid "You have been successfully logged in." msgstr "" -#: src/auth_playground/oauth.py:502 +#: src/auth_playground/oauth.py:514 #, python-brace-format msgid "Tokens obtained for {username}." msgstr "" -#: src/auth_playground/oauth.py:517 +#: src/auth_playground/oauth.py:529 #, python-brace-format msgid "The signing keys of the provider could not be fetched: {error}" msgstr "" -#: src/auth_playground/oauth.py:523 src/auth_playground/oauth.py:546 +#: src/auth_playground/oauth.py:535 src/auth_playground/oauth.py:558 #, python-brace-format msgid "The ID token is not valid: {error}" msgstr "" -#: src/auth_playground/oauth.py:539 src/auth_playground/oauth.py:550 +#: src/auth_playground/oauth.py:551 src/auth_playground/oauth.py:562 #, python-brace-format msgid "An error happened during login: {error}" msgstr "" -#: src/auth_playground/oauth.py:578 +#: src/auth_playground/oauth.py:590 msgid "The tokens were forgotten" msgstr "" -#: src/auth_playground/oauth.py:580 +#: src/auth_playground/oauth.py:592 msgid "You have been logged out" msgstr "" -#: src/auth_playground/oauth.py:630 +#: src/auth_playground/oauth.py:642 msgid "You have been logged out from the server" msgstr "" -#: src/auth_playground/oauth.py:644 +#: src/auth_playground/oauth.py:656 msgid "No refresh token available" msgstr "" -#: src/auth_playground/oauth.py:656 +#: src/auth_playground/oauth.py:668 msgid "Token successfully refreshed" msgstr "" -#: src/auth_playground/oauth.py:659 +#: src/auth_playground/oauth.py:671 #, python-brace-format msgid "An error happened during token refresh: {error}" msgstr "" +#: src/auth_playground/oauth.py:700 +msgid "No access token available" +msgstr "" + +#: src/auth_playground/oauth.py:704 +msgid "These tokens are not tied to a user." +msgstr "" + +#: src/auth_playground/oauth.py:709 +msgid "The provider has no UserInfo endpoint." +msgstr "" + +#: src/auth_playground/oauth.py:719 +msgid "The response is not JSON." +msgstr "" + +#: src/auth_playground/oauth.py:723 +msgid "The response is not a JSON object." +msgstr "" + +#: src/auth_playground/oauth.py:730 +msgid "User info received" +msgstr "" + +#: src/auth_playground/oauth.py:734 +#, python-brace-format +msgid "An error happened during the UserInfo request: {error}" +msgstr "" + #: src/auth_playground/steps.py:31 #, python-brace-format msgid "Step {number}, done:" @@ -774,8 +807,8 @@ msgstr "" #: src/auth_playground/templates/_client_key_preview.html:21 #: src/auth_playground/templates/_client_key_preview.html:27 #: src/auth_playground/templates/_tokens.html:36 -#: src/auth_playground/templates/_tokens.html:215 -#: src/auth_playground/templates/_tokens.html:240 +#: src/auth_playground/templates/_tokens.html:226 +#: src/auth_playground/templates/_tokens.html:251 #: src/auth_playground/templates/configure_client.html:20 msgid "Copy" msgstr "" @@ -979,7 +1012,7 @@ msgid "Issued at:" msgstr "" #: src/auth_playground/templates/_tokens.html:165 -#: src/auth_playground/templates/_tokens.html:225 +#: src/auth_playground/templates/_tokens.html:236 msgid "Expires at:" msgstr "" @@ -1007,44 +1040,56 @@ msgstr "" msgid "Authorized party:" msgstr "" -#: src/auth_playground/templates/_tokens.html:207 -msgid "Access token" +#: src/auth_playground/templates/_tokens.html:206 +msgid "UserInfo response" +msgstr "" + +#: src/auth_playground/templates/_tokens.html:208 +msgid "UserInfo endpoint:" msgstr "" #: src/auth_playground/templates/_tokens.html:209 +msgid "Received at:" +msgstr "" + +#: src/auth_playground/templates/_tokens.html:218 +msgid "Access token" +msgstr "" + +#: src/auth_playground/templates/_tokens.html:220 msgid "Used to access protected resources on behalf of the client." msgstr "" -#: src/auth_playground/templates/_tokens.html:211 +#: src/auth_playground/templates/_tokens.html:222 msgid "Used to access protected resources on behalf of the user." msgstr "" -#: src/auth_playground/templates/_tokens.html:219 +#: src/auth_playground/templates/_tokens.html:230 msgid "Token type:" msgstr "" -#: src/auth_playground/templates/_tokens.html:222 +#: src/auth_playground/templates/_tokens.html:233 msgid "Expires in:" msgstr "" -#: src/auth_playground/templates/_tokens.html:222 +#: src/auth_playground/templates/_tokens.html:233 #, python-format msgid "%(seconds)s seconds" msgstr "" -#: src/auth_playground/templates/_tokens.html:228 +#: src/auth_playground/templates/_tokens.html:239 msgid "Scope:" msgstr "" -#: src/auth_playground/templates/_tokens.html:236 +#: src/auth_playground/templates/_tokens.html:247 msgid "Refresh token" msgstr "" -#: src/auth_playground/templates/_tokens.html:237 +#: src/auth_playground/templates/_tokens.html:248 msgid "Used to obtain new access tokens without re-authentication." msgstr "" -#: src/auth_playground/templates/_tokens.html:248 +#: src/auth_playground/templates/_tokens.html:259 msgid "Token response" msgstr "" @@ -1506,27 +1551,27 @@ msgstr "" msgid "Silent refresh — auth_time and acr stay unchanged." msgstr "" -#: src/auth_playground/templates/tokens.html:30 +#: src/auth_playground/templates/tokens.html:35 msgid "Forget tokens" msgstr "" -#: src/auth_playground/templates/tokens.html:38 +#: src/auth_playground/templates/tokens.html:43 msgid "" "Local — this app only; you stay signed in at the provider, so the next " "sign-in may be silent (SSO)." msgstr "" -#: src/auth_playground/templates/tokens.html:51 +#: src/auth_playground/templates/tokens.html:56 msgid "" "Global — also ends the session at the provider (RP-Initiated Logout), " "preventing a silent re-login." msgstr "" -#: src/auth_playground/templates/tokens.html:57 +#: src/auth_playground/templates/tokens.html:62 msgid "Get tokens" msgstr "" -#: src/auth_playground/templates/tokens.html:58 +#: src/auth_playground/templates/tokens.html:63 msgid "" "Choose how the client obtains its tokens: for a user who signs in, or for" " the client itself." diff --git a/tests/test_userinfo.py b/tests/test_userinfo.py new file mode 100644 index 0000000..180cc16 --- /dev/null +++ b/tests/test_userinfo.py @@ -0,0 +1,247 @@ +import time +from unittest.mock import patch + +import pytest +import requests + +from tests.fake_provider import server_session + + +def sign_in(iam_server, iam_client, user, testapp): + """Sign the user in with the authorization code flow.""" + 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) + + +def userinfo_form(testapp): + """Return the UserInfo form of the current tokens tab.""" + return testapp.get("/en/tokens/current").forms["userinfo-form"] + + +def get_userinfo(testapp): + """Submit the UserInfo form of the current tokens tab.""" + return userinfo_form(testapp).submit("userinfo").maybe_follow() + + +def test_userinfo_shows_the_response(iam_server, iam_client, user, testapp): + """Test the UserInfo response is shown under the tokens.""" + sign_in(iam_server, iam_client, user, testapp) + + res = get_userinfo(testapp) + + assert "User info received" in res.text + assert "UserInfo response" in res.text + assert f"{iam_server.url}oauth/userinfo" in res.text + with testapp.session_transaction() as sess: + assert sess["userinfo"]["claims"]["sub"] == user.user_name + + +def test_userinfo_sends_an_expired_token_as_it_is( + iam_server, iam_client, user, testapp +): + """Test the token is sent even when the client thinks it expired. + + The provider decides, and the refresh token is not used behind the scenes. + """ + sign_in(iam_server, iam_client, user, testapp) + with testapp.session_transaction() as sess: + sess["token"] = {**sess["token"], "expires_at": int(time.time()) - 3600} + token = sess["token"] + + res = get_userinfo(testapp) + + assert "User info received" in res.text + with testapp.session_transaction() as sess: + assert sess["token"] == token + + +def test_new_tokens_forget_the_userinfo_response(iam_server, iam_client, user, testapp): + """Test a refresh forgets the response obtained with the previous access token.""" + sign_in(iam_server, iam_client, user, testapp) + res = get_userinfo(testapp) + + res = res.forms["refresh-form"].submit("refresh").maybe_follow() + + assert "Token successfully refreshed" in res.text + assert "UserInfo response" not in res.text + with testapp.session_transaction() as sess: + assert "userinfo" not in sess + + +def test_local_logout_forgets_the_userinfo_response( + iam_server, iam_client, user, testapp +): + """Test forgetting the tokens also forgets the UserInfo response.""" + sign_in(iam_server, iam_client, user, testapp) + get_userinfo(testapp) + + res = testapp.get("/en/tokens/logout") + res.forms["logout-local-form"].submit("logout") + + with testapp.session_transaction() as sess: + assert "userinfo" not in sess + + +def test_userinfo_shows_the_provider_error(iam_server, iam_client, user, testapp): + """Test an access token refused by the provider shows an error.""" + sign_in(iam_server, iam_client, user, testapp) + with testapp.session_transaction() as sess: + sess["token"] = {**sess["token"], "access_token": "invalid"} + + res = get_userinfo(testapp) + + assert "An error happened during the UserInfo request" in res.text + with testapp.session_transaction() as sess: + assert "userinfo" not in sess + + +def test_userinfo_with_an_unsupported_token_type(iam_server, iam_client, user, testapp): + """Test a token type that Authlib cannot send shows an error.""" + sign_in(iam_server, iam_client, user, testapp) + with testapp.session_transaction() as sess: + sess["token"] = {**sess["token"], "token_type": "DPoP"} + + res = get_userinfo(testapp) + + assert "An error happened during the UserInfo request" in res.text + assert "Unsupported token_type" in res.text + + +def provider_response(status, body, headers=None): + """Build the HTTP response the provider sends to the UserInfo request.""" + response = requests.Response() + response.status_code = status + response.reason = "Unauthorized" if status == 401 else "OK" + response.url = "https://provider.test/userinfo" + response._content = body + response.headers.update(headers or {}) + return response + + +@pytest.mark.parametrize( + "body,error", + [ + (b"eyJhbGciOiJSUzI1NiJ9.e30.c2ln", "The response is not JSON."), + (b'"text"', "The response is not a JSON object."), + (b"null", "The response is not a JSON object."), + ], +) +def test_userinfo_response_that_is_not_a_json_object( + iam_server, iam_client, user, testapp, body, error +): + """Test a response that is not a JSON object, like a signed response.""" + sign_in(iam_server, iam_client, user, testapp) + form = userinfo_form(testapp) + + with patch( + "auth_playground.oauth_client.TokenSession.send", + return_value=provider_response(200, body), + ): + res = form.submit("userinfo").maybe_follow() + + assert f"An error happened during the UserInfo request: {error}" in res.text + with testapp.session_transaction() as sess: + assert "userinfo" not in sess + + +def test_userinfo_shows_the_www_authenticate_error( + iam_server, iam_client, user, testapp +): + """Test the error of the WWW-Authenticate header is shown (RFC 6750 §3).""" + sign_in(iam_server, iam_client, user, testapp) + header = 'Bearer error="insufficient_scope"' + form = userinfo_form(testapp) + + with patch( + "auth_playground.oauth_client.TokenSession.send", + return_value=provider_response(401, b"", {"WWW-Authenticate": header}), + ): + res = form.submit("userinfo").maybe_follow() + + assert "insufficient_scope" in res.text + + +def test_userinfo_rejects_client_tokens(test_client, csrf_token): + """Test tokens issued to the client itself are not sent to the UserInfo endpoint.""" + with test_client.session_transaction() as sess: + sess["token"] = {"access_token": "test-access"} + sess["token_info"] = {"grant": "client_credentials", "obtained_at": 0} + + res = test_client.post( + "/userinfo", + data={"csrf_token": csrf_token(test_client)}, + follow_redirects=True, + ) + + assert b"These tokens are not tied to a user." in res.data + + +def test_userinfo_rejects_missing_csrf_token(test_client): + """Test the UserInfo request needs a CSRF token.""" + with test_client.session_transaction() as sess: + sess["token"] = {"access_token": "test-access"} + + res = test_client.post("/userinfo", data={}, follow_redirects=True) + + assert b"Invalid request" in res.data + + +def test_userinfo_without_access_token(test_client, csrf_token): + """Test the UserInfo request needs an access token.""" + res = test_client.post( + "/userinfo", + data={"csrf_token": csrf_token(test_client)}, + follow_redirects=True, + ) + + assert b"No access token available" in res.data + + +def test_userinfo_without_endpoint(provider_metadata, csrf_token, unconfigured_app): + """Test the UserInfo request needs a UserInfo endpoint.""" + client = unconfigured_app.test_client() + server_session(client, {"client_id": "client", "client_secret": "secret"}) + with client.session_transaction() as sess: + sess["token"] = {"access_token": "test-access"} + + res = client.post( + "/userinfo", + data={"csrf_token": csrf_token(client)}, + follow_redirects=True, + ) + + assert b"The provider has no UserInfo endpoint." in res.data + + +@pytest.mark.parametrize( + "grant,shown", + [("authorization_code", True), ("client_credentials", False)], +) +def test_userinfo_button_only_for_user_tokens(test_client, grant, shown): + """Test tokens issued to the client itself have no user to ask about.""" + with test_client.session_transaction() as sess: + sess["token"] = {"access_token": "test-access"} + sess["token_info"] = {"grant": grant, "obtained_at": 0} + + res = test_client.get("/en/tokens/current") + + assert (b"userinfo-form" in res.data) is shown + + +def test_userinfo_with_an_unreachable_provider(iam_server, iam_client, user, testapp): + """Test a network failure during the UserInfo request shows an error.""" + sign_in(iam_server, iam_client, user, testapp) + form = userinfo_form(testapp) + + with patch( + "auth_playground.oauth_client.TokenSession.send", + side_effect=requests.ConnectionError("Connection refused"), + ): + res = form.submit("userinfo").maybe_follow() + + assert ( + "An error happened during the UserInfo request: Connection refused" in res.text + )