Skip to main content

Crate oxitoken

Crate oxitoken 

Source
Expand description

§OxiToken

ci codecov crates.io docs.rs deps.rs msrv quality gate reliability security maintainability

oxitoken strives to be the fastest JWT encoding/decoding/validation library while simulteneously providing the best developer experience and extensibility.

  • Custom header and claims structs, no stringly-typed extras HashMap here
  • Custom KeyProvider support
    • A LocalKeystore implementation is provided that resolves keys via kid
    • If that doesn’t fit your use-case, implement your own KeyProvider, which can resolve keys via any abitrary header or claim field, even custom ones
  • Crypto backend agnostic
    • Reference implementations using aws-lc-rs and openssl are provided
      • Signer and VerificationKey traits permit using your own crypto backend (e.g. botan-rs, ring, HSMs, etc.)
      • Built-in providers can be fully disabled with feature flags
  • Supports all the IANA registered JWS algorithms
    • HMAC: HS256, HS384, HS512
    • RSA: RS256, RS384, RS512, PS256, PS384, PS512
    • ECDSA: ES256, ES384, ES512
    • EdDSA: EdDSA (i.e. Ed25519) and Ed448
    • Post-Quantum algorithms: ML-DSA-44, ML-DSA-65, ML-DSA-87

§Benchmarks

§Encoding Performance

Algorithmoxitokenjsonwebtoken% faster
HS256312.45 ns638.67 ns51.08 %
HS384417.19 ns856.24 ns51.28 %
HS512422.99 ns862.96 ns50.98 %
RS256480.22 µs762.61 µs37.03 %
RS3841525.74 µs2056.15 µs25.8 %
RS5123234.4 µs4051.46 µs20.17 %
PS256480.72 µs765.89 µs37.23 %
PS3841529.31 µs2098.04 µs27.11 %
PS5123252.92 µs4072.99 µs20.13 %
ES25616.47 µs25.57 µs35.58 %
ES38469.61 µs112.13 µs37.92 %
EdDSA6.51 µs12.48 µs47.85 %
ML-DSA-44140.91 µs
ML-DSA-65230.96 µs
ML-DSA-87284.98 µs

§Validation Performance

Algorithmoxitokenjsonwebtoken% faster
HS256623.51 ns1.7 µs63.28 %
HS384788.75 ns2 µs60.55 %
HS512789.81 ns2.05 µs61.48 %
RS25613.48 µs17.13 µs21.35 %
RS38426.87 µs32.88 µs18.28 %
RS51245.97 µs53.9 µs14.71 %
PS25613.34 µs17.39 µs23.3 %
PS38427.77 µs33.55 µs17.21 %
PS51246.92 µs54.86 µs14.48 %
ES25642.98 µs44.55 µs3.52 %
ES384163.61 µs165.55 µs1.17 %
EdDSA27.89 µs28.98 µs3.77 %
ML-DSA-4439.19 µs
ML-DSA-6563.38 µs
ML-DSA-87104.22 µs

Comparative benchmarks above were ran with aws-lc-rs backend for both oxitoken and jsonwebtoken. openssl is slightly slower than aws-lc-rs across the board, and rust_crypto is substantially slower.

§Example

use std::time::{
    SystemTime,
    UNIX_EPOCH,
};

use aws_lc_rs::signature::{
    ED25519,
    Ed25519KeyPair,
    KeyPair,
    ParsedPublicKey,
};
use oxitoken::{
    Algorithm,
    claims::{
        Exp,
        Iss,
    },
    encoding::encode,
    header::Alg,
    validation::{
        StaticKeyProvider,
        ValidationPipeline,
    },
};

// NOTE: Debug, PartialEq, and Eq are only required for the assertions in this
// example code and are not a requirement of oxitoken.
#[derive(Debug, serde::Deserialize, serde::Serialize, PartialEq, Eq)]
struct Header {
    alg: Algorithm,
}
impl Header {
    const fn new(alg: Algorithm) -> Self {
        Self { alg }
    }
}
impl Alg for Header {
    fn alg(&self) -> Algorithm {
        self.alg
    }
}

// NOTE: Debug, PartialEq, and Eq are only required for the assertions in this
// example code and are not a requirement of oxitoken.
#[derive(Debug, serde::Deserialize, serde::Serialize, PartialEq, Eq)]
struct Claims {
    iss: String,
    exp: i64,
}
impl Claims {
    fn new(iss: impl Into<String>) -> Self {
        // expire one hour from now
        let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
        Self {
            iss: iss.into(),
            exp: now.cast_signed() + 3600,
        }
    }
}
impl Iss for Claims {
    fn iss(&self) -> &str {
        &self.iss
    }
}
impl Exp for Claims {
    fn exp(&self) -> i64 {
        self.exp
    }
}

fn make_key() -> Ed25519KeyPair {
    Ed25519KeyPair::generate().unwrap()
}

fn make_parsed_public_key(key: &Ed25519KeyPair) -> ParsedPublicKey {
    ParsedPublicKey::new(&ED25519, key.public_key().as_ref()).unwrap()
}

fn main() {
    let header = Header::new(Algorithm::EdDSA);
    let claims = Claims::new("oxitoken.example.org");
    let key = make_key();
    let jwt = encode(&key, &header, &claims).unwrap();
    println!("JWT: {jwt}");

    let pubkey = make_parsed_public_key(&key);
    let validator =
        ValidationPipeline::<Header, Claims, StaticKeyProvider<_>>::builder(pubkey.into())
            .with_issuer_validator("oxitoken.example.org")
            .with_expiration_validator()
            .build();

    let (decoded_header, decoded_claims) = validator.verify(jwt.as_bytes()).unwrap();

    assert_eq!(decoded_header, header);
    assert_eq!(decoded_claims, claims);

    println!("JWT validated successfully");

    // an alternative that allows the compiler to infer the generic types,
    // permitting omittance of the turbofish.
    let pubkey = StaticKeyProvider::from(make_parsed_public_key(&key));
    let validator = ValidationPipeline::builder(pubkey)
        .with_issuer_validator("oxitoken.example.org")
        .build();
    let (decoded_header, decoded_claims): (Header, Claims) =
        validator.verify(jwt.as_bytes()).unwrap();

    assert_eq!(decoded_header, header);
    assert_eq!(decoded_claims, claims);
}

Modules§

claims
Traits supporting implementation of custom JWT header structs and reference-implementations of common JWT standards.
crypto
Crypto backend implementations (e.g. aws_lc, openssl)
dangerous
Provides dangerous (i.e. non-signature-verifying) JWT decoding functionality.
encoding
Functionality for encoding JWTs (including signing).
error
Error enums
header
Traits supporting implementation of custom JWT header structs and reference-implementations of common JWT standards.
validation
Functions and traits supporting JWT validation. Provides JWT validation functionality

Enums§

Algorithm
JWS Signature Algorithm