Skip to content

Accept predicate in constructor for JwtIssuerAuthenticationManagerRes… #10002

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
wants to merge 1 commit into from

Conversation

barrypitman
Copy link

Add a constructor to JwtIssuerAuthenticationManagerResolver to allow it to accept a Predicate to determine whether an issuer should be trusted or not. This allows for cases where the trusted issuers are not necessarily known at application startup. Since JwtIssuerAuthenticationManagerResolver is final and internal classes are private, this is not possible to extend it to support this use case without duplicating the whole class.

…olver

Add a constructor to JwtIssuerAuthenticationManagerResolver to allow it to accept a Predicate<String> to determine whether an issuer should be trusted or not. This allows for cases where the trusted issuers are not necessarily known at application startup. Since JwtIssuerAuthenticationManagerResolver is final and internal classes are private, this is not possible to extend it to support this use case without duplicating the whole class.
@pivotal-cla
Copy link

@barrypitman Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@pivotal-cla
Copy link

@barrypitman Thank you for signing the Contributor License Agreement!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 25, 2021
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.

Thanks, @barrypitman! I've left some feedback inline.

In addition to that feedback, will you please format your commit message?

@@ -86,6 +86,16 @@ public JwtIssuerAuthenticationManagerResolver(Collection<String> trustedIssuers)
new TrustedIssuerJwtAuthenticationManagerResolver(
Copy link
Contributor

Choose a reason for hiding this comment

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

Will you please update the copyright message to now include 2021?

* @param trustedIssuer a predicate to determine whether the issuer should be trusted or not
*/
public JwtIssuerAuthenticationManagerResolver(Predicate<String> trustedIssuer) {
this.authenticationManager = new ResolvingAuthenticationManager(
Copy link
Contributor

Choose a reason for hiding this comment

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

Please check for null constructor parameters. You can look at the other constructors as examples.

* Construct a {@link JwtIssuerAuthenticationManagerResolver} using the provided
* parameters
* @param trustedIssuer a predicate to determine whether the issuer should be trusted or not
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add @since 5.6

public JwtIssuerAuthenticationManagerResolver(Predicate<String> trustedIssuer) {
this.authenticationManager = new ResolvingAuthenticationManager(
new TrustedIssuerJwtAuthenticationManagerResolver(trustedIssuer));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add unit tests in JwtIssuerAuthenticationManagerResolverTests.

public JwtIssuerAuthenticationManagerResolver(Predicate<String> trustedIssuer) {
this.authenticationManager = new ResolvingAuthenticationManager(
new TrustedIssuerJwtAuthenticationManagerResolver(trustedIssuer));
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you able to add this same functionality for JwtIssuerReactiveAuthenticationManagerResolver?

@jzheaux jzheaux 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 Jul 26, 2021
@jzheaux
Copy link
Contributor

jzheaux commented Jul 26, 2021

Hi, @barrypitman! Are you able to apply the requested changes?

@jzheaux
Copy link
Contributor

jzheaux commented Nov 18, 2021

@barrypitman, thinking about this a bit more in conjunction with #10476, I think that the best constructor for these use cases is JwtIssuerAuthenticationManagerResolver(AuthenticationManagerResolver<String>), which already exists.

The reason is that introducing every constructor to allow for the myriad configurations applications want to do will become hard to maintain. In isolation, each seems reasonable and small, but the small changes start to add up.

Instead, I believe we can simplify composing a custom AuthenticationManagerResolver<String>.

For example, an AuthenticationManagerResolver<String> with a custom Predicate would look something like this:

public class MyAuthenticationManagerResolver implements AuthenticationManagerResolver<String> {
  Predicate<String> myPredicate = // ...

  @Cacheable(unless="#result==null")
  public AuthenticationManager resolve(String issuer) {
    if (!myPredicate.test(issuer)) {
      return null;
    }
    return new JwtAuthenticationProvider(JwtDecoders.fromIssuerLocation(issuer))::authenticate;
  }
}

// ...

@Bean 
AuthenticationManagerResolver<String> multitenantResolver() {
  return new JwtIssuerAuthenticationManagerResolver(new MyAuthenticationManagerResolver());
}

which, while already quite simple, could be further simplified by Spring Security exposing a default resolver like so:

public static AuthenticationManager fromIssuerLocation(String issuer) {
  return new JwtAuthenticationProvider(JwtDecoders.fromIssuerLocation(issuer))::authenticate;
}

As such, I'm going to close this PR. Please let me know if I've missed something that makes it more complex than I've described, and you and I can take another look at simplifying your use case.

@jzheaux jzheaux closed this Nov 18, 2021
@jzheaux jzheaux added the status: declined A suggestion or change that we don't feel we should currently apply label Nov 18, 2021
@barrypitman
Copy link
Author

No problem, thanks for the explanation Josh

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: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants