Skip to content

Allow in-memory authorized client services to be constructed with a map #5994

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

Closed

Conversation

vpavic
Copy link
Contributor

@vpavic vpavic commented Oct 18, 2018

This is the equivalent of #5918 only this time for InMemoryOAuth2AuthorizedClientService and its reactive counterpart.

@vpavic vpavic force-pushed the improve-authorized-client-service branch 2 times, most recently from 3b16c5e to 32557ff Compare November 12, 2018 17:28
@vpavic
Copy link
Contributor Author

vpavic commented Nov 12, 2018

I've updated this PR with a commit that that adds OAuth2AuthorizedClientId. I've put that commit as first, and updated the original commit to leverage the newly introduced identifier.

Please take a quick look before I get any further with this. The thing is that the only consumer of OAuth2AuthorizedClientService (and its reactive counterpart) is AuthenticatedPrincipalOAuth2AuthorizedClientRepository (and its reactive counterpart). I believe the APIs of those should also be updated to use OAuth2AuthorizedClientId in order to have a real benefit from the newly introduced identifier.

@vpavic vpavic force-pushed the improve-authorized-client-service branch from 32557ff to 68800e7 Compare November 12, 2018 17:48
@rwinch
Copy link
Member

rwinch commented Nov 14, 2018

@vpavic While I'm open to considering updating the APIs to use OAuth2AuthorizedClientId, I don't think we should make this a part of this PR as I don't think there is a need for it now. Instead, this should be limited to the Map based implementations. We can then consider updating the OAuth2AuthorizedClientService (and reactive equivalent) separately.

Thoughts?

@vpavic
Copy link
Contributor Author

vpavic commented Nov 14, 2018

I agree it's better to handle that separately. I'm going to update this PR with that in mind, and open a new one to consider API changes once this (i.e. OAuth2AuthorizedClientId) is merged.

@vpavic vpavic force-pushed the improve-authorized-client-service branch from 68800e7 to 143a6d9 Compare November 14, 2018 22:54
@vpavic
Copy link
Contributor Author

vpavic commented Nov 14, 2018

I've updated the PR.

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 updates. I have commented inline.

* @param clientRegistrationRepository the repository of client registrations
*/
public InMemoryOAuth2AuthorizedClientService(ClientRegistrationRepository clientRegistrationRepository) {
public InMemoryOAuth2AuthorizedClientService(
Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients,
Copy link
Member

Choose a reason for hiding this comment

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

Given Map is an optional parameter, I'd prefer it to be exposed via a setter. I understand that means it is mutable object, but this is the typical pattern Spring uses.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @rwinch that we should expose via setter instead - Map should be optional. Also, this object is mutable anyway given saveAuthorizedClient and removeAuthorizedClient.

Copy link
Contributor

Choose a reason for hiding this comment

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

@vpavic Apologies for back-tracking my previous comment but what are your thoughts on removing the setter and adding a 2nd constructor for both ClientRegistrationRepository and the Map?

*/
public static OAuth2AuthorizedClientId create(ClientRegistration clientRegistration,
String principalName) {
return new OAuth2AuthorizedClientId(clientRegistration.getRegistrationId(),
Copy link
Contributor

Choose a reason for hiding this comment

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

We should Assert.notNull on clientRegistration and Assert.hasText on principalName. Applying this update would than make Assert.notNull redundant in OAuth2AuthorizedClientId constructor.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the parameter ClientRegistration clientRegistration should be changed to String clientRegistrationId, which aligns with OAuth2AuthorizedClientService.loadAuthorizedClient(String clientRegistrationId, String principalName).

I'm also curious on why this factory method is needed? Alternatively, we can make the constructor public. What are your throughts?

public int hashCode() {
return Objects.hash(this.clientRegistrationId, this.principalName);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

It might be useful to implement toString as well?

}

@Test
public void equalsWhenDifferentRegistrationIdAndSamePrincipalThenShouldReturnTrue() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This method name should be changed to equalsWhenDifferentRegistrationIdAndSamePrincipalThenShouldReturnFalse

}

@Test
public void equalsWhenSameRegistrationIdAndDifferentPrincipalThenShouldReturnTrue() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This method name should be changed to equalsWhenSameRegistrationIdAndDifferentPrincipalThenShouldReturnFalse

}

@Test
public void hashCodeWhenDifferentRegistrationIdAndSamePrincipalThenShouldReturnTrue() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This method name should be changed to hashCodeWhenDifferentRegistrationIdAndSamePrincipalThenNotEqual

}

@Test
public void hashCodeWhenSameRegistrationIdAndDifferentPrincipalThenShouldReturnTrue() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This method name should be changed to hashCodeWhenSameRegistrationIdAndDifferentPrincipalThenNotEqual


/**
* Factory method for creating new {@link OAuth2AuthorizedClientId} using
* {@link ClientRegistration} and {@link Authentication}.
Copy link
Contributor

Choose a reason for hiding this comment

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

{@link Authentication} should be -> the principal name

@vpavic vpavic force-pushed the improve-authorized-client-service branch from 143a6d9 to 699347d Compare November 19, 2018 20:39
@vpavic vpavic force-pushed the improve-authorized-client-service branch from 699347d to 09d07ae Compare November 19, 2018 20:42
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 7, 2019
@vpavic
Copy link
Contributor Author

vpavic commented Jun 24, 2019

Since RC1 is around the corner, heads-up that reactive bits of #5918 need to get reverted. Or maybe all of it, if this PR doesn't make the cut.

@jgrandja jgrandja self-assigned this Jun 27, 2019
@jgrandja jgrandja added in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 27, 2019
@jgrandja
Copy link
Contributor

Thanks for the heads up @vpavic. I left a couple of comments. Also, would you be interested in submitting a PR that reverts the reactive bits of #5918?

@jgrandja
Copy link
Contributor

@vpavic Assuming we merge this PR and revert the reactive bits in #5918, this will allow a Map to be set by the caller for both InMemoryClientRegistrationRepository and InMemoryOAuth2AuthorizedClientService. However, the reactive counterparts InMemoryReactiveClientRegistrationRepository and InMemoryReactiveOAuth2AuthorizedClientService will not allow a Map to be set by the caller since this could introduce a blocking operation as @rwinch mentioned.

What are your thoughts on this? This will ultimately introduce inconsistent implementations between the two which I'm not sure at this point is the right way to go. Given that you raised this issue to allow for a distributed Map to be set, you will not be able to achieve this for a Reactive implementation/application. Do you have any concerns with this?

@vpavic
Copy link
Contributor Author

vpavic commented Jun 28, 2019

I'm afraid I won't have time to revisit this and the #5918 partial revert as I'm quite occupied by stuff for next Spring Session release so feel free to do what you feel is the best.

Generally I'm interested primarily in Servlet side of the things (and have included reactive bits for completeness sake) so that inconsistency is fine with me. I also don't think inconsistency should be the reason for not providing something that's supporting a perfectly valid (and IMO needed) use case for Servlet based implementation.

Also I'll express a concern that I've already express in other places, which I believe is important and is the motivation for this and other related PRs - all the store-like interfaces in new Spring Security OAuth 2.0 are only supported by in-memory implementations, and that's IMO something that you typically don't want to be running in production.

@jgrandja
Copy link
Contributor

@vpavic No worries if you don't have the time...totally understand. I'll take care of the remaining work.

I'm interested primarily in Servlet side of the things

Ok I don't see any issues on merging the changes for Servlet.

...only supported by in-memory implementations, and that's IMO something that you typically don't want to be running in production

Totally agreed and yes the in-memory implementations are meant for development only. It's up to the user to define @Bean of either ClientRegistrationRepository and/or OAuth2AuthorizedClientService for a production setup. We very well may provide other implementations in a later release but we've had other priorities to focus on as of late.

Thanks again for the reminder on this ticket and #5918...it almost slipped through the cracks.

@jgrandja jgrandja closed this in 9432670 Jul 8, 2019
jgrandja added a commit that referenced this pull request Jul 8, 2019
@jgrandja jgrandja added this to the 5.2.0.RC1 milestone Jul 8, 2019
@jgrandja
Copy link
Contributor

jgrandja commented Jul 8, 2019

@vpavic I just merged this along with a polish commit 23d61d4. I also did a partial revert of #5918 in this commit e554547.
Let me know if you see any issues with the updates.
Thanks.

@vpavic vpavic deleted the improve-authorized-client-service branch July 8, 2019 20:15
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.

4 participants