|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.config.annotation.web.configurers.saml2; |
| 18 | + |
| 19 | +import org.junit.After; |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Rule; |
| 23 | +import org.junit.Test; |
| 24 | +import org.opensaml.saml.saml2.core.Assertion; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.context.ConfigurableApplicationContext; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Import; |
| 29 | +import org.springframework.core.convert.converter.Converter; |
| 30 | +import org.springframework.mock.web.MockFilterChain; |
| 31 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 32 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 33 | +import org.springframework.security.authentication.AuthenticationManager; |
| 34 | +import org.springframework.security.authentication.AuthenticationProvider; |
| 35 | +import org.springframework.security.authentication.AuthenticationServiceException; |
| 36 | +import org.springframework.security.authentication.ProviderManager; |
| 37 | +import org.springframework.security.config.annotation.ObjectPostProcessor; |
| 38 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 39 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 40 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 41 | +import org.springframework.security.config.test.SpringTestRule; |
| 42 | +import org.springframework.security.core.Authentication; |
| 43 | +import org.springframework.security.core.AuthenticationException; |
| 44 | +import org.springframework.security.core.GrantedAuthority; |
| 45 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 46 | +import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper; |
| 47 | +import org.springframework.security.saml2.provider.service.authentication.OpenSamlAuthenticationProvider; |
| 48 | +import org.springframework.security.saml2.provider.service.authentication.Saml2Authentication; |
| 49 | +import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationToken; |
| 50 | +import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository; |
| 51 | +import org.springframework.security.saml2.provider.service.servlet.filter.Saml2WebSsoAuthenticationFilter; |
| 52 | +import org.springframework.security.web.FilterChainProxy; |
| 53 | +import org.springframework.security.web.context.HttpRequestResponseHolder; |
| 54 | +import org.springframework.security.web.context.HttpSessionSecurityContextRepository; |
| 55 | +import org.springframework.security.web.context.SecurityContextRepository; |
| 56 | +import org.springframework.test.util.ReflectionTestUtils; |
| 57 | +import org.springframework.test.web.servlet.MockMvc; |
| 58 | + |
| 59 | +import java.io.IOException; |
| 60 | +import java.time.Duration; |
| 61 | +import java.util.Base64; |
| 62 | +import java.util.Collection; |
| 63 | +import java.util.Collections; |
| 64 | +import javax.servlet.ServletException; |
| 65 | + |
| 66 | +import static org.assertj.core.api.Assertions.assertThat; |
| 67 | +import static org.codehaus.groovy.runtime.InvokerHelper.asList; |
| 68 | +import static org.mockito.ArgumentMatchers.anyString; |
| 69 | +import static org.mockito.Mockito.mock; |
| 70 | +import static org.mockito.Mockito.when; |
| 71 | +import static org.springframework.security.config.annotation.web.configurers.saml2.TestRelyingPartyRegistrations.saml2AuthenticationConfiguration; |
| 72 | + |
| 73 | +/** |
| 74 | + * Tests for different Java configuration for {@link Saml2LoginConfigurer} |
| 75 | + */ |
| 76 | +public class Saml2LoginConfigurerTests { |
| 77 | + |
| 78 | + private static final Converter<Assertion, Collection<? extends GrantedAuthority>> |
| 79 | + AUTHORITIES_EXTRACTOR = a -> asList(new SimpleGrantedAuthority("TEST")); |
| 80 | + private static final GrantedAuthoritiesMapper AUTHORITIES_MAPPER = |
| 81 | + authorities -> asList(new SimpleGrantedAuthority("TEST CONVERTED")); |
| 82 | + private static final Duration RESPONSE_TIME_VALIDATION_SKEW = Duration.ZERO; |
| 83 | + |
| 84 | + @Autowired |
| 85 | + private ConfigurableApplicationContext context; |
| 86 | + |
| 87 | + @Autowired |
| 88 | + private FilterChainProxy springSecurityFilterChain; |
| 89 | + |
| 90 | + @Autowired |
| 91 | + private RelyingPartyRegistrationRepository repository; |
| 92 | + |
| 93 | + @Autowired |
| 94 | + SecurityContextRepository securityContextRepository; |
| 95 | + |
| 96 | + @Rule |
| 97 | + public final SpringTestRule spring = new SpringTestRule(); |
| 98 | + |
| 99 | + @Autowired(required = false) |
| 100 | + MockMvc mvc; |
| 101 | + |
| 102 | + private MockHttpServletRequest request; |
| 103 | + private MockHttpServletResponse response; |
| 104 | + private MockFilterChain filterChain; |
| 105 | + |
| 106 | + @Before |
| 107 | + public void setup() { |
| 108 | + this.request = new MockHttpServletRequest("POST", ""); |
| 109 | + this.request.setServletPath("/login/saml2/sso/test-rp"); |
| 110 | + this.response = new MockHttpServletResponse(); |
| 111 | + this.filterChain = new MockFilterChain(); |
| 112 | + } |
| 113 | + |
| 114 | + @After |
| 115 | + public void cleanup() { |
| 116 | + if (this.context != null) { |
| 117 | + this.context.close(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void saml2LoginWhenConfiguringAuthenticationManagerThenTheManagerIsUsed() throws Exception { |
| 123 | + // setup application context |
| 124 | + this.spring.register(Saml2LoginConfigWithCustomAuthenticationManager.class).autowire(); |
| 125 | + performSaml2Login("ROLE_AUTH_MANAGER"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void saml2LoginWhenConfiguringAuthenticationDefaultsUsingCustomizerThenTheProviderIsConfigured() throws Exception { |
| 130 | + // setup application context |
| 131 | + this.spring.register(Saml2LoginConfigWithAuthenticationDefaultsWithPostProcessor.class).autowire(); |
| 132 | + validateSaml2WebSsoAuthenticationFilterConfiguration(); |
| 133 | + } |
| 134 | + |
| 135 | + private void validateSaml2WebSsoAuthenticationFilterConfiguration() { |
| 136 | + // get the OpenSamlAuthenticationProvider |
| 137 | + Saml2WebSsoAuthenticationFilter filter = getSaml2SsoFilter(this.springSecurityFilterChain); |
| 138 | + AuthenticationManager manager = |
| 139 | + (AuthenticationManager) ReflectionTestUtils.getField(filter, "authenticationManager"); |
| 140 | + ProviderManager pm = (ProviderManager) manager; |
| 141 | + AuthenticationProvider provider = pm.getProviders() |
| 142 | + .stream() |
| 143 | + .filter(p -> p instanceof OpenSamlAuthenticationProvider) |
| 144 | + .findFirst() |
| 145 | + .get(); |
| 146 | + Assert.assertSame(AUTHORITIES_EXTRACTOR, ReflectionTestUtils.getField(provider, "authoritiesExtractor")); |
| 147 | + Assert.assertSame(AUTHORITIES_MAPPER, ReflectionTestUtils.getField(provider, "authoritiesMapper")); |
| 148 | + Assert.assertSame(RESPONSE_TIME_VALIDATION_SKEW, ReflectionTestUtils.getField(provider, "responseTimeValidationSkew")); |
| 149 | + } |
| 150 | + |
| 151 | + private Saml2WebSsoAuthenticationFilter getSaml2SsoFilter(FilterChainProxy chain) { |
| 152 | + return (Saml2WebSsoAuthenticationFilter) chain.getFilters("/login/saml2/sso/test") |
| 153 | + .stream() |
| 154 | + .filter(f -> f instanceof Saml2WebSsoAuthenticationFilter) |
| 155 | + .findFirst() |
| 156 | + .get(); |
| 157 | + } |
| 158 | + |
| 159 | + private void performSaml2Login(String expected) throws IOException, ServletException { |
| 160 | + // setup authentication parameters |
| 161 | + this.request.setParameter( |
| 162 | + "SAMLResponse", |
| 163 | + Base64.getEncoder().encodeToString( |
| 164 | + "saml2-xml-response-object".getBytes() |
| 165 | + ) |
| 166 | + ); |
| 167 | + |
| 168 | + |
| 169 | + // perform test |
| 170 | + this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); |
| 171 | + |
| 172 | + // assertions |
| 173 | + Authentication authentication = this.securityContextRepository |
| 174 | + .loadContext(new HttpRequestResponseHolder(this.request, this.response)) |
| 175 | + .getAuthentication(); |
| 176 | + Assert.assertNotNull("Expected a valid authentication object.", authentication); |
| 177 | + assertThat(authentication.getAuthorities()).hasSize(1); |
| 178 | + assertThat(authentication.getAuthorities()).first() |
| 179 | + .isInstanceOf(SimpleGrantedAuthority.class).hasToString(expected); |
| 180 | + } |
| 181 | + |
| 182 | + |
| 183 | + @EnableWebSecurity |
| 184 | + @Import(Saml2LoginConfigBeans.class) |
| 185 | + static class Saml2LoginConfigWithCustomAuthenticationManager extends WebSecurityConfigurerAdapter { |
| 186 | + |
| 187 | + @Override |
| 188 | + protected void configure(HttpSecurity http) throws Exception { |
| 189 | + http.saml2Login() |
| 190 | + .authenticationManager( |
| 191 | + getAuthenticationManagerMock("ROLE_AUTH_MANAGER") |
| 192 | + ); |
| 193 | + super.configure(http); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + @EnableWebSecurity |
| 198 | + @Import(Saml2LoginConfigBeans.class) |
| 199 | + static class Saml2LoginConfigWithAuthenticationDefaultsWithPostProcessor extends WebSecurityConfigurerAdapter { |
| 200 | + |
| 201 | + @Override |
| 202 | + protected void configure(HttpSecurity http) throws Exception { |
| 203 | + ObjectPostProcessor<OpenSamlAuthenticationProvider> processor |
| 204 | + = new ObjectPostProcessor<OpenSamlAuthenticationProvider>() { |
| 205 | + @Override |
| 206 | + public <O extends OpenSamlAuthenticationProvider> O postProcess(O provider) { |
| 207 | + provider.setResponseTimeValidationSkew(RESPONSE_TIME_VALIDATION_SKEW); |
| 208 | + provider.setAuthoritiesMapper(AUTHORITIES_MAPPER); |
| 209 | + provider.setAuthoritiesExtractor(AUTHORITIES_EXTRACTOR); |
| 210 | + return provider; |
| 211 | + } |
| 212 | + }; |
| 213 | + |
| 214 | + http.saml2Login() |
| 215 | + .addObjectPostProcessor(processor) |
| 216 | + ; |
| 217 | + super.configure(http); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + private static AuthenticationManager getAuthenticationManagerMock(String role) { |
| 222 | + return new AuthenticationManager() { |
| 223 | + |
| 224 | + @Override |
| 225 | + public Authentication authenticate(Authentication authentication) |
| 226 | + throws AuthenticationException { |
| 227 | + if (!supports(authentication.getClass())) { |
| 228 | + throw new AuthenticationServiceException("not supported"); |
| 229 | + } |
| 230 | + return new Saml2Authentication( |
| 231 | + () -> "auth principal", |
| 232 | + "saml2 response", |
| 233 | + Collections.singletonList( |
| 234 | + new SimpleGrantedAuthority(role) |
| 235 | + ) |
| 236 | + ); |
| 237 | + } |
| 238 | + |
| 239 | + public boolean supports(Class<?> authentication) { |
| 240 | + return authentication.isAssignableFrom(Saml2AuthenticationToken.class); |
| 241 | + } |
| 242 | + }; |
| 243 | + } |
| 244 | + |
| 245 | + static class Saml2LoginConfigBeans { |
| 246 | + |
| 247 | + @Bean |
| 248 | + SecurityContextRepository securityContextRepository() { |
| 249 | + return new HttpSessionSecurityContextRepository(); |
| 250 | + } |
| 251 | + |
| 252 | + @Bean |
| 253 | + RelyingPartyRegistrationRepository relyingPartyRegistrationRepository() { |
| 254 | + RelyingPartyRegistrationRepository repository = mock(RelyingPartyRegistrationRepository.class); |
| 255 | + when(repository.findByRegistrationId(anyString())).thenReturn( |
| 256 | + saml2AuthenticationConfiguration() |
| 257 | + ); |
| 258 | + return repository; |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | +} |
0 commit comments