From f7e87663e2270f158e27e96225875a6ddaf629f1 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Sun, 20 Dec 2015 01:16:52 +0000 Subject: [PATCH] More examples + fix header struct access --- examples/claims.rs | 21 ++++++++++++++++++--- examples/custom_header.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/lib.rs | 12 ++++++------ 3 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 examples/custom_header.rs diff --git a/examples/claims.rs b/examples/claims.rs index 25af8d7..a8330fb 100644 --- a/examples/claims.rs +++ b/examples/claims.rs @@ -4,29 +4,44 @@ extern crate rustc_serialize; use jwt::{encode, decode, Header, Algorithm}; use jwt::errors::{Error}; + #[derive(Debug, RustcEncodable, RustcDecodable)] struct Claims { sub: String, company: String } +// Example validation implementation +impl Claims { + fn is_valid(self) -> bool { + if self.company != "ACME".to_owned() { + return false; + } + // expiration etc + + true + } +} + fn main() { let my_claims = Claims { sub: "b@b.com".to_owned(), company: "ACME".to_owned() }; let key = "secret"; - let token = match encode(&my_claims, key, Header::default()) { + let token = match encode(&my_claims, key.as_ref(), Header::default()) { Ok(t) => t, Err(_) => panic!() // in practice you would return the error }; - let claims = match decode::(&token, key.as_ref(), Algorithm::HS256) { + let token_data = match decode::(&token, key.as_ref(), Algorithm::HS256) { Ok(c) => c, Err(err) => match err { Error::InvalidToken => panic!(), // Example on how to handle a specific error _ => panic!() } }; - println!("{:?}", claims); + println!("{:?}", token_data.claims); + println!("{:?}", token_data.header); + println!("{:?}", token_data.claims.is_valid()); } diff --git a/examples/custom_header.rs b/examples/custom_header.rs new file mode 100644 index 0000000..62622d0 --- /dev/null +++ b/examples/custom_header.rs @@ -0,0 +1,38 @@ +extern crate jsonwebtoken as jwt; +extern crate rustc_serialize; + +use jwt::{encode, decode, Header, Algorithm}; +use jwt::errors::{Error}; + + +#[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() + }; + let key = "secret"; + + let mut header = Header::default(); + header.kid = Some("signing_key".to_owned()); + + let token = match encode(&my_claims, key.as_ref(), header) { + Ok(t) => t, + Err(_) => panic!() // in practice you would return the error + }; + + let token_data = match decode::(&token, key.as_ref(), Algorithm::HS256) { + Ok(c) => c, + Err(err) => match err { + Error::InvalidToken => panic!(), // Example on how to handle a specific error + _ => panic!() + } + }; + println!("{:?}", token_data.claims); + println!("{:?}", token_data.header); +} diff --git a/src/lib.rs b/src/lib.rs index 1a15b70..fb23678 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,10 +57,10 @@ impl Part for T where T: Encodable + Decodable { pub struct Header { typ: String, alg: Algorithm, - jku: Option, - kid: Option, - x5u: Option, - x5t: Option + pub jku: Option, + pub kid: Option, + pub x5u: Option, + pub x5t: Option } impl Header { @@ -85,8 +85,8 @@ impl Default for Header { #[derive(Debug)] /// The return type of a successful call to decode(...) pub struct TokenData { - header: Header, - claims: T + pub header: Header, + pub claims: T } /// Take the payload of a JWT and sign it using the algorithm given.