Keats / jsonwebtoken

JWT lib in rust
MIT License
1.67k stars 266 forks source link

ES256 decoding doesn't work #338

Closed amab8901 closed 11 months ago

amab8901 commented 11 months ago

How to reproduce error:

#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct ClaimsNR {
    pub sub: String,
    pub exp: usize,
}

#[cfg(test)]
mod tests {
    use tokio;
    use super::ClaimsNR;
    use jsonwebtoken::{DecodingKey, Validation, Algorithm, decode};

    #[tokio::test]
    async fn jwt_decode() {
        let jwt_pub_key: &str = "-----BEGIN PUBLIC KEY-----MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEEVs/o5+uQbTjL3chynL4wXgUg2R9q9UU8I5mEovUf86QZ7kOBIjJwqnzD1omageEHWwHdBO6B+dFabmdT9POxg==-----END PUBLIC KEY-----";
        let jwt = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.tyh-VfuzIxCyGYDlkBA7DfyjrqmSHu6pQ2hoZuFqUSLPNY2N0mpHb3nk5K17HWP_3cYHBw7AhHale5wky6-sVA";

        let _decoded = decode::<ClaimsNR>(
            &jwt,
            &DecodingKey::from_ec_pem(&jwt_pub_key.to_owned().into_bytes()).unwrap(),
            &Validation::new(Algorithm::ES256),
        ).unwrap(); // throws error: Err(Error(InvalidSignature))
    }
} 

I'm trying different values for jwt_pub_key and jwt. But no matter what values I put in there, it just doesn't work.

Attempted uses: I tried the values (pairs of public key and jwt token) coming from https://jwt.io/#debugger-io and from https://8gwifi.org/jwsgen.jsp but nothing seems to work. Do you have any instructions on how to correctly use it? I imagine it probably should work when you insert the "Serialize" value from https://8gwifi.org/jwsgen.jsp into jwt variable and "Public key" value from https://8gwifi.org/jwsgen.jsp into "jwt_pub_key".

Perhaps it's a bug that needs to be solved? I hope this gets solved asap so that my endpoint will work properly. Thanks 😊

amab8901 commented 11 months ago

nvm, I found that it works if I put a payload like this:

{
  "sub": "abc123",
  "exp": 100000000000000000
} 

in this JWT generator: https://8gwifi.org/jwsgen.jsp