Skip to content

Commit 92533fb

Browse files
committed
fix: Allow OidcAuthorizedClientRefreshedEventListener refreshing if authentication subclasses OAuth2AuthenticationToken
Signed-off-by: Michel Palourdio <mpalourdio@gmail.com> fix: Allow OidcAuthorizedClientRefreshedEventListener refreshing if authentication subclasses OAuth2AuthenticationToken
1 parent 26991bb commit 92533fb

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

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)