Compact JWT implementation in Rust
Minimalistic JSON web token (JWT) implementation with focus on type safety and secure cryptographic primitives.
Usage
Add this to your Crate.toml:
[]
= "0.7.0"
Basic token lifecycle
use ;
use ;
use ;
/// Custom claims encoded in the token.
// Choose time-related options for token creation / validation.
let time_options = default;
// Create a symmetric HMAC key, which will be used both to create and verify tokens.
let key = new;
// Create a token.
let header = default.with_key_id;
let claims = new
.set_duration_and_issuance
.set_not_before;
let token_string = Hs256.token?;
println!;
// Parse the token.
let token = new?;
// Before verifying the token, we might find the key which has signed the token
// using the `Header.key_id` field.
assert_eq!;
// Validate the token integrity.
let token: = Hs256.validate_integrity?;
// Validate additional conditions.
token.claims
.validate_expiration?
.validate_maturity?;
Ok::
See the crate docs for more examples of usage.
Features
- Algorithm-specific signing and verifying keys (i.e., type safety).
- Key strength requirements from RFC 7518 are expressed with wrapper types.
- Easy to extend to support new signing algorithms.
- The crate supports more compact CBOR encoding of the claims.
- Basic JWK functionality for key conversion from human-readable formats (JSON / YAML / TOML) and computing key thumbprints.
HS256,HS384andHS512algorithms are implemented via pure Rustsha2crate.- The crate supports
EdDSAalgorithm with the Ed25519 elliptic curve, andES256Kalgorithm with the secp256k1 elliptic curve. Both curves are widely used in crypto community and believed to be securely generated (there are some doubts about parameter generation for elliptic curves used in standardES*algorithms). - The
ES256algorithm is supported via pure Rustp256crate. - RSA algorithms (
RS*andPS*) are supported via pure Rustrsacrate. - The crate supports the
no_stdmode. No-std support and WASM compatibility are explicitly tested.
Missing features
- Built-in checks of some claims (e.g.,
iss– the token issuer). This is intentional: depending on the use case, such claims can have different semantics and thus be represented by different datatypes (e.g.,issmay be a human-readable short ID, a hex-encoded key digest, etc.) ES384andES512algorithms.
Alternatives
jsonwebtoken, frank_jwt or biscuit may be viable alternatives depending on the use case
(e.g., none of them seems to implement EdDSA or ES256K algorithms).
See also
- justwebtoken.io – educational mini-website that uses this library packaged in a WASM module.
License
Licensed under the Apache-2.0 license.