|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import org.junit.Before;
|
20 | 20 | import org.junit.Test;
|
21 | 21 | import org.junit.runner.RunWith;
|
| 22 | +import org.mockito.ArgumentCaptor; |
| 23 | +import org.mockito.Captor; |
22 | 24 | import org.mockito.Mock;
|
23 | 25 | import org.mockito.junit.MockitoJUnitRunner;
|
24 | 26 | import org.springframework.core.codec.ByteBufferEncoder;
|
@@ -94,6 +96,9 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests {
|
94 | 96 | @Mock
|
95 | 97 | private ServerWebExchange serverWebExchange;
|
96 | 98 |
|
| 99 | + @Captor |
| 100 | + private ArgumentCaptor<OAuth2AuthorizedClient> authorizedClientCaptor; |
| 101 | + |
97 | 102 | private ServerOAuth2AuthorizedClientExchangeFilterFunction function;
|
98 | 103 |
|
99 | 104 | private MockExchangeFunction exchange = new MockExchangeFunction();
|
@@ -260,7 +265,62 @@ public void filterWhenRefreshRequiredThenRefresh() {
|
260 | 265 | .subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
261 | 266 | .block();
|
262 | 267 |
|
263 |
| - verify(this.authorizedClientRepository).saveAuthorizedClient(any(), eq(authentication), any()); |
| 268 | + verify(this.authorizedClientRepository).saveAuthorizedClient(this.authorizedClientCaptor.capture(), eq(authentication), any()); |
| 269 | + |
| 270 | + OAuth2AuthorizedClient newAuthorizedClient = authorizedClientCaptor.getValue(); |
| 271 | + assertThat(newAuthorizedClient.getAccessToken()).isEqualTo(response.getAccessToken()); |
| 272 | + assertThat(newAuthorizedClient.getRefreshToken()).isEqualTo(response.getRefreshToken()); |
| 273 | + |
| 274 | + List<ClientRequest> requests = this.exchange.getRequests(); |
| 275 | + assertThat(requests).hasSize(2); |
| 276 | + |
| 277 | + ClientRequest request0 = requests.get(0); |
| 278 | + assertThat(request0.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ="); |
| 279 | + assertThat(request0.url().toASCIIString()).isEqualTo("https://example.com/login/oauth/access_token"); |
| 280 | + assertThat(request0.method()).isEqualTo(HttpMethod.POST); |
| 281 | + assertThat(getBody(request0)).isEqualTo("grant_type=refresh_token&refresh_token=refresh-token"); |
| 282 | + |
| 283 | + ClientRequest request1 = requests.get(1); |
| 284 | + assertThat(request1.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Bearer token-1"); |
| 285 | + assertThat(request1.url().toASCIIString()).isEqualTo("https://example.com"); |
| 286 | + assertThat(request1.method()).isEqualTo(HttpMethod.GET); |
| 287 | + assertThat(getBody(request1)).isEmpty(); |
| 288 | + } |
| 289 | + |
| 290 | + @Test |
| 291 | + public void filterWhenRefreshRequiredThenRefreshAndResponseDoesNotContainRefreshToken() { |
| 292 | + when(this.authorizedClientRepository.saveAuthorizedClient(any(), any(), any())).thenReturn(Mono.empty()); |
| 293 | + OAuth2AccessTokenResponse response = OAuth2AccessTokenResponse.withToken("token-1") |
| 294 | + .tokenType(OAuth2AccessToken.TokenType.BEARER) |
| 295 | + .expiresIn(3600) |
| 296 | +// .refreshToken(xxx) // No refreshToken in response |
| 297 | + .build(); |
| 298 | + when(this.exchange.getResponse().body(any())).thenReturn(Mono.just(response)); |
| 299 | + Instant issuedAt = Instant.now().minus(Duration.ofDays(1)); |
| 300 | + Instant accessTokenExpiresAt = issuedAt.plus(Duration.ofHours(1)); |
| 301 | + |
| 302 | + this.accessToken = new OAuth2AccessToken(this.accessToken.getTokenType(), |
| 303 | + this.accessToken.getTokenValue(), |
| 304 | + issuedAt, |
| 305 | + accessTokenExpiresAt); |
| 306 | + |
| 307 | + OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", issuedAt); |
| 308 | + OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration, |
| 309 | + "principalName", this.accessToken, refreshToken); |
| 310 | + ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com")) |
| 311 | + .attributes(oauth2AuthorizedClient(authorizedClient)) |
| 312 | + .build(); |
| 313 | + |
| 314 | + TestingAuthenticationToken authentication = new TestingAuthenticationToken("test", "this"); |
| 315 | + this.function.filter(request, this.exchange) |
| 316 | + .subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)) |
| 317 | + .block(); |
| 318 | + |
| 319 | + verify(this.authorizedClientRepository).saveAuthorizedClient(this.authorizedClientCaptor.capture(), eq(authentication), any()); |
| 320 | + |
| 321 | + OAuth2AuthorizedClient newAuthorizedClient = authorizedClientCaptor.getValue(); |
| 322 | + assertThat(newAuthorizedClient.getAccessToken()).isEqualTo(response.getAccessToken()); |
| 323 | + assertThat(newAuthorizedClient.getRefreshToken()).isEqualTo(authorizedClient.getRefreshToken()); |
264 | 324 |
|
265 | 325 | List<ClientRequest> requests = this.exchange.getRequests();
|
266 | 326 | assertThat(requests).hasSize(2);
|
|
0 commit comments