#jwt #jws #authentication #tokens #json

keygate-jwt

Easy to use, secure, opinionated JWT (JSON Web Tokens) implementation for Rust

2 stable releases

Uses new Rust 2021

1.1.0 Oct 31, 2022
1.0.0 Oct 31, 2022

#150 in Cryptography

Download history 140/week @ 2022-10-25 83/week @ 2022-11-01 5/week @ 2022-11-08

228 downloads per month

ISC license

94KB
2.5K SLoC

GitHub CI Docs.rs crates.io

keygate-jwt

A new JWT (JSON Web Tokens) implementation for Rust that focuses on simplicity, while avoiding common JWT security pitfalls.

keygate-jwt is opinionated and only supports secure signature algorithms:

JWT algorithm name Feature Description
ES256 ecdsa ECDSA over p256 / SHA-256
ES384 ecdsa ECDSA over p384 / SHA-384
ES256K ecdsa ECDSA over secp256k1 / SHA-256
EdDSA eddsa Ed25519

Additionally, you can enable support for the none algorithm, which is useful for testing purposes. This algorithm is disabled by default and should never be used to on its own. It can however be useful for non security relevant data. JWTs created with the none algorithm are not verified and can be tampered with. Additionally, the none algorithm does not provide any integrity guarantees. To make sure that the none algorithm is never used instead of a secure algorithm, it is marked as unsafe and requires an explicit opt-in.

keygate-jwt uses only pure Rust implementations, and can be compiled out of the box to WebAssembly/WASI.

Important: JWT's purpose is to verify that data has been created by a party knowing a secret key. It does not provide any kind of confidentiality: JWT data is simply encoded as BASE64, and is not encrypted.

Usage

cargo.toml:

[dependencies]
keygate-jwt = "1.0"

Errors are returned as keygate-jwt::Error values

Signatures

A signature requires a key pair: a secret key used to create tokens, and a public key, that can only verify them.

Always use a signature scheme if both parties do not ultimately trust each other, such as tokens exchanged between clients and API providers.

Key pairs and tokens creation

Key creation:

ES256

use keygate_jwt::prelude::*;

// create a new key pair for the `ES256` JWT algorithm
let key_pair = ES256KeyPair::generate();

// a public key can be extracted from a key pair:
let public_key = key_pair.public_key();

ES384

use keygate_jwt::prelude::*;

// create a new key pair for the `ES384` JWT algorithm
let key_pair = ES384KeyPair::generate();

// a public key can be extracted from a key pair:
let public_key = key_pair.public_key();

Keys can be exported as bytes for later reuse, and imported from bytes or, for RSA, from individual parameters, DER-encoded data or PEM-encoded data.

RSA key pair creation, using OpenSSL and PEM importation of the secret key:

openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -outform PEM -pubout -out public.pem
let key_pair = RS384KeyPair::from_pem(private_pem_file_content)?;
let public_key = RS384PublicKey::from_pem(public_pem_file_content)?;

Token creation and verification work the same way as with HS* algorithms, except that tokens are created with a key pair, and verified using the corresponding public key.

Token creation:

/// create claims valid for 2 hours
let claims = Claims::create(Duration::from_hours(2));
let token = key_pair.sign(claims)?;

Token verification:

let claims = public_key.verify_token::<NoCustomClaims>(&token, None)?;

Available verification options are identical to the ones used with symmetric algorithms.

Advanced usage

Custom claims

Claim objects support all the standard claims by default, and they can be set directly or via convenient helpers:

let claims = Claims::create(Duration::from_hours(2)).
    with_issuer("Example issuer").with_subject("Example subject");

But application-defined claims can also be defined. These simply have to be present in a serializable type (this requires the serde crate):

#[derive(Serialize, Deserialize)]
struct MyAdditionalData {
   user_is_admin: bool,
   user_country: String,
}
let my_additional_data = MyAdditionalData {
   user_is_admin: false,
   user_country: "FR".to_string(),
};

Claim creation with custom data:

let claims = Claims::with_custom_claims(my_additional_data, Duration::from_secs(30));

Claim verification with custom data. Note the presence of the custom data type:

let claims = public_key.verify_token::<MyAdditionalData>(&token, None)?;
let user_is_admin = claims.custom.user_is_admin;

Peeking at metadata before verification

Properties such as the key identifier can be useful prior to tag or signature verification in order to pick the right key out of a set.

let metadata = Token::decode_metadata(&token)?;
let key_id = metadata.key_id();
let algorithm = metadata.algorithm();
// all other standard properties are also accessible

IMPORTANT: neither the key ID nor the algorithm can be trusted. This is an unfixable design flaw of the JWT standard.

As a result, algorithm should be used only for debugging purposes, and never to select a key type. Similarly, key_id should be used only to select a key in a set of keys made for the same algorithm.

Mitigations against replay attacks

keygate-jwt includes mechanisms to mitigate replay attacks:

  • Nonces can be attached to new tokens using the with_nonce() claim function. The verification procedure can later reject any token that doesn't include the expected nonce (required_nonce verification option).
  • The verification procedure can reject tokens created too long ago, no matter what their expiration date is. This prevents tokens from malicious (or compromised) signers from being used for too long.
  • The verification procedure can reject tokens created before a date. For a given user, the date of the last successful authentication can be stored in a database, and used later along with this option to reject older (replayed) tokens.

Why yet another JWT crate

There are already several JWT crates for Rust, but none of them satisfied our needs:

  • no insecure algorithms (such as RSA or HS256) and hash functions (such as SHA1) are supported
  • minimal, rust-only dependencies

Credits

This crate is based on the jwt-simple project by Frank Denis.

Dependencies

~1–2.6MB
~58K SLoC