Skip to content

Commit 982f3f9

Browse files
committed
Add oauth2Login Reactive Test Support
Fixes gh-7828
1 parent 841275e commit 982f3f9

File tree

4 files changed

+378
-7
lines changed

4 files changed

+378
-7
lines changed

samples/boot/oauth2login-webflux/src/integration-test/java/sample/OAuth2LoginApplicationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@
3030
import org.springframework.test.web.reactive.server.WebTestClient;
3131

3232
import static org.hamcrest.core.StringContains.containsString;
33-
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockOidcLogin;
33+
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockOAuth2Login;
3434

3535
/**
3636
* Tests for {@link ReactiveOAuth2LoginApplication}
@@ -58,7 +58,7 @@ ServerOAuth2AuthorizedClientRepository authorizedClientRepository() {
5858
public void requestWhenMockOidcLoginThenIndex() {
5959
this.clientRegistrationRepository.findByRegistrationId("github")
6060
.map(clientRegistration ->
61-
this.test.mutateWith(mockOidcLogin().clientRegistration(clientRegistration))
61+
this.test.mutateWith(mockOAuth2Login().clientRegistration(clientRegistration))
6262
.get().uri("/")
6363
.exchange()
6464
.expectBody(String.class).value(containsString("GitHub"))

samples/boot/oauth2login-webflux/src/test/java/sample/OAuth2LoginControllerTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@
3636
import org.springframework.web.reactive.result.view.ViewResolver;
3737

3838
import static org.hamcrest.Matchers.containsString;
39-
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockOidcLogin;
39+
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockOAuth2Login;
4040
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity;
4141

4242
/**
@@ -77,7 +77,7 @@ public void setup() {
7777

7878
@Test
7979
public void indexGreetsAuthenticatedUser() {
80-
this.rest.mutateWith(mockOidcLogin())
80+
this.rest.mutateWith(mockOAuth2Login())
8181
.get().uri("/").exchange()
8282
.expectBody(String.class).value(containsString("test-subject"));
8383
}

test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityMockServerConfigurers.java

Lines changed: 190 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,10 @@
1919
import java.util.Arrays;
2020
import java.util.Collection;
2121
import java.util.Collections;
22+
import java.util.HashMap;
2223
import java.util.LinkedHashSet;
2324
import java.util.List;
25+
import java.util.Map;
2426
import java.util.Set;
2527
import java.util.function.Consumer;
2628
import java.util.function.Supplier;
@@ -52,6 +54,9 @@
5254
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
5355
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
5456
import org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority;
57+
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
58+
import org.springframework.security.oauth2.core.user.OAuth2User;
59+
import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
5560
import org.springframework.security.oauth2.jwt.Jwt;
5661
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
5762
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
@@ -147,6 +152,21 @@ public static JwtMutator mockJwt() {
147152
return new JwtMutator();
148153
}
149154

155+
/**
156+
* Updates the ServerWebExchange to establish a {@link SecurityContext} that has a
157+
* {@link OAuth2AuthenticationToken} for the
158+
* {@link Authentication}. All details are
159+
* declarative and do not require the corresponding OAuth 2.0 tokens to be valid.
160+
*
161+
* @return the {@link OAuth2LoginMutator} to further configure or use
162+
* @since 5.3
163+
*/
164+
public static OAuth2LoginMutator mockOAuth2Login() {
165+
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "access-token",
166+
null, null, Collections.singleton("user"));
167+
return new OAuth2LoginMutator(accessToken);
168+
}
169+
150170
/**
151171
* Updates the ServerWebExchange to establish a {@link SecurityContext} that has a
152172
* {@link OAuth2AuthenticationToken} for the
@@ -462,6 +482,175 @@ private <T extends WebTestClientConfigurer & MockServerConfigurer> T configurer(
462482
}
463483
}
464484

485+
/**
486+
* @author Josh Cummings
487+
* @since 5.3
488+
*/
489+
public final static class OAuth2LoginMutator implements WebTestClientConfigurer, MockServerConfigurer {
490+
private ClientRegistration clientRegistration;
491+
private OAuth2AccessToken accessToken;
492+
493+
private Supplier<Collection<GrantedAuthority>> authorities = this::defaultAuthorities;
494+
private Supplier<Map<String, Object>> attributes = this::defaultAttributes;
495+
private String nameAttributeKey = "sub";
496+
private Supplier<OAuth2User> oauth2User = this::defaultPrincipal;
497+
498+
private final ServerOAuth2AuthorizedClientRepository authorizedClientRepository =
499+
new WebSessionServerOAuth2AuthorizedClientRepository();
500+
501+
private OAuth2LoginMutator(OAuth2AccessToken accessToken) {
502+
this.accessToken = accessToken;
503+
this.clientRegistration = clientRegistrationBuilder().build();
504+
}
505+
506+
/**
507+
* Use the provided authorities in the {@link Authentication}
508+
*
509+
* @param authorities the authorities to use
510+
* @return the {@link OAuth2LoginMutator} for further configuration
511+
*/
512+
public OAuth2LoginMutator authorities(Collection<GrantedAuthority> authorities) {
513+
Assert.notNull(authorities, "authorities cannot be null");
514+
this.authorities = () -> authorities;
515+
this.oauth2User = this::defaultPrincipal;
516+
return this;
517+
}
518+
519+
/**
520+
* Use the provided authorities in the {@link Authentication}
521+
*
522+
* @param authorities the authorities to use
523+
* @return the {@link OAuth2LoginMutator} for further configuration
524+
*/
525+
public OAuth2LoginMutator authorities(GrantedAuthority... authorities) {
526+
Assert.notNull(authorities, "authorities cannot be null");
527+
this.authorities = () -> Arrays.asList(authorities);
528+
this.oauth2User = this::defaultPrincipal;
529+
return this;
530+
}
531+
532+
/**
533+
* Mutate the attributes using the given {@link Consumer}
534+
*
535+
* @param attributesConsumer The {@link Consumer} for mutating the {@Map} of attributes
536+
* @return the {@link OAuth2LoginMutator} for further configuration
537+
*/
538+
public OAuth2LoginMutator attributes(Consumer<Map<String, Object>> attributesConsumer) {
539+
Assert.notNull(attributesConsumer, "attributesConsumer cannot be null");
540+
this.attributes = () -> {
541+
Map<String, Object> attrs = new HashMap<>();
542+
attrs.put(this.nameAttributeKey, "test-subject");
543+
attributesConsumer.accept(attrs);
544+
return attrs;
545+
};
546+
this.oauth2User = this::defaultPrincipal;
547+
return this;
548+
}
549+
550+
/**
551+
* Use the provided key for the attribute containing the principal's name
552+
*
553+
* @param nameAttributeKey The attribute key to use
554+
* @return the {@link OAuth2LoginMutator} for further configuration
555+
*/
556+
public OAuth2LoginMutator nameAttributeKey(String nameAttributeKey) {
557+
Assert.notNull(nameAttributeKey, "nameAttributeKey cannot be null");
558+
this.nameAttributeKey = nameAttributeKey;
559+
this.oauth2User = this::defaultPrincipal;
560+
return this;
561+
}
562+
563+
/**
564+
* Use the provided {@link OAuth2User} as the authenticated user.
565+
*
566+
* @param oauth2User the {@link OAuth2User} to use
567+
* @return the {@link OAuth2LoginMutator} for further configuration
568+
*/
569+
public OAuth2LoginMutator oauth2User(OAuth2User oauth2User) {
570+
this.oauth2User = () -> oauth2User;
571+
return this;
572+
}
573+
574+
/**
575+
* Use the provided {@link ClientRegistration} as the client to authorize.
576+
* <p>
577+
* The supplied {@link ClientRegistration} will be registered into an
578+
* {@link WebSessionServerOAuth2AuthorizedClientRepository}. Tests relying on
579+
* {@link org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient}
580+
* annotations should register an {@link WebSessionServerOAuth2AuthorizedClientRepository} bean
581+
* to the application context.
582+
*
583+
* @param clientRegistration the {@link ClientRegistration} to use
584+
* @return the {@link OAuth2LoginMutator} for further configuration
585+
*/
586+
public OAuth2LoginMutator clientRegistration(ClientRegistration clientRegistration) {
587+
this.clientRegistration = clientRegistration;
588+
return this;
589+
}
590+
591+
@Override
592+
public void beforeServerCreated(WebHttpHandlerBuilder builder) {
593+
OAuth2AuthenticationToken token = getToken();
594+
builder.filters(addAuthorizedClientFilter(token));
595+
mockAuthentication(getToken()).beforeServerCreated(builder);
596+
}
597+
598+
@Override
599+
public void afterConfigureAdded(WebTestClient.MockServerSpec<?> serverSpec) {
600+
mockAuthentication(getToken()).afterConfigureAdded(serverSpec);
601+
}
602+
603+
@Override
604+
public void afterConfigurerAdded(
605+
WebTestClient.Builder builder,
606+
@Nullable WebHttpHandlerBuilder httpHandlerBuilder,
607+
@Nullable ClientHttpConnector connector) {
608+
OAuth2AuthenticationToken token = getToken();
609+
httpHandlerBuilder.filters(addAuthorizedClientFilter(token));
610+
mockAuthentication(token).afterConfigurerAdded(builder, httpHandlerBuilder, connector);
611+
}
612+
613+
private Consumer<List<WebFilter>> addAuthorizedClientFilter(OAuth2AuthenticationToken token) {
614+
OAuth2AuthorizedClient client = getClient();
615+
return filters -> filters.add(0, (exchange, chain) ->
616+
this.authorizedClientRepository.saveAuthorizedClient(client, token, exchange)
617+
.then(chain.filter(exchange)));
618+
}
619+
620+
private OAuth2AuthenticationToken getToken() {
621+
OAuth2User oauth2User = this.oauth2User.get();
622+
return new OAuth2AuthenticationToken(oauth2User, oauth2User.getAuthorities(), this.clientRegistration.getRegistrationId());
623+
}
624+
625+
private OAuth2AuthorizedClient getClient() {
626+
return new OAuth2AuthorizedClient(this.clientRegistration, getToken().getName(), this.accessToken);
627+
}
628+
629+
private ClientRegistration.Builder clientRegistrationBuilder() {
630+
return ClientRegistration.withRegistrationId("test")
631+
.authorizationGrantType(AuthorizationGrantType.PASSWORD)
632+
.clientId("test-client")
633+
.tokenUri("https://token-uri.example.org");
634+
}
635+
636+
private Collection<GrantedAuthority> defaultAuthorities() {
637+
Set<GrantedAuthority> authorities = new LinkedHashSet<>();
638+
authorities.add(new OAuth2UserAuthority(this.attributes.get()));
639+
for (String authority : this.accessToken.getScopes()) {
640+
authorities.add(new SimpleGrantedAuthority("SCOPE_" + authority));
641+
}
642+
return authorities;
643+
}
644+
645+
private Map<String, Object> defaultAttributes() {
646+
return Collections.singletonMap(this.nameAttributeKey, "test-subject");
647+
}
648+
649+
private OAuth2User defaultPrincipal() {
650+
return new DefaultOAuth2User(this.authorities.get(), this.attributes.get(), this.nameAttributeKey);
651+
}
652+
}
653+
465654
/**
466655
* @author Josh Cummings
467656
* @since 5.3

0 commit comments

Comments
 (0)