Skip to content

Commit dd584bf

Browse files
committed
fix: Allow OidcAuthorizedClientRefreshedEventListener refreshing if authentication subclasses OAuth2AuthenticationToken
Signed-off-by: Michel Palourdio <[email protected]>
1 parent 26991bb commit dd584bf

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

config/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/client/OidcUserRefreshedEventListenerConfigurationTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,19 @@ public void authorizeWhenAuthenticationIsNotOAuth2ThenOidcUserNotRefreshed() {
232232
}
233233

234234
@Test
235-
public void authorizeWhenAuthenticationIsCustomThenOidcUserNotRefreshed() {
235+
public void authorizeWhenAuthenticationIsCustomThenOidcUserRefreshed() {
236236
this.spring.register(OAuth2LoginWithOAuth2ClientConfig.class).autowire();
237237

238238
OAuth2AuthorizedClient authorizedClient = createAuthorizedClient();
239239
OAuth2AccessTokenResponse accessTokenResponse = createAccessTokenResponse(OidcScopes.OPENID);
240+
Jwt jwt = createJwt().build();
240241
given(this.authorizedClientRepository.loadAuthorizedClient(anyString(), any(Authentication.class),
241242
any(HttpServletRequest.class)))
242243
.willReturn(authorizedClient);
243244
given(this.refreshTokenAccessTokenResponseClient.getTokenResponse(any(OAuth2RefreshTokenGrantRequest.class)))
244245
.willReturn(accessTokenResponse);
246+
given(this.jwtDecoder.decode(anyString())).willReturn(jwt);
247+
given(this.oidcUserService.loadUser(any(OidcUserRequest.class))).willReturn(createOidcUser());
245248

246249
OidcUser oidcUser = createOidcUser();
247250
OAuth2AuthenticationToken authentication = new CustomOAuth2AuthenticationToken(oidcUser,
@@ -255,7 +258,10 @@ public void authorizeWhenAuthenticationIsCustomThenOidcUserNotRefreshed() {
255258
.build();
256259
OAuth2AuthorizedClient refreshedAuthorizedClient = this.authorizedClientManager.authorize(authorizeRequest);
257260
assertThat(refreshedAuthorizedClient).isNotNull();
258-
verifyNoInteractions(this.securityContextRepository, this.jwtDecoder, this.oidcUserService);
261+
assertThat(refreshedAuthorizedClient).isNotSameAs(authorizedClient);
262+
assertThat(refreshedAuthorizedClient.getClientRegistration()).isEqualTo(GOOGLE_CLIENT_REGISTRATION);
263+
assertThat(refreshedAuthorizedClient.getAccessToken()).isEqualTo(accessTokenResponse.getAccessToken());
264+
assertThat(refreshedAuthorizedClient.getRefreshToken()).isEqualTo(accessTokenResponse.getRefreshToken());
259265
}
260266

261267
@Test

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/oidc/authentication/OidcAuthorizedClientRefreshedEventListener.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,10 @@ public void onApplicationEvent(OAuth2AuthorizedClientRefreshedEvent event) {
106106

107107
// The current authentication must be an OAuth2AuthenticationToken
108108
Authentication authentication = this.securityContextHolderStrategy.getContext().getAuthentication();
109-
if (!(authentication instanceof OAuth2AuthenticationToken authenticationToken)
110-
|| authenticationToken.getClass() != OAuth2AuthenticationToken.class) {
109+
if (!(authentication instanceof OAuth2AuthenticationToken authenticationToken)) {
111110
// This event listener only handles the default authentication result. If the
112-
// application customizes the authentication result, then a custom event
113-
// handler should be provided.
111+
// application customizes the authentication result by not subclassing
112+
// OAuth2AuthenticationToken, then a custom event handler should be provided.
114113
return;
115114
}
116115

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/oidc/authentication/OidcAuthorizedClientRefreshedEventListenerTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,20 @@ public void onApplicationEventWhenAuthenticationIsNotOAuth2ThenOidcUserRefreshed
239239
}
240240

241241
@Test
242-
public void onApplicationEventWhenAuthenticationIsCustomThenOidcUserRefreshedEventNotPublished() {
242+
public void onApplicationEventWhenAuthenticationIsSubclassedThenOidcUserRefreshedEventPublished() {
243243
OAuth2AuthenticationToken authentication = new CustomOAuth2AuthenticationToken(this.oidcUser,
244244
this.oidcUser.getAuthorities(), this.clientRegistration.getRegistrationId());
245245
SecurityContextImpl securityContext = new SecurityContextImpl(authentication);
246246
given(this.securityContextHolderStrategy.getContext()).willReturn(securityContext);
247+
given(this.jwtDecoder.decode(anyString())).willReturn(this.jwt);
248+
given(this.userService.loadUser(any(OidcUserRequest.class))).willReturn(this.oidcUser);
247249

248250
OAuth2AuthorizedClientRefreshedEvent authorizedClientRefreshedEvent = new OAuth2AuthorizedClientRefreshedEvent(
249251
this.accessTokenResponse, this.authorizedClient);
250252
this.eventListener.onApplicationEvent(authorizedClientRefreshedEvent);
251253

252-
verify(this.securityContextHolderStrategy).getContext();
253-
verifyNoMoreInteractions(this.securityContextHolderStrategy);
254-
verifyNoInteractions(this.jwtDecoder, this.userService, this.applicationEventPublisher);
254+
verify(this.applicationEventPublisher).publishEvent(any(OidcUserRefreshedEvent.class));
255+
verifyNoMoreInteractions(this.applicationEventPublisher);
255256
}
256257

257258
@Test

0 commit comments

Comments
 (0)