You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Jwt decoding should support multiple algorithms #6883
We should add a strategy where Jwt decoders can delegate to other Jwt decoders based upon the algorithm that is in the JWT that was passed into it. This would allow supporting multiple algorithms returned by an IdP as discussed in #5465 (comment)
A possible way to do this would be to have a JwtDecoder implementation that calls other JwtDecoders, though note this might require decoding the JWT twice.
changed the title [-]Add JwtDecoder implementation that supports multiple algorithms[/-][+]Jwt decoding should support multiple algorithms[/+]on May 20, 2019
Would it be possible to implement a JwtDecoder that decodes the header of the given token first to determine the signing algorithm used. The signing algorithm would then be used to find a decoder that supports the signing algorithm.
publicclassDelegatingJwtDecoderimplementsJwtDecoder {
privateList<DelegatedJwtDecoder> decoders;
@OverridepublicJwtdecode(Stringtoken) throwsJwtException {
StringdecodedHeader = decodeHeader(token);
StringsigningAlg = getSigningAlgorithm(decodedHeader);
DelegatedJwtDecoderjwtDecoder = findDecoder(signingAlg);
returnjwtDecoder.decode(token);
}
privateStringdecodeHeader(Stringtoken) {
// decode the Base64 encoded header of the token
}
privateStringgetSigningAlgorithm(StringtokenHeader) {
// get the algorithm from the token header
}
privateDelegatedJwtDecoderfindDecoderSupportingAlgorithm(Stringalgorithm) {
returndecoders.stream()
.filter(decoder -> decoder.supportsAlgorithm(algorithm))
.findFirst()
.orElseThrow(() -> newIllegalArgumentException("No decoder found that support the signing algorithm!"));
}
}
The DelegatedJwtDecoder would have an additional boolean supportsAlgorithm(String algorithm) method which would receive the signing algorithm the given token was signed with.
@dnl50 I believe the example implementation you provided is similar to what @rwinch has suggested. However, I don't believe we need to introduce a new interface DelegatedJwtDecoder with the additional operation supportsAlgorithm().
Instead, we would provide a new implementation of JwtDecoder which would be constructed with a Map of alg to JwtDecoder to allow for this. It might not be a Map but would have to provide this type of mapping.
According to spring security reference, the algorithm can be override by providing a JwtDecoder bean, but it doesn't work after I did that because the initialization of NimbusJwtDecoderJwkSupport locates at line 212 of OidcAuthorizationCodeAuthenticationProvider.java
@compfantasy The PR #6495 introduced support in 5.2.0.M2 for a configurable JWS algorithm via OidcIdTokenDecoderFactory.setJwsAlgorithmResolver() or ReactiveOidcIdTokenDecoderFactory.setJwsAlgorithmResolver().
NOTE: NimbusJwtDecoderJwkSupport is deprecated in favour of NimbusJwtDecoder, which is used/configured by OidcIdTokenDecoderFactory.
@jgrandja Does this mean that I just need upgrade current version of spring-security-oauth2-jose-5.1.4.RELEASE to 5.2.0.M2, then configure jwsAlgorithm in client registration? do you have a simple demo project regarding this change?thanks
@jgrandja I already upgraded the version of spring-security-oauth2-client and spring-secuirty-oauth2-core to 5.2.0.M2, I can see there is a class named OidcIdTokenDecoderFactory, but how can i use setJwsAlgorithmResolver() to make JwsAlgorithm working in the spring context? I tried to create a new OidcTokenDecoderFactory bean and set the JwsAlgorithm to RS512, it doesn't work, and I tried to create a new JwtDecoder bean, it doesn't work for me as well, very appreciate if you can help here.
@compfantasy Can you please put together a minimal sample so I can see what you have so far and share via a GitHub repository. This will be the most effective way to help resolve this for you.
@compfantasy Can you please put together a minimal sample so I can see what you have so far and share via a GitHub repository. This will be the most effective way to help resolve this for you.
Do I need a CustomJwtDecoderFactory like
@Bean
public JwtDecoderFactory<ClientRegistration> customJwtDecoderFactory() {
return new CustomJwtDecoderFactory();
}
static class CustomJwtDecoderFactory implements JwtDecoderFactory<ClientRegistration> {
public JwtDecoder createDecoder(ClientRegistration reg) {
//...
return new CustomJwtDecoder();
}
}
then I create different decoders as
JWSKeySelector<SecurityContext> jwsKeySelector =
JWSAlgorithmFamilyJWSKeySelector.fromJWKSetURL(this.jwkSetUrl);
DefaultJWTProcessor<SecurityContext> jwtProcessor =
new DefaultJWTProcessor<>();
jwtProcessor.setJWSKeySelector(jwsKeySelector);
return new NimbusJwtDecoder(jwtProcessor);
the problem is how OAuth2LoginAuthenticationFilter can know which decoder to use given the registration?
Activity
[-]Add JwtDecoder implementation that supports multiple algorithms[/-][+]Jwt decoding should support multiple algorithms[/+]dnl50 commentedon Jun 3, 2019
Would it be possible to implement a
JwtDecoderthat decodes the header of the given token first to determine the signing algorithm used. The signing algorithm would then be used to find a decoder that supports the signing algorithm.The
DelegatedJwtDecoderwould have an additionalboolean supportsAlgorithm(String algorithm)method which would receive the signing algorithm the given token was signed with.jgrandja commentedon Jun 3, 2019
@dnl50 I believe the example implementation you provided is similar to what @rwinch has suggested. However, I don't believe we need to introduce a new interface
DelegatedJwtDecoderwith the additional operationsupportsAlgorithm().Instead, we would provide a new implementation of
JwtDecoderwhich would be constructed with aMapofalgtoJwtDecoderto allow for this. It might not be aMapbut would have to provide this type of mapping.compfantasy commentedon Jun 8, 2019
According to spring security reference, the algorithm can be override by providing a JwtDecoder bean, but it doesn't work after I did that because the initialization of NimbusJwtDecoderJwkSupport locates at line 212 of OidcAuthorizationCodeAuthenticationProvider.java
jgrandja commentedon Jun 10, 2019
@compfantasy The PR #6495 introduced support in 5.2.0.M2 for a configurable JWS algorithm via
OidcIdTokenDecoderFactory.setJwsAlgorithmResolver()orReactiveOidcIdTokenDecoderFactory.setJwsAlgorithmResolver().NOTE:
NimbusJwtDecoderJwkSupportis deprecated in favour ofNimbusJwtDecoder, which is used/configured byOidcIdTokenDecoderFactory.compfantasy commentedon Jun 13, 2019
@jgrandja Does this mean that I just need upgrade current version of spring-security-oauth2-jose-5.1.4.RELEASE to 5.2.0.M2, then configure jwsAlgorithm in client registration? do you have a simple demo project regarding this change?thanks
compfantasy commentedon Jul 18, 2019
@jgrandja I already upgraded the version of spring-security-oauth2-client and spring-secuirty-oauth2-core to 5.2.0.M2, I can see there is a class named OidcIdTokenDecoderFactory, but how can i use setJwsAlgorithmResolver() to make JwsAlgorithm working in the spring context? I tried to create a new OidcTokenDecoderFactory bean and set the JwsAlgorithm to RS512, it doesn't work, and I tried to create a new JwtDecoder bean, it doesn't work for me as well, very appreciate if you can help here.
jgrandja commentedon Jul 19, 2019
@compfantasy Can you please put together a minimal sample so I can see what you have so far and share via a GitHub repository. This will be the most effective way to help resolve this for you.
Multiple JWS Algorithms
Multiple JWS Algorithms
HungUnicorn commentedon Aug 12, 2022
Do I need a CustomJwtDecoderFactory like
then I create different decoders as
the problem is how
OAuth2LoginAuthenticationFiltercan know which decoder to use given the registration?