Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document that "exp" field is required #354

Open
dricair opened this issue Dec 17, 2023 · 2 comments
Open

Document that "exp" field is required #354

dricair opened this issue Dec 17, 2023 · 2 comments

Comments

@dricair
Copy link

dricair commented Dec 17, 2023

I tried to implement tests containing the code present in the doc for encode and decode.

But it triggers an error because "exp" claim is missing. It took me quite some time to understand that we need to explicitly add this field to the Claim structure. I think the doc should be updated to show it. Something like this for encode:

use serde::{Deserialize, Serialize};
use jsonwebtoken::{encode, Algorithm, Header, EncodingKey, get_current_timestamp};

#[derive(Debug, Serialize, Deserialize)]
struct Claims {
   sub: String,
   company: String,
   exp: i64
}

let my_claims = Claims {
    sub: "[email protected]".to_owned(),
    company: "ACME".to_owned(),
    // 5 minutes validity
    exp: get_current_timestamp() + 300
};

// my_claims is a struct that implements Serialize
// This will create a JWT using HS256 as algorithm
let token = encode(&Header::default(), &my_claims, &EncodingKey::from_secret("secret".as_ref())).unwrap();
@Keats
Copy link
Owner

Keats commented Dec 17, 2023

It's required if you don't turn it off in the https://docs.rs/jsonwebtoken/latest/jsonwebtoken/struct.Validation.html struct

@dricair
Copy link
Author

dricair commented Dec 17, 2023

Yes I agree, but it would be a lot easier to show it in the example, as it's only when testing that it tells it is required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants