Update docs

This commit is contained in:
Vincent Prouillet 2017-08-25 17:53:32 +09:00
parent 983380d1ab
commit 43a20030ee
2 changed files with 10 additions and 7 deletions

View File

@ -29,7 +29,7 @@ header.
### Encoding
```rust
let token = encode(&Header::default(), &my_claims, "secret".as_ref()).unwrap();
let token = encode(&Header::default(), &my_claims, "secret".as_ref())?;
```
In that example, `my_claims` is an instance of a Claims struct that derives `Serialize` and `Deserialize`.
The default algorithm is HS256.
@ -37,7 +37,7 @@ Look at custom headers section to see how to change that.
### Decoding
```rust
let token = decode::<Claims>(&token, "secret", &Validation::default()).unwrap();
let token = decode::<Claims>(&token, "secret", &Validation::default())?;
// token is a struct with 2 params: header and claims
```
`decode` can error for a variety of reasons:
@ -46,6 +46,13 @@ let token = decode::<Claims>(&token, "secret", &Validation::default()).unwrap();
- error while decoding base64 or the result of decoding base64 is not valid UTF-8
- validation of at least one reserved claim failed
In some cases, you will want to only decode the header:
```rust
let header = decode_header(&token)?;
```
### Validation
This library validates automatically the `iat`, `exp` and `nbf` claims if found. You can also validate the `sub`, `iss` and `aud` but
those require setting the expected value.
@ -69,9 +76,6 @@ validation.set_audience(&["Me", "You"]); // array of strings
let mut validation = Validation {algorithms: Some(vec![Algorithm::HS256]), ..Default::default()};
```
It's also possible to disable verifying the signature of a token by setting the `validate_signature` to `false`. This should
only be done if you know what you are doing.
### 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:
@ -80,7 +84,7 @@ If you want to set the `kid` parameter for example:
let mut header = Header::default();
header.kid = Some("blabla".to_owned());
header.alg = Algorithm::HS512;
let token = encode(&header, &my_claims, "secret".as_ref()).unwrap();
let token = encode(&header, &my_claims, "secret".as_ref())?;
```
Look at `examples/custom_header.rs` for a full working example.

View File

@ -132,7 +132,6 @@ pub fn decode<T: DeserializeOwned>(token: &str, key: &[u8], validation: &Validat
/// ```rust,ignore
/// use jsonwebtoken::decode_header;
///
///
/// let token = "a.jwt.token".to_string();
/// let header = decode_header(&token);
/// ```