From 18fdd85dbe0fd94b93a76a7c688406855de6b980 Mon Sep 17 00:00:00 2001 From: Alexis Rico Date: Mon, 21 Sep 2026 08:41:27 +0200 Subject: [PATCH] Resolve the organization when an invitation is confirmed InviteOrgActionTokenHandler sets the organization on the request context in preHandleToken, which is only reached through LoginActionsService.preHandleActionToken and so only runs on the registration entry points. An invitee who already has an account opens the action token URL, which executeActionToken dispatches straight to handleToken, leaving the context organization unset. Organizations.resolveOrganization then has neither a context organization nor an ORGANIZATION_ATTRIBUTE auth note, falls through to the membership check an invitee cannot pass yet, and returns null, so FreeMarkerLoginFormsProvider leaves org out of the login form model. An organization aware theme therefore cannot brand the confirm membership page and can only name the organization by its name. Set it in handleToken as well, under the same rule as preHandleToken: for an existing member, or once the invitation is known to be valid, so a revoked or expired link does not expose the organization's attributes. Cover it from the new test framework, with an organization login theme in the custom providers that prints what the org bean exposes, and give RealmBuilder a loginTheme option so the test can select that theme. Closes #52396 Signed-off-by: Alexis Rico --- .../InviteOrgActionTokenHandler.java | 3 + .../testframework/realm/RealmBuilder.java | 5 + .../OrganizationInvitationThemeTest.java | 109 ++++++++++++++++++ .../resources/META-INF/keycloak-themes.json | 6 + .../theme/organization/login/info.ftl | 28 +++++ .../theme/organization/login/theme.properties | 1 + 6 files changed, 152 insertions(+) create mode 100644 tests/base/src/test/java/org/keycloak/tests/organization/admin/OrganizationInvitationThemeTest.java create mode 100644 tests/custom-providers/src/main/resources/theme/organization/login/info.ftl create mode 100644 tests/custom-providers/src/main/resources/theme/organization/login/theme.properties diff --git a/services/src/main/java/org/keycloak/authentication/actiontoken/inviteorg/InviteOrgActionTokenHandler.java b/services/src/main/java/org/keycloak/authentication/actiontoken/inviteorg/InviteOrgActionTokenHandler.java index bcb1fcf0d93..2bfa239f261 100644 --- a/services/src/main/java/org/keycloak/authentication/actiontoken/inviteorg/InviteOrgActionTokenHandler.java +++ b/services/src/main/java/org/keycloak/authentication/actiontoken/inviteorg/InviteOrgActionTokenHandler.java @@ -143,6 +143,7 @@ public Response handleToken(InviteOrgActionToken token, ActionTokenContext r.loginTheme("organization")); + OrganizationResource organization = realm.admin().organizations().get(createOrganization().getId()); + String userId = createUser(INVITEE_EMAIL); + + organization.members().inviteExistingUser(userId).close(); + driver.open(getInvitationLink()); + infoPage.assertCurrent(); + + // the invitee is not a member yet, but the page still knows the organization they were invited to + String page = driver.page().getPageSource(); + assertThat(page, containsString("Sign-in to " + organizationName + " organization")); + assertThat(page, containsString("The key is value1, value2")); + assertThat(page, not(containsString("User is member of"))); + } + + private String createUser(String email) { + UserRepresentation user = UserBuilder.create() + .username(email) + .email(email) + .name("Invited", "User") + .emailVerified(true) + .build(); + String userId; + + try (Response response = realm.admin().users().create(user)) { + userId = ApiUtil.getCreatedId(response); + } + + realm.cleanup().add(r -> { + try { + r.users().get(userId).remove(); + } catch (NotFoundException ignored) { + } + }); + return userId; + } + + private String getInvitationLink() throws IOException { + assertTrue(mailServer.waitForIncomingEmail(10_000, 1), "invitation email not received"); + + MimeMessage message = mailServer.getLastReceivedMessage(); + assertNotNull(message); + return MailUtils.getLink(MailUtils.getBody(message).getHtml()).trim(); + } +} diff --git a/tests/custom-providers/src/main/resources/META-INF/keycloak-themes.json b/tests/custom-providers/src/main/resources/META-INF/keycloak-themes.json index 13722a8cf0c..6a2be004f7f 100644 --- a/tests/custom-providers/src/main/resources/META-INF/keycloak-themes.json +++ b/tests/custom-providers/src/main/resources/META-INF/keycloak-themes.json @@ -5,6 +5,12 @@ "types": [ "account" ] + }, + { + "name": "organization", + "types": [ + "login" + ] } ] } diff --git a/tests/custom-providers/src/main/resources/theme/organization/login/info.ftl b/tests/custom-providers/src/main/resources/theme/organization/login/info.ftl new file mode 100644 index 00000000000..ab9eeab7908 --- /dev/null +++ b/tests/custom-providers/src/main/resources/theme/organization/login/info.ftl @@ -0,0 +1,28 @@ +<#import "template.ftl" as layout> +<@layout.registrationLayout displayMessage=false; section> + <#if section = "header"> + <#if messageHeader??> + ${kcSanitize(msg("${messageHeader}"))?no_esc} + <#else> + ${message.summary} + + <#elseif section = "form"> +
+ <#if org??> + Sign-in to ${org.name} organization + <#list org.attributes?keys as key> + The ${key} is ${org.attributes[key]?join(", ")} + + <#if org.member> + User is member of ${org.name} + + <#else> + Sign-in to the realm + +

${kcSanitize(message.summary)?no_esc}

+ <#if actionUri?has_content> +

${msg("proceedWithAction")}

+ +
+ + diff --git a/tests/custom-providers/src/main/resources/theme/organization/login/theme.properties b/tests/custom-providers/src/main/resources/theme/organization/login/theme.properties new file mode 100644 index 00000000000..ee3f1d1eecc --- /dev/null +++ b/tests/custom-providers/src/main/resources/theme/organization/login/theme.properties @@ -0,0 +1 @@ +parent=keycloak