jwt-lab 0.1.1

JWT crate for Rust: decode, verify, sign, mutate, JWK/JWKS, algorithm selection, time validation, and secure APIs.
Documentation
use crate::{b64, types::{Header, Claims, Jwt}, Error, Result};

/// Decode a JWT token into its components
///
/// # Arguments
///
/// * `token` - The JWT token string to decode
///
/// # Returns
///
/// Returns a `Result` containing the decoded JWT or an error if the token is malformed
///
/// # Errors
///
/// - `Error::Malformed` if the token doesn't have exactly 3 parts separated by dots
/// - `Error::Base64` if base64url decoding fails
/// - `Error::Json` if JSON parsing of header or payload fails
pub fn decode(token: &str) -> Result<Jwt> {
    let parts: Vec<&str> = token.split('.').collect();
    if parts.len() != 3 { return Err(Error::Malformed); }
    let header: Header = serde_json::from_slice(&b64::decode_url(parts[0])?)
        .map_err(|e| Error::Json(e.to_string()))?;
    let claims_map: serde_json::Map<String, serde_json::Value> =
        serde_json::from_slice(&b64::decode_url(parts[1])?).map_err(|e| Error::Json(e.to_string()))?;
    Ok(Jwt {
        header,
        claims: Claims(claims_map),
        signature_b64: parts[2].to_string(),
        raw_header_b64: parts[0].to_string(),
        raw_payload_b64: parts[1].to_string(),
    })
}

impl crate::types::Jwt {
    /// Decode a JWT token into its components
    ///
    /// This is a convenience method that calls the `decode` function.
    ///
    /// # Arguments
    ///
    /// * `token` - The JWT token string to decode
    ///
    /// # Returns
    ///
    /// Returns a `Result` containing the decoded JWT or an error if the token is malformed
    pub fn decode(token: &str) -> Result<Self> { decode(token) }
}