More examples + fix header struct access
This commit is contained in:
parent
3930338105
commit
f7e87663e2
|
@ -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::<Claims>(&token, key.as_ref(), Algorithm::HS256) {
|
||||
let token_data = match decode::<Claims>(&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());
|
||||
}
|
||||
|
|
|
@ -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::<Claims>(&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);
|
||||
}
|
12
src/lib.rs
12
src/lib.rs
|
@ -57,10 +57,10 @@ impl<T> Part for T where T: Encodable + Decodable {
|
|||
pub struct Header {
|
||||
typ: String,
|
||||
alg: Algorithm,
|
||||
jku: Option<String>,
|
||||
kid: Option<String>,
|
||||
x5u: Option<String>,
|
||||
x5t: Option<String>
|
||||
pub jku: Option<String>,
|
||||
pub kid: Option<String>,
|
||||
pub x5u: Option<String>,
|
||||
pub x5t: Option<String>
|
||||
}
|
||||
|
||||
impl Header {
|
||||
|
@ -85,8 +85,8 @@ impl Default for Header {
|
|||
#[derive(Debug)]
|
||||
/// The return type of a successful call to decode(...)
|
||||
pub struct TokenData<T: Part> {
|
||||
header: Header,
|
||||
claims: T
|
||||
pub header: Header,
|
||||
pub claims: T
|
||||
}
|
||||
|
||||
/// Take the payload of a JWT and sign it using the algorithm given.
|
||||
|
|
Loading…
Reference in New Issue