forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure protocol forced reauthentication is correctly mapped during SA…
…ML identity brokering Closes keycloak#25980 Signed-off-by: Patrick Hamann <[email protected]>
- Loading branch information
Showing
3 changed files
with
135 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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
120 changes: 120 additions & 0 deletions
120
...an/tests/base/src/test/java/org/keycloak/testsuite/broker/KcSamlForceAuthnBrokerTest.java
This file contains 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,120 @@ | ||
/* | ||
* Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package org.keycloak.testsuite.broker; | ||
|
||
import org.keycloak.broker.saml.SAMLIdentityProviderConfig; | ||
import org.keycloak.dom.saml.v2.protocol.AuthnRequestType; | ||
import org.keycloak.saml.processing.api.saml.v2.request.SAML2Request; | ||
import org.keycloak.testsuite.saml.AbstractSamlTest; | ||
import org.keycloak.testsuite.updaters.IdentityProviderAttributeUpdater; | ||
import org.keycloak.testsuite.util.SamlClient; | ||
import org.keycloak.testsuite.util.SamlClient.Binding; | ||
import org.keycloak.testsuite.util.SamlClientBuilder; | ||
import java.io.Closeable; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.w3c.dom.Document; | ||
import static org.keycloak.testsuite.broker.BrokerTestTools.getConsumerRoot; | ||
|
||
/** | ||
* @author phamann | ||
*/ | ||
public final class KcSamlForceAuthnBrokerTest extends AbstractBrokerTest { | ||
|
||
@Override | ||
protected BrokerConfiguration getBrokerConfiguration() { | ||
return KcSamlBrokerConfiguration.INSTANCE; | ||
} | ||
|
||
// Issue #25980 | ||
@Test | ||
public void testForceAuthnNotSentInRequest() throws Exception { | ||
// If no ForceAuthn is sent in SAML AuthnRequest the value should be | ||
// read from the IdP configuration. | ||
try (Closeable idpUpdater = new IdentityProviderAttributeUpdater(identityProviderResource) | ||
.setAttribute(SAMLIdentityProviderConfig.FORCE_AUTHN, "false") | ||
.update()) | ||
{ | ||
// Build the login request document | ||
AuthnRequestType loginRep = SamlClient.createLoginRequestDocument(AbstractSamlTest.SAML_CLIENT_ID_SALES_POST, getConsumerRoot() "/sales-post/saml", null); | ||
Document doc = SAML2Request.convert(loginRep); | ||
new SamlClientBuilder() | ||
.authnRequest(getConsumerSamlEndpoint(bc.consumerRealmName()), doc, Binding.POST) | ||
.build() // Request to consumer IdP | ||
.login() | ||
.idp(bc.getIDPAlias()) | ||
.build() | ||
.processSamlResponse(Binding.POST) // AuthnRequest to producer IdP | ||
.targetAttributeSamlRequest() | ||
.transformDocument((document) -> { | ||
try | ||
{ | ||
// Find the AuthnRequest ForceAuthN attribute | ||
String attrValue = document.getDocumentElement().getAttributes().getNamedItem("ForceAuthn").getNodeValue(); | ||
Assert.assertEquals("Unexpected ForceAuthn attribute value", "false", attrValue); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new RuntimeException(ex); | ||
} | ||
}) | ||
.build() | ||
.execute(); | ||
} | ||
} | ||
|
||
// Issue #25980 | ||
@Test | ||
public void testForceAuthnSentInRequest() throws Exception { | ||
// If ForceAuthn=true is sent in SAML AuthnRequest it should be | ||
// forwarded to IdP regardless of the IdP ForceAuthn configuration. | ||
try (Closeable idpUpdater = new IdentityProviderAttributeUpdater(identityProviderResource) | ||
.setAttribute(SAMLIdentityProviderConfig.FORCE_AUTHN, "false") | ||
.update()) | ||
{ | ||
// Build the login request document | ||
AuthnRequestType loginRep = SamlClient.createLoginRequestDocument(AbstractSamlTest.SAML_CLIENT_ID_SALES_POST, getConsumerRoot() "/sales-post/saml", null); | ||
loginRep.setForceAuthn(true); // Set ForceAuthN in authentication request | ||
Document doc = SAML2Request.convert(loginRep); | ||
new SamlClientBuilder() | ||
.authnRequest(getConsumerSamlEndpoint(bc.consumerRealmName()), doc, Binding.POST) | ||
.build() // Request to consumer IdP | ||
.login() | ||
.idp(bc.getIDPAlias()) | ||
.build() | ||
.processSamlResponse(Binding.POST) // AuthnRequest to producer IdP | ||
.targetAttributeSamlRequest() | ||
.transformDocument((document) -> { | ||
try | ||
{ | ||
// Find the AuthnRequest ForceAuthN attribute | ||
String attrValue = document.getDocumentElement().getAttributes().getNamedItem("ForceAuthn").getNodeValue(); | ||
Assert.assertEquals("Unexpected ForceAuthn attribute value", "true", attrValue); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new RuntimeException(ex); | ||
} | ||
}) | ||
.build() | ||
.execute(); | ||
} | ||
} | ||
} |