jsonwebtoken/examples/custom_header.rs

44 lines
1.1 KiB
Rust
Raw Normal View History

extern crate jsonwebtoken as jwt;
#[macro_use]
extern crate serde_derive;
2017-04-11 01:41:44 -04:00
use jwt::{encode, decode, Header, Algorithm, Validation};
use jwt::errors::{ErrorKind};
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
sub: String,
2018-10-28 14:54:35 -04:00
company: String,
exp: usize,
}
fn main() {
let my_claims = Claims {
sub: "b@b.com".to_owned(),
2018-10-28 14:54:35 -04:00
company: "ACME".to_owned(),
exp: 10000000000,
};
let key = "secret";
let mut header = Header::default();
header.kid = Some("signing_key".to_owned());
header.alg = Algorithm::HS512;
2017-04-10 23:54:32 -04:00
let token = match encode(&header, &my_claims, key.as_ref()) {
Ok(t) => t,
Err(_) => panic!() // in practice you would return the error
};
2017-04-13 03:36:32 -04:00
println!("{:?}", token);
2017-10-22 07:20:01 -04:00
let token_data = match decode::<Claims>(&token, key.as_ref(), &Validation::new(Algorithm::HS512)) {
Ok(c) => c,
Err(err) => match *err.kind() {
ErrorKind::InvalidToken => panic!(), // Example on how to handle a specific error
_ => panic!()
}
};
println!("{:?}", token_data.claims);
println!("{:?}", token_data.header);
}