MCPcopy Create free account
hub / github.com/Keats/jsonwebtoken / main

Function main

examples/validation.rs:14–41  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12}
13
14fn main() {
15 let key = b"secret";
16 let my_claims = Claims {
17 aud: "me".to_owned(),
18 sub: "b@b.com".to_owned(),
19 company: "ACME".to_owned(),
20 exp: 10000000000,
21 };
22 let token = match encode(&Header::default(), &my_claims, &EncodingKey::from_secret(key)) {
23 Ok(t) => t,
24 Err(_) => panic!(), // in practice you would return the error
25 };
26
27 let mut validation = Validation::new(Algorithm::HS256);
28 validation.sub = Some("b@b.com".to_string());
29 validation.set_audience(&["me"]);
30 validation.set_required_spec_claims(&["exp", "sub", "aud"]);
31 let token_data = match decode::<Claims>(&token, &DecodingKey::from_secret(key), &validation) {
32 Ok(c) => c,
33 Err(err) => match *err.kind() {
34 ErrorKind::InvalidToken => panic!("Token is invalid"), // Example on how to handle a specific error
35 ErrorKind::InvalidIssuer => panic!("Issuer is invalid"), // Example on how to handle a specific error
36 _ => panic!("Some other errors"),
37 },
38 };
39 println!("{:?}", token_data.claims);
40 println!("{:?}", token_data.header);
41}

Callers

nothing calls this directly

Calls 4

set_audienceMethod · 0.80
encodeFunction · 0.50
kindMethod · 0.45

Tested by

no test coverage detected