Repository navigation
Migrate IdP-hint and nonce-parameter broker tests #52379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pedroigor
merged 2 commits into
keycloak:main
from
sguilhen:52244-broker-oidc-idp-hint-nonce
Sep 4, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
tests/base/src/test/java/org/keycloak/tests/broker/oidc/KcOidcBrokerHiddenIdpHintTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package org.keycloak.tests.broker.oidc; | ||
|
|
||
| import org.keycloak.testframework.annotations.InjectRealm; | ||
| import org.keycloak.testframework.annotations.KeycloakIntegrationTest; | ||
| import org.keycloak.testframework.injection.LifeCycle; | ||
| import org.keycloak.testframework.realm.ManagedRealm; | ||
| import org.keycloak.testframework.realm.RealmBuilder; | ||
| import org.keycloak.testframework.realm.RealmConfig; | ||
| import org.keycloak.tests.broker.AbstractKcOidcBrokerTest; | ||
|
|
||
| import org.junit.jupiter.api.Disabled; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| @KeycloakIntegrationTest | ||
| public class KcOidcBrokerHiddenIdpHintTest extends AbstractKcOidcBrokerTest { | ||
|
|
||
| @InjectRealm(ref = "consumer", lifecycle = LifeCycle.METHOD, | ||
| config = HiddenIdpConsumerRealmConfig.class) | ||
| ManagedRealm consumerRealm; | ||
|
|
||
| // The IDP is hidden from the login page, so the inherited base login tests (which click the social | ||
| // button to reach it) don't apply here - this class only exercises the hint-based redirect below. | ||
| @Override | ||
| @Test | ||
| @Disabled("IdP is hidden on the login page; use kc_idp_hint instead of clicking the social button") | ||
| public void testLogInAsUserInIDP() { | ||
| } | ||
|
|
||
| @Override | ||
| @Test | ||
| @Disabled("IdP is hidden on the login page; use kc_idp_hint instead of clicking the social button") | ||
| public void testLoginWithExistingUser() { | ||
| } | ||
|
|
||
| @Test | ||
| public void testSuccessfulRedirectToProviderHiddenOnLoginPage() { | ||
| oauth.loginForm() | ||
| .param("kc_idp_hint", IDP_OIDC_ALIAS) | ||
| .open(); | ||
|
|
||
| webDriver.waiting().until(d -> webDriver.getCurrentUrl().contains("/realms/" + PROVIDER_REALM + "/")); | ||
| assertTrue(webDriver.getCurrentUrl().contains("/realms/" + PROVIDER_REALM + "/"), | ||
| "Driver should be on the provider realm page right now"); | ||
|
|
||
| loginPage.fillLogin(getUserLogin(), getUserPassword()); | ||
| loginPage.submit(); | ||
|
|
||
| updateAccountInformation(); | ||
|
|
||
| assertTrue(oauth.parseLoginResponse().isSuccess()); | ||
| } | ||
|
|
||
| static class HiddenIdpConsumerRealmConfig implements RealmConfig { | ||
| @Override | ||
| public RealmBuilder configure(RealmBuilder realm) { | ||
| return configureConsumerRealm(realm, | ||
| createOidcIdentityProvider() | ||
| .hideOnLoginPage()); | ||
| } | ||
| } | ||
| } |
113 changes: 113 additions & 0 deletions
113
tests/base/src/test/java/org/keycloak/tests/broker/oidc/KcOidcBrokerIdpHintTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package org.keycloak.tests.broker.oidc; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.keycloak.representations.idm.ClientRepresentation; | ||
| import org.keycloak.testframework.annotations.KeycloakIntegrationTest; | ||
| import org.keycloak.testframework.ui.annotations.InjectPage; | ||
| import org.keycloak.testframework.ui.page.OAuthGrantPage; | ||
| import org.keycloak.tests.broker.AbstractKcOidcBrokerTest; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| @KeycloakIntegrationTest | ||
| public class KcOidcBrokerIdpHintTest extends AbstractKcOidcBrokerTest { | ||
|
|
||
| @InjectPage | ||
| OAuthGrantPage grantPage; | ||
|
|
||
| @Test | ||
| public void testSuccessfulRedirect() { | ||
| oauth.loginForm() | ||
| .param("kc_idp_hint", IDP_OIDC_ALIAS) | ||
| .open(); | ||
|
|
||
| webDriver.waiting().until(d -> webDriver.getCurrentUrl().contains("/realms/" + PROVIDER_REALM + "/")); | ||
| assertTrue(webDriver.getCurrentUrl().contains("/realms/" + PROVIDER_REALM + "/"), | ||
| "Driver should be on the provider realm page right now"); | ||
|
|
||
| loginPage.fillLogin(getUserLogin(), getUserPassword()); | ||
| loginPage.submit(); | ||
|
|
||
| updateAccountInformation(); | ||
|
|
||
| assertTrue(oauth.parseLoginResponse().isSuccess()); | ||
| assertTrue(webDriver.getCurrentUrl().contains(CONSUMER_REALM)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSuccessfulRedirectToProviderAfterLoginPageShown() { | ||
| oauth.openLoginForm(); | ||
| webDriver.waiting().until(d -> webDriver.getCurrentUrl().contains("/realms/" + CONSUMER_REALM + "/")); | ||
|
|
||
| String urlWithHint = webDriver.getCurrentUrl() + "&kc_idp_hint=" + IDP_OIDC_ALIAS; | ||
| webDriver.driver().navigate().to(urlWithHint); | ||
| webDriver.waiting().until(d -> webDriver.getCurrentUrl().contains("/realms/" + PROVIDER_REALM + "/")); | ||
| assertTrue(webDriver.getCurrentUrl().contains("/realms/" + PROVIDER_REALM + "/"), | ||
| "Driver should be on the provider realm page right now"); | ||
|
|
||
| // do the same thing a second time | ||
| webDriver.driver().navigate().to(urlWithHint); | ||
| webDriver.waiting().until(d -> webDriver.getCurrentUrl().contains("/realms/" + PROVIDER_REALM + "/")); | ||
| assertTrue(webDriver.getCurrentUrl().contains("/realms/" + PROVIDER_REALM + "/"), | ||
| "Driver should be on the provider realm page right now"); | ||
|
|
||
| // redirect shouldn't happen with a fresh login form (no hint) | ||
| oauth.openLoginForm(); | ||
| webDriver.waiting().until(d -> webDriver.getCurrentUrl().contains("/realms/" + CONSUMER_REALM + "/")); | ||
| assertTrue(webDriver.getCurrentUrl().contains("/realms/" + CONSUMER_REALM + "/"), | ||
| "Driver should be on the consumer realm page"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInvalidIdentityProviderHint() { | ||
| oauth.openLoginForm(); | ||
| webDriver.waiting().until(d -> webDriver.getCurrentUrl().contains("/realms/" + CONSUMER_REALM + "/")); | ||
|
|
||
| String url = webDriver.getCurrentUrl() + "&kc_idp_hint=bogus-idp"; | ||
| webDriver.driver().navigate().to(url); | ||
| webDriver.waiting().until(d -> webDriver.getCurrentUrl().contains("/realms/" + CONSUMER_REALM + "/")); | ||
|
|
||
| assertTrue(webDriver.getCurrentUrl().contains("/realms/" + CONSUMER_REALM + "/")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIdpHintWithErrorResponseReturnsToLoginPage() { | ||
| List<ClientRepresentation> clients = providerRealm.admin().clients().findByClientId(CLIENT_ID); | ||
| assertEquals(1, clients.size()); | ||
| ClientRepresentation brokerClient = clients.get(0); | ||
| brokerClient.setConsentRequired(true); | ||
| providerRealm.admin().clients().get(brokerClient.getId()).update(brokerClient); | ||
|
|
||
| try { | ||
| oauth.loginForm() | ||
| .param("kc_idp_hint", IDP_OIDC_ALIAS) | ||
| .open(); | ||
|
|
||
| webDriver.waiting().until(d -> webDriver.getCurrentUrl().contains("/realms/" + PROVIDER_REALM + "/")); | ||
| assertTrue(webDriver.getCurrentUrl().contains("/realms/" + PROVIDER_REALM + "/"), | ||
| "Driver should be on the provider realm page right now"); | ||
|
|
||
| loginPage.fillLogin(getUserLogin(), getUserPassword()); | ||
| loginPage.submit(); | ||
|
|
||
| updateAccountInformationIfPresent(); | ||
|
|
||
| grantPage.assertCurrent(); | ||
| grantPage.cancel(); | ||
|
|
||
| webDriver.waiting().until(d -> webDriver.getCurrentUrl().contains("/realms/" + CONSUMER_REALM + "/")); | ||
| assertTrue(webDriver.getCurrentUrl().contains("/realms/" + CONSUMER_REALM + "/"), | ||
| "Driver should be back on consumer login page after denial"); | ||
|
|
||
| assertTrue(webDriver.driver().getPageSource().contains("Access denied"), | ||
| "Error message should be displayed"); | ||
| } finally { | ||
| brokerClient.setConsentRequired(false); | ||
| providerRealm.admin().clients().get(brokerClient.getId()).update(brokerClient); | ||
| } | ||
| } | ||
| } |
119 changes: 119 additions & 0 deletions
119
tests/base/src/test/java/org/keycloak/tests/broker/oidc/KcOidcBrokerNonceParameterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package org.keycloak.tests.broker.oidc; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.keycloak.broker.oidc.OIDCIdentityProvider; | ||
| import org.keycloak.jose.jws.JWSInput; | ||
| import org.keycloak.jose.jws.JWSInputException; | ||
| import org.keycloak.protocol.ProtocolMapperUtils; | ||
| import org.keycloak.protocol.oidc.OIDCLoginProtocol; | ||
| import org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper; | ||
| import org.keycloak.protocol.oidc.mappers.UserSessionNoteMapper; | ||
| import org.keycloak.representations.IDToken; | ||
| import org.keycloak.representations.idm.IdentityProviderRepresentation; | ||
| import org.keycloak.representations.idm.ProtocolMapperRepresentation; | ||
| import org.keycloak.testframework.annotations.InjectRealm; | ||
| import org.keycloak.testframework.annotations.KeycloakIntegrationTest; | ||
| import org.keycloak.testframework.injection.LifeCycle; | ||
| import org.keycloak.testframework.realm.ClientBuilder; | ||
| import org.keycloak.testframework.realm.ManagedRealm; | ||
| import org.keycloak.testframework.realm.RealmBuilder; | ||
| import org.keycloak.testframework.realm.RealmConfig; | ||
| import org.keycloak.tests.broker.AbstractKcOidcBrokerTest; | ||
| import org.keycloak.testsuite.util.oauth.AccessTokenResponse; | ||
| import org.keycloak.testsuite.util.oauth.AuthorizationEndpointResponse; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| @KeycloakIntegrationTest | ||
| public class KcOidcBrokerNonceParameterTest extends AbstractKcOidcBrokerTest { | ||
|
|
||
| @InjectRealm(ref = "consumer", lifecycle = LifeCycle.METHOD, | ||
| config = NonceConsumerRealmConfig.class) | ||
| ManagedRealm consumerRealm; | ||
|
|
||
| @Test | ||
| public void testNonceSet() { | ||
| disableUpdateProfileOnFirstLogin(); | ||
|
|
||
| oauth.client("consumer-client"); | ||
|
|
||
| AuthorizationEndpointResponse authzResponse = doLoginSocialWithNonce("123456"); | ||
| assertTrue(authzResponse.isSuccess()); | ||
| AccessTokenResponse response = oauth.doAccessTokenRequest(authzResponse.getCode()); | ||
| IDToken idToken = toIdToken(response.getIdToken()); | ||
|
|
||
| assertEquals("123456", idToken.getNonce()); | ||
| String federatedIdTokenString = (String) idToken.getOtherClaims().get(OIDCIdentityProvider.FEDERATED_ID_TOKEN); | ||
| assertNotNull(federatedIdTokenString); | ||
| IDToken federatedIdToken = toIdToken(federatedIdTokenString); | ||
| assertNotNull(federatedIdToken.getNonce()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNonceNotSet() { | ||
| disableUpdateProfileOnFirstLogin(); | ||
|
|
||
| IdentityProviderRepresentation idpRep = consumerRealm.admin().identityProviders().get(IDP_OIDC_ALIAS).toRepresentation(); | ||
| idpRep.getConfig().put("disableNonce", Boolean.TRUE.toString()); | ||
| consumerRealm.admin().identityProviders().get(IDP_OIDC_ALIAS).update(idpRep); | ||
|
|
||
| oauth.client("consumer-client"); | ||
|
|
||
| AuthorizationEndpointResponse authzResponse = doLoginSocialWithNonce(null); | ||
| assertTrue(authzResponse.isSuccess()); | ||
| AccessTokenResponse response = oauth.doAccessTokenRequest(authzResponse.getCode()); | ||
| IDToken idToken = toIdToken(response.getIdToken()); | ||
|
|
||
| assertNull(idToken.getNonce()); | ||
| String federatedIdTokenString = (String) idToken.getOtherClaims().get(OIDCIdentityProvider.FEDERATED_ID_TOKEN); | ||
| assertNotNull(federatedIdTokenString); | ||
| IDToken federatedIdToken = toIdToken(federatedIdTokenString); | ||
| assertNull(federatedIdToken.getNonce()); | ||
| } | ||
|
|
||
| private AuthorizationEndpointResponse doLoginSocialWithNonce(String nonce) { | ||
| oauth.loginForm().nonce(nonce).open(); | ||
| loginPage.clickSocial(IDP_OIDC_ALIAS); | ||
| loginPage.fillLogin(getUserLogin(), getUserPassword()); | ||
| loginPage.submit(); | ||
| return oauth.parseLoginResponse(); | ||
| } | ||
|
|
||
| private IDToken toIdToken(String encoded) { | ||
| try { | ||
| return new JWSInput(encoded).readJsonContent(IDToken.class); | ||
| } catch (JWSInputException cause) { | ||
| throw new RuntimeException("Failed to deserialize token", cause); | ||
| } | ||
| } | ||
|
|
||
| static class NonceConsumerRealmConfig implements RealmConfig { | ||
| @Override | ||
| public RealmBuilder configure(RealmBuilder realm) { | ||
| ProtocolMapperRepresentation sessionNoteMapper = new ProtocolMapperRepresentation(); | ||
| sessionNoteMapper.setName(OIDCIdentityProvider.FEDERATED_ID_TOKEN); | ||
| sessionNoteMapper.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); | ||
| sessionNoteMapper.setProtocolMapper(UserSessionNoteMapper.PROVIDER_ID); | ||
| sessionNoteMapper.setConfig(Map.of( | ||
| ProtocolMapperUtils.USER_SESSION_NOTE, | ||
| OIDCIdentityProvider.FEDERATED_ID_TOKEN + ":" + IDP_OIDC_ALIAS, | ||
| OIDCAttributeMapperHelper.TOKEN_CLAIM_NAME, | ||
| OIDCIdentityProvider.FEDERATED_ID_TOKEN, | ||
| OIDCAttributeMapperHelper.INCLUDE_IN_ID_TOKEN, | ||
| Boolean.TRUE.toString())); | ||
|
|
||
| return configureConsumerRealm(realm, | ||
| createOidcIdentityProvider()) | ||
| .clients(ClientBuilder.create("consumer-client") | ||
| .publicClient() | ||
| .redirectUris("*") | ||
| .protocolMappers(sessionNoteMapper)); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.