|
29 | 29 | import org.junit.jupiter.api.BeforeEach; |
30 | 30 | import org.junit.jupiter.api.Test; |
31 | 31 | import org.junit.jupiter.api.extension.ExtendWith; |
| 32 | +import org.mockito.Mockito; |
32 | 33 |
|
33 | 34 | import org.springframework.beans.factory.BeanCreationException; |
34 | 35 | import org.springframework.beans.factory.NoUniqueBeanDefinitionException; |
|
50 | 51 | import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig; |
51 | 52 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
52 | 53 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 54 | +import org.springframework.security.config.annotation.web.configurers.oauth2.client.OAuth2LoginConfigurerTests.OAuth2LoginConfigCustomWithPostProcessor.SpyObjectPostProcessor; |
53 | 55 | import org.springframework.security.config.oauth2.client.CommonOAuth2Provider; |
54 | 56 | import org.springframework.security.config.test.SpringTestContext; |
55 | 57 | import org.springframework.security.config.test.SpringTestContextExtension; |
56 | 58 | import org.springframework.security.context.DelegatingApplicationListener; |
57 | 59 | import org.springframework.security.core.Authentication; |
58 | | -import org.springframework.security.core.AuthenticationException; |
59 | 60 | import org.springframework.security.core.GrantedAuthority; |
60 | 61 | import org.springframework.security.core.authority.AuthorityUtils; |
61 | 62 | import org.springframework.security.core.authority.SimpleGrantedAuthority; |
@@ -218,28 +219,6 @@ public void oauth2Login() throws Exception { |
218 | 219 | .hasToString("OAUTH2_USER"); |
219 | 220 | } |
220 | 221 |
|
221 | | - // gh-17175 |
222 | | - @Test |
223 | | - public void postProcessorSucceedsWhenProcessorReturnsAuthenticationProvider() throws Exception { |
224 | | - loadConfig(OAuth2LoginConfigCustomWithPostProcessor.class); |
225 | | - // setup authorization request |
226 | | - OAuth2AuthorizationRequest authorizationRequest = createOAuth2AuthorizationRequest(); |
227 | | - this.authorizationRequestRepository.saveAuthorizationRequest(authorizationRequest, this.request, this.response); |
228 | | - // setup authentication parameters |
229 | | - this.request.setParameter("code", "code123"); |
230 | | - this.request.setParameter("state", authorizationRequest.getState()); |
231 | | - // perform test |
232 | | - this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); |
233 | | - // assertions |
234 | | - Authentication authentication = this.securityContextRepository |
235 | | - .loadContext(new HttpRequestResponseHolder(this.request, this.response)) |
236 | | - .getAuthentication(); |
237 | | - assertThat(authentication.getAuthorities()).hasSize(1); |
238 | | - assertThat(authentication.getAuthorities()).first() |
239 | | - .isInstanceOf(OAuth2UserAuthority.class) |
240 | | - .hasToString("OAUTH2_USER"); |
241 | | - } |
242 | | - |
243 | 222 | @Test |
244 | 223 | public void requestWhenCustomSecurityContextHolderStrategyThenUses() throws Exception { |
245 | 224 | loadConfig(OAuth2LoginConfig.class, SecurityContextChangedListenerConfig.class); |
@@ -735,6 +714,22 @@ public void oidcLoginWhenOAuth2ClientBeansConfiguredThenNotShared() throws Excep |
735 | 714 | verifyNoInteractions(clientRegistrationRepository, authorizedClientRepository); |
736 | 715 | } |
737 | 716 |
|
| 717 | + // gh-17175 |
| 718 | + @Test |
| 719 | + public void oauth2LoginWhenAuthenticationProviderPostProcessorThenUses() throws Exception { |
| 720 | + loadConfig(OAuth2LoginConfigCustomWithPostProcessor.class); |
| 721 | + // setup authorization request |
| 722 | + OAuth2AuthorizationRequest authorizationRequest = createOAuth2AuthorizationRequest(); |
| 723 | + this.authorizationRequestRepository.saveAuthorizationRequest(authorizationRequest, this.request, this.response); |
| 724 | + // setup authentication parameters |
| 725 | + this.request.setParameter("code", "code123"); |
| 726 | + this.request.setParameter("state", authorizationRequest.getState()); |
| 727 | + // perform test |
| 728 | + this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); |
| 729 | + // assertions |
| 730 | + verify(this.context.getBean(SpyObjectPostProcessor.class).spy).authenticate(any()); |
| 731 | + } |
| 732 | + |
738 | 733 | private void loadConfig(Class<?>... configs) { |
739 | 734 | AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); |
740 | 735 | applicationContext.register(configs); |
@@ -1335,50 +1330,46 @@ OAuth2AuthorizedClientRepository authorizedClientRepository() { |
1335 | 1330 |
|
1336 | 1331 | @Configuration |
1337 | 1332 | @EnableWebSecurity |
1338 | | - static class OAuth2LoginConfigCustomWithPostProcessor |
1339 | | - extends CommonLambdaSecurityFilterChainConfig { |
| 1333 | + static class OAuth2LoginConfigCustomWithPostProcessor { |
1340 | 1334 |
|
1341 | | - private ClientRegistrationRepository clientRegistrationRepository = new InMemoryClientRegistrationRepository( |
| 1335 | + private final ClientRegistrationRepository clientRegistrationRepository = new InMemoryClientRegistrationRepository( |
1342 | 1336 | GOOGLE_CLIENT_REGISTRATION); |
1343 | 1337 |
|
1344 | | - OAuth2AuthorizationRequestResolver resolver = mock(OAuth2AuthorizationRequestResolver.class); |
| 1338 | + private final ObjectPostProcessor<AuthenticationProvider> postProcessor = new SpyObjectPostProcessor(); |
1345 | 1339 |
|
1346 | 1340 | @Bean |
1347 | 1341 | SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
1348 | 1342 | // @formatter:off |
1349 | 1343 | http |
1350 | | - .oauth2Login((oauth2Login) -> |
1351 | | - oauth2Login |
1352 | | - .clientRegistrationRepository(this.clientRegistrationRepository) |
1353 | | -// .authorizedClientRepository(this.authorizedClientRepository) |
1354 | | - .withObjectPostProcessor(new CustomProcessor()) |
1355 | | - ); |
| 1344 | + .oauth2Login((oauth2Login) -> oauth2Login |
| 1345 | + .clientRegistrationRepository(this.clientRegistrationRepository) |
| 1346 | + .withObjectPostProcessor(this.postProcessor) |
| 1347 | + ); |
1356 | 1348 | // @formatter:on |
1357 | | - return super.configureFilterChain(http); |
| 1349 | + return http.build(); |
1358 | 1350 | } |
1359 | 1351 |
|
1360 | | - class CustomProcessor implements ObjectPostProcessor<AuthenticationProvider> { |
1361 | | - @Override |
1362 | | - public <O extends AuthenticationProvider> O postProcess(O object) { |
1363 | | - AuthenticationProvider p = new NoopWrapperProvider(object); |
| 1352 | + @Bean |
| 1353 | + ObjectPostProcessor<AuthenticationProvider> mockPostProcessor() { |
| 1354 | + return this.postProcessor; |
| 1355 | + } |
1364 | 1356 |
|
1365 | | - return (O) p; |
1366 | | - } |
| 1357 | + @Bean |
| 1358 | + HttpSessionOAuth2AuthorizationRequestRepository oauth2AuthorizationRequestRepository() { |
| 1359 | + return new HttpSessionOAuth2AuthorizationRequestRepository(); |
1367 | 1360 | } |
1368 | 1361 |
|
1369 | | - record NoopWrapperProvider( |
1370 | | - AuthenticationProvider delegate |
1371 | | - ) implements AuthenticationProvider { |
| 1362 | + static class SpyObjectPostProcessor implements ObjectPostProcessor<AuthenticationProvider> { |
1372 | 1363 |
|
1373 | | - @Override |
1374 | | - public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
1375 | | - return delegate.authenticate(authentication); |
1376 | | - } |
| 1364 | + AuthenticationProvider spy; |
1377 | 1365 |
|
1378 | 1366 | @Override |
1379 | | - public boolean supports(Class<?> authentication) { |
1380 | | - return delegate.supports(authentication); |
| 1367 | + public <O extends AuthenticationProvider> O postProcess(O object) { |
| 1368 | + O spy = Mockito.spy(object); |
| 1369 | + this.spy = spy; |
| 1370 | + return spy; |
1381 | 1371 | } |
| 1372 | + |
1382 | 1373 | } |
1383 | 1374 |
|
1384 | 1375 | } |
|
0 commit comments