2015-11-02 18:27:28 -05:00
|
|
|
extern crate jsonwebtoken as jwt;
|
2015-11-02 15:34:11 -05:00
|
|
|
extern crate rustc_serialize;
|
|
|
|
|
2015-11-03 11:00:52 -05:00
|
|
|
use jwt::{encode, decode, Algorithm};
|
|
|
|
use jwt::errors::{Error};
|
2015-11-02 15:34:11 -05:00
|
|
|
|
|
|
|
#[derive(Debug, RustcEncodable, RustcDecodable)]
|
|
|
|
struct Claims {
|
|
|
|
sub: String,
|
|
|
|
company: String
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let my_claims = Claims {
|
|
|
|
sub: "b@b.com".to_owned(),
|
|
|
|
company: "ACME".to_owned()
|
|
|
|
};
|
2015-11-02 16:04:58 -05:00
|
|
|
let key = "secret";
|
2015-11-05 12:10:55 -05:00
|
|
|
let token = match encode(&my_claims, key, Algorithm::HS256) {
|
2015-11-03 11:00:52 -05:00
|
|
|
Ok(t) => t,
|
|
|
|
Err(_) => panic!() // in practice you would return the error
|
|
|
|
};
|
|
|
|
|
2015-11-05 13:09:39 -05:00
|
|
|
let claims = match decode::<Claims>(&token, key, Algorithm::HS256) {
|
2015-11-03 11:00:52 -05:00
|
|
|
Ok(c) => c,
|
|
|
|
Err(err) => match err {
|
|
|
|
Error::InvalidToken => panic!(), // Example on how to handle a specific error
|
|
|
|
_ => panic!()
|
|
|
|
}
|
|
|
|
};
|
2015-11-02 15:34:11 -05:00
|
|
|
}
|