#jwt #token #authorization

no-std jwt-compact

Minimalistic JWT implementation with focus on type safety and secure cryptographic primitives

9 releases (5 breaking)

0.6.0 Nov 1, 2022
0.5.0 Dec 29, 2021
0.5.0-beta.1 Oct 21, 2021
0.4.0 May 24, 2021
0.1.0 Jul 1, 2019

#106 in Cryptography

Download history 402/week @ 2022-10-13 474/week @ 2022-10-20 570/week @ 2022-10-27 617/week @ 2022-11-03 798/week @ 2022-11-10 933/week @ 2022-11-17 1152/week @ 2022-11-24 1423/week @ 2022-12-01 1492/week @ 2022-12-08 1665/week @ 2022-12-15 625/week @ 2022-12-22 1184/week @ 2022-12-29 1447/week @ 2023-01-05 1169/week @ 2023-01-12 1660/week @ 2023-01-19 1212/week @ 2023-01-26

5,836 downloads per month
Used in 14 crates (5 directly)

Apache-2.0

155KB
3K SLoC

Compact JWT implementation in Rust

Build Status License: Apache-2.0 rust 1.60+ required no_std supported

Documentation: Docs.rs crate docs (master)

Minimalistic JSON web token (JWT) implementation with focus on type safety and secure cryptographic primitives.

Usage

Add this to your Crate.toml:

[dependencies]
jwt-compact = "0.6.0"

Basic token lifecycle

use chrono::{Duration, Utc};
use jwt_compact::{prelude::*, alg::{Hs256, Hs256Key}};
use serde::{Serialize, Deserialize};

/// Custom claims encoded in the token.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct CustomClaims {
    #[serde(rename = "sub")]
    subject: String,
    // other fields...
}

// Choose time-related options for token creation / validation.
let time_options = TimeOptions::default();
// Create a symmetric HMAC key, which will be used both to create and verify tokens.
let key = Hs256Key::new(b"super_secret_key_donut_steel");
// Create a token.
let header = Header::default().with_key_id("my-key");
let claims = Claims::new(CustomClaims { subject: "alice".to_owned() })
    .set_duration_and_issuance(&time_options, Duration::hours(1))
    .set_not_before(Utc::now());
let token_string = Hs256.token(header, &claims, &key)?;
println!("token: {}", token_string);

// Parse the token.
let token = UntrustedToken::new(&token_string)?;
// Before verifying the token, we might find the key which has signed the token
// using the `Header.key_id` field.
assert_eq!(token.header().key_id.as_deref(), Some("my-key"));
// Validate the token integrity.
let token: Token<CustomClaims> = Hs256.validate_integrity(&token, &key)?;
// Validate additional conditions.
token.claims()
    .validate_expiration(&time_options)?
    .validate_maturity(&time_options)?;
Ok::<_, anyhow::Error>(())

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, HS384 and HS512 algorithms are implemented via pure Rust sha2 crate.
  • The crate supports EdDSA algorithm with the Ed25519 elliptic curve, and ES256K algorithm 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 standard ES* algorithms).
  • The ES256 algorithm is supported via pure Rust p256 crate.
  • RSA algorithms (RS* and PS*) are supported via pure Rust rsa crate.
  • The crate supports the no_std mode. 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., iss may be a human-readable short ID, a hex-encoded key digest, etc.)
  • ES384 and ES512 algorithms.

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.

Dependencies

~2.5–6MB
~108K SLoC