Go to file
Markus Unterwaditzer d6d0ce058b Allow non-UTF8 keys for decoding too 2015-12-16 18:40:28 +01:00
benches Rewrite decode 2015-11-06 22:45:47 +00:00
examples Allow non-UTF8 keys for decoding too 2015-12-16 18:40:28 +01:00
src Allow non-UTF8 keys for decoding too 2015-12-16 18:40:28 +01:00
.editorconfig Initial commit 2015-10-31 15:37:15 +00:00
.gitignore Initial commit 2015-10-31 15:37:15 +00:00
.travis.yml Add example + travis 2015-11-02 20:34:11 +00:00
Cargo.toml Updated docs 2015-11-08 12:08:44 +00:00
LICENSE Move benches to a folder + add license 2015-11-02 21:15:45 +00:00
README.md Update version number in doc 2015-11-10 16:38:41 +00:00

README.md

jsonwebtoken

Build Status

Installation

Add the following to Cargo.toml:

jsonwebtoken = "0.2"
rustc-serialize = "0.3"

How to use

There is a complete example in examples/claims.rs but here's a quick one.

In terms of imports:

extern crate jsonwebtoken as jwt;
extern crate rustc_serialize;

use jwt::{encode, decode, Algorithm};

Encoding

let token = encode(&my_claims, "secret", Algorithm::HS256);

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

let claims = decode::<Claims>(&token, "secret", Algorithm::HS256);

In addition to the normal base64/json decoding errors, decode can return two custom errors:

  • InvalidToken: if the token is not a valid JWT
  • InvalidSignature: if the signature doesn't match
  • WrongAlgorithmHeader: if the alg in the header doesn't match the one given to decode

Validation

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.

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:

test bench_decode ... bench:       2,537 ns/iter (+/- 813)
test bench_encode ... bench:       2,847 ns/iter (+/- 131)