From 43a20030eef4b5ff96d46e7fad67f3a7aaf96d25 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Fri, 25 Aug 2017 17:53:32 +0900 Subject: [PATCH] Update docs --- README.md | 16 ++++++++++------ src/lib.rs | 1 - 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 476444e..46c448d 100644 --- a/README.md +++ b/README.md @@ -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::(&token, "secret", &Validation::default()).unwrap(); +let token = decode::(&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::(&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. diff --git a/src/lib.rs b/src/lib.rs index 39576d5..6ddb704 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -132,7 +132,6 @@ pub fn decode(token: &str, key: &[u8], validation: &Validat /// ```rust,ignore /// use jsonwebtoken::decode_header; /// -/// /// let token = "a.jwt.token".to_string(); /// let header = decode_header(&token); /// ```