From 6b83a45b2ecf3f15e2d77b5883b1edf2fd20314e Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Tue, 10 Sep 2024 12:25:17 +0200 Subject: [PATCH] Propagate locale when using app initiated registration URL Fixes #13505 Signed-off-by: Thomas Darimont --- .../oidc/endpoints/AuthorizationEndpoint.java | 2 + .../keycloak/testsuite/util/OAuthClient.java | 4 ++ .../actions/AppInitiatedRegistrationTest.java | 51 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/AppInitiatedRegistrationTest.java diff --git a/services/src/main/java/org/keycloak/protocol/oidc/endpoints/AuthorizationEndpoint.java b/services/src/main/java/org/keycloak/protocol/oidc/endpoints/AuthorizationEndpoint.java index 6c7bb06376..6666f9a575 100755 --- a/services/src/main/java/org/keycloak/protocol/oidc/endpoints/AuthorizationEndpoint.java +++ b/services/src/main/java/org/keycloak/protocol/oidc/endpoints/AuthorizationEndpoint.java @@ -49,6 +49,7 @@ import org.keycloak.services.clientpolicy.context.PreAuthorizationRequestContext import org.keycloak.services.messages.Messages; import org.keycloak.services.resources.LoginActionsService; import org.keycloak.services.util.CacheControlUtil; +import org.keycloak.services.util.LocaleUtil; import org.keycloak.sessions.AuthenticationSessionModel; import org.keycloak.util.TokenUtil; @@ -351,6 +352,7 @@ public class AuthorizationEndpoint extends AuthorizationEndpointBase { AuthenticationProcessor processor = createProcessor(authenticationSession, flowId, LoginActionsService.REGISTRATION_PATH); authenticationSession.setClientNote(APP_INITIATED_FLOW, LoginActionsService.REGISTRATION_PATH); + LocaleUtil.processLocaleParam(session, realm, authenticationSession); return processor.authenticate(); } diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/OAuthClient.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/OAuthClient.java index 707db97be0..58e617d3be 100644 --- a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/OAuthClient.java +++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/OAuthClient.java @@ -1512,6 +1512,10 @@ public class OAuthClient { return this.getLoginFormUrl(this.baseUrl); } + public String getRegisterationsUrl() { + return this.getLoginFormUrl(this.baseUrl).replace("openid-connect/auth", "openid-connect/registrations"); + } + public String getLoginFormUrl(String baseUrl) { UriBuilder b = OIDCLoginProtocolService.authUrl(UriBuilder.fromUri(baseUrl)); if (responseType != null) { diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/AppInitiatedRegistrationTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/AppInitiatedRegistrationTest.java new file mode 100644 index 0000000000..6fab9dbe17 --- /dev/null +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/AppInitiatedRegistrationTest.java @@ -0,0 +1,51 @@ +package org.keycloak.testsuite.actions; + +import jakarta.ws.rs.core.UriBuilder; +import org.jboss.arquillian.graphene.page.Page; +import org.junit.Before; +import org.junit.Test; +import org.keycloak.locale.LocaleSelectorProvider; +import org.keycloak.representations.idm.RealmRepresentation; +import org.keycloak.representations.idm.UserRepresentation; +import org.keycloak.testsuite.AbstractTestRealmKeycloakTest; +import org.keycloak.testsuite.Assert; +import org.keycloak.testsuite.admin.ApiUtil; +import org.keycloak.testsuite.pages.AppPage; +import org.keycloak.testsuite.pages.RegisterPage; + +public class AppInitiatedRegistrationTest extends AbstractTestRealmKeycloakTest { + + @Page + protected AppPage appPage; + + @Page + protected RegisterPage registerPage; + + @Override + public void configureTestRealm(RealmRepresentation testRealm) { + } + + @Before + public void before() { + ApiUtil.removeUserByUsername(testRealm(), "test-user@localhost"); + } + + @Test + public void ensureLocaleParameterIsPropagatedDuringAppInitiatedRegistration() { + + var appInitiatedRegisterUrlBuilder = UriBuilder.fromUri(oauth.getRegisterationsUrl()); + appInitiatedRegisterUrlBuilder.queryParam(LocaleSelectorProvider.KC_LOCALE_PARAM, "en"); + var appInitiatedRegisterUrl = appInitiatedRegisterUrlBuilder.build().toString(); + + driver.navigate().to(appInitiatedRegisterUrl); + + registerPage.assertCurrent(); + registerPage.register("first", "last", "test-user@localhost", "test-user", "test","test"); + + appPage.assertCurrent(); + + UserRepresentation user = testRealm().users().searchByEmail("test-user@localhost", true).get(0); + // ensure that the locale was set on the user + Assert.assertEquals("en", user.getAttributes().get("locale").get(0)); + } +}