This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/AudienceRestriction in repository https://gitbox.apache.org/repos/asf/cxf-fediz.git
commit c6f16751c61ab826972fb26c4bd0ac16c3550c81 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Tue Nov 24 17:11:36 2026 +0000 Validate the token AudienceRestriction in the Spring plugin --- .../FederationAuthenticationProvider.java | 33 ++++++++++ .../apache/cxf/fediz/spring/messages.properties | 1 + ...derationAuthenticationProviderAudienceTest.java | 73 ++++++++++++++++++++++ 3 files changed, 107 insertions(+) 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 6eaafb9d..b7905703 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 @@ -19,6 +19,8 @@ package org.apache.cxf.fediz.spring.authentication; +import java.util.List; + import org.apache.cxf.fediz.core.config.FedizContext; import org.apache.cxf.fediz.core.processor.FedizProcessor; import org.apache.cxf.fediz.core.processor.FedizProcessorFactory; @@ -115,6 +117,8 @@ public class FederationAuthenticationProvider implements AuthenticationProvider, FedizProcessorFactory.newFedizProcessor(fedContext.getProtocol()); FedizResponse wfRes = wfProc.processRequest(wfReq, fedContext); + validateAudienceRestriction(wfRes.getAudience(), fedContext.getAudienceUris()); + final UserDetails userDetails = loadUserByFederationResponse(wfRes); userDetailsChecker.check(userDetails); return new FederationAuthenticationToken(userDetails, authentication.getCredentials(), @@ -125,6 +129,35 @@ public class FederationAuthenticationProvider implements AuthenticationProvider, } } + /** + * Validate the AudienceRestriction of the received token 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 + * @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); + throw new BadCredentialsException(messages.getMessage("FederationAuthenticationProvider.badAudience", + "Token AudienceRestriction doesn't match with the configured audience URIs")); + } + } + /** * Template method for retrieving the UserDetails based on the federation response (wresult parameter). * diff --git a/plugins/spring/src/main/resources/org/apache/cxf/fediz/spring/messages.properties b/plugins/spring/src/main/resources/org/apache/cxf/fediz/spring/messages.properties index 8d3d4bb6..0a0db7d9 100644 --- a/plugins/spring/src/main/resources/org/apache/cxf/fediz/spring/messages.properties +++ b/plugins/spring/src/main/resources/org/apache/cxf/fediz/spring/messages.properties @@ -1,2 +1,3 @@ FederationAuthenticationProvider.incorrectKey=The presented FederationAuthenticationToken does not contain the expected key FederationAuthenticationProvider.noSignInRequest=Failed to get SignIn request +FederationAuthenticationProvider.badAudience=Token AudienceRestriction doesn't match with the configured audience URIs diff --git a/plugins/spring/src/test/java/org/apache/cxf/fediz/spring/authentication/FederationAuthenticationProviderAudienceTest.java b/plugins/spring/src/test/java/org/apache/cxf/fediz/spring/authentication/FederationAuthenticationProviderAudienceTest.java new file mode 100644 index 00000000..0cd29dc9 --- /dev/null +++ b/plugins/spring/src/test/java/org/apache/cxf/fediz/spring/authentication/FederationAuthenticationProviderAudienceTest.java @@ -0,0 +1,73 @@ +/** + * 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.spring.authentication; + +import java.util.Collections; +import java.util.List; + +import org.springframework.security.authentication.BadCredentialsException; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Tests for the AudienceRestriction validation in the FederationAuthenticationProvider. + */ +public class FederationAuthenticationProviderAudienceTest { + + private static final String AUDIENCE = "urn:org:apache:cxf:fediz:fedizhelloworld"; + + private final FederationAuthenticationProvider provider = new FederationAuthenticationProvider(); + + @Test + public void testMatchingAudience() { + provider.validateAudienceRestriction(AUDIENCE, Collections.singletonList(AUDIENCE)); + } + + @Test + public void testMatchingOneOfSeveralAudiences() { + provider.validateAudienceRestriction(AUDIENCE, List.of("urn:org:apache:cxf:fediz:other", AUDIENCE)); + } + + @Test + public void testNonMatchingAudience() { + Assertions.assertThrows(BadCredentialsException.class, () -> + provider.validateAudienceRestriction("urn:org:apache:cxf:fediz:other", + Collections.singletonList(AUDIENCE))); + } + + @Test + public void testMissingAudienceWithConfiguredAudiences() { + Assertions.assertThrows(BadCredentialsException.class, () -> + provider.validateAudienceRestriction(null, Collections.singletonList(AUDIENCE))); + } + + @Test + public void testAudienceWithNoConfiguredAudiences() { + Assertions.assertThrows(BadCredentialsException.class, () -> + provider.validateAudienceRestriction(AUDIENCE, Collections.emptyList())); + } + + @Test + public void testNoAudienceAndNoConfiguredAudiences() { + provider.validateAudienceRestriction(null, Collections.emptyList()); + } + +}
