Expand description
JWT authentication and authorization for Armature
This crate provides JSON Web Token (JWT) support for the Armature framework, including token generation, verification, and management.
§Examples
§Basic Usage
use armature_jwt::{JwtConfig, JwtManager, StandardClaims};
// Create JWT configuration
let config = JwtConfig::new("your-secret-key".to_string());
let manager = JwtManager::new(config)?;
// Create claims
let claims = StandardClaims::new()
.with_subject("user123".to_string())
.with_expiration(3600); // 1 hour
// Sign a token
let token = manager.sign(&claims)?;
// Verify and decode
let decoded: StandardClaims = manager.verify(&token)?;
assert_eq!(decoded.sub, Some("user123".to_string()));§Custom Claims
use armature_jwt::{Claims, JwtConfig, JwtManager};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct UserClaims {
email: String,
role: String,
}
let config = JwtConfig::new("secret".to_string());
let manager = JwtManager::new(config)?;
let claims = Claims::new(UserClaims {
email: "user@example.com".to_string(),
role: "admin".to_string(),
})
.with_subject("123".to_string())
.with_expiration(7200);
let token = manager.sign(&claims)?;
let decoded: Claims<UserClaims> = manager.verify(&token)?;
assert_eq!(decoded.custom.email, "user@example.com");Re-exports§
pub use claims::Claims;pub use claims::StandardClaims;pub use config::JwtConfig;pub use error::JwtError;pub use error::Result;pub use service::JwtService;pub use token::Token;pub use token::TokenPair;
Modules§
Structs§
- Decoding
Key - All the different kind of keys we can use to decode a JWT. This key can be re-used so make sure you only initialize it once if you can for better performance.
- Encoding
Key - A key to encode a JWT with. Can be a secret, a PEM-encoded key or a DER-encoded key. This key can be re-used so make sure you only initialize it once if you can for better performance.
- Header
- A basic JWT header, the alg defaults to HS256 and typ is automatically
set to
JWT. All the other fields are optional. - JwtManager
- JWT service for token management
- Validation
- Contains the various validations that are applied after decoding a JWT.
Enums§
- Algorithm
- The algorithms supported for signing/verifying JWTs