From ab9f8a698b9148844639f2d5ba22fb43e22483ba Mon Sep 17 00:00:00 2001 From: Stefan Guilhen Date: Thu, 3 Sep 2026 12:00:55 -0300 Subject: [PATCH 1/2] Move SAML broker tests to new testsuite saml package Pure git rename of the legacy signed-document-only SAML broker test into the new testsuite saml package. The phase-1 KcSamlBrokerTest already in the new suite also moves from the flat broker package into saml/, establishing the same protocol-subpackage layout the oidc/ tests already use. No content changes; the new-testsuite adaptation follows in the next commit. Signed-off-by: Stefan Guilhen --- .../org/keycloak/tests/broker/{ => saml}/KcSamlBrokerTest.java | 0 .../tests/broker/saml}/KcSamlSignedDocumentOnlyBrokerTest.java | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/base/src/test/java/org/keycloak/tests/broker/{ => saml}/KcSamlBrokerTest.java (100%) rename {testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/broker => tests/base/src/test/java/org/keycloak/tests/broker/saml}/KcSamlSignedDocumentOnlyBrokerTest.java (100%) diff --git a/tests/base/src/test/java/org/keycloak/tests/broker/KcSamlBrokerTest.java b/tests/base/src/test/java/org/keycloak/tests/broker/saml/KcSamlBrokerTest.java similarity index 100% rename from tests/base/src/test/java/org/keycloak/tests/broker/KcSamlBrokerTest.java rename to tests/base/src/test/java/org/keycloak/tests/broker/saml/KcSamlBrokerTest.java diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/broker/KcSamlSignedDocumentOnlyBrokerTest.java b/tests/base/src/test/java/org/keycloak/tests/broker/saml/KcSamlSignedDocumentOnlyBrokerTest.java similarity index 100% rename from testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/broker/KcSamlSignedDocumentOnlyBrokerTest.java rename to tests/base/src/test/java/org/keycloak/tests/broker/saml/KcSamlSignedDocumentOnlyBrokerTest.java From 369d9a945343653cfff57524a15d13a419c4d774 Mon Sep 17 00:00:00 2001 From: Stefan Guilhen Date: Thu, 3 Sep 2026 12:05:09 -0300 Subject: [PATCH 2/2] Migrate KcSamlSignedDocumentOnlyBrokerTest and SAML package reorg Configures SAML provider to sign documents only (not assertions) and the consumer IDP to validate signatures but not require signed assertions. The legacy test had a bug where KcSamlSignedBrokerConfiguration inherited its INSTANCE from KcSamlBrokerConfiguration, so the signing config overrides were never applied. The new test correctly applies the configuration via @BeforeEach using the provider realm's auto-generated RSA signing certificate. Also restores the legacy signed-AuthnRequest coverage that the original migration dropped: client signature validation on the provider SAML client (saml.client.signature plus the consumer's signing certificate) and wantAuthnRequestsSigned on the consumer IdP. Relocates KcSamlBrokerTest from the flat broker/ package into saml/, establishing the same protocol-subpackage layout the oidc/ tests already use (only a package declaration + import fix - no behavior change). Adapted to the merged abstract-class baseline: the concrete class extends AbstractKcSamlBrokerTest with no realm field overrides (both provider and consumer use the default SAML config), just the @BeforeEach that configures document-only signing. Carries forward the fixes from prior batches (independently re-derived per batch since this branch is off upstream/main): enableUnmanagedAttributes(), disableUpdateProfileOnFirstLogin(), and the assertUserCreatedInConsumerRealm() extraction. No SAML-side equivalent of the OIDC configureBrokerEndpoints() clientId-lookup fix is needed here - that bug was specific to the OIDC base's hardcoded CLIENT_ID constant, which this batch doesn't touch. Closes #52250 Signed-off-by: Stefan Guilhen --- .../tests/broker/AbstractBrokerLoginTest.java | 6 +- .../tests/broker/AbstractBrokerTest.java | 45 +++++++ .../tests/broker/saml/KcSamlBrokerTest.java | 3 +- .../KcSamlSignedDocumentOnlyBrokerTest.java | 123 +++++++----------- 4 files changed, 96 insertions(+), 81 deletions(-) diff --git a/tests/base/src/test/java/org/keycloak/tests/broker/AbstractBrokerLoginTest.java b/tests/base/src/test/java/org/keycloak/tests/broker/AbstractBrokerLoginTest.java index 47113199741..60801eb1451 100644 --- a/tests/base/src/test/java/org/keycloak/tests/broker/AbstractBrokerLoginTest.java +++ b/tests/base/src/test/java/org/keycloak/tests/broker/AbstractBrokerLoginTest.java @@ -27,9 +27,13 @@ protected void loginThroughIdpAndLogout() { protected void loginUser() { logInAsUserInIDP(); - updateAccountInformation(); + assertUserCreatedInConsumerRealm(); + } + // Reused by concrete tests that override loginUser() with a custom pre-login step (extra query + // params, IdP-hint, locale checks, etc.) but still need the same post-login assertions. + protected void assertUserCreatedInConsumerRealm() { ManagedRealm consumerRealm = getConsumerRealm(); UserRepresentation userRep = AccountHelper.getUserRepresentation( consumerRealm.admin(), getUserLogin()); diff --git a/tests/base/src/test/java/org/keycloak/tests/broker/AbstractBrokerTest.java b/tests/base/src/test/java/org/keycloak/tests/broker/AbstractBrokerTest.java index 5228d77ce66..e21e2640256 100644 --- a/tests/base/src/test/java/org/keycloak/tests/broker/AbstractBrokerTest.java +++ b/tests/base/src/test/java/org/keycloak/tests/broker/AbstractBrokerTest.java @@ -3,8 +3,14 @@ import java.util.List; import java.util.Set; +import org.keycloak.authentication.authenticators.broker.IdpCreateUserIfUniqueAuthenticatorFactory; +import org.keycloak.models.AuthenticationExecutionModel; import org.keycloak.models.UserModel; +import org.keycloak.models.utils.DefaultAuthenticationFlows; +import org.keycloak.representations.idm.AuthenticationExecutionInfoRepresentation; +import org.keycloak.representations.idm.AuthenticatorConfigRepresentation; import org.keycloak.representations.idm.FederatedIdentityRepresentation; +import org.keycloak.representations.idm.IdentityProviderRepresentation; import org.keycloak.representations.idm.RequiredActionProviderRepresentation; import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.testframework.oauth.OAuthClient; @@ -16,6 +22,7 @@ import org.keycloak.testframework.ui.page.LoginPage; import org.keycloak.testframework.ui.webdriver.ManagedWebDriver; import org.keycloak.testsuite.util.AccountHelper; +import org.keycloak.testsuite.util.userprofile.UserProfileUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -71,6 +78,44 @@ void relaxProviderProfileVerification() { } } + // Tests that map custom claims onto user attributes (e.g. IdP mappers writing a non-standard + // attribute) need the declarative user profile to accept attributes outside its managed schema, + // otherwise they're silently dropped when the user is created or later updated (e.g. by the + // first-broker-login review-profile form). + @BeforeEach + void enableUnmanagedAttributes() { + UserProfileUtil.enableUnmanagedAttributes(getConsumerRealm().admin().users().userProfile()); + UserProfileUtil.enableUnmanagedAttributes(getProviderRealm().admin().users().userProfile()); + } + + // Turns off the first-broker-login review-profile step for flows that want to test something else + // without the review-profile page interrupting the redirect back to the consumer. Disabling the flow + // execution alone isn't enough: the new-testsuite consumer realm enables the VERIFY_PROFILE required + // action by default, which would still intercept the incomplete imported user (the provider user has + // no first/last name on purpose), so that is disabled too, mirroring the legacy suite (which never + // enabled VERIFY_PROFILE at all). + protected void disableUpdateProfileOnFirstLogin() { + var flows = getConsumerRealm().admin().flows(); + for (AuthenticationExecutionInfoRepresentation execution : + flows.getExecutions(DefaultAuthenticationFlows.FIRST_BROKER_LOGIN_FLOW)) { + if (IdpCreateUserIfUniqueAuthenticatorFactory.PROVIDER_ID.equals(execution.getProviderId())) { + execution.setRequirement(AuthenticationExecutionModel.Requirement.ALTERNATIVE.name()); + flows.updateExecutions(DefaultAuthenticationFlows.FIRST_BROKER_LOGIN_FLOW, execution); + } else if (execution.getAlias() != null + && execution.getAlias().equals(DefaultAuthenticationFlows.IDP_REVIEW_PROFILE_CONFIG_ALIAS)) { + AuthenticatorConfigRepresentation config = flows.getAuthenticatorConfig(execution.getAuthenticationConfig()); + config.getConfig().put("update.profile.on.first.login", IdentityProviderRepresentation.UPFLM_OFF); + flows.updateAuthenticatorConfig(config.getId(), config); + } + } + for (RequiredActionProviderRepresentation action : getConsumerRealm().admin().flows().getRequiredActions()) { + if (UserModel.RequiredAction.VERIFY_PROFILE.name().equals(action.getAlias())) { + action.setEnabled(false); + getConsumerRealm().admin().flows().updateRequiredAction(action.getAlias(), action); + } + } + } + protected String getUserLogin() { return USER_LOGIN; } diff --git a/tests/base/src/test/java/org/keycloak/tests/broker/saml/KcSamlBrokerTest.java b/tests/base/src/test/java/org/keycloak/tests/broker/saml/KcSamlBrokerTest.java index 8f1728c2176..682ccd1063a 100644 --- a/tests/base/src/test/java/org/keycloak/tests/broker/saml/KcSamlBrokerTest.java +++ b/tests/base/src/test/java/org/keycloak/tests/broker/saml/KcSamlBrokerTest.java @@ -1,6 +1,7 @@ -package org.keycloak.tests.broker; +package org.keycloak.tests.broker.saml; import org.keycloak.testframework.annotations.KeycloakIntegrationTest; +import org.keycloak.tests.broker.AbstractKcSamlBrokerTest; @KeycloakIntegrationTest public class KcSamlBrokerTest extends AbstractKcSamlBrokerTest { diff --git a/tests/base/src/test/java/org/keycloak/tests/broker/saml/KcSamlSignedDocumentOnlyBrokerTest.java b/tests/base/src/test/java/org/keycloak/tests/broker/saml/KcSamlSignedDocumentOnlyBrokerTest.java index 771a9aeb0af..a9164741455 100644 --- a/tests/base/src/test/java/org/keycloak/tests/broker/saml/KcSamlSignedDocumentOnlyBrokerTest.java +++ b/tests/base/src/test/java/org/keycloak/tests/broker/saml/KcSamlSignedDocumentOnlyBrokerTest.java @@ -1,86 +1,51 @@ -package org.keycloak.testsuite.broker; +package org.keycloak.tests.broker.saml; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.keycloak.models.IdentityProviderSyncMode; +import org.keycloak.broker.saml.SAMLIdentityProviderConfig; +import org.keycloak.crypto.Algorithm; +import org.keycloak.crypto.KeyUse; +import org.keycloak.protocol.saml.SamlConfigAttributes; import org.keycloak.representations.idm.ClientRepresentation; import org.keycloak.representations.idm.IdentityProviderRepresentation; -import org.keycloak.representations.idm.RealmRepresentation; - -import static org.keycloak.testsuite.broker.BrokerTestConstants.IDP_SAML_SIGN_CERT; -import static org.keycloak.testsuite.broker.BrokerTestConstants.IDP_SAML_SIGN_KEY; -import static org.keycloak.testsuite.broker.BrokerTestConstants.REALM_PRIVATE_KEY; -import static org.keycloak.testsuite.broker.BrokerTestConstants.REALM_PUBLIC_KEY; - -public class KcSamlSignedDocumentOnlyBrokerTest extends AbstractBrokerTest { - - public static class KcSamlSignedBrokerConfiguration extends KcSamlBrokerConfiguration { - - @Override - public RealmRepresentation createProviderRealm() { - RealmRepresentation realm = super.createProviderRealm(); - - realm.setPublicKey(REALM_PUBLIC_KEY); - realm.setPrivateKey(REALM_PRIVATE_KEY); - - return realm; - } - - @Override - public RealmRepresentation createConsumerRealm() { - RealmRepresentation realm = super.createConsumerRealm(); - - realm.setPublicKey(REALM_PUBLIC_KEY); - realm.setPrivateKey(REALM_PRIVATE_KEY); - - return realm; - } - - @Override - public List createProviderClients() { - List clientRepresentationList = super.createProviderClients(); - - for (ClientRepresentation client : clientRepresentationList) { - client.setClientAuthenticatorType("client-secret"); - client.setSurrogateAuthRequired(false); - - Map attributes = client.getAttributes(); - if (attributes == null) { - attributes = new HashMap<>(); - client.setAttributes(attributes); - } - - attributes.put("saml.assertion.signature", "false"); - attributes.put("saml.server.signature", "true"); - attributes.put("saml.client.signature", "true"); - attributes.put("saml.signature.algorithm", "RSA_SHA256"); - attributes.put("saml.signing.private.key", IDP_SAML_SIGN_KEY); - attributes.put("saml.signing.certificate", IDP_SAML_SIGN_CERT); - } - - return clientRepresentationList; - } - - @Override - public IdentityProviderRepresentation setUpIdentityProvider(IdentityProviderSyncMode syncMode) { - IdentityProviderRepresentation result = super.setUpIdentityProvider(syncMode); - - Map config = result.getConfig(); - - config.put("validateSignature", "true"); - config.put("wantAssertionsSigned", "false"); - config.put("wantAuthnRequestsSigned", "true"); - config.put("signingCertificate", IDP_SAML_SIGN_CERT); - - return result; - } +import org.keycloak.representations.idm.KeysMetadataRepresentation.KeyMetadataRepresentation; +import org.keycloak.testframework.annotations.KeycloakIntegrationTest; +import org.keycloak.testframework.realm.ManagedRealm; +import org.keycloak.tests.broker.AbstractKcSamlBrokerTest; + +import org.junit.jupiter.api.BeforeEach; + +@KeycloakIntegrationTest +public class KcSamlSignedDocumentOnlyBrokerTest extends AbstractKcSamlBrokerTest { + + @BeforeEach + void configureSignedDocumentOnly() { + String providerSigningCert = activeRs256SigCert(providerRealm); + String consumerSigningCert = activeRs256SigCert(consumerRealm); + + String samlClientId = "http://localhost:8080/realms/" + CONSUMER_REALM; + ClientRepresentation client = providerRealm.admin().clients().findByClientId(samlClientId).get(0); + client.getAttributes().put(SamlConfigAttributes.SAML_ASSERTION_SIGNATURE, "false"); + client.getAttributes().put(SamlConfigAttributes.SAML_SERVER_SIGNATURE, "true"); + client.getAttributes().put(SamlConfigAttributes.SAML_SIGNATURE_ALGORITHM, "RSA_SHA256"); + // Require and validate the consumer's signature on incoming AuthnRequests. + client.getAttributes().put(SamlConfigAttributes.SAML_CLIENT_SIGNATURE_ATTRIBUTE, "true"); + client.getAttributes().put(SamlConfigAttributes.SAML_SIGNING_CERTIFICATE_ATTRIBUTE, consumerSigningCert); + providerRealm.admin().clients().get(client.getId()).update(client); + + IdentityProviderRepresentation idp = consumerRealm.admin() + .identityProviders().get(getIdpAlias()).toRepresentation(); + idp.getConfig().put(SAMLIdentityProviderConfig.VALIDATE_SIGNATURE, "true"); + idp.getConfig().put(SAMLIdentityProviderConfig.WANT_ASSERTIONS_SIGNED, "false"); + idp.getConfig().put(SAMLIdentityProviderConfig.SIGNING_CERTIFICATE_KEY, providerSigningCert); + // Sign the AuthnRequests sent to the provider so it can validate them. + idp.getConfig().put(SAMLIdentityProviderConfig.WANT_AUTHN_REQUESTS_SIGNED, "true"); + consumerRealm.admin().identityProviders().get(getIdpAlias()).update(idp); } - @Override - protected BrokerConfiguration getBrokerConfiguration() { - return KcSamlSignedBrokerConfiguration.INSTANCE; + private static String activeRs256SigCert(ManagedRealm realm) { + return realm.admin().keys().getKeyMetadata().getKeys().stream() + .filter(k -> k.getCertificate() != null && KeyUse.SIG.equals(k.getUse()) + && Algorithm.RS256.equals(k.getAlgorithm())) + .map(KeyMetadataRepresentation::getCertificate) + .findFirst().orElseThrow(); } - }