Skip to content
Advertisement

Unable to parse JWK in Java

I implemented a rest authorization server that returns the public-key for a given keyId in the JWK format using the com.nimbusds:nimbus-jose-jwt:9.13 package. The code looks something like this:

JavaScript

This code returns a JWK key in the following format:

JavaScript

On the client side (java), I try to parse the jwk with the following code:

JavaScript

However, the client is unable to parse the key since parse throws an exception (Missing parameter "kty"). I see that JWK.parse requires a kty key in main JWT josn body, while the default serialization of JWK embeds the kty key within requiredParams key. When I try jwk.toString(), I do see the kty key in the main json body.

Why doesn’t serialization/deserialization of the native JWK object work in a straight-forward manner? What would be the best way to fix this without implementing a custom jwt structure or a serializer/deserializer?

Update 1: This code will work if we change the return type from JWK to Map<String, Object> or String and handle deserialization on the client-side. However, it would be better if the package natively does the (de)serialization for us.

Advertisement

Answer

The answer is to use String for (de)serialization for those facing this problem. Why, you ask? According to the RFC, JWK is a string in the JSON format. While nimbusds:nimbus-jose-jwt defines a JWK object, any APIs that return valid JWK (or JWKSet) can assume that it’s a string.

I also raised this issue with the developers of this package, and they recommended using String or Map<String, Object> for (de)serialization.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement