-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Ensure ID Token is updated after refresh token #16589
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2002-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.oauth2.client.event; | ||
|
||
import org.springframework.context.ApplicationEvent; | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; | ||
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; | ||
|
||
/** | ||
* An event that is published when an OAuth2 access token is refreshed. | ||
*/ | ||
public class OAuth2TokenRefreshedEvent extends ApplicationEvent { | ||
|
||
private final OAuth2AuthorizedClient authorizedClient; | ||
|
||
private final OAuth2AccessTokenResponse accessTokenResponse; | ||
|
||
public OAuth2TokenRefreshedEvent(Object source, OAuth2AuthorizedClient authorizedClient, | ||
OAuth2AccessTokenResponse accessTokenResponse) { | ||
super(source); | ||
this.authorizedClient = authorizedClient; | ||
this.accessTokenResponse = accessTokenResponse; | ||
} | ||
|
||
public OAuth2AuthorizedClient getAuthorizedClient() { | ||
return this.authorizedClient; | ||
} | ||
|
||
public OAuth2AccessTokenResponse getAccessTokenResponse() { | ||
return this.accessTokenResponse; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2002-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.oauth2.client.oidc.authentication; | ||
|
||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; | ||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | ||
import org.springframework.security.oauth2.client.event.OAuth2TokenRefreshedEvent; | ||
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; | ||
import org.springframework.security.oauth2.core.oidc.OidcIdToken; | ||
import org.springframework.security.oauth2.core.oidc.StandardClaimNames; | ||
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser; | ||
import org.springframework.security.oauth2.core.oidc.user.OidcUser; | ||
|
||
/** | ||
* An {@link ApplicationListener} that listens for {@link OAuth2TokenRefreshedEvent}s | ||
*/ | ||
public class RefreshOidcIdTokenHandler implements ApplicationListener<OAuth2TokenRefreshedEvent> { | ||
|
||
private final OidcAuthorizationCodeAuthenticationProvider oidcAuthorizationCodeAuthenticationProvider; | ||
|
||
public RefreshOidcIdTokenHandler( | ||
OidcAuthorizationCodeAuthenticationProvider oidcAuthorizationCodeAuthenticationProvider) { | ||
this.oidcAuthorizationCodeAuthenticationProvider = oidcAuthorizationCodeAuthenticationProvider; | ||
} | ||
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. See above comment. We don't want to couple the listener with the |
||
|
||
@Override | ||
public void onApplicationEvent(OAuth2TokenRefreshedEvent event) { | ||
OAuth2AuthorizedClient authorizedClient = event.getAuthorizedClient(); | ||
OAuth2AccessTokenResponse accessTokenResponse = event.getAccessTokenResponse(); | ||
OidcIdToken refreshedOidcToken = this.oidcAuthorizationCodeAuthenticationProvider | ||
.createOidcToken(authorizedClient.getClientRegistration(), accessTokenResponse); | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
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. Can you please adjust this to use a |
||
if (authentication instanceof OAuth2AuthenticationToken oauth2AuthenticationToken) { | ||
if (authentication.getPrincipal() instanceof DefaultOidcUser defaultOidcUser) { | ||
OidcUser oidcUser = new DefaultOidcUser(defaultOidcUser.getAuthorities(), refreshedOidcToken, | ||
defaultOidcUser.getUserInfo(), StandardClaimNames.SUB); | ||
SecurityContextHolder.getContext() | ||
.setAuthentication(new OAuth2AuthenticationToken(oidcUser, oidcUser.getAuthorities(), | ||
oauth2AuthenticationToken.getAuthorizedClientRegistrationId())); | ||
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. Can you please adjust this to create a new empty |
||
} | ||
} | ||
} | ||
|
||
} |
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.
We will want to do something else here instead of reuse this method directly. We don't want to couple the event handler with the existing
AuthenticationProvider
. For now, I think it's perfectly fine to duplicate this code in the listener and see how it looks.