From e7d856efb32221993abac08a9538ef10e151b201 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Sun, 20 Dec 2015 00:58:46 +0000 Subject: [PATCH] Update docs a bit --- README.md | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7d1a96b..6a18eb0 100644 --- a/README.md +++ b/README.md @@ -18,19 +18,20 @@ In terms of imports: extern crate jsonwebtoken as jwt; extern crate rustc_serialize; -use jwt::{encode, decode, Algorithm}; +use jwt::{encode, decode, Header, Algorithm}; ``` ### Encoding ```rust -let token = encode(&my_claims, "secret", Algorithm::HS256); +let token = encode(&my_claims, "secret".as_ref(), Header::default()).unwrap(); ``` In that example, `my_claims` is an instance of the Claims struct. The struct you are using for your claims should derive `RustcEncodable` and `RustcDecodable`. ### Decoding ```rust -let claims = decode::(&token, "secret", Algorithm::HS256); +let token = decode::(&token, "secret", Algorithm::HS256).unwrap(); +// token is a struct with 2 params: header and claims ``` In addition to the normal base64/json decoding errors, `decode` can return two custom errors: @@ -42,12 +43,19 @@ In addition to the normal base64/json decoding errors, `decode` can return two c Right now, the library only validates the algorithm type used but does not verify claims such as expiration. Feel free to add a `validate` method to your claims struct to handle that. +### Custom headers +All the parameters from the RFC are supported but the default header only has `typ` and `alg` set: all the other fields are optional. +If you want to set the `kid` parameter for example: + +```rust +let mut header = Header::default(); +header.kid = Some("blabla".to_owned()); +let token = encode(&my_claims, "secret".as_ref(), header).unwrap(); +``` + ## Algorithms Right now, only SHA family is supported: SHA256, SHA384 and SHA512. -## Missing -The header is currently not customisable and therefore does not support things like kid right now. - ## Performance On my thinkpad 440s for a 2 claims struct using SHA256: