|
@@ -13,7 +13,6 @@
|
|
* See the License for the specific language governing permissions and
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
* limitations under the License.
|
|
*/
|
|
*/
|
|
-
|
|
|
|
package org.springframework.security.oauth2.client.web.reactive.function.client;
|
|
package org.springframework.security.oauth2.client.web.reactive.function.client;
|
|
|
|
|
|
import org.junit.After;
|
|
import org.junit.After;
|
|
@@ -28,6 +27,9 @@ import org.springframework.core.codec.ByteBufferEncoder;
|
|
import org.springframework.core.codec.CharSequenceEncoder;
|
|
import org.springframework.core.codec.CharSequenceEncoder;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpMethod;
|
|
import org.springframework.http.HttpMethod;
|
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
|
+import org.springframework.http.RequestEntity;
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.codec.EncoderHttpMessageWriter;
|
|
import org.springframework.http.codec.EncoderHttpMessageWriter;
|
|
import org.springframework.http.codec.FormHttpMessageWriter;
|
|
import org.springframework.http.codec.FormHttpMessageWriter;
|
|
import org.springframework.http.codec.HttpMessageWriter;
|
|
import org.springframework.http.codec.HttpMessageWriter;
|
|
@@ -45,24 +47,31 @@ import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.authority.AuthorityUtils;
|
|
import org.springframework.security.core.authority.AuthorityUtils;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
|
|
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
|
|
|
|
+import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider;
|
|
|
|
+import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder;
|
|
|
|
+import org.springframework.security.oauth2.client.RefreshTokenOAuth2AuthorizedClientProvider;
|
|
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
|
|
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
|
|
|
|
+import org.springframework.security.oauth2.client.endpoint.DefaultClientCredentialsTokenResponseClient;
|
|
|
|
+import org.springframework.security.oauth2.client.endpoint.DefaultRefreshTokenTokenResponseClient;
|
|
import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient;
|
|
import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient;
|
|
import org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequest;
|
|
import org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequest;
|
|
|
|
+import org.springframework.security.oauth2.client.endpoint.OAuth2RefreshTokenGrantRequest;
|
|
import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
|
import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
|
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
|
|
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
|
|
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
|
|
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
|
|
|
|
+import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager;
|
|
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
|
|
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
|
|
import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
|
import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
|
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
|
|
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
|
|
import org.springframework.security.oauth2.core.endpoint.TestOAuth2AccessTokenResponses;
|
|
import org.springframework.security.oauth2.core.endpoint.TestOAuth2AccessTokenResponses;
|
|
import org.springframework.security.oauth2.core.user.OAuth2User;
|
|
import org.springframework.security.oauth2.core.user.OAuth2User;
|
|
|
|
+import org.springframework.web.client.RestOperations;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import org.springframework.web.reactive.function.BodyInserter;
|
|
import org.springframework.web.reactive.function.BodyInserter;
|
|
import org.springframework.web.reactive.function.client.ClientRequest;
|
|
import org.springframework.web.reactive.function.client.ClientRequest;
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
-import reactor.core.publisher.Mono;
|
|
|
|
|
|
|
|
import java.net.URI;
|
|
import java.net.URI;
|
|
import java.time.Duration;
|
|
import java.time.Duration;
|
|
@@ -76,6 +85,7 @@ import java.util.function.Consumer;
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.assertj.core.api.Assertions.assertThatCode;
|
|
import static org.assertj.core.api.Assertions.assertThatCode;
|
|
|
|
+import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
import static org.mockito.Mockito.*;
|
|
import static org.mockito.Mockito.*;
|
|
@@ -95,6 +105,8 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
@Mock
|
|
@Mock
|
|
private OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsTokenResponseClient;
|
|
private OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsTokenResponseClient;
|
|
@Mock
|
|
@Mock
|
|
|
|
+ private OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> refreshTokenTokenResponseClient;
|
|
|
|
+ @Mock
|
|
private WebClient.RequestHeadersSpec<?> spec;
|
|
private WebClient.RequestHeadersSpec<?> spec;
|
|
@Captor
|
|
@Captor
|
|
private ArgumentCaptor<Consumer<Map<String, Object>>> attrs;
|
|
private ArgumentCaptor<Consumer<Map<String, Object>>> attrs;
|
|
@@ -106,14 +118,13 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
*/
|
|
*/
|
|
private Map<String, Object> result = new HashMap<>();
|
|
private Map<String, Object> result = new HashMap<>();
|
|
|
|
|
|
- private ServletOAuth2AuthorizedClientExchangeFilterFunction function = new ServletOAuth2AuthorizedClientExchangeFilterFunction();
|
|
|
|
|
|
+ private ServletOAuth2AuthorizedClientExchangeFilterFunction function;
|
|
|
|
|
|
private MockExchangeFunction exchange = new MockExchangeFunction();
|
|
private MockExchangeFunction exchange = new MockExchangeFunction();
|
|
|
|
|
|
private Authentication authentication;
|
|
private Authentication authentication;
|
|
|
|
|
|
- private ClientRegistration registration = TestClientRegistrations.clientRegistration()
|
|
|
|
- .build();
|
|
|
|
|
|
+ private ClientRegistration registration = TestClientRegistrations.clientRegistration().build();
|
|
|
|
|
|
private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
|
|
private OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
|
|
"token-0",
|
|
"token-0",
|
|
@@ -123,6 +134,16 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
@Before
|
|
@Before
|
|
public void setup() {
|
|
public void setup() {
|
|
this.authentication = new TestingAuthenticationToken("test", "this");
|
|
this.authentication = new TestingAuthenticationToken("test", "this");
|
|
|
|
+ OAuth2AuthorizedClientProvider authorizedClientProvider =
|
|
|
|
+ OAuth2AuthorizedClientProviderBuilder.builder()
|
|
|
|
+ .authorizationCode()
|
|
|
|
+ .refreshToken(configurer -> configurer.accessTokenResponseClient(this.refreshTokenTokenResponseClient))
|
|
|
|
+ .clientCredentials(configurer -> configurer.accessTokenResponseClient(this.clientCredentialsTokenResponseClient))
|
|
|
|
+ .build();
|
|
|
|
+ DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager(
|
|
|
|
+ this.clientRegistrationRepository, this.authorizedClientRepository);
|
|
|
|
+ authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
|
|
|
|
+ this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
|
|
}
|
|
}
|
|
|
|
|
|
@After
|
|
@After
|
|
@@ -131,6 +152,35 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
RequestContextHolder.resetRequestAttributes();
|
|
RequestContextHolder.resetRequestAttributes();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void constructorWhenAuthorizedClientManagerIsNullThenThrowIllegalArgumentException() {
|
|
|
|
+ assertThatThrownBy(() -> new ServletOAuth2AuthorizedClientExchangeFilterFunction(null))
|
|
|
|
+ .isInstanceOf(IllegalArgumentException.class);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void setClientCredentialsTokenResponseClientWhenClientIsNullThenThrowIllegalArgumentException() {
|
|
|
|
+ assertThatThrownBy(() -> this.function.setClientCredentialsTokenResponseClient(null))
|
|
|
|
+ .isInstanceOf(IllegalArgumentException.class)
|
|
|
|
+ .hasMessage("clientCredentialsTokenResponseClient cannot be null");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void setClientCredentialsTokenResponseClientWhenNotDefaultAuthorizedClientManagerThenThrowIllegalStateException() {
|
|
|
|
+ assertThatThrownBy(() -> this.function.setClientCredentialsTokenResponseClient(new DefaultClientCredentialsTokenResponseClient()))
|
|
|
|
+ .isInstanceOf(IllegalStateException.class)
|
|
|
|
+ .hasMessage("The client cannot be set when the constructor used is \"ServletOAuth2AuthorizedClientExchangeFilterFunction(OAuth2AuthorizedClientManager)\". " +
|
|
|
|
+ "Instead, use the constructor \"ServletOAuth2AuthorizedClientExchangeFilterFunction(ClientRegistrationRepository, OAuth2AuthorizedClientRepository)\".");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void setAccessTokenExpiresSkewWhenNotDefaultAuthorizedClientManagerThenThrowIllegalStateException() {
|
|
|
|
+ assertThatThrownBy(() -> this.function.setAccessTokenExpiresSkew(Duration.ofSeconds(30)))
|
|
|
|
+ .isInstanceOf(IllegalStateException.class)
|
|
|
|
+ .hasMessage("The accessTokenExpiresSkew cannot be set when the constructor used is \"ServletOAuth2AuthorizedClientExchangeFilterFunction(OAuth2AuthorizedClientManager)\". " +
|
|
|
|
+ "Instead, use the constructor \"ServletOAuth2AuthorizedClientExchangeFilterFunction(ClientRegistrationRepository, OAuth2AuthorizedClientRepository)\".");
|
|
|
|
+ }
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void defaultRequestRequestResponseWhenNullRequestContextThenRequestAndResponseNull() {
|
|
public void defaultRequestRequestResponseWhenNullRequestContextThenRequestAndResponseNull() {
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
@@ -156,8 +206,6 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void defaultRequestAuthenticationWhenAuthenticationSetThenAuthenticationSet() {
|
|
public void defaultRequestAuthenticationWhenAuthenticationSetThenAuthenticationSet() {
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
SecurityContextHolder.getContext().setAuthentication(this.authentication);
|
|
SecurityContextHolder.getContext().setAuthentication(this.authentication);
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
assertThat(getAuthentication(attrs)).isEqualTo(this.authentication);
|
|
assertThat(getAuthentication(attrs)).isEqualTo(this.authentication);
|
|
@@ -166,8 +214,6 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void defaultRequestOAuth2AuthorizedClientWhenOAuth2AuthorizationClientAndClientIdThenNotOverride() {
|
|
public void defaultRequestOAuth2AuthorizedClientWhenOAuth2AuthorizationClientAndClientIdThenNotOverride() {
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
"principalName", this.accessToken);
|
|
"principalName", this.accessToken);
|
|
oauth2AuthorizedClient(authorizedClient).accept(this.result);
|
|
oauth2AuthorizedClient(authorizedClient).accept(this.result);
|
|
@@ -178,8 +224,6 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void defaultRequestOAuth2AuthorizedClientWhenAuthenticationNullAndClientRegistrationIdNullThenOAuth2AuthorizedClientNull() {
|
|
public void defaultRequestOAuth2AuthorizedClientWhenAuthenticationNullAndClientRegistrationIdNullThenOAuth2AuthorizedClientNull() {
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
assertThat(getOAuth2AuthorizedClient(attrs)).isNull();
|
|
assertThat(getOAuth2AuthorizedClient(attrs)).isNull();
|
|
verifyZeroInteractions(this.authorizedClientRepository);
|
|
verifyZeroInteractions(this.authorizedClientRepository);
|
|
@@ -187,8 +231,6 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void defaultRequestOAuth2AuthorizedClientWhenAuthenticationWrongTypeAndClientRegistrationIdNullThenOAuth2AuthorizedClientNull() {
|
|
public void defaultRequestOAuth2AuthorizedClientWhenAuthenticationWrongTypeAndClientRegistrationIdNullThenOAuth2AuthorizedClientNull() {
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
assertThat(getOAuth2AuthorizedClient(attrs)).isNull();
|
|
assertThat(getOAuth2AuthorizedClient(attrs)).isNull();
|
|
verifyZeroInteractions(this.authorizedClientRepository);
|
|
verifyZeroInteractions(this.authorizedClientRepository);
|
|
@@ -208,8 +250,6 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void defaultRequestOAuth2AuthorizedClientWhenDefaultTrueAndAuthenticationAndClientRegistrationIdNullThenOAuth2AuthorizedClient() {
|
|
public void defaultRequestOAuth2AuthorizedClientWhenDefaultTrueAndAuthenticationAndClientRegistrationIdNullThenOAuth2AuthorizedClient() {
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
this.function.setDefaultOAuth2AuthorizedClient(true);
|
|
this.function.setDefaultOAuth2AuthorizedClient(true);
|
|
OAuth2User user = mock(OAuth2User.class);
|
|
OAuth2User user = mock(OAuth2User.class);
|
|
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
|
|
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
|
|
@@ -217,7 +257,10 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
"principalName", this.accessToken);
|
|
"principalName", this.accessToken);
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).thenReturn(authorizedClient);
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).thenReturn(authorizedClient);
|
|
|
|
+ when(this.clientRegistrationRepository.findByRegistrationId(any())).thenReturn(this.registration);
|
|
authentication(token).accept(this.result);
|
|
authentication(token).accept(this.result);
|
|
|
|
+ httpServletRequest(new MockHttpServletRequest()).accept(this.result);
|
|
|
|
+ httpServletResponse(new MockHttpServletResponse()).accept(this.result);
|
|
|
|
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
|
|
|
|
@@ -227,8 +270,6 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void defaultRequestOAuth2AuthorizedClientWhenDefaultFalseAndAuthenticationAndClientRegistrationIdNullThenOAuth2AuthorizedClient() {
|
|
public void defaultRequestOAuth2AuthorizedClientWhenDefaultFalseAndAuthenticationAndClientRegistrationIdNullThenOAuth2AuthorizedClient() {
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
OAuth2User user = mock(OAuth2User.class);
|
|
OAuth2User user = mock(OAuth2User.class);
|
|
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
|
|
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
|
|
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(user, authorities, "id");
|
|
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(user, authorities, "id");
|
|
@@ -241,16 +282,17 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void defaultRequestOAuth2AuthorizedClientWhenAuthenticationAndClientRegistrationIdThenIdIsExplicit() {
|
|
public void defaultRequestOAuth2AuthorizedClientWhenAuthenticationAndClientRegistrationIdThenIdIsExplicit() {
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
OAuth2User user = mock(OAuth2User.class);
|
|
OAuth2User user = mock(OAuth2User.class);
|
|
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
|
|
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
|
|
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(user, authorities, "id");
|
|
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(user, authorities, "id");
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
"principalName", this.accessToken);
|
|
"principalName", this.accessToken);
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).thenReturn(authorizedClient);
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).thenReturn(authorizedClient);
|
|
|
|
+ when(this.clientRegistrationRepository.findByRegistrationId(any())).thenReturn(this.registration);
|
|
authentication(token).accept(this.result);
|
|
authentication(token).accept(this.result);
|
|
clientRegistrationId("explicit").accept(this.result);
|
|
clientRegistrationId("explicit").accept(this.result);
|
|
|
|
+ httpServletRequest(new MockHttpServletRequest()).accept(this.result);
|
|
|
|
+ httpServletResponse(new MockHttpServletResponse()).accept(this.result);
|
|
|
|
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
|
|
|
|
@@ -260,12 +302,13 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void defaultRequestOAuth2AuthorizedClientWhenAuthenticationNullAndClientRegistrationIdThenOAuth2AuthorizedClient() {
|
|
public void defaultRequestOAuth2AuthorizedClientWhenAuthenticationNullAndClientRegistrationIdThenOAuth2AuthorizedClient() {
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
|
|
+ when(this.clientRegistrationRepository.findByRegistrationId(any())).thenReturn(this.registration);
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
"principalName", this.accessToken);
|
|
"principalName", this.accessToken);
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).thenReturn(authorizedClient);
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).thenReturn(authorizedClient);
|
|
clientRegistrationId("id").accept(this.result);
|
|
clientRegistrationId("id").accept(this.result);
|
|
|
|
+ httpServletRequest(new MockHttpServletRequest()).accept(this.result);
|
|
|
|
+ httpServletResponse(new MockHttpServletResponse()).accept(this.result);
|
|
|
|
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
|
|
|
|
@@ -276,54 +319,53 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
@Test
|
|
@Test
|
|
public void defaultRequestWhenClientCredentialsThenAuthorizedClient() {
|
|
public void defaultRequestWhenClientCredentialsThenAuthorizedClient() {
|
|
this.registration = TestClientRegistrations.clientCredentials().build();
|
|
this.registration = TestClientRegistrations.clientCredentials().build();
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
- this.function.setClientCredentialsTokenResponseClient(this.clientCredentialsTokenResponseClient);
|
|
|
|
when(this.clientRegistrationRepository.findByRegistrationId(any())).thenReturn(this.registration);
|
|
when(this.clientRegistrationRepository.findByRegistrationId(any())).thenReturn(this.registration);
|
|
OAuth2AccessTokenResponse accessTokenResponse = TestOAuth2AccessTokenResponses
|
|
OAuth2AccessTokenResponse accessTokenResponse = TestOAuth2AccessTokenResponses
|
|
.accessTokenResponse().build();
|
|
.accessTokenResponse().build();
|
|
- when(this.clientCredentialsTokenResponseClient.getTokenResponse(any())).thenReturn(
|
|
|
|
- accessTokenResponse);
|
|
|
|
|
|
+ when(this.clientCredentialsTokenResponseClient.getTokenResponse(any())).thenReturn(accessTokenResponse);
|
|
|
|
|
|
clientRegistrationId(this.registration.getRegistrationId()).accept(this.result);
|
|
clientRegistrationId(this.registration.getRegistrationId()).accept(this.result);
|
|
|
|
|
|
|
|
+ MockHttpServletRequest request = new MockHttpServletRequest();
|
|
|
|
+ MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
+ RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, response));
|
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(this.authentication);
|
|
|
|
+
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
OAuth2AuthorizedClient authorizedClient = getOAuth2AuthorizedClient(attrs);
|
|
OAuth2AuthorizedClient authorizedClient = getOAuth2AuthorizedClient(attrs);
|
|
|
|
|
|
assertThat(authorizedClient.getAccessToken()).isEqualTo(accessTokenResponse.getAccessToken());
|
|
assertThat(authorizedClient.getAccessToken()).isEqualTo(accessTokenResponse.getAccessToken());
|
|
assertThat(authorizedClient.getClientRegistration()).isEqualTo(this.registration);
|
|
assertThat(authorizedClient.getClientRegistration()).isEqualTo(this.registration);
|
|
- assertThat(authorizedClient.getPrincipalName()).isEqualTo("anonymousUser");
|
|
|
|
|
|
+ assertThat(authorizedClient.getPrincipalName()).isEqualTo("test");
|
|
assertThat(authorizedClient.getRefreshToken()).isEqualTo(accessTokenResponse.getRefreshToken());
|
|
assertThat(authorizedClient.getRefreshToken()).isEqualTo(accessTokenResponse.getRefreshToken());
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void defaultRequestWhenDefaultClientRegistrationIdThenAuthorizedClient() {
|
|
public void defaultRequestWhenDefaultClientRegistrationIdThenAuthorizedClient() {
|
|
this.registration = TestClientRegistrations.clientCredentials().build();
|
|
this.registration = TestClientRegistrations.clientCredentials().build();
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
this.function.setDefaultClientRegistrationId(this.registration.getRegistrationId());
|
|
this.function.setDefaultClientRegistrationId(this.registration.getRegistrationId());
|
|
- this.function.setClientCredentialsTokenResponseClient(this.clientCredentialsTokenResponseClient);
|
|
|
|
when(this.clientRegistrationRepository.findByRegistrationId(any())).thenReturn(this.registration);
|
|
when(this.clientRegistrationRepository.findByRegistrationId(any())).thenReturn(this.registration);
|
|
OAuth2AccessTokenResponse accessTokenResponse = TestOAuth2AccessTokenResponses
|
|
OAuth2AccessTokenResponse accessTokenResponse = TestOAuth2AccessTokenResponses
|
|
.accessTokenResponse().build();
|
|
.accessTokenResponse().build();
|
|
- when(this.clientCredentialsTokenResponseClient.getTokenResponse(any())).thenReturn(
|
|
|
|
- accessTokenResponse);
|
|
|
|
|
|
+ when(this.clientCredentialsTokenResponseClient.getTokenResponse(any())).thenReturn(accessTokenResponse);
|
|
|
|
+
|
|
|
|
+ MockHttpServletRequest request = new MockHttpServletRequest();
|
|
|
|
+ MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
+ RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, response));
|
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(this.authentication);
|
|
|
|
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
Map<String, Object> attrs = getDefaultRequestAttributes();
|
|
OAuth2AuthorizedClient authorizedClient = getOAuth2AuthorizedClient(attrs);
|
|
OAuth2AuthorizedClient authorizedClient = getOAuth2AuthorizedClient(attrs);
|
|
|
|
|
|
assertThat(authorizedClient.getAccessToken()).isEqualTo(accessTokenResponse.getAccessToken());
|
|
assertThat(authorizedClient.getAccessToken()).isEqualTo(accessTokenResponse.getAccessToken());
|
|
assertThat(authorizedClient.getClientRegistration()).isEqualTo(this.registration);
|
|
assertThat(authorizedClient.getClientRegistration()).isEqualTo(this.registration);
|
|
- assertThat(authorizedClient.getPrincipalName()).isEqualTo("anonymousUser");
|
|
|
|
|
|
+ assertThat(authorizedClient.getPrincipalName()).isEqualTo("test");
|
|
assertThat(authorizedClient.getRefreshToken()).isEqualTo(accessTokenResponse.getRefreshToken());
|
|
assertThat(authorizedClient.getRefreshToken()).isEqualTo(accessTokenResponse.getRefreshToken());
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void defaultRequestWhenClientIdNotFoundThenIllegalArgumentException() {
|
|
public void defaultRequestWhenClientIdNotFoundThenIllegalArgumentException() {
|
|
this.registration = TestClientRegistrations.clientCredentials().build();
|
|
this.registration = TestClientRegistrations.clientCredentials().build();
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
-
|
|
|
|
clientRegistrationId(this.registration.getRegistrationId()).accept(this.result);
|
|
clientRegistrationId(this.registration.getRegistrationId()).accept(this.result);
|
|
|
|
|
|
assertThatCode(() -> getDefaultRequestAttributes())
|
|
assertThatCode(() -> getDefaultRequestAttributes())
|
|
@@ -353,8 +395,11 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
public void filterWhenAuthorizedClientThenAuthorizationHeader() {
|
|
public void filterWhenAuthorizedClientThenAuthorizationHeader() {
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
"principalName", this.accessToken);
|
|
"principalName", this.accessToken);
|
|
|
|
+
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
|
|
+ .attributes(httpServletRequest(new MockHttpServletRequest()))
|
|
|
|
+ .attributes(httpServletResponse(new MockHttpServletResponse()))
|
|
.build();
|
|
.build();
|
|
|
|
|
|
this.function.filter(request, this.exchange).block();
|
|
this.function.filter(request, this.exchange).block();
|
|
@@ -366,9 +411,12 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
public void filterWhenExistingAuthorizationThenSingleAuthorizationHeader() {
|
|
public void filterWhenExistingAuthorizationThenSingleAuthorizationHeader() {
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
"principalName", this.accessToken);
|
|
"principalName", this.accessToken);
|
|
|
|
+
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
.header(HttpHeaders.AUTHORIZATION, "Existing")
|
|
.header(HttpHeaders.AUTHORIZATION, "Existing")
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
|
|
+ .attributes(httpServletRequest(new MockHttpServletRequest()))
|
|
|
|
+ .attributes(httpServletResponse(new MockHttpServletResponse()))
|
|
.build();
|
|
.build();
|
|
|
|
|
|
this.function.filter(request, this.exchange).block();
|
|
this.function.filter(request, this.exchange).block();
|
|
@@ -384,47 +432,43 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
.expiresIn(3600)
|
|
.expiresIn(3600)
|
|
.refreshToken("refresh-1")
|
|
.refreshToken("refresh-1")
|
|
.build();
|
|
.build();
|
|
- when(this.exchange.getResponse().body(any())).thenReturn(Mono.just(response));
|
|
|
|
|
|
+ when(this.refreshTokenTokenResponseClient.getTokenResponse(any())).thenReturn(response);
|
|
|
|
+
|
|
Instant issuedAt = Instant.now().minus(Duration.ofDays(1));
|
|
Instant issuedAt = Instant.now().minus(Duration.ofDays(1));
|
|
Instant accessTokenExpiresAt = issuedAt.plus(Duration.ofHours(1));
|
|
Instant accessTokenExpiresAt = issuedAt.plus(Duration.ofHours(1));
|
|
-
|
|
|
|
this.accessToken = new OAuth2AccessToken(this.accessToken.getTokenType(),
|
|
this.accessToken = new OAuth2AccessToken(this.accessToken.getTokenType(),
|
|
this.accessToken.getTokenValue(),
|
|
this.accessToken.getTokenValue(),
|
|
issuedAt,
|
|
issuedAt,
|
|
accessTokenExpiresAt);
|
|
accessTokenExpiresAt);
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
-
|
|
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", issuedAt);
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", issuedAt);
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
"principalName", this.accessToken, refreshToken);
|
|
"principalName", this.accessToken, refreshToken);
|
|
|
|
+
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
.attributes(authentication(this.authentication))
|
|
.attributes(authentication(this.authentication))
|
|
|
|
+ .attributes(httpServletRequest(new MockHttpServletRequest()))
|
|
|
|
+ .attributes(httpServletResponse(new MockHttpServletResponse()))
|
|
.build();
|
|
.build();
|
|
|
|
|
|
this.function.filter(request, this.exchange).block();
|
|
this.function.filter(request, this.exchange).block();
|
|
|
|
|
|
- verify(this.authorizedClientRepository).saveAuthorizedClient(this.authorizedClientCaptor.capture(), eq(this.authentication), any(), any());
|
|
|
|
|
|
+ verify(this.refreshTokenTokenResponseClient).getTokenResponse(any());
|
|
|
|
+ verify(this.authorizedClientRepository).saveAuthorizedClient(
|
|
|
|
+ this.authorizedClientCaptor.capture(), eq(this.authentication), any(), any());
|
|
|
|
|
|
OAuth2AuthorizedClient newAuthorizedClient = authorizedClientCaptor.getValue();
|
|
OAuth2AuthorizedClient newAuthorizedClient = authorizedClientCaptor.getValue();
|
|
assertThat(newAuthorizedClient.getAccessToken()).isEqualTo(response.getAccessToken());
|
|
assertThat(newAuthorizedClient.getAccessToken()).isEqualTo(response.getAccessToken());
|
|
assertThat(newAuthorizedClient.getRefreshToken()).isEqualTo(response.getRefreshToken());
|
|
assertThat(newAuthorizedClient.getRefreshToken()).isEqualTo(response.getRefreshToken());
|
|
|
|
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
- assertThat(requests).hasSize(2);
|
|
|
|
|
|
+ assertThat(requests).hasSize(1);
|
|
|
|
|
|
ClientRequest request0 = requests.get(0);
|
|
ClientRequest request0 = requests.get(0);
|
|
- assertThat(request0.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ=");
|
|
|
|
- assertThat(request0.url().toASCIIString()).isEqualTo("https://example.com/login/oauth/access_token");
|
|
|
|
- assertThat(request0.method()).isEqualTo(HttpMethod.POST);
|
|
|
|
- assertThat(getBody(request0)).isEqualTo("grant_type=refresh_token&refresh_token=refresh-token");
|
|
|
|
-
|
|
|
|
- ClientRequest request1 = requests.get(1);
|
|
|
|
- assertThat(request1.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Bearer token-1");
|
|
|
|
- assertThat(request1.url().toASCIIString()).isEqualTo("https://example.com");
|
|
|
|
- assertThat(request1.method()).isEqualTo(HttpMethod.GET);
|
|
|
|
- assertThat(getBody(request1)).isEmpty();
|
|
|
|
|
|
+ assertThat(request0.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Bearer token-1");
|
|
|
|
+ assertThat(request0.url().toASCIIString()).isEqualTo("https://example.com");
|
|
|
|
+ assertThat(request0.method()).isEqualTo(HttpMethod.GET);
|
|
|
|
+ assertThat(getBody(request0)).isEmpty();
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
@@ -434,62 +478,67 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
.expiresIn(3600)
|
|
.expiresIn(3600)
|
|
// .refreshToken(xxx) // No refreshToken in response
|
|
// .refreshToken(xxx) // No refreshToken in response
|
|
.build();
|
|
.build();
|
|
- when(this.exchange.getResponse().body(any())).thenReturn(Mono.just(response));
|
|
|
|
|
|
+
|
|
|
|
+ RestOperations refreshTokenClient = mock(RestOperations.class);
|
|
|
|
+ when(refreshTokenClient.exchange(any(RequestEntity.class), eq(OAuth2AccessTokenResponse.class)))
|
|
|
|
+ .thenReturn(new ResponseEntity(response, HttpStatus.OK));
|
|
|
|
+ DefaultRefreshTokenTokenResponseClient refreshTokenTokenResponseClient = new DefaultRefreshTokenTokenResponseClient();
|
|
|
|
+ refreshTokenTokenResponseClient.setRestOperations(refreshTokenClient);
|
|
|
|
+
|
|
|
|
+ RefreshTokenOAuth2AuthorizedClientProvider authorizedClientProvider = new RefreshTokenOAuth2AuthorizedClientProvider();
|
|
|
|
+ authorizedClientProvider.setAccessTokenResponseClient(refreshTokenTokenResponseClient);
|
|
|
|
+ DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager(
|
|
|
|
+ this.clientRegistrationRepository, this.authorizedClientRepository);
|
|
|
|
+ authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
|
|
|
|
+ this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
|
|
|
|
+
|
|
Instant issuedAt = Instant.now().minus(Duration.ofDays(1));
|
|
Instant issuedAt = Instant.now().minus(Duration.ofDays(1));
|
|
Instant accessTokenExpiresAt = issuedAt.plus(Duration.ofHours(1));
|
|
Instant accessTokenExpiresAt = issuedAt.plus(Duration.ofHours(1));
|
|
-
|
|
|
|
this.accessToken = new OAuth2AccessToken(this.accessToken.getTokenType(),
|
|
this.accessToken = new OAuth2AccessToken(this.accessToken.getTokenType(),
|
|
this.accessToken.getTokenValue(),
|
|
this.accessToken.getTokenValue(),
|
|
issuedAt,
|
|
issuedAt,
|
|
accessTokenExpiresAt);
|
|
accessTokenExpiresAt);
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
-
|
|
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", issuedAt);
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", issuedAt);
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
"principalName", this.accessToken, refreshToken);
|
|
"principalName", this.accessToken, refreshToken);
|
|
|
|
+
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
.attributes(authentication(this.authentication))
|
|
.attributes(authentication(this.authentication))
|
|
|
|
+ .attributes(httpServletRequest(new MockHttpServletRequest()))
|
|
|
|
+ .attributes(httpServletResponse(new MockHttpServletResponse()))
|
|
.build();
|
|
.build();
|
|
|
|
|
|
this.function.filter(request, this.exchange).block();
|
|
this.function.filter(request, this.exchange).block();
|
|
|
|
|
|
|
|
+ verify(refreshTokenClient).exchange(any(RequestEntity.class), eq(OAuth2AccessTokenResponse.class));
|
|
verify(this.authorizedClientRepository).saveAuthorizedClient(this.authorizedClientCaptor.capture(), eq(this.authentication), any(), any());
|
|
verify(this.authorizedClientRepository).saveAuthorizedClient(this.authorizedClientCaptor.capture(), eq(this.authentication), any(), any());
|
|
|
|
|
|
OAuth2AuthorizedClient newAuthorizedClient = authorizedClientCaptor.getValue();
|
|
OAuth2AuthorizedClient newAuthorizedClient = authorizedClientCaptor.getValue();
|
|
assertThat(newAuthorizedClient.getAccessToken()).isEqualTo(response.getAccessToken());
|
|
assertThat(newAuthorizedClient.getAccessToken()).isEqualTo(response.getAccessToken());
|
|
- assertThat(newAuthorizedClient.getRefreshToken()).isEqualTo(refreshToken);
|
|
|
|
|
|
+ assertThat(newAuthorizedClient.getRefreshToken().getTokenValue()).isEqualTo(refreshToken.getTokenValue());
|
|
|
|
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
- assertThat(requests).hasSize(2);
|
|
|
|
|
|
+ assertThat(requests).hasSize(1);
|
|
|
|
|
|
ClientRequest request0 = requests.get(0);
|
|
ClientRequest request0 = requests.get(0);
|
|
- assertThat(request0.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ=");
|
|
|
|
- assertThat(request0.url().toASCIIString()).isEqualTo("https://example.com/login/oauth/access_token");
|
|
|
|
- assertThat(request0.method()).isEqualTo(HttpMethod.POST);
|
|
|
|
- assertThat(getBody(request0)).isEqualTo("grant_type=refresh_token&refresh_token=refresh-token");
|
|
|
|
-
|
|
|
|
- ClientRequest request1 = requests.get(1);
|
|
|
|
- assertThat(request1.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Bearer token-1");
|
|
|
|
- assertThat(request1.url().toASCIIString()).isEqualTo("https://example.com");
|
|
|
|
- assertThat(request1.method()).isEqualTo(HttpMethod.GET);
|
|
|
|
- assertThat(getBody(request1)).isEmpty();
|
|
|
|
|
|
+ assertThat(request0.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Bearer token-1");
|
|
|
|
+ assertThat(request0.url().toASCIIString()).isEqualTo("https://example.com");
|
|
|
|
+ assertThat(request0.method()).isEqualTo(HttpMethod.GET);
|
|
|
|
+ assertThat(getBody(request0)).isEmpty();
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void filterWhenClientCredentialsTokenNotExpiredThenUseCurrentToken() {
|
|
public void filterWhenClientCredentialsTokenNotExpiredThenUseCurrentToken() {
|
|
this.registration = TestClientRegistrations.clientCredentials().build();
|
|
this.registration = TestClientRegistrations.clientCredentials().build();
|
|
-
|
|
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
- this.function.setClientCredentialsTokenResponseClient(this.clientCredentialsTokenResponseClient);
|
|
|
|
-
|
|
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
"principalName", this.accessToken, null);
|
|
"principalName", this.accessToken, null);
|
|
|
|
+
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
.attributes(authentication(this.authentication))
|
|
.attributes(authentication(this.authentication))
|
|
|
|
+ .attributes(httpServletRequest(new MockHttpServletRequest()))
|
|
|
|
+ .attributes(httpServletResponse(new MockHttpServletResponse()))
|
|
.build();
|
|
.build();
|
|
|
|
|
|
this.function.filter(request, this.exchange).block();
|
|
this.function.filter(request, this.exchange).block();
|
|
@@ -519,27 +568,26 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
Instant issuedAt = Instant.now().minus(Duration.ofDays(1));
|
|
Instant issuedAt = Instant.now().minus(Duration.ofDays(1));
|
|
Instant accessTokenExpiresAt = issuedAt.plus(Duration.ofHours(1));
|
|
Instant accessTokenExpiresAt = issuedAt.plus(Duration.ofHours(1));
|
|
-
|
|
|
|
this.accessToken = new OAuth2AccessToken(this.accessToken.getTokenType(),
|
|
this.accessToken = new OAuth2AccessToken(this.accessToken.getTokenType(),
|
|
this.accessToken.getTokenValue(),
|
|
this.accessToken.getTokenValue(),
|
|
issuedAt,
|
|
issuedAt,
|
|
accessTokenExpiresAt);
|
|
accessTokenExpiresAt);
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
- this.function.setClientCredentialsTokenResponseClient(this.clientCredentialsTokenResponseClient);
|
|
|
|
|
|
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
"principalName", this.accessToken, null);
|
|
"principalName", this.accessToken, null);
|
|
|
|
+
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
.attributes(authentication(this.authentication))
|
|
.attributes(authentication(this.authentication))
|
|
|
|
+ .attributes(httpServletRequest(new MockHttpServletRequest()))
|
|
|
|
+ .attributes(httpServletResponse(new MockHttpServletResponse()))
|
|
.build();
|
|
.build();
|
|
|
|
|
|
this.function.filter(request, this.exchange).block();
|
|
this.function.filter(request, this.exchange).block();
|
|
|
|
|
|
verify(this.authorizedClientRepository).saveAuthorizedClient(any(), eq(this.authentication), any(), any());
|
|
verify(this.authorizedClientRepository).saveAuthorizedClient(any(), eq(this.authentication), any(), any());
|
|
|
|
|
|
- verify(clientCredentialsTokenResponseClient).getTokenResponse(any());
|
|
|
|
|
|
+ verify(this.clientCredentialsTokenResponseClient).getTokenResponse(any());
|
|
|
|
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
assertThat(requests).hasSize(1);
|
|
assertThat(requests).hasSize(1);
|
|
@@ -558,54 +606,46 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
.expiresIn(3600)
|
|
.expiresIn(3600)
|
|
.refreshToken("refresh-1")
|
|
.refreshToken("refresh-1")
|
|
.build();
|
|
.build();
|
|
- when(this.exchange.getResponse().body(any())).thenReturn(Mono.just(response));
|
|
|
|
|
|
+ when(this.refreshTokenTokenResponseClient.getTokenResponse(any())).thenReturn(response);
|
|
|
|
+
|
|
Instant issuedAt = Instant.now().minus(Duration.ofDays(1));
|
|
Instant issuedAt = Instant.now().minus(Duration.ofDays(1));
|
|
Instant accessTokenExpiresAt = issuedAt.plus(Duration.ofHours(1));
|
|
Instant accessTokenExpiresAt = issuedAt.plus(Duration.ofHours(1));
|
|
-
|
|
|
|
this.accessToken = new OAuth2AccessToken(this.accessToken.getTokenType(),
|
|
this.accessToken = new OAuth2AccessToken(this.accessToken.getTokenType(),
|
|
- this.accessToken.getTokenValue(),
|
|
|
|
- issuedAt,
|
|
|
|
- accessTokenExpiresAt);
|
|
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
-
|
|
|
|
|
|
+ this.accessToken.getTokenValue(), issuedAt, accessTokenExpiresAt);
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", issuedAt);
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", issuedAt);
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
"principalName", this.accessToken, refreshToken);
|
|
"principalName", this.accessToken, refreshToken);
|
|
|
|
+
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
|
|
+ .attributes(httpServletRequest(new MockHttpServletRequest()))
|
|
|
|
+ .attributes(httpServletResponse(new MockHttpServletResponse()))
|
|
.build();
|
|
.build();
|
|
|
|
|
|
- this.function.filter(request, this.exchange)
|
|
|
|
- .block();
|
|
|
|
|
|
+ this.function.filter(request, this.exchange).block();
|
|
|
|
|
|
|
|
+ verify(this.refreshTokenTokenResponseClient).getTokenResponse(any());
|
|
verify(this.authorizedClientRepository).saveAuthorizedClient(any(), any(), any(), any());
|
|
verify(this.authorizedClientRepository).saveAuthorizedClient(any(), any(), any(), any());
|
|
|
|
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
- assertThat(requests).hasSize(2);
|
|
|
|
|
|
+ assertThat(requests).hasSize(1);
|
|
|
|
|
|
ClientRequest request0 = requests.get(0);
|
|
ClientRequest request0 = requests.get(0);
|
|
- assertThat(request0.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ=");
|
|
|
|
- assertThat(request0.url().toASCIIString()).isEqualTo("https://example.com/login/oauth/access_token");
|
|
|
|
- assertThat(request0.method()).isEqualTo(HttpMethod.POST);
|
|
|
|
- assertThat(getBody(request0)).isEqualTo("grant_type=refresh_token&refresh_token=refresh-token");
|
|
|
|
-
|
|
|
|
- ClientRequest request1 = requests.get(1);
|
|
|
|
- assertThat(request1.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Bearer token-1");
|
|
|
|
- assertThat(request1.url().toASCIIString()).isEqualTo("https://example.com");
|
|
|
|
- assertThat(request1.method()).isEqualTo(HttpMethod.GET);
|
|
|
|
- assertThat(getBody(request1)).isEmpty();
|
|
|
|
|
|
+ assertThat(request0.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Bearer token-1");
|
|
|
|
+ assertThat(request0.url().toASCIIString()).isEqualTo("https://example.com");
|
|
|
|
+ assertThat(request0.method()).isEqualTo(HttpMethod.GET);
|
|
|
|
+ assertThat(getBody(request0)).isEmpty();
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void filterWhenRefreshTokenNullThenShouldRefreshFalse() {
|
|
public void filterWhenRefreshTokenNullThenShouldRefreshFalse() {
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
-
|
|
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
"principalName", this.accessToken);
|
|
"principalName", this.accessToken);
|
|
|
|
+
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
|
|
+ .attributes(httpServletRequest(new MockHttpServletRequest()))
|
|
|
|
+ .attributes(httpServletResponse(new MockHttpServletResponse()))
|
|
.build();
|
|
.build();
|
|
|
|
|
|
this.function.filter(request, this.exchange).block();
|
|
this.function.filter(request, this.exchange).block();
|
|
@@ -622,14 +662,14 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void filterWhenNotExpiredThenShouldRefreshFalse() {
|
|
public void filterWhenNotExpiredThenShouldRefreshFalse() {
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository,
|
|
|
|
- this.authorizedClientRepository);
|
|
|
|
-
|
|
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", this.accessToken.getIssuedAt());
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", this.accessToken.getIssuedAt());
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
"principalName", this.accessToken, refreshToken);
|
|
"principalName", this.accessToken, refreshToken);
|
|
|
|
+
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
|
|
+ .attributes(httpServletRequest(new MockHttpServletRequest()))
|
|
|
|
+ .attributes(httpServletResponse(new MockHttpServletResponse()))
|
|
.build();
|
|
.build();
|
|
|
|
|
|
this.function.filter(request, this.exchange).block();
|
|
this.function.filter(request, this.exchange).block();
|
|
@@ -647,8 +687,6 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
// gh-6483
|
|
// gh-6483
|
|
@Test
|
|
@Test
|
|
public void filterWhenChainedThenDefaultsStillAvailable() throws Exception {
|
|
public void filterWhenChainedThenDefaultsStillAvailable() throws Exception {
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(
|
|
|
|
- this.clientRegistrationRepository, this.authorizedClientRepository);
|
|
|
|
this.function.afterPropertiesSet(); // Hooks.onLastOperator() initialized
|
|
this.function.afterPropertiesSet(); // Hooks.onLastOperator() initialized
|
|
this.function.setDefaultOAuth2AuthorizedClient(true);
|
|
this.function.setDefaultOAuth2AuthorizedClient(true);
|
|
|
|
|
|
@@ -664,9 +702,12 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(
|
|
this.registration, "principalName", this.accessToken);
|
|
this.registration, "principalName", this.accessToken);
|
|
|
|
+
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(eq(authentication.getAuthorizedClientRegistrationId()),
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(eq(authentication.getAuthorizedClientRegistrationId()),
|
|
eq(authentication), eq(servletRequest))).thenReturn(authorizedClient);
|
|
eq(authentication), eq(servletRequest))).thenReturn(authorizedClient);
|
|
|
|
|
|
|
|
+ when(this.clientRegistrationRepository.findByRegistrationId(eq(authentication.getAuthorizedClientRegistrationId()))).thenReturn(this.registration);
|
|
|
|
+
|
|
// Default request attributes set
|
|
// Default request attributes set
|
|
final ClientRequest request1 = ClientRequest.create(GET, URI.create("https://example1.com"))
|
|
final ClientRequest request1 = ClientRequest.create(GET, URI.create("https://example1.com"))
|
|
.attributes(attrs -> attrs.putAll(getDefaultRequestAttributes())).build();
|
|
.attributes(attrs -> attrs.putAll(getDefaultRequestAttributes())).build();
|
|
@@ -698,8 +739,6 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void filterWhenRequestAttributesNotSetAndHooksNotInitThenDefaultsNotAvailable() throws Exception {
|
|
public void filterWhenRequestAttributesNotSetAndHooksNotInitThenDefaultsNotAvailable() throws Exception {
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(
|
|
|
|
- this.clientRegistrationRepository, this.authorizedClientRepository);
|
|
|
|
// this.function.afterPropertiesSet(); // Hooks.onLastOperator() NOT initialized
|
|
// this.function.afterPropertiesSet(); // Hooks.onLastOperator() NOT initialized
|
|
this.function.setDefaultOAuth2AuthorizedClient(true);
|
|
this.function.setDefaultOAuth2AuthorizedClient(true);
|
|
|
|
|
|
@@ -729,8 +768,6 @@ public class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void filterWhenRequestAttributesNotSetAndHooksInitHooksResetThenDefaultsNotAvailable() throws Exception {
|
|
public void filterWhenRequestAttributesNotSetAndHooksInitHooksResetThenDefaultsNotAvailable() throws Exception {
|
|
- this.function = new ServletOAuth2AuthorizedClientExchangeFilterFunction(
|
|
|
|
- this.clientRegistrationRepository, this.authorizedClientRepository);
|
|
|
|
this.function.afterPropertiesSet(); // Hooks.onLastOperator() initialized
|
|
this.function.afterPropertiesSet(); // Hooks.onLastOperator() initialized
|
|
this.function.destroy(); // Hooks.onLastOperator() released
|
|
this.function.destroy(); // Hooks.onLastOperator() released
|
|
this.function.setDefaultOAuth2AuthorizedClient(true);
|
|
this.function.setDefaultOAuth2AuthorizedClient(true);
|