|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.net.URLDecoder;
|
21 |
| -import java.time.Duration; |
22 | 21 | import java.util.Base64;
|
23 |
| -import java.util.Collection; |
24 | 22 | import java.util.Collections;
|
25 | 23 |
|
26 | 24 | import jakarta.servlet.ServletException;
|
|
32 | 30 | import org.junit.jupiter.api.Test;
|
33 | 31 | import org.junit.jupiter.api.extension.ExtendWith;
|
34 | 32 | import org.mockito.ArgumentCaptor;
|
35 |
| -import org.opensaml.saml.saml2.core.Assertion; |
36 | 33 |
|
37 | 34 | import org.springframework.beans.factory.BeanCreationException;
|
38 | 35 | import org.springframework.beans.factory.annotation.Autowired;
|
39 | 36 | import org.springframework.context.ConfigurableApplicationContext;
|
40 | 37 | import org.springframework.context.annotation.Bean;
|
41 | 38 | import org.springframework.context.annotation.Configuration;
|
42 | 39 | import org.springframework.context.annotation.Import;
|
43 |
| -import org.springframework.core.convert.converter.Converter; |
44 | 40 | import org.springframework.http.MediaType;
|
45 | 41 | import org.springframework.mock.web.MockFilterChain;
|
46 | 42 | import org.springframework.mock.web.MockHttpServletRequest;
|
47 | 43 | import org.springframework.mock.web.MockHttpServletResponse;
|
48 | 44 | import org.springframework.mock.web.MockHttpSession;
|
49 | 45 | import org.springframework.security.authentication.AuthenticationManager;
|
50 |
| -import org.springframework.security.authentication.AuthenticationProvider; |
51 | 46 | import org.springframework.security.authentication.AuthenticationServiceException;
|
52 |
| -import org.springframework.security.authentication.ProviderManager; |
53 | 47 | import org.springframework.security.config.Customizer;
|
54 |
| -import org.springframework.security.config.annotation.ObjectPostProcessor; |
55 | 48 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
56 | 49 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
57 | 50 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
58 | 51 | import org.springframework.security.config.test.SpringTestContext;
|
59 | 52 | import org.springframework.security.config.test.SpringTestContextExtension;
|
60 | 53 | import org.springframework.security.core.Authentication;
|
61 | 54 | import org.springframework.security.core.AuthenticationException;
|
62 |
| -import org.springframework.security.core.GrantedAuthority; |
63 | 55 | import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
64 | 56 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
65 |
| -import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper; |
66 | 57 | import org.springframework.security.saml2.core.Saml2ErrorCodes;
|
67 | 58 | import org.springframework.security.saml2.core.Saml2Utils;
|
68 | 59 | import org.springframework.security.saml2.core.TestSaml2X509Credentials;
|
69 | 60 | import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
|
70 |
| -import org.springframework.security.saml2.provider.service.authentication.OpenSaml4AuthenticationProvider; |
71 |
| -import org.springframework.security.saml2.provider.service.authentication.OpenSamlAuthenticationProvider; |
72 | 61 | import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
|
73 | 62 | import org.springframework.security.saml2.provider.service.authentication.Saml2Authentication;
|
74 | 63 | import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
|
|
77 | 66 | import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration;
|
78 | 67 | import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository;
|
79 | 68 | import org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations;
|
80 |
| -import org.springframework.security.saml2.provider.service.servlet.filter.Saml2WebSsoAuthenticationFilter; |
81 | 69 | import org.springframework.security.saml2.provider.service.web.DefaultRelyingPartyRegistrationResolver;
|
82 | 70 | import org.springframework.security.saml2.provider.service.web.RelyingPartyRegistrationResolver;
|
83 | 71 | import org.springframework.security.saml2.provider.service.web.Saml2AuthenticationRequestRepository;
|
|
91 | 79 | import org.springframework.security.web.context.HttpRequestResponseHolder;
|
92 | 80 | import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
|
93 | 81 | import org.springframework.security.web.context.SecurityContextRepository;
|
94 |
| -import org.springframework.test.util.ReflectionTestUtils; |
95 | 82 | import org.springframework.test.web.servlet.MockMvc;
|
96 | 83 | import org.springframework.test.web.servlet.MvcResult;
|
97 | 84 | import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
|
121 | 108 | @ExtendWith(SpringTestContextExtension.class)
|
122 | 109 | public class Saml2LoginConfigurerTests {
|
123 | 110 |
|
124 |
| - private static final Converter<Assertion, Collection<? extends GrantedAuthority>> AUTHORITIES_EXTRACTOR = ( |
125 |
| - a) -> Collections.singletonList(new SimpleGrantedAuthority("TEST")); |
126 |
| - |
127 |
| - private static final GrantedAuthoritiesMapper AUTHORITIES_MAPPER = (authorities) -> Collections |
128 |
| - .singletonList(new SimpleGrantedAuthority("TEST CONVERTED")); |
129 |
| - |
130 |
| - private static final Duration RESPONSE_TIME_VALIDATION_SKEW = Duration.ZERO; |
131 |
| - |
132 | 111 | private static final String SIGNED_RESPONSE = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48c2FtbDJwOlJlc3BvbnNlIHhtbG5zOnNhbWwycD0idXJuOm9hc2lzOm5hbWVzOnRjOlNBTUw6Mi4wOnByb3RvY29sIiBEZXN0aW5hdGlvbj0iaHR0cHM6Ly9ycC5leGFtcGxlLm9yZy9hY3MiIElEPSJfYzE3MzM2YTAtNTM1My00MTQ5LWI3MmMtMDNkOWY5YWYzMDdlIiBJc3N1ZUluc3RhbnQ9IjIwMjAtMDgtMDRUMjI6MDQ6NDUuMDE2WiIgVmVyc2lvbj0iMi4wIj48c2FtbDI6SXNzdWVyIHhtbG5zOnNhbWwyPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6YXNzZXJ0aW9uIj5hcC1lbnRpdHktaWQ8L3NhbWwyOklzc3Vlcj48ZHM6U2lnbmF0dXJlIHhtbG5zOmRzPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwLzA5L3htbGRzaWcjIj4KPGRzOlNpZ25lZEluZm8+Cj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tMDRUMjE6NTk6NDUuMDgwWiIgTm90T25PckFmdGVyPSIyMDQwLTA3LTMwVDIyOjA1OjA2LjA4N1oiLz48L3NhbWwyOkFzc2VydGlvbj48L3NhbWwycDpSZXNwb25zZT4=";
|
133 | 112 |
|
134 | 113 | private static final AuthenticationConverter AUTHENTICATION_CONVERTER = mock(AuthenticationConverter.class);
|
@@ -197,14 +176,6 @@ public void saml2LoginWhenDefaultAndSamlAuthenticationManagerThenSamlManagerIsUs
|
197 | 176 | performSaml2Login("ROLE_AUTH_MANAGER");
|
198 | 177 | }
|
199 | 178 |
|
200 |
| - @Test |
201 |
| - public void saml2LoginWhenConfiguringAuthenticationDefaultsUsingCustomizerThenTheProviderIsConfigured() |
202 |
| - throws Exception { |
203 |
| - // setup application context |
204 |
| - this.spring.register(Saml2LoginConfigWithAuthenticationDefaultsWithPostProcessor.class).autowire(); |
205 |
| - validateSaml2WebSsoAuthenticationFilterConfiguration(); |
206 |
| - } |
207 |
| - |
208 | 179 | @Test
|
209 | 180 | public void authenticationRequestWhenAuthenticationRequestResolverBeanThenUses() throws Exception {
|
210 | 181 | this.spring.register(CustomAuthenticationRequestResolverBean.class).autowire();
|
@@ -362,22 +333,6 @@ public void getFaviconWhenDefaultConfigurationThenDoesNotSaveAuthnRequest() thro
|
362 | 333 | .andExpect(redirectedUrl("http://localhost/saml2/authenticate/registration-id"));
|
363 | 334 | }
|
364 | 335 |
|
365 |
| - private void validateSaml2WebSsoAuthenticationFilterConfiguration() { |
366 |
| - // get the OpenSamlAuthenticationProvider |
367 |
| - Saml2WebSsoAuthenticationFilter filter = getSaml2SsoFilter(this.springSecurityFilterChain); |
368 |
| - AuthenticationManager manager = (AuthenticationManager) ReflectionTestUtils.getField(filter, |
369 |
| - "authenticationManager"); |
370 |
| - ProviderManager pm = (ProviderManager) manager; |
371 |
| - AuthenticationProvider provider = pm.getProviders().stream() |
372 |
| - .filter((p) -> p instanceof OpenSaml4AuthenticationProvider).findFirst().get(); |
373 |
| - assertThat(provider).isNotNull(); |
374 |
| - } |
375 |
| - |
376 |
| - private Saml2WebSsoAuthenticationFilter getSaml2SsoFilter(FilterChainProxy chain) { |
377 |
| - return (Saml2WebSsoAuthenticationFilter) chain.getFilters("/login/saml2/sso/test").stream() |
378 |
| - .filter((f) -> f instanceof Saml2WebSsoAuthenticationFilter).findFirst().get(); |
379 |
| - } |
380 |
| - |
381 | 336 | private void performSaml2Login(String expected) throws IOException, ServletException {
|
382 | 337 | // setup authentication parameters
|
383 | 338 | this.request.setRequestURI("/login/saml2/sso/registration-id");
|
@@ -460,28 +415,6 @@ protected void configure(HttpSecurity http) throws Exception {
|
460 | 415 |
|
461 | 416 | }
|
462 | 417 |
|
463 |
| - @Configuration |
464 |
| - @EnableWebSecurity |
465 |
| - @Import(Saml2LoginConfigBeans.class) |
466 |
| - static class Saml2LoginConfigWithAuthenticationDefaultsWithPostProcessor extends WebSecurityConfigurerAdapter { |
467 |
| - |
468 |
| - @Override |
469 |
| - protected void configure(HttpSecurity http) throws Exception { |
470 |
| - ObjectPostProcessor<OpenSamlAuthenticationProvider> processor = new ObjectPostProcessor<OpenSamlAuthenticationProvider>() { |
471 |
| - @Override |
472 |
| - public <O extends OpenSamlAuthenticationProvider> O postProcess(O provider) { |
473 |
| - provider.setResponseTimeValidationSkew(RESPONSE_TIME_VALIDATION_SKEW); |
474 |
| - provider.setAuthoritiesMapper(AUTHORITIES_MAPPER); |
475 |
| - provider.setAuthoritiesExtractor(AUTHORITIES_EXTRACTOR); |
476 |
| - return provider; |
477 |
| - } |
478 |
| - }; |
479 |
| - http.saml2Login().addObjectPostProcessor(processor); |
480 |
| - super.configure(http); |
481 |
| - } |
482 |
| - |
483 |
| - } |
484 |
| - |
485 | 418 | @Configuration
|
486 | 419 | @EnableWebSecurity
|
487 | 420 | @Import(Saml2LoginConfigBeans.class)
|
|
0 commit comments