jwtoken-0.1.2 has been yanked.
jwtoken
A flexible utility library for encoding and decoding JSON Web Tokens (JWT).
Installation
Add jwtoken
to your Cargo.toml
:
[]
# Basic usage
= "0.1.2"
# With key generation utilities (for random secrets, RSA keypairs)
= { = "0.1.2", = ["key-gen"] }
# Enable HS256 algorithm
= { = "0.1.2", = ["hs256"] }
# Enable RS256 algorithm
= { = "0.1.2", = ["rs256"] }
# Enable all features
= { = "0.1.2", = ["full"] }
Usage
HMAC-SHA256 (HS256)
HS256 uses a shared secret key for both signing and verification:
use ;
RS256 (RSA-SHA256)
RS256 uses an RSA key pair, with the private key for signing and the public key for verification:
use ;
API Reference
JWT Encoder
let jwt = new
.claim // Add a plain value
.claim_json // Add a JSON value
.encode?; // Sign and encode to string
JWT Decoder
let decoded = decode?;
// Access claims
let user_id = decoded.claim;
let name = decoded.claim;
// Access headers
let algorithm = decoded.header;
Algorithms
HS256 (HMAC-SHA256)
use HS256;
let algorithm = HS256 new;
RS256 (RSA-SHA256)
use ;
// Requires the "key-gen" and "rs256" features
let = rsa_keypair.unwrap;
// For signing
let signer = new;
// For verifying
let verifier = new;
Error Handling
The library uses a custom JwtError
enum for error handling:
use JwtError;
match result