Skip to content
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

DefaultReactiveOAuth2AuthorizedClientManager never calls UnAuthenticatedServerOAuth2AuthorizedClientRepository #7544

Closed
frzme opened this issue Oct 18, 2019 · 12 comments
Assignees
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) status: backported An issue that has been backported to maintenance branches type: regression A regression from a previous release
Milestone

Comments

@frzme
Copy link

frzme commented Oct 18, 2019

Summary

When using UnAuthenticatedServerOAuth2AuthorizedClientRepository together with the DefaultReactiveOAuth2AuthorizedClientManager authorized clients are not correctly loaded/saved.

This seems to relate to #7468 (@jgrandja )

It seems that when using the UnAuthenticatedServerOAuth2AuthorizedClientRepository it is (and can) never be called from DefaultReactiveOAuth2AuthorizedClientManager as it tries to flatMap a Mono<ServerWebExchange (in the method loadAuthorizedClient). In situations there UnAuthenticatedServerOAuth2AuthorizedClientRepository can/should be used the WebExchange well be null/empty the Mono will be empty and therefore the code in flatMap will not execute.
If ServerWebExchange would be present UnAuthenticatedServerOAuth2AuthorizedClientRepository would throw an Exception there therefore needs to be a way for the ClientManager to call the repository without a WebExchange.

Version

Spring Security 5.2.0 via Spring Boot 2.2.0

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 18, 2019
@jgrandja
Copy link
Contributor

@frzme This issue may be related to #7546. Can you confirm?

@jgrandja jgrandja added in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 23, 2019
@jgrandja jgrandja self-assigned this Oct 23, 2019
@frzme
Copy link
Author

frzme commented Oct 23, 2019

It seems related but I believe it to be not the same. If the Mono returned from currentServerWebExchange() inside

is empty the whole map function on

.map(exchange -> {
this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, principal, exchange);
return authorizedClient;
})
will not run

It might make sense to .map(exchange -> Optional::of).defaultIfEmpty(Optional.empty()) and then optionalExchange.get() to statisty the wish of UnAuthenticatedServerOAuth2AuthorizedClientRepository to receive a null exchange.

@frzme
Copy link
Author

frzme commented Oct 24, 2019

For further context: I'm using the OAuth2 support in the WebClient outside the context of a WebFlux application and requests I'm doing are not related to the SecurityContext associated with a web user. This also seems to be the scope/usecase for the UnAuthenticatedServerOAuth2AuthorizedClientRepository

@jgrandja
Copy link
Contributor

@frzme DefaultReactiveOAuth2AuthorizedClientManager is designed to work within a request context ServerWebExchange. The same applies for DefaultOAuth2AuthorizedClientManager, as it should also be used within a HttpServletRequest context.

I see that you need to use it outside of a request context. We do have a new implementation introduced in 5.2.0, AuthorizedClientServiceOAuth2AuthorizedClientManager, that is intended to be used outside of a HttpServletRequest context. However, we don't have an equivalent reactive implementation. It would be fairly trivial to implement though.

Does this help?

@frzme
Copy link
Author

frzme commented Oct 24, 2019

I believe that would help! I have no issues using a different ClientManager, it however was unexpected to me during migration to 5.2.0 that that would be necessary. Will you be providing such a reactive ReactiveAuthorizedClientServiceOAuth2AuthorizedClientManager and if so will it happen in the scope of 5.2.x - having UnAuthenticatedServerOAuth2AuthorizedClientRepository not working anymore in 5.2.x would be an regression?

@jgrandja
Copy link
Contributor

@frzme

having UnAuthenticatedServerOAuth2AuthorizedClientRepository not working anymore in 5.2.x would be an regression

No it wouldn't be. As mentioned, DefaultReactiveOAuth2AuthorizedClientManager is designed to work within a request context ServerWebExchange. So configuring it with UnAuthenticatedServerOAuth2AuthorizedClientRepository would be a misconfiguration since it does not support ServerWebExchange.

I have no issues using a different ClientManager, it however was unexpected to me during migration to 5.2.0 that that would be necessary

With the introduction of ReactiveOAuth2AuthorizedClientManager and OAuth2AuthorizedClientManager in 5.2.0, the intent is to provide implementation(s) of the AuthorizedClientManager to meet different requirements. Even though UnAuthenticatedServerOAuth2AuthorizedClientRepository is meant to work outside of a request context, the caller should also support this as well. It's much more flexible to implement this at the main controller component, which is the AuthorizedClientManager. Also note, that there is no equivalent Servlet implementation of UnAuthenticatedServerOAuth2AuthorizedClientRepository. This is the purpose of AuthorizedClientServiceOAuth2AuthorizedClientManager.

@jgrandja
Copy link
Contributor

@frzme I just logged #7569. Would you be interested in submitting a PR for this?

@jgrandja
Copy link
Contributor

jgrandja commented Nov 1, 2019

@frzme I'm going to close this issue and associated PR since DefaultReactiveOAuth2AuthorizedClientManager is designed to work within a request context and therefore works-as-designed. Keep track of #7569 as this is the ReactiveOAuth2AuthorizedClientManager implementation you will need to use to operate outside of a request context.

@jgrandja jgrandja closed this as completed Nov 1, 2019
@jgrandja jgrandja added the status: invalid An issue that we don't feel is valid label Nov 1, 2019
@frzme
Copy link
Author

frzme commented Nov 8, 2019

Thank you and sorry for being unresponsive. I think it is rather unfortunate that this exact behavior which worked in the previous version of spring security (and there were also examples doing just this) is now now "broken-as-designed". It also fails transparently - for users it looks like it might be working, it's just re-requesting oauth tokens with every request, which makes it rather hard to spot this.

I'm however fine with a natively supported alternate configuration. I see that someone has already provided one in the linked Issue, I'm happy about that and hope it can become part of spring-security 5.2.2.

@jgrandja
Copy link
Contributor

@frzme I'm re-opening and will address this in 5.2.x. My initial analysis was incorrect. Existing applications using UnAuthenticatedServerOAuth2AuthorizedClientRepository and upgrading to 5.2 should still work. I'll get back to this shortly.

@jgrandja jgrandja reopened this Nov 11, 2019
@jgrandja jgrandja added type: bug A general bug and removed status: invalid An issue that we don't feel is valid labels Nov 11, 2019
@jgrandja jgrandja added this to the 5.2.2 milestone Nov 11, 2019
@jgrandja jgrandja added type: regression A regression from a previous release and removed type: bug A general bug labels Nov 28, 2019
@jgrandja jgrandja modified the milestones: 5.2.2, 5.3.0.M1 Nov 28, 2019
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.2.x labels Nov 28, 2019
@jgrandja
Copy link
Contributor

@frzme The fix #7684 has been applied in 5.2.x. This should resolve your issue. Let me know how it goes. Thanks again for reporting this.

@jgrandja
Copy link
Contributor

@frzme FYI, we just merged the new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager and backported to 5.2.x #7717

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) status: backported An issue that has been backported to maintenance branches type: regression A regression from a previous release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants