chopin-auth 0.5.34

Zero-overhead JWT authentication and RBAC for the Chopin framework.
Documentation
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! JWT encoding, decoding, and lifecycle management.
//!
//! Central types:
//! - [`JwtManager`] — sign and verify tokens, configure algorithms and validation.
//! - [`AuthError`] — typed errors for invalid, expired, and revoked tokens.
//! - [`HasJti`] — opt-in trait for revocation support via JWT IDs.
//!
//! # Example
//!
//! ```rust,ignore
//! use chopin_auth::jwt::{JwtManager, HasJti};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Debug, Serialize, Deserialize)]
//! struct Claims { sub: String, jti: String, exp: u64 }
//!
//! impl HasJti for Claims {
//!     fn jti(&self) -> Option<&str> { Some(&self.jti) }
//! }
//!
//! let mgr = JwtManager::new(b"my-secret");
//! let claims = Claims { sub: "user42".into(), jti: "abc".into(), exp: 9999999999 };
//! let token  = mgr.sign(&claims).unwrap();
//! let back: Claims = mgr.verify(&token).unwrap();
//! ```
// src/jwt.rs
use jsonwebtoken::{Algorithm, DecodingKey, EncodingKey, Header, Validation, decode, encode};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::sync::Arc;

use crate::revocation::TokenBlacklist;

// ─── Error type ──────────────────────────────────────────────────────────────

/// Errors that can occur during JWT encode / decode operations.
#[derive(Debug)]
pub enum AuthError {
    /// The token is syntactically invalid, has a bad signature, or fails
    /// validation (e.g. wrong algorithm, audience, issuer).
    InvalidToken(String),
    /// The token's `exp` claim has passed.
    Expired,
    /// The token's JTI is on the revocation blacklist.
    Revoked,
    /// The manager has no encoding key; cannot sign tokens.
    EncodingKeyMissing,
    /// Signing the claims failed.
    Encode(String),
    /// A configuration or internal error unrelated to the token itself.
    Internal(String),
}

impl fmt::Display for AuthError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidToken(e) => write!(f, "invalid token: {e}"),
            Self::Expired => f.write_str("token expired"),
            Self::Revoked => f.write_str("token revoked"),
            Self::EncodingKeyMissing => f.write_str("no encoding key configured"),
            Self::Encode(e) => write!(f, "encoding failed: {e}"),
            Self::Internal(e) => write!(f, "internal error: {e}"),
        }
    }
}

impl std::error::Error for AuthError {}

// ─── HasJti trait ─────────────────────────────────────────────────────────────

/// Implemented by claims types that carry a JWT ID (`jti`) for revocation checks.
///
/// The **default implementation returns `None`**, so any claims type can opt out
/// of revocation with an empty one-line impl:
///
/// ```rust
/// # use chopin_auth::HasJti;
/// struct MyClaims { sub: String, exp: u64 }
/// impl HasJti for MyClaims {}
/// ```
///
/// To enable revocation, return the `jti` field:
///
/// ```rust
/// # use chopin_auth::HasJti;
/// struct MyClaims { sub: String, jti: String, exp: u64 }
/// impl HasJti for MyClaims {
///     fn jti(&self) -> Option<&str> { Some(&self.jti) }
/// }
/// ```
pub trait HasJti {
    /// Return the `jti` claim value, or `None` if the claims do not carry one.
    fn jti(&self) -> Option<&str> {
        None
    }
}

// ─── JwtConfig ───────────────────────────────────────────────────────────────

/// Low-level configuration for a [`JwtManager`].
///
/// Prefer the constructor methods on [`JwtManager`] over constructing this directly.
pub struct JwtConfig {
    pub decoding_key: DecodingKey,
    pub encoding_key: Option<EncodingKey>,
    pub validation: Validation,
}

// ─── JwtManager ──────────────────────────────────────────────────────────────

/// A cloneable JWT manager for encoding and decoding tokens.
///
/// Constructors:
/// - [`JwtManager::new`]                – HMAC-SHA256 (sign + verify)
/// - [`JwtManager::verify_only`]        – HMAC-SHA256 (verify only)
/// - [`JwtManager::from_rsa_pem`]       – RS256 (sign + verify)
/// - [`JwtManager::from_rsa_public_pem`] – RS256 (verify only)
/// - [`JwtManager::from_ec_pem`]        – ES256 (sign + verify)
/// - [`JwtManager::from_ec_public_pem`] – ES256 (verify only)
/// - [`JwtManager::with_config`]        – fully custom config
///
/// Add a revocation blacklist with [`JwtManager::with_blacklist`].
#[derive(Clone)]
pub struct JwtManager {
    config: Arc<JwtConfig>,
    blacklist: Option<TokenBlacklist>,
}

impl JwtManager {
    /// Construct a manager using a shared HMAC-SHA256 secret (sign + verify).
    pub fn new(secret: &[u8]) -> Self {
        let mut validation = Validation::new(Algorithm::HS256);
        validation.leeway = 60; // 1 minute clock-skew leeway
        Self {
            config: Arc::new(JwtConfig {
                decoding_key: DecodingKey::from_secret(secret),
                encoding_key: Some(EncodingKey::from_secret(secret)),
                validation,
            }),
            blacklist: None,
        }
    }

    /// Construct a verify-only manager (no signing key) for HMAC-SHA256.
    pub fn verify_only(secret: &[u8]) -> Self {
        let mut validation = Validation::new(Algorithm::HS256);
        validation.leeway = 60;
        Self {
            config: Arc::new(JwtConfig {
                decoding_key: DecodingKey::from_secret(secret),
                encoding_key: None,
                validation,
            }),
            blacklist: None,
        }
    }

    /// Construct a manager from a PEM-encoded RSA private/public key pair (RS256).
    ///
    /// Both the private key (for signing) and the public key (for verification)
    /// must be provided. For verify-only use, see [`JwtManager::from_rsa_public_pem`].
    pub fn from_rsa_pem(private_key_pem: &[u8], public_key_pem: &[u8]) -> Result<Self, AuthError> {
        let encoding_key = EncodingKey::from_rsa_pem(private_key_pem)
            .map_err(|e| AuthError::Internal(format!("RSA private key: {e}")))?;
        let decoding_key = DecodingKey::from_rsa_pem(public_key_pem)
            .map_err(|e| AuthError::Internal(format!("RSA public key: {e}")))?;
        Ok(Self {
            config: Arc::new(JwtConfig {
                encoding_key: Some(encoding_key),
                decoding_key,
                validation: Validation::new(Algorithm::RS256),
            }),
            blacklist: None,
        })
    }

    /// Construct a **verify-only** manager from a PEM-encoded RSA public key (RS256).
    ///
    /// No signing key is stored; calling [`JwtManager::encode`] will return
    /// [`AuthError::EncodingKeyMissing`]. Ideal for microservices that only
    /// consume tokens, never issue them.
    pub fn from_rsa_public_pem(public_key_pem: &[u8]) -> Result<Self, AuthError> {
        let decoding_key = DecodingKey::from_rsa_pem(public_key_pem)
            .map_err(|e| AuthError::Internal(format!("RSA public key: {e}")))?;
        Ok(Self {
            config: Arc::new(JwtConfig {
                encoding_key: None,
                decoding_key,
                validation: Validation::new(Algorithm::RS256),
            }),
            blacklist: None,
        })
    }

    /// Construct a manager from a PEM-encoded EC private/public key pair (ES256).
    ///
    /// Both keys are required for signing and verification. For verify-only use,
    /// see [`JwtManager::from_ec_public_pem`].
    pub fn from_ec_pem(private_key_pem: &[u8], public_key_pem: &[u8]) -> Result<Self, AuthError> {
        let encoding_key = EncodingKey::from_ec_pem(private_key_pem)
            .map_err(|e| AuthError::Internal(format!("EC private key: {e}")))?;
        let decoding_key = DecodingKey::from_ec_pem(public_key_pem)
            .map_err(|e| AuthError::Internal(format!("EC public key: {e}")))?;
        Ok(Self {
            config: Arc::new(JwtConfig {
                encoding_key: Some(encoding_key),
                decoding_key,
                validation: Validation::new(Algorithm::ES256),
            }),
            blacklist: None,
        })
    }

    /// Construct a **verify-only** manager from a PEM-encoded EC public key (ES256).
    ///
    /// No signing key is stored; calling [`JwtManager::encode`] will return
    /// [`AuthError::EncodingKeyMissing`]. Ideal for microservices that only
    /// consume tokens, never issue them.
    pub fn from_ec_public_pem(public_key_pem: &[u8]) -> Result<Self, AuthError> {
        let decoding_key = DecodingKey::from_ec_pem(public_key_pem)
            .map_err(|e| AuthError::Internal(format!("EC public key: {e}")))?;
        Ok(Self {
            config: Arc::new(JwtConfig {
                encoding_key: None,
                decoding_key,
                validation: Validation::new(Algorithm::ES256),
            }),
            blacklist: None,
        })
    }

    /// Construct a manager from a fully custom [`JwtConfig`].
    pub fn with_config(config: JwtConfig) -> Self {
        Self {
            config: Arc::new(config),
            blacklist: None,
        }
    }

    /// Attach a revocation blacklist, returning the updated manager.
    ///
    /// ```rust,ignore
    /// let bl = TokenBlacklist::new();
    /// let manager = JwtManager::new(b"secret").with_blacklist(bl.clone());
    /// // Later: bl.revoke(jti, Some(exp));
    /// ```
    pub fn with_blacklist(mut self, blacklist: TokenBlacklist) -> Self {
        self.blacklist = Some(blacklist);
        self
    }

    /// Decode and verify a JWT, optionally checking revocation.
    ///
    /// If a [`TokenBlacklist`] is attached and `T::jti()` returns `Some(jti)`,
    /// the JTI is checked for revocation after the signature is verified.
    ///
    /// # Errors
    /// - [`AuthError::Expired`]      – the `exp` claim has passed.
    /// - [`AuthError::Revoked`]      – the JTI is on the blacklist.
    /// - [`AuthError::InvalidToken`] – signature or format error.
    pub fn decode<T>(&self, token: &str) -> Result<T, AuthError>
    where
        T: for<'de> Deserialize<'de> + HasJti,
    {
        let token_data = decode::<T>(token, &self.config.decoding_key, &self.config.validation)
            .map_err(|e| match e.kind() {
                jsonwebtoken::errors::ErrorKind::ExpiredSignature => AuthError::Expired,
                _ => AuthError::InvalidToken(e.to_string()),
            })?;

        if let Some(bl) = &self.blacklist
            && let Some(jti) = token_data.claims.jti()
            && bl.is_revoked(jti)
        {
            return Err(AuthError::Revoked);
        }

        Ok(token_data.claims)
    }

    /// Sign a set of claims, returning a compact JWT string.
    ///
    /// # Errors
    /// - [`AuthError::EncodingKeyMissing`] – no signing key is configured.
    /// - [`AuthError::Encode`]             – claims serialisation failed.
    pub fn encode<T: Serialize>(&self, claims: &T) -> Result<String, AuthError> {
        let key = self
            .config
            .encoding_key
            .as_ref()
            .ok_or(AuthError::EncodingKeyMissing)?;
        encode(&Header::default(), claims, key).map_err(|e| AuthError::Encode(e.to_string()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug, PartialEq, Serialize, Deserialize)]
    struct TestClaims {
        sub: String,
        exp: u64,
    }

    // Opt out of revocation — no `jti` field.
    impl HasJti for TestClaims {}

    fn far_future_exp() -> u64 {
        // 9999-01-01 00:00:00 UTC as a Unix timestamp
        253_370_764_800_u64
    }

    #[test]
    fn test_encode_decode_roundtrip() {
        let mgr = JwtManager::new(b"test-secret-key");
        let claims = TestClaims {
            sub: "user-42".to_string(),
            exp: far_future_exp(),
        };
        let token = mgr.encode(&claims).expect("encode should succeed");
        assert!(!token.is_empty());
        let decoded: TestClaims = mgr.decode(&token).expect("decode should succeed");
        assert_eq!(decoded, claims);
    }

    #[test]
    fn test_decode_wrong_secret_fails() {
        let mgr_sign = JwtManager::new(b"correct-secret");
        let mgr_verify = JwtManager::new(b"wrong-secret");
        let claims = TestClaims {
            sub: "user-1".to_string(),
            exp: far_future_exp(),
        };
        let token = mgr_sign.encode(&claims).expect("encode must succeed");
        let result: Result<TestClaims, _> = mgr_verify.decode(&token);
        assert!(result.is_err(), "decode with wrong secret should fail");
    }

    #[test]
    fn test_decode_invalid_token_fails() {
        let mgr = JwtManager::new(b"any-secret");
        let result: Result<TestClaims, _> = mgr.decode("not.a.jwt");
        assert!(result.is_err(), "decode of garbage should fail");
    }

    #[test]
    fn test_decode_mangled_token_fails() {
        let mgr = JwtManager::new(b"secret");
        let claims = TestClaims {
            sub: "u".to_string(),
            exp: far_future_exp(),
        };
        let mut token = mgr.encode(&claims).expect("encode ok");
        // flip last byte of signature
        let last = token.pop().unwrap();
        token.push(if last == 'A' { 'B' } else { 'A' });
        let result: Result<TestClaims, _> = mgr.decode(&token);
        assert!(result.is_err(), "mangled token should fail");
    }

    #[test]
    fn test_clone_shares_key() {
        let mgr1 = JwtManager::new(b"shared-key");
        let mgr2 = mgr1.clone();
        let claims = TestClaims {
            sub: "u".to_string(),
            exp: far_future_exp(),
        };
        let token = mgr1.encode(&claims).unwrap();
        // mgr2 must decode tokens signed with mgr1 (shares Arc<JwtConfig>)
        let decoded: TestClaims = mgr2.decode(&token).expect("clone should decode");
        assert_eq!(decoded.sub, "u");
    }

    #[test]
    fn test_encode_without_key_returns_error() {
        let config = JwtConfig {
            decoding_key: DecodingKey::from_secret(b"secret"),
            encoding_key: None,
            validation: Validation::new(Algorithm::HS256),
        };
        let mgr = JwtManager::with_config(config);
        let claims = TestClaims {
            sub: "x".to_string(),
            exp: far_future_exp(),
        };
        let result = mgr.encode(&claims);
        assert!(
            matches!(result, Err(AuthError::EncodingKeyMissing)),
            "expected EncodingKeyMissing, got {result:?}"
        );
    }

    #[test]
    fn test_revoked_token_rejected() {
        use crate::revocation::TokenBlacklist;

        #[derive(Debug, PartialEq, Serialize, Deserialize)]
        struct ClaimsWithJti {
            sub: String,
            jti: String,
            exp: u64,
        }
        impl HasJti for ClaimsWithJti {
            fn jti(&self) -> Option<&str> {
                Some(&self.jti)
            }
        }

        let blacklist = TokenBlacklist::new();
        let mgr = JwtManager::new(b"s").with_blacklist(blacklist.clone());
        let claims = ClaimsWithJti {
            sub: "u".into(),
            jti: "unique-jti-1".into(),
            exp: far_future_exp(),
        };
        let token = mgr.encode(&claims).unwrap();

        // Valid before revocation.
        mgr.decode::<ClaimsWithJti>(&token)
            .expect("should be valid before revocation");

        // Revoke and verify rejection.
        blacklist.revoke("unique-jti-1".into(), None);
        let result = mgr.decode::<ClaimsWithJti>(&token);
        assert!(
            matches!(result, Err(AuthError::Revoked)),
            "revoked token should be rejected, got {result:?}"
        );
    }

    #[test]
    fn test_expired_token_returns_expired_error() {
        let mgr = JwtManager::new(b"secret");
        // exp = 1 — Unix epoch 1970-01-01, long past
        let claims = serde_json::json!({ "sub": "u", "exp": 1_u64 });
        let token = encode(
            &Header::default(),
            &claims,
            &EncodingKey::from_secret(b"secret"),
        )
        .unwrap();
        let result: Result<TestClaims, _> = mgr.decode(&token);
        assert!(
            matches!(result, Err(AuthError::Expired)),
            "expected Expired, got {result:?}"
        );
    }
}