#jwt #api #token

jwt_boiler

Boilerplate for jwt signing and decrypting

1 unstable release

Uses new Rust 2021

0.1.0 Nov 2, 2022

#740 in Cryptography

27 downloads per month

MIT license

5KB
78 lines

boiler-jwt

Boilerplate code for JWT token signing and decrypting.

Add to Cargo.toml

[dependencies]
boiler-jwt = "0.1.0"

JWT Signing and Verifying Example

use boiler_jwt::{to_token, from_token, to_value, from_value};

let app_secret = "my-secret";
let user_id = 1;

// create a jwt with a BTreeMap<String, Value> containing an "id" field
let jwt = to_token(app_secret, [
    ("id".into(), to_value(user_id).unwrap())
].into()).unwrap();

// convert back to a BTreeMap<String, Value>
let mut claims = from_token(app_secret, &jwt).unwrap();
assert_eq!(1, from_value::<i32>(claims.remove("id").unwrap()).unwrap());

Password Hashing and Verifying Example

use boiler_jwt::{to_hash, verify};
let pass = "foo";
let hash = to_hash(pass);
assert!(verify(pass, &hash));
assert!(!verify("bar", &hash));

Dependencies

~1.4–2.2MB
~47K SLoC