Keats / jsonwebtoken

JWT lib in rust
MIT License
1.62k stars 252 forks source link

Decode without validation? #330

Closed lcmgh closed 9 months ago

lcmgh commented 9 months ago

Hi!

I want to extract the exp timestamp from the token without validating it in order to build a small lib that requests a new token before the current one expires. Hence I don't need to validate it but read the exp date.

Is that possible?

Keats commented 9 months ago

Decode with a dummy key and use https://docs.rs/jsonwebtoken/latest/jsonwebtoken/struct.Validation.html#method.insecure_disable_signature_validation

lcmgh commented 9 months ago

Thanks @Keats.

let mut no_validation = Validation::default();
no_validation.insecure_disable_signature_validation();

let dummy_decoding_key = DecodingKey::from_rsa_components("", "").unwrap();

let decoded_token =
    decode::<HashMap<String, serde_json::Value>>(token, &dummy_decoding_key, &no_validation)
        .unwrap();
println!("{:?}", decoded_token);