toolcraft-jwt
A lightweight JWT (JSON Web Token) library for Rust with support for access and refresh tokens.
Features
- 🔐 Access and refresh token generation
- ✅ Token validation with configurable rules
- ⏱️ Customizable token expiration times
- 🔄 Refresh token rotation support
- 🎯 Type-safe error handling
- 🚀 Simple and intuitive API
Installation
Add this to your Cargo.toml:
[]
= "*"
Check the crates.io page for the latest version.
Quick Start
use ;
Advanced Usage
Token Generation
// Generate token pair
let token_pair = jwt.generate_token_pair?;
Token Validation
// Validate access token
let claims = jwt.validate_access_token?;
println!;
println!;
println!;
println!;
// Validate refresh token
let claims = jwt.validate_refresh_token?;
Token Refresh
// Use refresh token to get new access token
let new_access_token = jwt.refresh_access_token?;
Custom Configuration
use JwtCfg;
let config = JwtCfg ;
Configuration File Example
[]
= "/etc/myapp/jwt"
= "my-issuer"
= "my-api"
= 900
= 604800
= true
= true
# key_dir contains 4 files:
# access_private_key.pem
# access_public_key.pem
# refresh_private_key.pem
# refresh_public_key.pem
Error Handling
use Error;
match jwt.validate_access_token
Verify-Only (Public Key)
use ;
let verifier = new?;
let claims = verifier.validate_token?;
println!;
API Reference
JwtCfg
Configuration struct for JWT settings:
access_private_key_pem: Ed25519 private key PEM for access tokensaccess_public_key_pem: Ed25519 public key PEM for access tokensrefresh_private_key_pem: Ed25519 private key PEM for refresh tokensrefresh_public_key_pem: Ed25519 public key PEM for refresh tokenskey_dir: Optional key directory, if set reads 4 fixed files:access_private_key.pemaccess_public_key.pemrefresh_private_key.pemrefresh_public_key.pem
issuer: Expected issuer claimaudience: Expected audience claimaccess_token_duration: Access token lifetime in secondsrefresh_token_duration: Refresh token lifetime in secondsaccess_key_validate_exp: Whether to validate access token expirationrefresh_key_validate_exp: Whether to validate refresh token expiration
Jwt Methods
new(cfg: JwtCfg)- Create a new JWT instancegenerate_token_pair(sub: String, ext: Option<Value>) -> TokenPair- Generate access and refresh tokensvalidate_access_token(token: &str)- Validate access tokenvalidate_refresh_token(token: &str)- Validate refresh tokenrefresh_access_token(refresh_token: &str)- Generate new access token from refresh tokenVerifyJwt::new(public_key_pem, cfg)- Create verifier with fixediss/audvalidation configVerifyJwtCfg- Verifier config (issuerandaudience)VerifyJwt::validate_token(token: &str)- Validate token using public keyAccessTokenVerifier- verification trait implemented by bothJwtandVerifyJwt
Claims
JWT claims structure:
iss: Issueraud: Audiencesub: Subject (typically user ID)exp: Expiration time (Unix timestamp)iat: Issued at time (Unix timestamp)ext: Optional extension payload (serde_json::Value)
Security Considerations
- Key Pairs: Use dedicated Ed25519 key pairs for production
- Key Rotation: Regularly rotate your secret keys
- HTTPS Only: Always transmit tokens over HTTPS
- Storage: Never store tokens in localStorage; use httpOnly cookies when possible
- Expiration: Use short expiration times for access tokens
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.