-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Fixes to refresh tokens. #15508
Merged
Merged
Fixes to refresh tokens. #15508
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fa909b7
Fix expired token error handling with non reactive application.
mshima 9bfaef2
Don't fail when h2 console fail to start.
mshima ee26843
Ignore failed refresh token on reactive apps.
mshima 33d5589
Add configuration to set oauth2 clockSkew for refresh token.
mshima 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 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 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 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 |
---|---|---|
|
@@ -46,6 +46,7 @@ public class OAuth2ReactiveRefreshTokensWebFilter implements WebFilter { | |
.filter(principal -> principal instanceof OAuth2AuthenticationToken) | ||
.cast(OAuth2AuthenticationToken.class) | ||
.flatMap(authentication -> authorizedClient(exchange, authentication)) | ||
.onErrorResume(e -> 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. It should redirect to the oauth2 login here. |
||
.thenReturn(exchange) | ||
.flatMap(chain::filter); | ||
} | ||
|
This file contains 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 |
---|---|---|
|
@@ -30,6 +30,14 @@ import org.springframework.security.oauth2.client.OAuth2AuthorizeRequest; | |
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager; | ||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; | ||
import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizationRequestResolver; | ||
import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestRedirectFilter; | ||
import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestResolver; | ||
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository; | ||
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; | ||
import org.springframework.security.web.DefaultRedirectStrategy; | ||
import org.springframework.security.web.RedirectStrategy; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
|
@@ -40,16 +48,39 @@ import org.springframework.web.filter.OncePerRequestFilter; | |
public class OAuth2RefreshTokensWebFilter extends OncePerRequestFilter { | ||
|
||
private final OAuth2AuthorizedClientManager clientManager; | ||
private final OAuth2AuthorizedClientRepository authorizedClientRepository; | ||
private final OAuth2AuthorizationRequestResolver authorizationRequestResolver; | ||
private final RedirectStrategy authorizationRedirectStrategy = new DefaultRedirectStrategy(); | ||
|
||
public OAuth2RefreshTokensWebFilter(OAuth2AuthorizedClientManager clientManager) { | ||
public OAuth2RefreshTokensWebFilter( | ||
OAuth2AuthorizedClientManager clientManager, | ||
OAuth2AuthorizedClientRepository authorizedClientRepository, | ||
ClientRegistrationRepository clientRegistrationRepository | ||
) { | ||
this.clientManager = clientManager; | ||
this.authorizedClientRepository = authorizedClientRepository; | ||
this.authorizationRequestResolver = | ||
new DefaultOAuth2AuthorizationRequestResolver( | ||
clientRegistrationRepository, | ||
OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI | ||
); | ||
} | ||
|
||
@Override | ||
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException { | ||
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws IOException, ServletException { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
if ((authentication instanceof OAuth2AuthenticationToken)) { | ||
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 remove the doubled parenthesis |
||
authorizedClient((OAuth2AuthenticationToken) authentication); | ||
try { | ||
OAuth2AuthorizedClient authorizedClient = authorizedClient((OAuth2AuthenticationToken) authentication); | ||
this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, authentication, request, response); | ||
} catch (Exception e) { | ||
OAuth2AuthorizationRequest authorizationRequest = this.authorizationRequestResolver.resolve(request); | ||
if (authorizationRequest != null) { | ||
this.authorizationRedirectStrategy.sendRedirect(request, response, authorizationRequest.getAuthorizationRequestUri()); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
|
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.
This looks strange. Why do we need a password here and where does it come from?
@bdemers Can you have a look at this? We're trying to get refresh tokens working in JHipster.
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.
It's the default for the DefaultOAuth2AuthorizedClientManager
https://github.com/spring-projects/spring-security/blob/006b9b960797d279b31cf8c8d16f1549c5632b2c/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/DefaultOAuth2AuthorizedClientManager.java#L91-L96
I've added to let the user easily change clockSkew, but can be removed.