1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! JWT token management for the EJ authentication system.
//!
//! This module provides functions for creating and validating JSON Web Tokens (JWT)
//! used throughout the EJ framework for stateless authentication. It handles token
//! signing, verification, and claim extraction with secure defaults.
//!
//! # Usage
//!
//! The module provides two main functions for JWT operations:
//! - [`jwt_encode`]: Create signed JWT tokens from claim data
//! - [`jwt_decode`]: Validate and extract claims from JWT tokens
//!
//! # Examples
//!
//! ```rust
//! use ej_auth::jwt::{jwt_encode, jwt_decode};
//! use serde::{Serialize, Deserialize};
//! use std::env;
//! unsafe { env::set_var("JWT_SECRET", "MySuperSecret"); }
//!
//! #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
//! struct UserClaims {
//! user_id: String,
//! role: String,
//! exp: usize,
//! }
//!
//! // Create a token
//! let claims = UserClaims {
//! user_id: "admin".to_string(),
//! role: "administrator".to_string(),
//! exp: 4118335200,
//! };
//!
//! let token = jwt_encode(&claims).unwrap();
//!
//! // Validate and decode the token
//! let decoded = jwt_decode::<UserClaims>(&token).unwrap();
//! assert_eq!(claims, decoded.claims);
//! ```
use crate*;
use LazyLock;
use ;
use ;
/// Lazily initialized cryptographic keys for JWT operations.
///
/// Keys are loaded once from the JWT_SECRET environment variable and reused
/// for all token operations. This provides better performance than recreating
/// keys for each operation while maintaining security.
static KEYS: = new;
/// JWT signing algorithm used throughout the EJ framework.
static ALGORITHM: = new;
/// Cryptographic key pair for JWT signing and verification.
/// Creates a signed JWT token from the provided claims.
///
/// This function serializes the claims data and creates a signed JWT token
/// using the configured signing algorithm and secret. The resulting token
/// can be used for authentication across EJ services.
///
/// # Arguments
///
/// * `body` - Claims data to encode in the token (must be serializable)
///
/// # Returns
///
/// * `Ok(String)` - Base64-encoded JWT token
/// * `Err(Error)` - Token creation or serialization errors
///
/// # Security Notes
///
/// - Claims are not encrypted, only signed for integrity
/// - Include expiration claims to prevent token replay attacks
/// - Keep payload minimal to reduce token size and attack surface
///
/// # Example
///
/// ```rust
/// use ej_auth::jwt::{jwt_encode, jwt_decode};
/// use serde::{Serialize, Deserialize};
/// use std::env;
/// unsafe { env::set_var("JWT_SECRET", "MySuperSecret"); }
///
/// #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
/// struct BuilderClaims {
/// builder_id: String,
/// exp: usize,
/// }
///
/// let claims = BuilderClaims {
/// builder_id: "builder-001".to_string(),
/// exp: 4118335200,
/// };
///
/// let token = jwt_encode(&claims).unwrap();
/// let token_data = jwt_decode::<BuilderClaims>(&token).unwrap();
/// assert_eq!(claims, token_data.claims);
/// ```
/// Validates and decodes a JWT token to extract claims.
///
/// This function verifies the token signature, validates the structure,
/// and deserializes the claims data. Only tokens signed with the correct
/// secret and matching algorithm will be accepted.
///
/// # Arguments
///
/// * `token` - JWT token string to validate and decode
///
/// # Returns
///
/// * `Ok(TokenData<T>)` - Validated token with extracted claims
/// * `Err(Error)` - Invalid token, signature mismatch, or deserialization errors
///
/// # Validation
///
/// The function performs these validation steps:
/// - Signature verification using the configured secret
/// - Algorithm validation (must match HS256)
/// - Token structure validation
/// - Claims deserialization
///
/// See `jwt_encode` for a code example
/// ```