JWK JSON public key gerneration.

Issue #79 invalid
unnikrishnan kv created an issue

Hi Brian, Could you please clarify my doubt regarding JWK JSON public key gerneration. Client provided JSON like { "jwk": [ { "alg": "RSA", "mod": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "exp": "AQAB" } ] } How i can create public key from above JOSN. Could you please help me to slove this. Thanks, Unnikrishnan

Comments (3)

  1. Brian Campbell repo owner

    Something like this with the just the JSON object from the array:

            PublicJsonWebKey publicJsonWebKey = PublicJsonWebKey.Factory.newPublicJwk("\"{ \"alg\": \"RSA\", \"mod\": \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\", \"exp\": \"AQAB\" }\"");
            PublicKey publicKey = publicJsonWebKey.getPublicKey();
    

    Or if you can parse that object into a map, there's a PublicJsonWebKey.Factory.newPublicJwk method that takes a map too.

    Or something like this to make that JSON into a proper JWKS first and give that to a new JsonWebKeySet object:

            String json = "{\"jwk\": [ { \"alg\": \"RSA\", \"mod\": \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\", \"exp\": \"AQAB\" }]}";;
            json = jwkJson.replaceFirst("jwks", JsonWebKeySet.JWK_SET_MEMBER_NAME);
            JsonWebKeySet jwks = new JsonWebKeySet(json);
            JsonWebKey next = jwks.getJsonWebKeys().iterator().next();
            Key key = next.getKey();
            PublicKey publicKey = (PublicKey) key;
    
  2. Brian Campbell repo owner

    Actually, that's not right. Per https://tools.ietf.org/html/rfc7518#section-6.3 there should be a "kty":"RSA" rather than "alg":"RSA" and an "n" rather than "mod" and "e" rather than "exp"). So more like this:

            json = json.replaceFirst("jwk", JsonWebKeySet.JWK_SET_MEMBER_NAME);
            json = json.replaceFirst(JsonWebKey.ALGORITHM_PARAMETER, JsonWebKey.KEY_TYPE_PARAMETER);
            json = json.replaceFirst("\"mod\"", "\"" + RsaJsonWebKey.MODULUS_MEMBER_NAME + "\"");
            json = json.replaceFirst("\"exp\"", "\"" + RsaJsonWebKey.EXPONENT_MEMBER_NAME + "\"");
    
            JsonWebKeySet jwks = new JsonWebKeySet(json);
            JsonWebKey next = jwks.getJsonWebKeys().iterator().next();
            Key key = next.getKey();
            PublicKey publicKey = (PublicKey) key;
    
  3. Log in to comment