-
Notifications
You must be signed in to change notification settings - Fork 6k
WebClient support should get new access token when expired and client_credentials #6127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
warrenbailey
wants to merge
1
commit into
spring-projects:master
from
warrenbailey:gh-5893-new-client-credentials-token
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
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.ServerOAuth2AuthorizedClientRepository; | ||
import org.springframework.security.oauth2.core.OAuth2AccessToken; | ||
import org.springframework.security.oauth2.core.OAuth2RefreshToken; | ||
|
@@ -67,6 +68,7 @@ | |
import static org.assertj.core.api.Assertions.assertThat; | ||
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; | ||
|
@@ -86,6 +88,9 @@ public class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests { | |
@Mock | ||
private ReactiveClientRegistrationRepository clientRegistrationRepository; | ||
|
||
@Mock | ||
private OAuth2AuthorizedClientResolver oAuth2AuthorizedClientResolver; | ||
|
||
@Mock | ||
private ServerWebExchange serverWebExchange; | ||
|
||
|
@@ -144,6 +149,88 @@ public void filterWhenExistingAuthorizationThenSingleAuthorizationHeader() { | |
assertThat(headers.get(HttpHeaders.AUTHORIZATION)).containsOnly("Bearer " + this.accessToken.getTokenValue()); | ||
} | ||
|
||
@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.oAuth2AuthorizedClientResolver); | ||
|
||
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.oAuth2AuthorizedClientResolver.clientCredentials(any(), any(), any())).thenReturn(Mono.just(newAuthorizedClient)); | ||
when(this.oAuth2AuthorizedClientResolver.createDefaultedRequest(any(), any(), any())).thenReturn(Mono.just(r)); | ||
|
||
when(this.authorizedClientRepository.saveAuthorizedClient(any(), any(), any())).thenReturn(Mono.empty()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, otherwise I end up with a null pointer in the authorizedWithClientCredentials method line 273 |
||
|
||
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); | ||
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com")) | ||
.attributes(oauth2AuthorizedClient(authorizedClient)) | ||
.build(); | ||
|
||
|
||
this.function.filter(request, this.exchange) | ||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)) | ||
.block(); | ||
|
||
verify(this.authorizedClientRepository).saveAuthorizedClient(any(), eq(authentication), any()); | ||
verify(this.oAuth2AuthorizedClientResolver).clientCredentials(any(), any(), any()); | ||
verify(this.oAuth2AuthorizedClientResolver).createDefaultedRequest(any(), any(), any()); | ||
|
||
List<ClientRequest> requests = this.exchange.getRequests(); | ||
assertThat(requests).hasSize(1); | ||
ClientRequest request1 = requests.get(0); | ||
assertThat(request1.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Bearer new-token"); | ||
assertThat(request1.url().toASCIIString()).isEqualTo("https://example.com"); | ||
assertThat(request1.method()).isEqualTo(HttpMethod.GET); | ||
assertThat(getBody(request1)).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void filterWhenClientCredentialsTokenNotExpiredThenUseCurrentToken() { | ||
TestingAuthenticationToken authentication = new TestingAuthenticationToken("test", "this"); | ||
ClientRegistration registration = TestClientRegistrations.clientCredentials().build(); | ||
|
||
this.function = new ServerOAuth2AuthorizedClientExchangeFilterFunction(this.authorizedClientRepository, this.oAuth2AuthorizedClientResolver); | ||
|
||
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(registration, | ||
"principalName", this.accessToken, null); | ||
ClientRequest request = ClientRequest.create(GET, URI.create("https://example.com")) | ||
.attributes(oauth2AuthorizedClient(authorizedClient)) | ||
.build(); | ||
|
||
this.function.filter(request, this.exchange) | ||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)) | ||
.block(); | ||
|
||
verify(this.oAuth2AuthorizedClientResolver, never()).clientCredentials(any(), any(), any()); | ||
verify(this.oAuth2AuthorizedClientResolver, never()).createDefaultedRequest(any(), any(), any()); | ||
|
||
List<ClientRequest> requests = this.exchange.getRequests(); | ||
assertThat(requests).hasSize(1); | ||
ClientRequest request1 = requests.get(0); | ||
assertThat(request1.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Bearer token-0"); | ||
assertThat(request1.url().toASCIIString()).isEqualTo("https://example.com"); | ||
assertThat(request1.method()).isEqualTo(HttpMethod.GET); | ||
assertThat(getBody(request1)).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void filterWhenRefreshRequiredThenRefresh() { | ||
when(this.authorizedClientRepository.saveAuthorizedClient(any(), any(), any())).thenReturn(Mono.empty()); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's rename to
authorizedClientResolver
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look's like this one change was missed