15 stable releases

Uses old Rust 2015

3.1.3 Aug 11, 2022
3.1.2 Nov 6, 2019
3.1.1 May 23, 2019
3.1.0 Dec 7, 2018
2.1.0 Jul 30, 2015

#182 in Authentication

Download history 1395/week @ 2024-07-23 1211/week @ 2024-07-30 1464/week @ 2024-08-06 1040/week @ 2024-08-13 1114/week @ 2024-08-20 806/week @ 2024-08-27 1481/week @ 2024-09-03 1799/week @ 2024-09-10 1126/week @ 2024-09-17 940/week @ 2024-09-24 1397/week @ 2024-10-01 1245/week @ 2024-10-08 1452/week @ 2024-10-15 1388/week @ 2024-10-22 1500/week @ 2024-10-29 1063/week @ 2024-11-05

5,513 downloads per month
Used in 6 crates

Apache-2.0

39KB
777 lines

Frank JWT Build Status crates.io

Implementation of JSON Web Tokens in Rust.

Algorithms and features supported

  • HS256
  • HS384
  • HS512
  • RS256
  • RS384
  • RS512
  • ES256
  • ES384
  • ES512
  • Sign
  • Verify
  • iss (issuer) check
  • sub (subject) check
  • aud (audience) check
  • exp (expiration time) check
  • nbf (not before time) check
  • iat (issued at) check
  • jti (JWT id) check

Usage

Put this into your Cargo.toml:

[dependencies]
frank_jwt = "<current version of frank_jwt>"

And this in your crate root:

extern crate frank_jwt;
#[macro_use] extern crate serde_json;


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

Example

//HS256
let mut payload = json!({
    "key1": "val1",
    "key2": "val2"
});

let mut header = json!({});
let secret = "secret123";
let jwt = encode(&header, secret.to_string(), &payload, Algorithm::HS256);

//RS256
use std::env;

let mut payload = json!({
    "key1": "val1",
    "key2": "val2"
});

let mut header = json!({});
let mut keypath = env::current_dir().unwrap();
keypath.push("some_folder");
keypath.push("my_rsa_2048_key.pem");
let jwt = encode(&header, &keypath.to_path_buf(), &payload, Algorithm::RS256);
let (header, payload) = decode(&jwt, &keypath.to_path_buf(), Algorithm::RS256, &ValidationOptions::default());

Validation Options

The ValidationOptions structure allows for control over which checks should be preformed when decoding a JWT. Calling new on this will provide a default set of values. There is also a dangerous function that will return validation options that doesn't perform any checking.

The default values are:

  • Perform expiry check
  • Allow 0 leeway for the expiry check.

It's worth noting that if the expiry check is requested and an exp claim is not within the JWT the check will fail validation.

License

Apache 2.0

Tests

cargo test

Contributors

TODO

Dependencies

~2.4–4MB
~83K SLoC