Update docs a bit

This commit is contained in:
Vincent Prouillet 2015-12-20 00:58:46 +00:00
parent 275c95281f
commit e7d856efb3
1 changed files with 14 additions and 6 deletions

View File

@ -18,19 +18,20 @@ In terms of imports:
extern crate jsonwebtoken as jwt; extern crate jsonwebtoken as jwt;
extern crate rustc_serialize; extern crate rustc_serialize;
use jwt::{encode, decode, Algorithm}; use jwt::{encode, decode, Header, Algorithm};
``` ```
### Encoding ### Encoding
```rust ```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. 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`. The struct you are using for your claims should derive `RustcEncodable` and `RustcDecodable`.
### Decoding ### Decoding
```rust ```rust
let claims = decode::<Claims>(&token, "secret", Algorithm::HS256); let token = decode::<Claims>(&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: 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. 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. 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 ## Algorithms
Right now, only SHA family is supported: SHA256, SHA384 and SHA512. 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 ## Performance
On my thinkpad 440s for a 2 claims struct using SHA256: On my thinkpad 440s for a 2 claims struct using SHA256: