This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch 4.1.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/4.1.x-fixes by this push:
new 4699055ac8b Apply SAML audience, OneTimeUse and revocation settings in
SamlTokenInterceptor (#3804)
4699055ac8b is described below
commit 4699055ac8b12571a039e05e0b48885b90bd73a4
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Tue Dec 1 05:59:17 2026 +0000
Apply SAML audience, OneTimeUse and revocation settings in
SamlTokenInterceptor (#3804)
(cherry picked from commit 96973d0e3fcab2b64a64181ee251a1d3627a1f2c)
---
.../ws/security/wss4j/SamlTokenInterceptor.java | 19 +++++++
.../apache/cxf/systest/ws/saml/SamlTokenTest.java | 65 ++++++++++++++++++++++
2 files changed, 84 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 f4892e8ec3c..6a79433208f 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
@@ -56,6 +56,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;
@@ -217,6 +218,70 @@ 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("client.xml");
+
+ Bus bus = bf.createBus(busFile.toString());
+ BusFactory.setDefaultBus(bus);
+ BusFactory.setThreadDefaultBus(bus);
+
+ URL wsdl = SamlTokenTest.class.getResource("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 {