Keats / jsonwebtoken

JWT lib in rust
MIT License
1.64k stars 260 forks source link

How to get JWK from library? #272

Closed rchatsiri closed 1 year ago

rchatsiri commented 1 year ago

Hello contributor,

I see on example declares JWK list set by constant value( string value fixed) example such as

fn get_valid_jwk_rsa() -> jwk::JwkSet {
        return serde_json::from_str(&r#"{
            "keys": [
              {
                "kty": "RSA",
                "n": "6S7asUuzq5Q_3U9rbs-PkDVIdjgmtgWreG5qWPx-value",
                "e": "AQAB",
                "kid": "test-rsa",
                "alg":"RS256"
              }
            ]
          }"#).unwrap();
    }

The example such python using the jwcrypto library return the key information e and n value for decode token with 'n' and 'e' values.

   from jwcrypto import jwk
   pem_file = open("rsa256_cert.pub' , "rb)
   key = jwk.JWK.from_pem(pem_file.read())
   public_key = key.export_public()
   print("\n key: "+ public_key)

The value on public_key variable return information about key information such 'kty', 'n' , 'e', 'kid' and 'all'. How we can get JWK from PEM format from jsonwebtoken library?

Keats commented 1 year ago

I think https://github.com/Keats/jsonwebtoken/pull/267 will do what you want?

rchatsiri commented 1 year ago

The question may be unclear. In finally step we must add the json of jwk variable into DecodingKey::from_jwk(&jwk).unwrap(). How we create the json data such modules data by using the library? An example like below.

let jwk =  Jwk::from_pem(public_key.as_ref()).unwrap();
let res = decode::<Claims>(
         &encrypted,
         &DecodingKey::from_jwk(&jwk).unwrap(),
         &Validation::new(Algorithm::EdDSA),
     );
assert!(res.is_ok());
Keats commented 1 year ago

You can't create JWK with this library, only consume them.