jwt-lab 0.1.1

JWT crate for Rust: decode, verify, sign, mutate, JWK/JWKS, algorithm selection, time validation, and secure APIs.
Documentation
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Supported JWT algorithms
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum Algorithm {
    /// HMAC with SHA-256
    HS256,
    /// HMAC with SHA-384
    HS384,
    /// HMAC with SHA-512
    HS512,
    /// RSASSA-PKCS1-v1_5 with SHA-256
    RS256,
    /// RSASSA-PKCS1-v1_5 with SHA-384
    RS384,
    /// RSASSA-PKCS1-v1_5 with SHA-512
    RS512,
    /// ECDSA using P-256 and SHA-256
    ES256,
    /// ECDSA using P-384 and SHA-384
    ES384,
    /// ECDSA using P-521 and SHA-512
    ES512,
    /// EdDSA using Ed25519
    EdDSA,
}

/// JWT Header containing algorithm and optional fields
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Header {
    /// The signing algorithm
    pub alg: Algorithm,
    /// Token type (typically "JWT")
    #[serde(default)]
    pub typ: Option<String>,
    /// Key ID for key selection
    #[serde(default)]
    pub kid: Option<String>,
    /// Additional header fields
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

/// JWT Claims (payload) as a JSON object
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Claims(pub serde_json::Map<String, Value>);

impl Claims {
    /// Get a claim value by key
    pub fn get(&self, k: &str) -> Option<&Value> { self.0.get(k) }
    /// Get the claims as a JSON map
    pub fn map(&self) -> &serde_json::Map<String, Value> { &self.0 }
    /// Get mutable access to the claims map
    pub fn map_mut(&mut self) -> &mut serde_json::Map<String, Value> { &mut self.0 }
}

/// A decoded JWT token
#[derive(Debug, Clone)]
pub struct Jwt {
    /// The JWT header
    pub header: Header,
    /// The JWT claims (payload)
    pub claims: Claims,
    /// The signature as base64url-encoded string
    pub signature_b64: String,
    /// The raw header as base64url-encoded string
    pub raw_header_b64: String,
    /// The raw payload as base64url-encoded string
    pub raw_payload_b64: String,
}

#[cfg(feature = "explain")]
/// Detailed explanation of JWT processing steps
#[derive(Debug, Clone)]
pub struct Explanation {
    /// List of processing steps performed
    pub steps: Vec<String>,
}