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:
[]
= "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 *;
// create a new key pair for the `ES256` JWT algorithm
let key_pair = generate;
// a public key can be extracted from a key pair:
let public_key = key_pair.public_key;
ES384
use *;
// create a new key pair for the `ES384` JWT algorithm
let key_pair = 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:
let key_pair = from_pem?;
let public_key = from_pem?;
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 = create;
let token = key_pair.sign?;
Token verification:
let claims = public_key.?;
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 = create.
with_issuer.with_subject;
But application-defined claims can also be defined. These simply have to be present in a serializable type (this requires the serde crate):
let my_additional_data = MyAdditionalData ;
Claim creation with custom data:
let claims = with_custom_claims;
Claim verification with custom data. Note the presence of the custom data type:
let claims = public_key.?;
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 = decode_metadata?;
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_nonceverification 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
RSAorHS256) and hash functions (such asSHA1) are supported - minimal, rust-only dependencies
Credits
This crate is based on the jwt-simple project by Frank Denis.