|
@@ -36,15 +36,23 @@ import org.springframework.http.codec.json.Jackson2JsonEncoder;
|
|
|
import org.springframework.http.codec.multipart.MultipartHttpMessageWriter;
|
|
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
import org.springframework.mock.http.client.reactive.MockClientHttpRequest;
|
|
|
+import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
|
|
+import org.springframework.mock.web.server.MockServerWebExchange;
|
|
|
import org.springframework.security.authentication.TestingAuthenticationToken;
|
|
|
import org.springframework.security.core.authority.AuthorityUtils;
|
|
|
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
|
|
|
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
|
|
|
+import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientProvider;
|
|
|
+import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientProviderBuilder;
|
|
|
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
|
|
|
+import org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequest;
|
|
|
+import org.springframework.security.oauth2.client.endpoint.OAuth2RefreshTokenGrantRequest;
|
|
|
+import org.springframework.security.oauth2.client.endpoint.ReactiveOAuth2AccessTokenResponseClient;
|
|
|
+import org.springframework.security.oauth2.client.endpoint.WebClientReactiveClientCredentialsTokenResponseClient;
|
|
|
import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
|
|
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
|
|
|
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
|
|
|
-import org.springframework.security.oauth2.client.web.reactive.function.client.OAuth2AuthorizedClientResolver.Request;
|
|
|
+import org.springframework.security.oauth2.client.web.server.DefaultServerOAuth2AuthorizedClientManager;
|
|
|
import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository;
|
|
|
import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
|
|
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
|
|
@@ -68,12 +76,10 @@ import java.util.Map;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
+import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
|
-import static org.mockito.Mockito.never;
|
|
|
-import static org.mockito.Mockito.verify;
|
|
|
-import static org.mockito.Mockito.verifyZeroInteractions;
|
|
|
-import static org.mockito.Mockito.when;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
import static org.springframework.http.HttpMethod.GET;
|
|
|
import static org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction.clientRegistrationId;
|
|
|
import static org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient;
|
|
@@ -91,10 +97,12 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
private ReactiveClientRegistrationRepository clientRegistrationRepository;
|
|
|
|
|
|
@Mock
|
|
|
- private OAuth2AuthorizedClientResolver authorizedClientResolver;
|
|
|
+ private ReactiveOAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsTokenResponseClient;
|
|
|
|
|
|
@Mock
|
|
|
- private ServerWebExchange serverWebExchange;
|
|
|
+ private ReactiveOAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> refreshTokenTokenResponseClient;
|
|
|
+
|
|
|
+ private ServerWebExchange serverWebExchange = MockServerWebExchange.builder(MockServerHttpRequest.get("/")).build();
|
|
|
|
|
|
@Captor
|
|
|
private ArgumentCaptor<OAuth2AuthorizedClient> authorizedClientCaptor;
|
|
@@ -113,7 +121,45 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Before
|
|
|
public void setup() {
|
|
|
- this.function = new ServerOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository, this.authorizedClientRepository);
|
|
|
+ ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
|
|
|
+ ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
|
|
|
+ .authorizationCode()
|
|
|
+ .refreshToken(configurer -> configurer.accessTokenResponseClient(this.refreshTokenTokenResponseClient))
|
|
|
+ .clientCredentials(configurer -> configurer.accessTokenResponseClient(this.clientCredentialsTokenResponseClient))
|
|
|
+ .build();
|
|
|
+ DefaultServerOAuth2AuthorizedClientManager authorizedClientManager = new DefaultServerOAuth2AuthorizedClientManager(
|
|
|
+ this.clientRegistrationRepository, this.authorizedClientRepository);
|
|
|
+ authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
|
|
|
+ this.function = new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void constructorWhenAuthorizedClientManagerIsNullThenThrowIllegalArgumentException() {
|
|
|
+ assertThatThrownBy(() -> new ServerOAuth2AuthorizedClientExchangeFilterFunction(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 WebClientReactiveClientCredentialsTokenResponseClient()))
|
|
|
+ .isInstanceOf(IllegalStateException.class)
|
|
|
+ .hasMessage("The client cannot be set when the constructor used is \"ServerOAuth2AuthorizedClientExchangeFilterFunction(ServerOAuth2AuthorizedClientManager)\". " +
|
|
|
+ "Instead, use the constructor \"ServerOAuth2AuthorizedClientExchangeFilterFunction(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 \"ServerOAuth2AuthorizedClientExchangeFilterFunction(ServerOAuth2AuthorizedClientManager)\". " +
|
|
|
+ "Instead, use the constructor \"ServerOAuth2AuthorizedClientExchangeFilterFunction(ClientRegistrationRepository, OAuth2AuthorizedClientRepository)\".");
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -134,7 +180,9 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
|
.build();
|
|
|
|
|
|
- this.function.filter(request, this.exchange).block();
|
|
|
+ this.function.filter(request, this.exchange)
|
|
|
+ .subscriberContext(serverWebExchange())
|
|
|
+ .block();
|
|
|
|
|
|
assertThat(this.exchange.getRequest().headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Bearer " + this.accessToken.getTokenValue());
|
|
|
}
|
|
@@ -148,7 +196,9 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
|
.build();
|
|
|
|
|
|
- this.function.filter(request, this.exchange).block();
|
|
|
+ this.function.filter(request, this.exchange)
|
|
|
+ .subscriberContext(serverWebExchange())
|
|
|
+ .block();
|
|
|
|
|
|
HttpHeaders headers = this.exchange.getRequest().headers();
|
|
|
assertThat(headers.get(HttpHeaders.AUTHORIZATION)).containsOnly("Bearer " + this.accessToken.getTokenValue());
|
|
@@ -156,47 +206,35 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
|
public void filterWhenClientCredentialsTokenExpiredThenGetNewToken() {
|
|
|
- TestingAuthenticationToken authentication = new TestingAuthenticationToken("test", "this");
|
|
|
- ClientRegistration registration = TestClientRegistrations.clientCredentials().build();
|
|
|
- String clientRegistrationId = registration.getClientId();
|
|
|
-
|
|
|
- this.function = new ServerOAuth2AuthorizedClientExchangeFilterFunction(this.authorizedClientRepository, this.authorizedClientResolver);
|
|
|
-
|
|
|
- OAuth2AccessToken newAccessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
|
|
|
- "new-token",
|
|
|
- Instant.now(),
|
|
|
- Instant.now().plus(Duration.ofDays(1)));
|
|
|
- OAuth2AuthorizedClient newAuthorizedClient = new OAuth2AuthorizedClient(registration,
|
|
|
- "principalName", newAccessToken, null);
|
|
|
- Request r = new Request(clientRegistrationId, authentication, null);
|
|
|
- when(this.authorizedClientResolver.clientCredentials(any(), any(), any())).thenReturn(Mono.just(newAuthorizedClient));
|
|
|
- when(this.authorizedClientResolver.createDefaultedRequest(any(), any(), any())).thenReturn(Mono.just(r));
|
|
|
-
|
|
|
- when(this.authorizedClientRepository.saveAuthorizedClient(any(), any(), any())).thenReturn(Mono.empty());
|
|
|
+ OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("new-token")
|
|
|
+ .tokenType(OAuth2AccessToken.TokenType.BEARER)
|
|
|
+ .expiresIn(360)
|
|
|
+ .build();
|
|
|
+ when(this.clientCredentialsTokenResponseClient.getTokenResponse(any())).thenReturn(Mono.just(accessTokenResponse));
|
|
|
|
|
|
+ ClientRegistration registration = TestClientRegistrations.clientCredentials().build();
|
|
|
Instant issuedAt = Instant.now().minus(Duration.ofDays(1));
|
|
|
Instant accessTokenExpiresAt = issuedAt.plus(Duration.ofHours(1));
|
|
|
-
|
|
|
OAuth2AccessToken accessToken = new OAuth2AccessToken(this.accessToken.getTokenType(),
|
|
|
this.accessToken.getTokenValue(),
|
|
|
issuedAt,
|
|
|
accessTokenExpiresAt);
|
|
|
-
|
|
|
-
|
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(registration,
|
|
|
"principalName", accessToken, null);
|
|
|
+
|
|
|
+ TestingAuthenticationToken authentication = new TestingAuthenticationToken("test", "this");
|
|
|
+
|
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
|
.build();
|
|
|
|
|
|
-
|
|
|
this.function.filter(request, this.exchange)
|
|
|
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
|
|
+ .subscriberContext(serverWebExchange())
|
|
|
.block();
|
|
|
|
|
|
+ verify(this.clientCredentialsTokenResponseClient).getTokenResponse(any());
|
|
|
verify(this.authorizedClientRepository).saveAuthorizedClient(any(), eq(authentication), any());
|
|
|
- verify(this.authorizedClientResolver).clientCredentials(any(), any(), any());
|
|
|
- verify(this.authorizedClientResolver).createDefaultedRequest(any(), any(), any());
|
|
|
|
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
|
assertThat(requests).hasSize(1);
|
|
@@ -212,8 +250,6 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
TestingAuthenticationToken authentication = new TestingAuthenticationToken("test", "this");
|
|
|
ClientRegistration registration = TestClientRegistrations.clientCredentials().build();
|
|
|
|
|
|
- this.function = new ServerOAuth2AuthorizedClientExchangeFilterFunction(this.authorizedClientRepository, this.authorizedClientResolver);
|
|
|
-
|
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(registration,
|
|
|
"principalName", this.accessToken, null);
|
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
@@ -222,10 +258,10 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
this.function.filter(request, this.exchange)
|
|
|
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
|
|
+ .subscriberContext(serverWebExchange())
|
|
|
.block();
|
|
|
|
|
|
- verify(this.authorizedClientResolver, never()).clientCredentials(any(), any(), any());
|
|
|
- verify(this.authorizedClientResolver, never()).createDefaultedRequest(any(), any(), any());
|
|
|
+ verify(this.clientCredentialsTokenResponseClient, never()).getTokenResponse(any());
|
|
|
|
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
|
assertThat(requests).hasSize(1);
|
|
@@ -238,75 +274,23 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
|
|
|
@Test
|
|
|
public void filterWhenRefreshRequiredThenRefresh() {
|
|
|
- when(this.authorizedClientRepository.saveAuthorizedClient(any(), any(), any())).thenReturn(Mono.empty());
|
|
|
OAuth2AccessTokenResponse response = OAuth2AccessTokenResponse.withToken("token-1")
|
|
|
.tokenType(OAuth2AccessToken.TokenType.BEARER)
|
|
|
.expiresIn(3600)
|
|
|
.refreshToken("refresh-1")
|
|
|
.build();
|
|
|
- when(this.exchange.getResponse().body(any())).thenReturn(Mono.just(response));
|
|
|
- Instant issuedAt = Instant.now().minus(Duration.ofDays(1));
|
|
|
- Instant accessTokenExpiresAt = issuedAt.plus(Duration.ofHours(1));
|
|
|
-
|
|
|
- this.accessToken = new OAuth2AccessToken(this.accessToken.getTokenType(),
|
|
|
- this.accessToken.getTokenValue(),
|
|
|
- issuedAt,
|
|
|
- accessTokenExpiresAt);
|
|
|
-
|
|
|
- OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", issuedAt);
|
|
|
- OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
- "principalName", this.accessToken, refreshToken);
|
|
|
- ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
|
- .attributes(oauth2AuthorizedClient(authorizedClient))
|
|
|
- .build();
|
|
|
-
|
|
|
- TestingAuthenticationToken authentication = new TestingAuthenticationToken("test", "this");
|
|
|
- this.function.filter(request, this.exchange)
|
|
|
- .subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
|
|
- .block();
|
|
|
-
|
|
|
- verify(this.authorizedClientRepository).saveAuthorizedClient(this.authorizedClientCaptor.capture(), eq(authentication), any());
|
|
|
+ when(this.refreshTokenTokenResponseClient.getTokenResponse(any())).thenReturn(Mono.just(response));
|
|
|
|
|
|
- OAuth2AuthorizedClient newAuthorizedClient = authorizedClientCaptor.getValue();
|
|
|
- assertThat(newAuthorizedClient.getAccessToken()).isEqualTo(response.getAccessToken());
|
|
|
- assertThat(newAuthorizedClient.getRefreshToken()).isEqualTo(response.getRefreshToken());
|
|
|
-
|
|
|
- List<ClientRequest> requests = this.exchange.getRequests();
|
|
|
- assertThat(requests).hasSize(2);
|
|
|
-
|
|
|
- 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();
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void filterWhenRefreshRequiredThenRefreshAndResponseDoesNotContainRefreshToken() {
|
|
|
- when(this.authorizedClientRepository.saveAuthorizedClient(any(), any(), any())).thenReturn(Mono.empty());
|
|
|
- OAuth2AccessTokenResponse response = OAuth2AccessTokenResponse.withToken("token-1")
|
|
|
- .tokenType(OAuth2AccessToken.TokenType.BEARER)
|
|
|
- .expiresIn(3600)
|
|
|
-// .refreshToken(xxx) // No refreshToken in response
|
|
|
- .build();
|
|
|
- when(this.exchange.getResponse().body(any())).thenReturn(Mono.just(response));
|
|
|
Instant issuedAt = Instant.now().minus(Duration.ofDays(1));
|
|
|
Instant accessTokenExpiresAt = issuedAt.plus(Duration.ofHours(1));
|
|
|
-
|
|
|
this.accessToken = new OAuth2AccessToken(this.accessToken.getTokenType(),
|
|
|
this.accessToken.getTokenValue(),
|
|
|
issuedAt,
|
|
|
accessTokenExpiresAt);
|
|
|
-
|
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", issuedAt);
|
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken, refreshToken);
|
|
|
+
|
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
|
.build();
|
|
@@ -314,42 +298,37 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
TestingAuthenticationToken authentication = new TestingAuthenticationToken("test", "this");
|
|
|
this.function.filter(request, this.exchange)
|
|
|
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
|
|
+ .subscriberContext(serverWebExchange())
|
|
|
.block();
|
|
|
|
|
|
+ verify(this.refreshTokenTokenResponseClient).getTokenResponse(any());
|
|
|
verify(this.authorizedClientRepository).saveAuthorizedClient(this.authorizedClientCaptor.capture(), eq(authentication), any());
|
|
|
|
|
|
OAuth2AuthorizedClient newAuthorizedClient = authorizedClientCaptor.getValue();
|
|
|
assertThat(newAuthorizedClient.getAccessToken()).isEqualTo(response.getAccessToken());
|
|
|
- assertThat(newAuthorizedClient.getRefreshToken()).isEqualTo(authorizedClient.getRefreshToken());
|
|
|
+ assertThat(newAuthorizedClient.getRefreshToken()).isEqualTo(response.getRefreshToken());
|
|
|
|
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
|
- assertThat(requests).hasSize(2);
|
|
|
+ assertThat(requests).hasSize(1);
|
|
|
|
|
|
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
|
|
|
public void filterWhenRefreshRequiredAndEmptyReactiveSecurityContextThenSaved() {
|
|
|
- when(this.authorizedClientRepository.saveAuthorizedClient(any(), any(), any())).thenReturn(Mono.empty());
|
|
|
OAuth2AccessTokenResponse response = OAuth2AccessTokenResponse.withToken("token-1")
|
|
|
.tokenType(OAuth2AccessToken.TokenType.BEARER)
|
|
|
.expiresIn(3600)
|
|
|
.refreshToken("refresh-1")
|
|
|
.build();
|
|
|
- when(this.exchange.getResponse().body(any())).thenReturn(Mono.just(response));
|
|
|
+ when(this.refreshTokenTokenResponseClient.getTokenResponse(any())).thenReturn(Mono.just(response));
|
|
|
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.getTokenValue(),
|
|
|
issuedAt,
|
|
@@ -363,24 +342,20 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
.build();
|
|
|
|
|
|
this.function.filter(request, this.exchange)
|
|
|
+ .subscriberContext(serverWebExchange())
|
|
|
.block();
|
|
|
|
|
|
+ verify(this.refreshTokenTokenResponseClient).getTokenResponse(any());
|
|
|
verify(this.authorizedClientRepository).saveAuthorizedClient(any(), any(), any());
|
|
|
|
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
|
- assertThat(requests).hasSize(2);
|
|
|
+ assertThat(requests).hasSize(1);
|
|
|
|
|
|
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
|
|
@@ -391,7 +366,9 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
|
.build();
|
|
|
|
|
|
- this.function.filter(request, this.exchange).block();
|
|
|
+ this.function.filter(request, this.exchange)
|
|
|
+ .subscriberContext(serverWebExchange())
|
|
|
+ .block();
|
|
|
|
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
|
assertThat(requests).hasSize(1);
|
|
@@ -412,7 +389,9 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
.attributes(oauth2AuthorizedClient(authorizedClient))
|
|
|
.build();
|
|
|
|
|
|
- this.function.filter(request, this.exchange).block();
|
|
|
+ this.function.filter(request, this.exchange)
|
|
|
+ .subscriberContext(serverWebExchange())
|
|
|
+ .block();
|
|
|
|
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
|
assertThat(requests).hasSize(1);
|
|
@@ -430,12 +409,13 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken, refreshToken);
|
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).thenReturn(Mono.just(authorizedClient));
|
|
|
- when(this.clientRegistrationRepository.findByRegistrationId(any())).thenReturn(Mono.just(this.registration));
|
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
|
.attributes(clientRegistrationId(this.registration.getRegistrationId()))
|
|
|
.build();
|
|
|
|
|
|
- this.function.filter(request, this.exchange).block();
|
|
|
+ this.function.filter(request, this.exchange)
|
|
|
+ .subscriberContext(serverWebExchange())
|
|
|
+ .block();
|
|
|
|
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
|
assertThat(requests).hasSize(1);
|
|
@@ -454,11 +434,12 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken, refreshToken);
|
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).thenReturn(Mono.just(authorizedClient));
|
|
|
- when(this.clientRegistrationRepository.findByRegistrationId(any())).thenReturn(Mono.just(this.registration));
|
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
|
.build();
|
|
|
|
|
|
- this.function.filter(request, this.exchange).block();
|
|
|
+ this.function.filter(request, this.exchange)
|
|
|
+ .subscriberContext(serverWebExchange())
|
|
|
+ .block();
|
|
|
|
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
|
assertThat(requests).hasSize(1);
|
|
@@ -478,7 +459,6 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken, refreshToken);
|
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).thenReturn(Mono.just(authorizedClient));
|
|
|
- when(this.clientRegistrationRepository.findByRegistrationId(any())).thenReturn(Mono.just(this.registration));
|
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
|
.build();
|
|
|
|
|
@@ -488,6 +468,7 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
this.function
|
|
|
.filter(request, this.exchange)
|
|
|
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
|
|
+ .subscriberContext(serverWebExchange())
|
|
|
.block();
|
|
|
|
|
|
List<ClientRequest> requests = this.exchange.getRequests();
|
|
@@ -526,7 +507,6 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
|
|
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration,
|
|
|
"principalName", this.accessToken, refreshToken);
|
|
|
when(this.authorizedClientRepository.loadAuthorizedClient(any(), any(), any())).thenReturn(Mono.just(authorizedClient));
|
|
|
- when(this.clientRegistrationRepository.findByRegistrationId(any())).thenReturn(Mono.just(this.registration));
|
|
|
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com"))
|
|
|
.attributes(clientRegistrationId(this.registration.getRegistrationId()))
|
|
|
.build();
|