Go to file
Vincent Prouillet b3663e90c6 Uncomment alg comparison 2015-11-02 21:22:21 +00:00
benches Move benches to a folder + add license 2015-11-02 21:15:45 +00:00
examples Start of README 2015-11-02 21:04:58 +00:00
src Uncomment alg comparison 2015-11-02 21:22:21 +00: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 Move benches to a folder + add license 2015-11-02 21:15:45 +00:00
LICENSE Move benches to a folder + add license 2015-11-02 21:15:45 +00:00
README.md Uncomment alg comparison 2015-11-02 21:22:21 +00:00

README.md

JWT

Build Status

Dependencies

You will need to add rustc-serialize to your Cargo.toml in order to use this crate.

How to use

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

Encoding

// encode<T: Part>(claims: T, secret: String, algorithm: Algorithm) -> Result<String, Error>
let token = encode::<Claims>(my_claims, "secret".to_owned(), 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

// decode<T: Part>(token: String, secret: String, algorithm: Algorithm) -> Result<T, Error>
let claims = decode::<Claims>(token.to_owned(), "secret".to_owned(), 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

Algorithms

Right now, only SHA256 is supported.

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:

test bench_decode ... bench:       7,106 ns/iter (+/- 5,354)
test bench_encode ... bench:       3,453 ns/iter (+/- 140)