jwt_lab/
types.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4/// Supported JWT algorithms
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
6#[serde(rename_all = "UPPERCASE")]
7pub enum Algorithm {
8    /// HMAC with SHA-256
9    HS256,
10    /// HMAC with SHA-384
11    HS384,
12    /// HMAC with SHA-512
13    HS512,
14    /// RSASSA-PKCS1-v1_5 with SHA-256
15    RS256,
16    /// RSASSA-PKCS1-v1_5 with SHA-384
17    RS384,
18    /// RSASSA-PKCS1-v1_5 with SHA-512
19    RS512,
20    /// ECDSA using P-256 and SHA-256
21    ES256,
22    /// ECDSA using P-384 and SHA-384
23    ES384,
24    /// ECDSA using P-521 and SHA-512
25    ES512,
26    /// EdDSA using Ed25519
27    EdDSA,
28}
29
30/// JWT Header containing algorithm and optional fields
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct Header {
33    /// The signing algorithm
34    pub alg: Algorithm,
35    /// Token type (typically "JWT")
36    #[serde(default)]
37    pub typ: Option<String>,
38    /// Key ID for key selection
39    #[serde(default)]
40    pub kid: Option<String>,
41    /// Additional header fields
42    #[serde(flatten)]
43    pub extra: serde_json::Map<String, Value>,
44}
45
46/// JWT Claims (payload) as a JSON object
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct Claims(pub serde_json::Map<String, Value>);
49
50impl Claims {
51    /// Get a claim value by key
52    pub fn get(&self, k: &str) -> Option<&Value> { self.0.get(k) }
53    /// Get the claims as a JSON map
54    pub fn map(&self) -> &serde_json::Map<String, Value> { &self.0 }
55    /// Get mutable access to the claims map
56    pub fn map_mut(&mut self) -> &mut serde_json::Map<String, Value> { &mut self.0 }
57}
58
59/// A decoded JWT token
60#[derive(Debug, Clone)]
61pub struct Jwt {
62    /// The JWT header
63    pub header: Header,
64    /// The JWT claims (payload)
65    pub claims: Claims,
66    /// The signature as base64url-encoded string
67    pub signature_b64: String,
68    /// The raw header as base64url-encoded string
69    pub raw_header_b64: String,
70    /// The raw payload as base64url-encoded string
71    pub raw_payload_b64: String,
72}
73
74#[cfg(feature = "explain")]
75/// Detailed explanation of JWT processing steps
76#[derive(Debug, Clone)]
77pub struct Explanation {
78    /// List of processing steps performed
79    pub steps: Vec<String>,
80}