|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.config.annotation.web.reactive; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 21 | +import reactor.core.publisher.Mono; |
| 22 | + |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.context.ApplicationContext; |
| 25 | +import org.springframework.context.annotation.Bean; |
| 26 | +import org.springframework.security.config.test.SpringTestContext; |
| 27 | +import org.springframework.security.config.test.SpringTestContextExtension; |
| 28 | +import org.springframework.security.config.web.server.ServerHttpSecurity; |
| 29 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; |
| 30 | +import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientManager; |
| 31 | +import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient; |
| 32 | +import org.springframework.security.oauth2.client.registration.ClientRegistration; |
| 33 | +import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository; |
| 34 | +import org.springframework.security.oauth2.client.registration.TestClientRegistrations; |
| 35 | +import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository; |
| 36 | +import org.springframework.security.oauth2.core.TestOAuth2AccessTokens; |
| 37 | +import org.springframework.security.web.server.SecurityWebFilterChain; |
| 38 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 39 | +import org.springframework.web.bind.annotation.GetMapping; |
| 40 | +import org.springframework.web.bind.annotation.RestController; |
| 41 | +import org.springframework.web.reactive.config.EnableWebFlux; |
| 42 | + |
| 43 | +import static org.mockito.ArgumentMatchers.any; |
| 44 | +import static org.mockito.BDDMockito.given; |
| 45 | +import static org.mockito.Mockito.mock; |
| 46 | +import static org.mockito.Mockito.verify; |
| 47 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 48 | + |
| 49 | +/** |
| 50 | + * Tests for {@link ReactiveOAuth2ClientImportSelector}. |
| 51 | + * |
| 52 | + * @author Alavudin Kuttikkattil |
| 53 | + */ |
| 54 | +@ExtendWith(SpringTestContextExtension.class) |
| 55 | +public class ReactiveOAuth2ClientImportSelectorTest { |
| 56 | + |
| 57 | + public final SpringTestContext spring = new SpringTestContext(this); |
| 58 | + |
| 59 | + WebTestClient client; |
| 60 | + |
| 61 | + @Autowired |
| 62 | + public void setApplicationContext(ApplicationContext context) { |
| 63 | + // @formatter:off |
| 64 | + this.client = WebTestClient |
| 65 | + .bindToApplicationContext(context) |
| 66 | + .build(); |
| 67 | + // @formatter:on |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void requestWhenAuthorizedClientManagerConfiguredThenUsed() { |
| 72 | + String clientRegistrationId = "client"; |
| 73 | + String principalName = "user"; |
| 74 | + ReactiveClientRegistrationRepository clientRegistrationRepository = mock( |
| 75 | + ReactiveClientRegistrationRepository.class); |
| 76 | + ServerOAuth2AuthorizedClientRepository authorizedClientRepository = mock( |
| 77 | + ServerOAuth2AuthorizedClientRepository.class); |
| 78 | + ReactiveOAuth2AuthorizedClientManager authorizedClientManager = mock( |
| 79 | + ReactiveOAuth2AuthorizedClientManager.class); |
| 80 | + ClientRegistration clientRegistration = TestClientRegistrations.clientCredentials() |
| 81 | + .registrationId(clientRegistrationId).build(); |
| 82 | + OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(clientRegistration, principalName, |
| 83 | + TestOAuth2AccessTokens.noScopes()); |
| 84 | + given(authorizedClientManager.authorize(any())).willReturn(Mono.just(authorizedClient)); |
| 85 | + OAuth2AuthorizedClientManagerRegisteredConfig.CLIENT_REGISTRATION_REPOSITORY = clientRegistrationRepository; |
| 86 | + OAuth2AuthorizedClientManagerRegisteredConfig.AUTHORIZED_CLIENT_REPOSITORY = authorizedClientRepository; |
| 87 | + OAuth2AuthorizedClientManagerRegisteredConfig.AUTHORIZED_CLIENT_MANAGER = authorizedClientManager; |
| 88 | + this.spring.register(OAuth2AuthorizedClientManagerRegisteredConfig.class).autowire(); |
| 89 | + // @formatter:off |
| 90 | + this.client |
| 91 | + .get() |
| 92 | + .uri("http://localhost/authorized-client") |
| 93 | + .headers((headers) -> headers.setBasicAuth("user", "password")).exchange().expectStatus().isOk() |
| 94 | + .expectBody(String.class).isEqualTo("resolved"); |
| 95 | + // @formatter:on |
| 96 | + verify(authorizedClientManager).authorize(any()); |
| 97 | + verifyNoInteractions(clientRegistrationRepository); |
| 98 | + verifyNoInteractions(authorizedClientRepository); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void requestWhenAuthorizedClientManagerNotConfigureThenUseDefaultAuthorizedClientManager() { |
| 103 | + String clientRegistrationId = "client"; |
| 104 | + String principalName = "user"; |
| 105 | + ReactiveClientRegistrationRepository clientRegistrationRepository = mock( |
| 106 | + ReactiveClientRegistrationRepository.class); |
| 107 | + ServerOAuth2AuthorizedClientRepository authorizedClientRepository = mock( |
| 108 | + ServerOAuth2AuthorizedClientRepository.class); |
| 109 | + ClientRegistration clientRegistration = TestClientRegistrations.clientCredentials() |
| 110 | + .registrationId(clientRegistrationId).build(); |
| 111 | + OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(clientRegistration, principalName, |
| 112 | + TestOAuth2AccessTokens.noScopes()); |
| 113 | + OAuth2AuthorizedClientManagerRegisteredConfig.CLIENT_REGISTRATION_REPOSITORY = clientRegistrationRepository; |
| 114 | + OAuth2AuthorizedClientManagerRegisteredConfig.AUTHORIZED_CLIENT_REPOSITORY = authorizedClientRepository; |
| 115 | + OAuth2AuthorizedClientManagerRegisteredConfig.AUTHORIZED_CLIENT_MANAGER = null; |
| 116 | + given(authorizedClientRepository.loadAuthorizedClient(any(), any(), any())) |
| 117 | + .willReturn(Mono.just(authorizedClient)); |
| 118 | + this.spring.register(OAuth2AuthorizedClientManagerRegisteredConfig.class).autowire(); |
| 119 | + // @formatter:off |
| 120 | + this.client |
| 121 | + .get() |
| 122 | + .uri("http://localhost/authorized-client") |
| 123 | + .headers((headers) -> headers.setBasicAuth("user", "password")).exchange().expectStatus().isOk() |
| 124 | + .expectBody(String.class).isEqualTo("resolved"); |
| 125 | + // @formatter:on |
| 126 | + } |
| 127 | + |
| 128 | + @EnableWebFlux |
| 129 | + @EnableWebFluxSecurity |
| 130 | + static class OAuth2AuthorizedClientManagerRegisteredConfig { |
| 131 | + |
| 132 | + static ReactiveClientRegistrationRepository CLIENT_REGISTRATION_REPOSITORY; |
| 133 | + static ServerOAuth2AuthorizedClientRepository AUTHORIZED_CLIENT_REPOSITORY; |
| 134 | + static ReactiveOAuth2AuthorizedClientManager AUTHORIZED_CLIENT_MANAGER; |
| 135 | + |
| 136 | + @Bean |
| 137 | + SecurityWebFilterChain springSecurity(ServerHttpSecurity http) { |
| 138 | + return http.build(); |
| 139 | + } |
| 140 | + |
| 141 | + @Bean |
| 142 | + ReactiveClientRegistrationRepository clientRegistrationRepository() { |
| 143 | + return CLIENT_REGISTRATION_REPOSITORY; |
| 144 | + } |
| 145 | + |
| 146 | + @Bean |
| 147 | + ServerOAuth2AuthorizedClientRepository authorizedClientRepository() { |
| 148 | + return AUTHORIZED_CLIENT_REPOSITORY; |
| 149 | + } |
| 150 | + |
| 151 | + @Bean |
| 152 | + ReactiveOAuth2AuthorizedClientManager authorizedClientManager() { |
| 153 | + return AUTHORIZED_CLIENT_MANAGER; |
| 154 | + } |
| 155 | + |
| 156 | + @RestController |
| 157 | + class Controller { |
| 158 | + |
| 159 | + @GetMapping("/authorized-client") |
| 160 | + String authorizedClient( |
| 161 | + @RegisteredOAuth2AuthorizedClient("client1") OAuth2AuthorizedClient authorizedClient) { |
| 162 | + return (authorizedClient != null) ? "resolved" : "not-resolved"; |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments