This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/consolidation in repository https://gitbox.apache.org/repos/asf/cxf-fediz.git
commit c82778c74b0ff20c645acec6453540e1794d100a Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Wed Nov 25 10:18:10 2026 +0000 Consolidate the AudienceRestriction validation in the plugins --- .../cxf/fediz/core/handler/SigninHandler.java | 27 +------- .../apache/cxf/fediz/core/util/AudienceUtils.java | 74 ++++++++++++++++++++++ .../cxf/fediz/core/util/AudienceUtilsTest.java | 69 ++++++++++++++++++++ .../cxf/plugin/FedizRedirectBindingFilter.java | 29 ++------- .../cxf/fediz/jetty9/FederationLoginService.java | 23 +------ .../FederationAuthenticationProvider.java | 16 +---- 6 files changed, 154 insertions(+), 84 deletions(-) diff --git a/plugins/core/src/main/java/org/apache/cxf/fediz/core/handler/SigninHandler.java b/plugins/core/src/main/java/org/apache/cxf/fediz/core/handler/SigninHandler.java index 8f5bdec3..832ed0c6 100644 --- a/plugins/core/src/main/java/org/apache/cxf/fediz/core/handler/SigninHandler.java +++ b/plugins/core/src/main/java/org/apache/cxf/fediz/core/handler/SigninHandler.java @@ -19,7 +19,6 @@ package org.apache.cxf.fediz.core.handler; import java.security.cert.X509Certificate; -import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -36,6 +35,7 @@ import org.apache.cxf.fediz.core.processor.FedizProcessor; import org.apache.cxf.fediz.core.processor.FedizProcessorFactory; import org.apache.cxf.fediz.core.processor.FedizRequest; import org.apache.cxf.fediz.core.processor.FedizResponse; +import org.apache.cxf.fediz.core.util.AudienceUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -125,30 +125,7 @@ public class SigninHandler<T> implements RequestHandler<T> { } protected boolean validateAudienceRestrictions(String audience, String requestURL) { - // Validate the AudienceRestriction in Security Token (e.g. SAML) - // validate against the configured list of audienceURIs - List<String> audienceURIs = fedizContext.getAudienceUris(); - boolean validAudience = audienceURIs.isEmpty() && audience == null; - if (!validAudience && audience != null) { - - for (String a : audienceURIs) { - if (audience.startsWith(a)) { - validAudience = true; - LOG.debug("Token audience matches with valid URIs."); - break; - } - } - - if (!validAudience) { - LOG.warn("Token AudienceRestriction [{}] doesn't match with specified list of URIs.", audience); - LOG.debug("Authenticated URIs are: {}", audienceURIs); - } - - if (LOG.isDebugEnabled() && requestURL != null && requestURL.indexOf(audience) == -1) { - LOG.debug("Token AudienceRestriction doesn't match with request URL [{}] [{}]", audience, requestURL); - } - } - return validAudience; + return AudienceUtils.validateAudienceRestrictions(audience, fedizContext.getAudienceUris(), requestURL); } public String getResponseToken(HttpServletRequest request) { diff --git a/plugins/core/src/main/java/org/apache/cxf/fediz/core/util/AudienceUtils.java b/plugins/core/src/main/java/org/apache/cxf/fediz/core/util/AudienceUtils.java new file mode 100644 index 00000000..edfb7c9f --- /dev/null +++ b/plugins/core/src/main/java/org/apache/cxf/fediz/core/util/AudienceUtils.java @@ -0,0 +1,74 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.cxf.fediz.core.util; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility methods for validating the AudienceRestriction of a received security token. + */ +public final class AudienceUtils { + + private static final Logger LOG = LoggerFactory.getLogger(AudienceUtils.class); + + private AudienceUtils() { + // complete + } + + /** + * Validate the AudienceRestriction of a received security token (e.g. SAML) against the configured list + * of audience URIs. A token without an audience is only accepted if no audience URIs are configured, and + * a token with an audience is only accepted if it matches one of the configured audience URIs. + * + * @param audience the audience of the received token (may be null) + * @param audienceURIs the configured audience URIs + * @param requestURL the request URL, only used for debug logging (may be null) + * @return true if the audience is valid for this relying party + */ + public static boolean validateAudienceRestrictions(String audience, List<String> audienceURIs, + String requestURL) { + boolean validAudience = audienceURIs.isEmpty() && audience == null; + if (!validAudience && audience == null) { + LOG.warn("Token has no AudienceRestriction but a list of audience URIs is configured."); + } else if (!validAudience) { + for (String a : audienceURIs) { + if (audience.startsWith(a)) { + validAudience = true; + LOG.debug("Token audience matches with valid URIs."); + break; + } + } + + if (!validAudience) { + LOG.warn("Token AudienceRestriction [{}] doesn't match with specified list of URIs.", audience); + LOG.debug("Authenticated URIs are: {}", audienceURIs); + } + + if (LOG.isDebugEnabled() && requestURL != null && requestURL.indexOf(audience) == -1) { + LOG.debug("Token AudienceRestriction doesn't match with request URL [{}] [{}]", audience, requestURL); + } + } + return validAudience; + } + +} diff --git a/plugins/core/src/test/java/org/apache/cxf/fediz/core/util/AudienceUtilsTest.java b/plugins/core/src/test/java/org/apache/cxf/fediz/core/util/AudienceUtilsTest.java new file mode 100644 index 00000000..7e8d22ca --- /dev/null +++ b/plugins/core/src/test/java/org/apache/cxf/fediz/core/util/AudienceUtilsTest.java @@ -0,0 +1,69 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.cxf.fediz.core.util; + +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Tests for the AudienceRestriction validation in AudienceUtils. + */ +public class AudienceUtilsTest { + + private static final String AUDIENCE = "urn:org:apache:cxf:fediz:fedizhelloworld"; + + @Test + public void testMatchingAudience() { + Assertions.assertTrue( + AudienceUtils.validateAudienceRestrictions(AUDIENCE, Collections.singletonList(AUDIENCE), null)); + } + + @Test + public void testMatchingOneOfSeveralAudiences() { + Assertions.assertTrue(AudienceUtils.validateAudienceRestrictions(AUDIENCE, + List.of("urn:org:apache:cxf:fediz:other", AUDIENCE), "https://localhost/fedizhelloworld")); + } + + @Test + public void testNonMatchingAudience() { + Assertions.assertFalse(AudienceUtils.validateAudienceRestrictions("urn:org:apache:cxf:fediz:other", + Collections.singletonList(AUDIENCE), null)); + } + + @Test + public void testMissingAudienceWithConfiguredAudiences() { + Assertions.assertFalse( + AudienceUtils.validateAudienceRestrictions(null, Collections.singletonList(AUDIENCE), null)); + } + + @Test + public void testAudienceWithNoConfiguredAudiences() { + Assertions.assertFalse(AudienceUtils.validateAudienceRestrictions(AUDIENCE, Collections.emptyList(), null)); + } + + @Test + public void testNoAudienceAndNoConfiguredAudiences() { + Assertions.assertTrue(AudienceUtils.validateAudienceRestrictions(null, Collections.emptyList(), null)); + } + +} diff --git a/plugins/cxf/src/main/java/org/apache/cxf/fediz/cxf/plugin/FedizRedirectBindingFilter.java b/plugins/cxf/src/main/java/org/apache/cxf/fediz/cxf/plugin/FedizRedirectBindingFilter.java index b0658de0..f75f1a57 100644 --- a/plugins/cxf/src/main/java/org/apache/cxf/fediz/cxf/plugin/FedizRedirectBindingFilter.java +++ b/plugins/cxf/src/main/java/org/apache/cxf/fediz/cxf/plugin/FedizRedirectBindingFilter.java @@ -58,6 +58,7 @@ import org.apache.cxf.fediz.core.processor.FedizProcessorFactory; import org.apache.cxf.fediz.core.processor.FedizRequest; import org.apache.cxf.fediz.core.processor.FedizResponse; import org.apache.cxf.fediz.core.processor.RedirectionResponse; +import org.apache.cxf.fediz.core.util.AudienceUtils; import org.apache.cxf.fediz.core.util.CookieUtils; import org.apache.cxf.fediz.cxf.plugin.state.ResponseState; import org.apache.cxf.helpers.IOUtils; @@ -485,31 +486,9 @@ public class FedizRedirectBindingFilter extends AbstractServiceProviderFilter ) { // Validate the AudienceRestriction in Security Token (e.g. SAML) // against the configured list of audienceURIs - if (wfRes.getAudience() == null) { - if (!audienceURIs.isEmpty()) { - LOG.warn("Token has no AudienceRestriction but a list of audience URIs is configured."); - throw ExceptionUtils.toForbiddenException(null, null); - } - } else { - boolean validAudience = false; - for (String a : audienceURIs) { - if (wfRes.getAudience().startsWith(a)) { - validAudience = true; - break; - } - } - - if (!validAudience) { - LOG.warn("Token AudienceRestriction [" + wfRes.getAudience() - + "] doesn't match with specified list of URIs."); - throw ExceptionUtils.toForbiddenException(null, null); - } - - if (LOG.isDebugEnabled() && request.getRequestURL().indexOf(wfRes.getAudience()) == -1) { - LOG.debug("Token AudienceRestriction doesn't match with request URL [" - + wfRes.getAudience() + "] [" - + request.getRequestURL() + "]"); - } + if (!AudienceUtils.validateAudienceRestrictions(wfRes.getAudience(), audienceURIs, + request.getRequestURL().toString())) { + throw ExceptionUtils.toForbiddenException(null, null); } } diff --git a/plugins/jetty9/src/main/java/org/apache/cxf/fediz/jetty9/FederationLoginService.java b/plugins/jetty9/src/main/java/org/apache/cxf/fediz/jetty9/FederationLoginService.java index c1c33578..6c2e5ea5 100644 --- a/plugins/jetty9/src/main/java/org/apache/cxf/fediz/jetty9/FederationLoginService.java +++ b/plugins/jetty9/src/main/java/org/apache/cxf/fediz/jetty9/FederationLoginService.java @@ -33,6 +33,7 @@ import org.apache.cxf.fediz.core.processor.FedizProcessor; import org.apache.cxf.fediz.core.processor.FedizProcessorFactory; import org.apache.cxf.fediz.core.processor.FedizRequest; import org.apache.cxf.fediz.core.processor.FedizResponse; +import org.apache.cxf.fediz.core.util.AudienceUtils; import org.eclipse.jetty.security.IdentityService; import org.eclipse.jetty.security.LoginService; import org.eclipse.jetty.server.UserIdentity; @@ -99,26 +100,8 @@ public class FederationLoginService extends AbstractLifeCycle implements LoginSe // Validate the AudienceRestriction in Security Token (e.g. SAML) // against the configured list of audienceURIs - List<String> audienceURIs = config.getAudienceUris(); - if (wfRes.getAudience() == null) { - if (!audienceURIs.isEmpty()) { - LOG.warn("Token has no AudienceRestriction but a list of audience URIs is configured."); - return null; - } - } else { - boolean validAudience = false; - for (String a : audienceURIs) { - if (wfRes.getAudience().startsWith(a)) { - validAudience = true; - break; - } - } - - if (!validAudience) { - LOG.warn("Token AudienceRestriction [" + wfRes.getAudience() - + "] doesn't match with specified list of URIs."); - return null; - } + if (!AudienceUtils.validateAudienceRestrictions(wfRes.getAudience(), config.getAudienceUris(), null)) { + return null; } // Add "Authenticated" role diff --git a/plugins/spring/src/main/java/org/apache/cxf/fediz/spring/authentication/FederationAuthenticationProvider.java b/plugins/spring/src/main/java/org/apache/cxf/fediz/spring/authentication/FederationAuthenticationProvider.java index b7905703..4671027e 100644 --- a/plugins/spring/src/main/java/org/apache/cxf/fediz/spring/authentication/FederationAuthenticationProvider.java +++ b/plugins/spring/src/main/java/org/apache/cxf/fediz/spring/authentication/FederationAuthenticationProvider.java @@ -26,6 +26,7 @@ import org.apache.cxf.fediz.core.processor.FedizProcessor; import org.apache.cxf.fediz.core.processor.FedizProcessorFactory; import org.apache.cxf.fediz.core.processor.FedizRequest; import org.apache.cxf.fediz.core.processor.FedizResponse; +import org.apache.cxf.fediz.core.util.AudienceUtils; import org.apache.cxf.fediz.spring.FederationConfig; import org.apache.cxf.fediz.spring.SpringFedizMessageSource; import org.slf4j.Logger; @@ -139,20 +140,7 @@ public class FederationAuthenticationProvider implements AuthenticationProvider, * @throws BadCredentialsException if the audience is not valid for this relying party */ protected void validateAudienceRestriction(String audience, List<String> audienceURIs) { - boolean validAudience = audienceURIs.isEmpty() && audience == null; - if (!validAudience && audience != null) { - for (String a : audienceURIs) { - if (audience.startsWith(a)) { - validAudience = true; - LOG.debug("Token audience matches with valid URIs."); - break; - } - } - } - - if (!validAudience) { - LOG.warn("Token AudienceRestriction [{}] doesn't match with specified list of URIs.", audience); - LOG.debug("Authenticated URIs are: {}", audienceURIs); + if (!AudienceUtils.validateAudienceRestrictions(audience, audienceURIs, null)) { throw new BadCredentialsException(messages.getMessage("FederationAuthenticationProvider.badAudience", "Token AudienceRestriction doesn't match with the configured audience URIs")); }
