This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/saml-configuration in repository https://gitbox.apache.org/repos/asf/cxf.git
commit ae518007d9f49a07448e38d6d832f727bab27c94 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Mon Nov 30 19:16:39 2026 +0000 Apply SAML audience, OneTimeUse and revocation settings in SamlTokenInterceptor --- .../ws/security/wss4j/SamlTokenInterceptor.java | 19 ++++++ .../apache/cxf/systest/ws/saml/SamlTokenTest.java | 69 ++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptor.java b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptor.java index f3de0d9cfcb..3c954c61961 100644 --- a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptor.java +++ b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptor.java @@ -35,11 +35,13 @@ import org.w3c.dom.Element; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.common.classloader.ClassLoaderUtils; +import org.apache.cxf.common.util.PropertyUtils; import org.apache.cxf.common.util.StringUtils; import org.apache.cxf.headers.Header; import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.helpers.DOMUtils; import org.apache.cxf.interceptor.security.DefaultSecurityContext; +import org.apache.cxf.rt.security.saml.utils.SAMLUtils; import org.apache.cxf.rt.security.utils.SecurityUtils; import org.apache.cxf.security.SecurityContext; import org.apache.cxf.security.transport.TLSSessionInfo; @@ -48,6 +50,7 @@ import org.apache.cxf.ws.policy.AssertionInfoMap; import org.apache.cxf.ws.security.SecurityConstants; import org.apache.cxf.ws.security.policy.PolicyUtils; import org.apache.wss4j.common.WSS4JConstants; +import org.apache.wss4j.common.cache.ReplayCache; import org.apache.wss4j.common.crypto.Crypto; import org.apache.wss4j.common.crypto.CryptoFactory; import org.apache.wss4j.common.crypto.PasswordEncryptor; @@ -186,6 +189,22 @@ public class SamlTokenInterceptor extends AbstractTokenInterceptor { data.setSigVerCrypto(getCrypto(SecurityConstants.SIGNATURE_CRYPTO, SecurityConstants.SIGNATURE_PROPERTIES, message)); + // Add Audience Restrictions for SAML + data.setAudienceRestrictions(SAMLUtils.getAudienceRestrictions(message, true)); + + // Configure replay caching for SAML OneTimeUse Assertions + ReplayCache samlCache = + WSS4JUtils.getReplayCache( + message, SecurityConstants.ENABLE_SAML_ONE_TIME_USE_CACHE, + SecurityConstants.SAML_ONE_TIME_USE_CACHE_INSTANCE + ); + data.setSamlOneTimeUseReplayCache(samlCache); + + boolean enableRevocation = + PropertyUtils.isTrue(SecurityUtils.getSecurityPropertyValue(SecurityConstants.ENABLE_REVOCATION, + message)); + data.setEnableRevocation(enableRevocation); + WSDocInfo wsDocInfo = new WSDocInfo(tokenElement.getOwnerDocument()); data.setWsDocInfo(wsDocInfo); diff --git a/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/SamlTokenTest.java b/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/SamlTokenTest.java index a7d5e030ca9..ca6f7106b39 100644 --- a/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/SamlTokenTest.java +++ b/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/SamlTokenTest.java @@ -57,6 +57,7 @@ import org.junit.BeforeClass; import org.junit.runner.RunWith; import org.junit.runners.Parameterized.Parameters; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -226,6 +227,74 @@ public class SamlTokenTest extends AbstractBusClientServerTestBase { bus.shutdown(true); } + // Audience restrictions must be enforced when there is no security binding + @org.junit.Test + public void testSaml1SupportingAudienceRestriction() throws Exception { + + SpringBusFactory bf = new SpringBusFactory(); + URL busFile = SamlTokenTest.class.getResource(JavaUtils.isFIPSEnabled() + ? "client-fips.xml" + : "client.xml"); + + Bus bus = bf.createBus(busFile.toString()); + BusFactory.setDefaultBus(bus); + BusFactory.setThreadDefaultBus(bus); + + URL wsdl = SamlTokenTest.class.getResource(JavaUtils.isFIPSEnabled() + ? "DoubleItSaml-fips.wsdl" + : "DoubleItSaml.wsdl"); + Service service = Service.create(wsdl, SERVICE_QNAME); + QName portQName = new QName(NAMESPACE, "DoubleItSaml1SupportingPort"); + DoubleItPortType saml1Port = + service.getPort(portQName, DoubleItPortType.class); + String portNumber = PORT2; + if (STAX_PORT.equals(test.getPort())) { + portNumber = STAX_PORT2; + } + updateAddressPort(saml1Port, portNumber); + + if (test.isStreaming()) { + SecurityTestUtil.enableStreaming(saml1Port); + } + + // Create a signed SAML Token with an AudienceRestrictionCondition for this endpoint + ConditionsBean conditions = new ConditionsBean(); + List<AudienceRestrictionBean> audienceRestrictions = new ArrayList<>(); + AudienceRestrictionBean audienceRestriction = new AudienceRestrictionBean(); + audienceRestriction.setAudienceURIs(Collections.singletonList( + "https://localhost:" + portNumber + "/DoubleItSaml1Supporting")); + audienceRestrictions.add(audienceRestriction); + conditions.setAudienceRestrictions(audienceRestrictions); + + SamlCallbackHandler samlCallbackHandler = new SamlCallbackHandler(false, true); + samlCallbackHandler.setConfirmationMethod(SAML1Constants.CONF_BEARER); + samlCallbackHandler.setConditions(conditions); + ((BindingProvider)saml1Port).getRequestContext().put( + SecurityConstants.SAML_CALLBACK_HANDLER, samlCallbackHandler + ); + + assertEquals(50, saml1Port.doubleIt(25)); + + // Now use an audience restriction for a different endpoint + audienceRestriction = new AudienceRestrictionBean(); + audienceRestriction.setAudienceURIs(Collections.singletonList( + "https://localhost:" + portNumber + "/DoubleItSaml1Supportingunknown")); + audienceRestrictions.clear(); + audienceRestrictions.add(audienceRestriction); + conditions.setAudienceRestrictions(audienceRestrictions); + samlCallbackHandler.setConditions(conditions); + + try { + saml1Port.doubleIt(25); + fail("Failure expected on unknown AudienceRestriction"); + } catch (jakarta.xml.ws.soap.SOAPFaultException ex) { + // expected + } + + ((java.io.Closeable)saml1Port).close(); + bus.shutdown(true); + } + // Self-signing (see CXF-5248) @org.junit.Test public void testSaml1SupportingSelfSigned() throws Exception {
