Convert JwkSet
for "jwks_uri" endpoint
#982
-
We are using JJWT for handling signing of our JWTs. We are wanting to expose public key information in a "jwks_uri" as part of our oauth metadata. We see this repo's readme shows the library's support of JWKs are meant for this very purpose:
We are creating a JWK set like following: PublicKey publicKey = getSigningPublicKey();
PublicJwk<PublicKey> publicJwk = Jwks.builder().key(publicKey).build();
JwkSet jwks = Jwks.set().add(publicJwk).build(); However, we are falling short on how to convert these JJWT objects to the required output mentioned in the JWKS endpoint spec. It seems like reinventing the wheel to manually process the JJWT objects to convert to custom objects for the endpoint response; our guess is there is a way to handle this conversion/output, but we haven't yet found how this is accomplished. If it makes a difference, we are using ES256 keys (ECDSA using P-256 and SHA-256). Could someone please advise on how it can be done? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hi @l3ender ! This is a good question, as it may not be readily obvious that All that's necessary is to return the For example, if using Spring Boot, you could have something like: @RestController
public class TestController {
@GetMapping(value = "/jwks", produces = "application/jwk-set+json")
public JwkSet getJwks() {
PublicKey publicKey = getSigningPublicKey();
PublicJwk<PublicKey> publicJwk = Jwks.builder().key(publicKey).build();
JwkSet jwks = Jwks.set().add(publicJwk).build();
return jwks; // <-- Spring will marshall a Map<String, Object> directly to JSON
}
} If not using Spring or something similar, you can use Jackson or your JSON marshaller of choice to do it directly. A quick/easy way to do this is to use JJWT's JacksonSerializer serializer = new JacksonSerializer(myApplicationObjectMapper);
serializer.serialize(jwks, httpResponse.getOutputStream()); These are just two quick/rough examples that hopefully gives you some ideas. Basically, anything you use to render JSON from a Hopefully that helps, but if not, please let us know! |
Beta Was this translation helpful? Give feedback.
Ah, dang, I see the problem now, thank you for linking to #976, that was the clue I needed to remember the issue affecting you in this case.
The problem you're seeing is a result of the following:
The
RedactedSupplier
serialization exception should only surface when encountering private or secret key material (and the ObjectMapper has not been configured with the JJWT module), because that supplier concept helps ensure that private/secret information isn't accidentally leaked in applications. In other words, you want that exception when such secret material is present because it likely means a serialization attempt is happening when you probably don't want it to.But since you're serializ…