Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
public final class RefreshTokenOAuth2AuthorizedClientProvider
implements OAuth2AuthorizedClientProvider, ApplicationEventPublisherAware {

private OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> accessTokenResponseClient = new RestClientRefreshTokenTokenResponseClient();
private @Nullable OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> accessTokenResponseClient;

private @Nullable ApplicationEventPublisher applicationEventPublisher;

Expand Down Expand Up @@ -114,14 +114,21 @@ public final class RefreshTokenOAuth2AuthorizedClientProvider
private OAuth2AccessTokenResponse getTokenResponse(OAuth2AuthorizedClient authorizedClient,
OAuth2RefreshTokenGrantRequest refreshTokenGrantRequest) {
try {
return this.accessTokenResponseClient.getTokenResponse(refreshTokenGrantRequest);
return getAccessTokenResponseClient().getTokenResponse(refreshTokenGrantRequest);
}
catch (OAuth2AuthorizationException ex) {
throw new ClientAuthorizationException(ex.getError(),
authorizedClient.getClientRegistration().getRegistrationId(), ex);
}
}

private OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> getAccessTokenResponseClient() {
if (this.accessTokenResponseClient == null) {
this.accessTokenResponseClient = new RestClientRefreshTokenTokenResponseClient();
}
return this.accessTokenResponseClient;
}

private boolean hasTokenExpired(OAuth2Token token) {
Instant expiresAt = token.getExpiresAt();
return expiresAt != null && this.clock.instant().isAfter(expiresAt.minus(this.clockSkew));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.springframework.security.oauth2.core.TestOAuth2RefreshTokens;
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
import org.springframework.security.oauth2.core.endpoint.TestOAuth2AccessTokenResponses;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
Expand Down Expand Up @@ -160,6 +161,21 @@ public void authorizeWhenAuthorizedAndAccessTokenNotExpiredThenNotReauthorize()
assertThat(this.authorizedClientProvider.authorize(authorizationContext)).isNull();
}

@Test
public void authorizeWhenAuthorizedAndAccessTokenNotExpiredThenDoesNotInitializeDefaultAccessTokenResponseClient() {
RefreshTokenOAuth2AuthorizedClientProvider authorizedClientProvider = new RefreshTokenOAuth2AuthorizedClientProvider();
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.clientRegistration,
this.principal.getName(), TestOAuth2AccessTokens.noScopes(), this.authorizedClient.getRefreshToken());
// @formatter:off
OAuth2AuthorizationContext authorizationContext = OAuth2AuthorizationContext
.withAuthorizedClient(authorizedClient)
.principal(this.principal)
.build();
// @formatter:on
assertThat(authorizedClientProvider.authorize(authorizationContext)).isNull();
assertThat(ReflectionTestUtils.getField(authorizedClientProvider, "accessTokenResponseClient")).isNull();
}

// gh-7511
@Test
public void authorizeWhenAuthorizedAndAccessTokenNotExpiredButClockSkewForcesExpiryThenReauthorize() {
Expand Down