A Rust implementation of Json Web Tokens
Installation
jsonwebtokens = "1"
serde_json = "1"
Then, in your code:
use json;
use Value;
use jsonwebtokens as jwt;
use ;
Usage
The main two types are Algorithm
which encapsulates a chosen cryptographic
function for signing or verifying tokens, and a Verifier
that gives a
flexible way of describing how incoming tokens should be checked.
Creating an Algorithm
up front means we don't have to repeatedly parse
associated secrets or keys.
The builder pattern used for describing a Verifier
keeps code ergonimic no
matter if you have simple or elaborate verification requirements.
There is also a low-level (::raw
) API available in
case you need more control over splitting, decoding, deserializing and
verifying tokens.
Signing a token
with a symmetric secret:
let alg = new_hmac?;
let header = json!;
let claims = json!;
let token = encode?;
or if the secret isn't a string pass it base64 encoded:
let alg = new_hmac_b64?;
with an RSA private key:
let alg = new_rsa_pem_signer?;
let header = json!;
let claims = json!;
let token = encode?;
Verifying tokens
with a symmetric secret:
let alg = new_hmac?;
let verifier = create
.issuer
.audience
.build?;
let claims: Value = verifier.verify?;
with an RSA private key:
let alg = new_rsa_pem_verifier?;
let verifier = create
.issuer
.audience
.build?;
let claims: Value = verifier.verify?;
Verifying standard claims
let alg = new_hmac?;
let verifier = create
.issuer
.audience
.subject
.nonce
.leeway // give this much leeway (in seconds) when validating exp, nbf and iat claims
.build?;
let claims: Value = verifier.verify?;
Verifying custom claims
let alg = new_hmac?;
let verifier = create
.claim_equals
.claim_matches
.claim_equals_one_of
.claim_matches_one_of
.build?;
let claims: Value = verifier.verify?;
Verifying timestamps (or not)
let alg = new_hmac?;
let verifier = create
.leeway // give this much leeway when validating exp, nbf and iat claims
.ignore_exp // ignore expiry
.ignore_nbf // ignore 'not before time'
.ignore_iat // ignore issue time
.build?;
let claims: Value = verifier.verify?;
Low-level Usage
In case you have more particular decoding and/or validation requirements than are currently handled with the above, high-level APIs, enough of the lower-level implementation details are exposed to allow you to manually split, decode and verify a JWT token.
Just split a token into component parts
let TokenSlices = split_token?;
Just parse the header
use Value;
let header: Value = decode_header_only;
Base64 decode header or claims and deserialize JSON
Equivalent to raw::decode_header_only()
:
let TokenSlices = split_token?;
let header = decode_json_token_slice?;
Or, decode and deserialize just the claims:
let TokenSlices = split_token?;
let claims = decode_json_token_slice?;
Manually split, decode and verify a token
let alg = new_hmac?;
let verifier = create
// snip
.build?;
let TokenSlices = split_token?;
let header = decode_json_token_slice?;
verify_signature_only?;
let claims = decode_json_token_slice?;
verifier.verify_claims_only?;
Algorithms Supported
Array of supported algorithms. The following algorithms are currently supported.
alg Parameter Value | Digital Signature or MAC Algorithm |
---|---|
HS256 | HMAC using SHA-256 hash algorithm |
HS384 | HMAC using SHA-384 hash algorithm |
HS512 | HMAC using SHA-512 hash algorithm |
RS256 | RSASSA-PKCS1-v1_5 using SHA-256 hash algorithm |
RS384 | RSASSA-PKCS1-v1_5 using SHA-384 hash algorithm |
RS512 | RSASSA-PKCS1-v1_5 using SHA-512 hash algorithm |
PS256 | RSASSA-PSS using SHA-256 hash algorithm |
PS384 | RSASSA-PSS using SHA-384 hash algorithm |
PS512 | RSASSA-PSS using SHA-512 hash algorithm |
ES256 | ECDSA using P-256 curve and SHA-256 hash algorithm (only PKCS#8 format PEM) |
ES384 | ECDSA using P-384 curve and SHA-384 hash algorithm (only PKCS#8 format PEM) |
none | No digital signature or MAC value included |