-
Notifications
You must be signed in to change notification settings - Fork 40.9k
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
Support custom token validators for OAuth2 Resource Server auto-configuration #35874
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ | |
* @author Artsiom Yudovin | ||
* @author HaiTao Zhang | ||
* @author Mushtaq Ahmed | ||
* @author Roman Golovin | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
class OAuth2ResourceServerJwtConfiguration { | ||
|
@@ -72,8 +73,12 @@ static class JwtDecoderConfiguration { | |
|
||
private final OAuth2ResourceServerProperties.Jwt properties; | ||
|
||
JwtDecoderConfiguration(OAuth2ResourceServerProperties properties) { | ||
private final List<OAuth2TokenValidator<Jwt>> customOAuth2TokenValidators; | ||
|
||
JwtDecoderConfiguration(OAuth2ResourceServerProperties properties, | ||
List<OAuth2TokenValidator<Jwt>> customOAuth2TokenValidators) { | ||
this.properties = properties.getJwt(); | ||
this.customOAuth2TokenValidators = customOAuth2TokenValidators; | ||
} | ||
|
||
@Bean | ||
|
@@ -97,14 +102,17 @@ private void jwsAlgorithms(Set<SignatureAlgorithm> signatureAlgorithms) { | |
} | ||
|
||
private OAuth2TokenValidator<Jwt> getValidators(OAuth2TokenValidator<Jwt> defaultValidator) { | ||
List<String> audiences = this.properties.getAudiences(); | ||
if (CollectionUtils.isEmpty(audiences)) { | ||
return defaultValidator; | ||
} | ||
List<OAuth2TokenValidator<Jwt>> validators = new ArrayList<>(); | ||
validators.add(defaultValidator); | ||
validators.add(new JwtClaimValidator<List<String>>(JwtClaimNames.AUD, | ||
(aud) -> aud != null && !Collections.disjoint(aud, audiences))); | ||
validators.addAll(this.customOAuth2TokenValidators); | ||
List<String> audiences = this.properties.getAudiences(); | ||
if (!CollectionUtils.isEmpty(audiences)) { | ||
validators.add(new JwtClaimValidator<List<String>>(JwtClaimNames.AUD, | ||
(aud) -> aud != null && !Collections.disjoint(aud, audiences))); | ||
} | ||
if (validators.size() == 1) { | ||
return validators.get(0); | ||
} | ||
return new DelegatingOAuth2TokenValidator<>(validators); | ||
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. If there are no audiences and no custom validators, we should return the 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. I added a check for number of validators in the list to avoid duplication of the |
||
} | ||
|
||
|
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.
If there are no audiences and no custom validators, we should return the
defaultValidator
rather than creating aDelegatingOAuth2TokenValidator
with a single delegate.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.
I added a check for number of validators in the list to avoid duplication of the
CollectionUtils.isEmpty(audiences)
check. I think this way it will be easier to add new predefined validators like the one for audiences if needed.