-
Notifications
You must be signed in to change notification settings - Fork 6k
The selectJwk method of NimbusJwtEncoder class should not throw Exception when jwks size great than one #16170
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
The selectJwk method of NimbusJwtEncoder class should not throw Exception when jwks size great than one #16170
Comments
Thanks for reaching out, @douxiaofeng99. Note that while you are able to specify a kid or a thumbprint in the I think a good start would be to introduce a setter that allows applications to indicate which JWK to pick. Something like: setJwkSelector(Converter<List<JWK>, JWK> selector); Then applications could do: setJwkSelector(List::getFirst); // or
setJwkSelector(List::getLast); // or
setJwkSelector((jwks) -> jwks.stream().max(Comparator.comparing(JWK::getIssueTime)).orElseThrow()) Given the following from the RFC:
and to stay passive, the default should continue to be an exception. That said, I think it would also be helpful to change the error message to be something like:
How well would this work for you and are you able to submit a PR that adds this? |
Hello, @jzheaux: I understand your point above as adding a selector for the JWKs list to choose which JWK to use after fetching them. The selectJwk method has been updated as follows:
The main change involves moving the null-check earlier in the logic. If a converter is set, it selects and returns the JWK using the converter. If no converter is present, the method continues with the original logic. If that’s the case, I will submit a PR. |
Signed-off-by: douxiaofeng99 <[email protected]>
Signed-off-by: douxiaofeng99 <[email protected]>
Closes spring-projectsgh-16170 Signed-off-by: douxiaofeng99 <[email protected]>
Closes spring-projectsgh-16170 Signed-off-by: douxiaofeng99 <[email protected]>
Make so that it runs only when selection is needed. Require the provided selector be non-null. Add Tests. Issue spring-projectsgh-16170
Make so that it runs only when selection is needed. Require the provided selector be non-null. Add Tests. Issue gh-16170
Superceded by #16570 |
Describe the bug

I implemented a rotating JWKS using Redis, where a new JWK is generated at regular intervals, and the old JWKs are also retained for a certain period. In this scenario, the selectJwk method of NimbusJwtEncoder retrieves multiple JWKs when selecting the JWKs. This happens because the jwkSelector only sets the algorithm but does not provide any kid, leading to the exception being thrown below.
To Reproduce
Steps to reproduce the behavior.
Expected behavior
remove the block:
if (jwks.size() > 1) {
throw new JwtEncodingException(String.format(ENCODING_ERROR_MESSAGE_TEMPLATE,
"Found multiple JWK signing keys for algorithm '" + headers.getAlgorithm().getName() + "'"));
}
because in the last the method, return jwks.get(0); already use first jwk.
Sample
A link to a GitHub repository with a minimal, reproducible sample.
Reports that include a sample will take priority over reports that do not.
At times, we may require a sample, so it is good to try and include a sample up front.
The text was updated successfully, but these errors were encountered: