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

Introduce Reactive OAuth2AuthorizedClient Manager/Provider #7116

Conversation

jgrandja
Copy link
Contributor

@jgrandja jgrandja commented Jul 17, 2019

This PR addresses the reactive implementation of #6811.

@jgrandja jgrandja requested review from rwinch and jzheaux July 17, 2019 21:29
@jgrandja jgrandja added in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) type: enhancement A general enhancement labels Jul 17, 2019
@jgrandja jgrandja self-assigned this Jul 17, 2019
@jgrandja jgrandja force-pushed the gh-6811-webclient-ext-reuse-reactive branch 2 times, most recently from 3d0fd4d to 1e7e4bd Compare July 25, 2019 20:34
@jgrandja jgrandja changed the title Introduce ReactiveOAuth2AuthorizedClientProvider Introduce Reactive OAuth2AuthorizedClient Manager/Provider Jul 25, 2019
@jgrandja jgrandja added this to the 5.2.0.M4 milestone Jul 25, 2019
@jgrandja
Copy link
Contributor Author

@rwinch @jzheaux I just merged #6845 and rebased this PR off master. As an FYI, all the review feedback from #6845 has also been applied to this PR. I believe this is close to merging. It would be ideal if we can merge this for M4 as well. Feel free to add polish in order to get this merged while I am away. Thank you.

@jgrandja jgrandja force-pushed the gh-6811-webclient-ext-reuse-reactive branch from 1e7e4bd to d5e4325 Compare July 26, 2019 18:50
OAuth2Error oauth2Error = new OAuth2Error(INVALID_TOKEN_RESPONSE_ERROR_CODE,
"An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: " +
"HTTP Status Code " + response.rawStatusCode(), null);
throw new OAuth2AuthorizationException(oauth2Error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible resource leak
from org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec#exchange javadoc

NOTE: You must always use one of the body or entity methods of the response to ensure resources are released. See ClientResponse for more details.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robotmrv I'm not sure I'm following? The ClientResponse.body method is used for a successful response...

response.body(oauth2AccessTokenResponse())

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ClientResponse.body method is used for a successful response...

exactly
it is requirement to consume body in any case
there are several issues related to this problem (e.g. spring-projects/spring-framework#22005 (comment) )
see last paragraph from reference guide https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-client-exchange
So my proposal is just to use in case of error

return clientResponse.bodyToMono(Void.class)
    .then(Mono.error(new OAuth2AuthorizationException(oauth2Error)));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this @robotmrv. I'll update similar to your proposal.

@jzheaux jzheaux modified the milestones: 5.2.0.M4, 5.2.0.RC1 Aug 5, 2019
Copy link
Member

@rwinch rwinch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @jgrandja! I have provided feedback inline

}

private boolean hasTokenExpired(AbstractOAuth2Token token) {
return token.getExpiresAt().isBefore(Instant.now().minus(this.clockSkew));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably allow injecting a Clock vs using Instant.now. This comment should be applied globally throughout this PR. Additionally, we should do the same on the imperative side

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This enhancement is tracked in this ticket #7114

Copy link
Contributor

@jzheaux jzheaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jgrandja looks good, just pending the one comment from @robotmrv

.body(tokenRequestBody(refreshTokenGrantRequest))
.exchange()
.flatMap(response -> {
if (!response.statusCode().is2xxSuccessful()) {
Copy link
Contributor

@robotmrv robotmrv Aug 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to catch IllegalArgumentException for non-standard status code
Not sure about real probability of the problem but
there are several related issues
spring-cloud/spring-cloud-sleuth#1382
spring-projects/spring-framework#23367
spring-projects/spring-framework#23366

Copy link
Member

@rwinch rwinch Aug 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @robotmrv You are right we need to ensure we don't cause a leak by calling statusCode() when the status is unknown. Instead we should do something like:

HttpStatus status = HttpStatus.resolve(response.rawStatusCode());
if (status == null || !status.is2xxSuccessful()) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jgrandja I see that this case was not covered in your final commit.
Just check that You did not miss it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did forget! Thanks for letting me know @robotmrv :) I added this polish f0515a0

@jgrandja jgrandja closed this in 46756d2 Aug 21, 2019
@jgrandja jgrandja deleted the gh-6811-webclient-ext-reuse-reactive branch August 21, 2019 18:35
jgrandja added a commit that referenced this pull request Aug 22, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this pull request Aug 26, 2019
kostya05983 pushed a commit to kostya05983/spring-security that referenced this pull request Aug 26, 2019
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) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants