|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import java.util.Arrays;
|
20 | 20 | import java.util.Collection;
|
21 | 21 | import java.util.Collections;
|
| 22 | +import java.util.HashMap; |
22 | 23 | import java.util.LinkedHashSet;
|
23 | 24 | import java.util.List;
|
| 25 | +import java.util.Map; |
24 | 26 | import java.util.Set;
|
25 | 27 | import java.util.function.Consumer;
|
26 | 28 | import java.util.function.Supplier;
|
|
52 | 54 | import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
|
53 | 55 | import org.springframework.security.oauth2.core.oidc.user.OidcUser;
|
54 | 56 | 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; |
55 | 60 | import org.springframework.security.oauth2.jwt.Jwt;
|
56 | 61 | import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
|
57 | 62 | import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
|
@@ -147,6 +152,21 @@ public static JwtMutator mockJwt() {
|
147 | 152 | return new JwtMutator();
|
148 | 153 | }
|
149 | 154 |
|
| 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 | + |
150 | 170 | /**
|
151 | 171 | * Updates the ServerWebExchange to establish a {@link SecurityContext} that has a
|
152 | 172 | * {@link OAuth2AuthenticationToken} for the
|
@@ -462,6 +482,175 @@ private <T extends WebTestClientConfigurer & MockServerConfigurer> T configurer(
|
462 | 482 | }
|
463 | 483 | }
|
464 | 484 |
|
| 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 | + |
465 | 654 | /**
|
466 | 655 | * @author Josh Cummings
|
467 | 656 | * @since 5.3
|
|
0 commit comments