Skip to content

Commit 9f88ef8

Browse files
committed
Polish Post-Processor Test
Issue gh-17175 Signed-off-by: Josh Cummings <[email protected]>
1 parent 46283b3 commit 9f88ef8

File tree

1 file changed

+40
-50
lines changed

1 file changed

+40
-50
lines changed

config/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/client/OAuth2LoginConfigurerTests.java

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.junit.jupiter.api.BeforeEach;
3030
import org.junit.jupiter.api.Test;
3131
import org.junit.jupiter.api.extension.ExtendWith;
32+
import org.mockito.Mockito;
3233

3334
import org.springframework.beans.factory.BeanCreationException;
3435
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
@@ -45,17 +46,16 @@
4546
import org.springframework.mock.web.MockHttpServletResponse;
4647
import org.springframework.security.authentication.AuthenticationProvider;
4748
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
48-
import org.springframework.security.config.Customizer;
4949
import org.springframework.security.config.ObjectPostProcessor;
5050
import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig;
5151
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5252
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
53+
import org.springframework.security.config.annotation.web.configurers.oauth2.client.OAuth2LoginConfigurerTests.OAuth2LoginConfigCustomWithPostProcessor.SpyObjectPostProcessor;
5354
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
5455
import org.springframework.security.config.test.SpringTestContext;
5556
import org.springframework.security.config.test.SpringTestContextExtension;
5657
import org.springframework.security.context.DelegatingApplicationListener;
5758
import org.springframework.security.core.Authentication;
58-
import org.springframework.security.core.AuthenticationException;
5959
import org.springframework.security.core.GrantedAuthority;
6060
import org.springframework.security.core.authority.AuthorityUtils;
6161
import org.springframework.security.core.authority.SimpleGrantedAuthority;
@@ -218,28 +218,6 @@ public void oauth2Login() throws Exception {
218218
.hasToString("OAUTH2_USER");
219219
}
220220

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-
243221
@Test
244222
public void requestWhenCustomSecurityContextHolderStrategyThenUses() throws Exception {
245223
loadConfig(OAuth2LoginConfig.class, SecurityContextChangedListenerConfig.class);
@@ -735,6 +713,22 @@ public void oidcLoginWhenOAuth2ClientBeansConfiguredThenNotShared() throws Excep
735713
verifyNoInteractions(clientRegistrationRepository, authorizedClientRepository);
736714
}
737715

716+
// gh-17175
717+
@Test
718+
public void oauth2LoginWhenAuthenticationProviderPostProcessorThenUses() throws Exception {
719+
loadConfig(OAuth2LoginConfigCustomWithPostProcessor.class);
720+
// setup authorization request
721+
OAuth2AuthorizationRequest authorizationRequest = createOAuth2AuthorizationRequest();
722+
this.authorizationRequestRepository.saveAuthorizationRequest(authorizationRequest, this.request, this.response);
723+
// setup authentication parameters
724+
this.request.setParameter("code", "code123");
725+
this.request.setParameter("state", authorizationRequest.getState());
726+
// perform test
727+
this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain);
728+
// assertions
729+
verify(this.context.getBean(SpyObjectPostProcessor.class).spy).authenticate(any());
730+
}
731+
738732
private void loadConfig(Class<?>... configs) {
739733
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
740734
applicationContext.register(configs);
@@ -1335,50 +1329,46 @@ OAuth2AuthorizedClientRepository authorizedClientRepository() {
13351329

13361330
@Configuration
13371331
@EnableWebSecurity
1338-
static class OAuth2LoginConfigCustomWithPostProcessor
1339-
extends CommonLambdaSecurityFilterChainConfig {
1332+
static class OAuth2LoginConfigCustomWithPostProcessor {
13401333

1341-
private ClientRegistrationRepository clientRegistrationRepository = new InMemoryClientRegistrationRepository(
1334+
private final ClientRegistrationRepository clientRegistrationRepository = new InMemoryClientRegistrationRepository(
13421335
GOOGLE_CLIENT_REGISTRATION);
13431336

1344-
OAuth2AuthorizationRequestResolver resolver = mock(OAuth2AuthorizationRequestResolver.class);
1337+
private final ObjectPostProcessor<AuthenticationProvider> postProcessor = new SpyObjectPostProcessor();
13451338

13461339
@Bean
13471340
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
13481341
// @formatter:off
13491342
http
1350-
.oauth2Login((oauth2Login) ->
1351-
oauth2Login
1352-
.clientRegistrationRepository(this.clientRegistrationRepository)
1353-
// .authorizedClientRepository(this.authorizedClientRepository)
1354-
.withObjectPostProcessor(new CustomProcessor())
1355-
);
1343+
.oauth2Login((oauth2Login) -> oauth2Login
1344+
.clientRegistrationRepository(this.clientRegistrationRepository)
1345+
.withObjectPostProcessor(this.postProcessor)
1346+
);
13561347
// @formatter:on
1357-
return super.configureFilterChain(http);
1348+
return http.build();
13581349
}
13591350

1360-
class CustomProcessor implements ObjectPostProcessor<AuthenticationProvider> {
1361-
@Override
1362-
public <O extends AuthenticationProvider> O postProcess(O object) {
1363-
AuthenticationProvider p = new NoopWrapperProvider(object);
1351+
@Bean
1352+
ObjectPostProcessor<AuthenticationProvider> mockPostProcessor() {
1353+
return this.postProcessor;
1354+
}
13641355

1365-
return (O) p;
1366-
}
1356+
@Bean
1357+
HttpSessionOAuth2AuthorizationRequestRepository oauth2AuthorizationRequestRepository() {
1358+
return new HttpSessionOAuth2AuthorizationRequestRepository();
13671359
}
13681360

1369-
record NoopWrapperProvider(
1370-
AuthenticationProvider delegate
1371-
) implements AuthenticationProvider {
1361+
static class SpyObjectPostProcessor implements ObjectPostProcessor<AuthenticationProvider> {
13721362

1373-
@Override
1374-
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
1375-
return delegate.authenticate(authentication);
1376-
}
1363+
AuthenticationProvider spy;
13771364

13781365
@Override
1379-
public boolean supports(Class<?> authentication) {
1380-
return delegate.supports(authentication);
1366+
public <O extends AuthenticationProvider> O postProcess(O object) {
1367+
O spy = Mockito.spy(object);
1368+
this.spy = spy;
1369+
return spy;
13811370
}
1371+
13821372
}
13831373

13841374
}

0 commit comments

Comments
 (0)