phoenix-crypto 0.1.0

JWT, blind indexes, authenticated encryption, and password hashing for Phoenix
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
use std::{
    collections::HashMap,
    marker::PhantomData,
    ops::Deref,
    sync::Arc,
    time::{Duration, SystemTime, UNIX_EPOCH},
};

use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use http::{HeaderValue, header};
use jsonwebtoken::{
    Algorithm, DecodingKey, EncodingKey, Header, Validation, decode, decode_header, encode,
};
use phoenix_http::{
    BoxFuture, FromRequest, IntoResponse, Middleware, Next, Request, Response, StatusCode,
};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use thiserror::Error;
use zeroize::Zeroizing;

const MIN_SECRET_BYTES: usize = 32;

/// Standard JWT claims plus an application-defined payload.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct JwtClaims<T> {
    pub sub: String,
    pub jti: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sid: Option<String>,
    pub exp: u64,
    pub iat: u64,
    pub nbf: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub iss: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aud: Option<String>,
    #[serde(flatten)]
    pub custom: T,
}

/// A symmetric JWT key identified by `kid` for safe rotation.
#[derive(Clone)]
pub struct JwtKey {
    id: String,
    secret: Zeroizing<Vec<u8>>,
}

impl JwtKey {
    /// Construct an HS256 key. Secrets shorter than 256 bits are rejected.
    ///
    /// # Errors
    ///
    /// Returns an error for an empty key ID or a secret shorter than 32 bytes.
    pub fn new(id: impl Into<String>, secret: impl AsRef<[u8]>) -> Result<Self, JwtError> {
        let id = id.into();
        if id.trim().is_empty() {
            return Err(JwtError::InvalidKeyId);
        }
        if secret.as_ref().len() < MIN_SECRET_BYTES {
            return Err(JwtError::WeakKey);
        }
        Ok(Self {
            id,
            secret: Zeroizing::new(secret.as_ref().to_vec()),
        })
    }

    #[must_use]
    pub fn id(&self) -> &str {
        &self.id
    }
}

impl std::fmt::Debug for JwtKey {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("JwtKey")
            .field("id", &self.id)
            .field("secret", &"[REDACTED]")
            .finish()
    }
}

#[derive(Clone, Debug)]
pub struct JwtConfig {
    pub ttl: Duration,
    pub leeway: Duration,
    pub issuer: Option<String>,
    pub audience: Option<String>,
}

impl Default for JwtConfig {
    fn default() -> Self {
        Self {
            ttl: Duration::from_mins(15),
            leeway: Duration::from_secs(30),
            issuer: None,
            audience: None,
        }
    }
}

impl JwtConfig {
    #[must_use]
    pub const fn new(ttl: Duration) -> Self {
        Self {
            ttl,
            leeway: Duration::from_secs(30),
            issuer: None,
            audience: None,
        }
    }

    #[must_use]
    pub const fn leeway(mut self, leeway: Duration) -> Self {
        self.leeway = leeway;
        self
    }

    #[must_use]
    pub fn issuer(mut self, issuer: impl Into<String>) -> Self {
        self.issuer = Some(issuer.into());
        self
    }

    #[must_use]
    pub fn audience(mut self, audience: impl Into<String>) -> Self {
        self.audience = Some(audience.into());
        self
    }
}

/// HS256 JWT issuer and verifier with explicit key rotation.
#[derive(Clone, Debug)]
pub struct JwtManager {
    active_key_id: String,
    keys: HashMap<String, JwtKey>,
    config: JwtConfig,
}

impl JwtManager {
    /// Create a manager with one active signing and verification key.
    ///
    /// # Errors
    ///
    /// Returns an error when TTL is zero or configured issuer/audience is empty.
    pub fn new(active: JwtKey, config: JwtConfig) -> Result<Self, JwtError> {
        validate_config(&config)?;
        let active_key_id = active.id.clone();
        Ok(Self {
            active_key_id,
            keys: HashMap::from([(active.id.clone(), active)]),
            config,
        })
    }

    /// Add an old or future verification key without changing the signing key.
    #[must_use]
    pub fn with_verification_key(mut self, key: JwtKey) -> Self {
        self.keys.insert(key.id.clone(), key);
        self
    }

    /// Lifetime of newly issued access tokens.
    #[must_use]
    pub const fn access_token_ttl(&self) -> Duration {
        self.config.ttl
    }

    /// Issue a signed JWT using the active key and configured TTL.
    ///
    /// # Errors
    ///
    /// Returns an error when the clock or serializer fails.
    pub fn issue<T: Serialize>(
        &self,
        subject: impl Into<String>,
        custom: T,
    ) -> Result<String, JwtError> {
        self.issue_at(subject.into(), custom, unix_timestamp()?)
    }

    fn issue_at<T: Serialize>(
        &self,
        subject: String,
        custom: T,
        now: u64,
    ) -> Result<String, JwtError> {
        self.issue_at_in_family(subject, custom, now, None)
            .map(|(token, _)| token)
    }

    pub(crate) fn issue_at_in_family<T: Serialize>(
        &self,
        subject: String,
        custom: T,
        now: u64,
        session_id: Option<String>,
    ) -> Result<(String, JwtClaims<T>), JwtError> {
        if subject.trim().is_empty() {
            return Err(JwtError::InvalidSubject);
        }
        if session_id
            .as_ref()
            .is_some_and(|session_id| session_id.trim().is_empty())
        {
            return Err(JwtError::InvalidSessionId);
        }
        validate_custom_claims(&custom)?;
        let key = self
            .keys
            .get(&self.active_key_id)
            .ok_or(JwtError::UnknownKey)?;
        let claims = JwtClaims {
            sub: subject,
            jti: random_identifier(),
            sid: session_id,
            exp: now.saturating_add(self.config.ttl.as_secs()),
            iat: now,
            nbf: now,
            iss: self.config.issuer.clone(),
            aud: self.config.audience.clone(),
            custom,
        };
        let mut header = Header::new(Algorithm::HS256);
        header.kid = Some(key.id.clone());
        let token = encode(
            &header,
            &claims,
            &EncodingKey::from_secret(key.secret.as_ref()),
        )
        .map_err(JwtError::Token)?;
        Ok((token, claims))
    }

    /// Verify signature, algorithm, key ID, time claims, issuer, and audience.
    ///
    /// # Errors
    ///
    /// Returns an error for malformed, unknown-key, expired, or policy-invalid tokens.
    pub fn verify<T: DeserializeOwned>(&self, token: &str) -> Result<JwtClaims<T>, JwtError> {
        let header = decode_header(token).map_err(JwtError::Token)?;
        if header.alg != Algorithm::HS256 {
            return Err(JwtError::InvalidAlgorithm);
        }
        let key_id = header.kid.ok_or(JwtError::MissingKeyId)?;
        let key = self.keys.get(&key_id).ok_or(JwtError::UnknownKey)?;
        let mut validation = Validation::new(Algorithm::HS256);
        validation.leeway = self.config.leeway.as_secs();
        validation.validate_nbf = true;
        let mut required = vec!["exp", "iat", "nbf", "sub", "jti"];
        if let Some(issuer) = &self.config.issuer {
            validation.set_issuer(&[issuer]);
            required.push("iss");
        }
        if let Some(audience) = &self.config.audience {
            validation.set_audience(&[audience]);
            required.push("aud");
        }
        validation.set_required_spec_claims(&required);
        let claims = decode::<JwtClaims<T>>(
            token,
            &DecodingKey::from_secret(key.secret.as_ref()),
            &validation,
        )
        .map(|data| data.claims)
        .map_err(JwtError::Token)?;
        if claims.sub.trim().is_empty() {
            return Err(JwtError::InvalidSubject);
        }
        if claims.jti.trim().is_empty() {
            return Err(JwtError::InvalidTokenId);
        }
        if claims
            .sid
            .as_ref()
            .is_some_and(|session_id| session_id.trim().is_empty())
        {
            return Err(JwtError::InvalidSessionId);
        }
        Ok(claims)
    }
}

fn validate_custom_claims(custom: &impl Serialize) -> Result<(), JwtError> {
    const RESERVED: &[&str] = &["sub", "jti", "sid", "exp", "iat", "nbf", "iss", "aud"];
    let value = serde_json::to_value(custom).map_err(JwtError::CustomClaims)?;
    let object = value
        .as_object()
        .ok_or(JwtError::CustomClaimsMustBeObject)?;
    if object.keys().any(|key| RESERVED.contains(&key.as_str())) {
        return Err(JwtError::ReservedClaim);
    }
    Ok(())
}

fn validate_config(config: &JwtConfig) -> Result<(), JwtError> {
    if config.ttl.is_zero() {
        return Err(JwtError::InvalidTtl);
    }
    if config
        .issuer
        .as_ref()
        .is_some_and(|value| value.trim().is_empty())
        || config
            .audience
            .as_ref()
            .is_some_and(|value| value.trim().is_empty())
    {
        return Err(JwtError::InvalidClaimPolicy);
    }
    Ok(())
}

/// Verified JWT claims extracted by [`JwtAuth`].
pub struct Jwt<T>(Arc<JwtClaims<T>>);

impl<T> Jwt<T> {
    pub(crate) fn from_verified(claims: JwtClaims<T>) -> Self {
        Self(Arc::new(claims))
    }
}

impl<T> Clone for Jwt<T> {
    fn clone(&self) -> Self {
        Self(Arc::clone(&self.0))
    }
}

impl<T> std::fmt::Debug for Jwt<T> {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.debug_tuple("Jwt").field(&"[VERIFIED]").finish()
    }
}

impl<T> Deref for Jwt<T> {
    type Target = JwtClaims<T>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> FromRequest for Jwt<T>
where
    T: Send + Sync + 'static,
{
    type Rejection = JwtRejection;

    fn from_request(request: &Request) -> Result<Self, Self::Rejection> {
        request
            .extensions()
            .get::<Self>()
            .cloned()
            .ok_or(JwtRejection)
    }
}

/// Bearer middleware that verifies a JWT and inserts typed claims into request extensions.
#[derive(Clone, Debug)]
pub struct JwtAuth<T> {
    manager: Arc<JwtManager>,
    marker: PhantomData<fn() -> T>,
}

impl<T> JwtAuth<T> {
    #[must_use]
    pub fn new(manager: impl Into<Arc<JwtManager>>) -> Self {
        Self {
            manager: manager.into(),
            marker: PhantomData,
        }
    }
}

impl<T> Middleware for JwtAuth<T>
where
    T: DeserializeOwned + Send + Sync + 'static,
{
    fn handle(&self, mut request: Request, next: Next) -> BoxFuture<Response> {
        let manager = Arc::clone(&self.manager);
        Box::pin(async move {
            let token = request
                .headers()
                .get(header::AUTHORIZATION)
                .and_then(|value| value.to_str().ok())
                .and_then(bearer_token);
            let Some(token) = token else {
                return JwtRejection.into_response();
            };
            let Ok(claims) = manager.verify::<T>(token) else {
                return JwtRejection.into_response();
            };
            request.extensions_mut().insert(Jwt::from_verified(claims));
            next.run(request).await
        })
    }
}

pub(crate) fn bearer_token(value: &str) -> Option<&str> {
    let (scheme, token) = value.split_once(' ')?;
    (scheme.eq_ignore_ascii_case("bearer")
        && !token.is_empty()
        && !token.chars().any(char::is_whitespace))
    .then_some(token)
}

#[derive(Clone, Copy, Debug, Error)]
#[error("A valid bearer token is required.")]
pub struct JwtRejection;

impl IntoResponse for JwtRejection {
    fn into_response(self) -> Response {
        let mut response = Response::text("Unauthorized").with_status(StatusCode::UNAUTHORIZED);
        response
            .headers_mut()
            .insert(header::WWW_AUTHENTICATE, HeaderValue::from_static("Bearer"));
        response
    }
}

#[derive(Debug, Error)]
pub enum JwtError {
    #[error("JWT key IDs cannot be empty")]
    InvalidKeyId,
    #[error("JWT HS256 keys must contain at least 32 bytes")]
    WeakKey,
    #[error("JWT time-to-live must be greater than zero")]
    InvalidTtl,
    #[error("JWT issuer and audience policies cannot be empty")]
    InvalidClaimPolicy,
    #[error("JWT subject cannot be empty")]
    InvalidSubject,
    #[error("JWT token ID cannot be empty")]
    InvalidTokenId,
    #[error("JWT session ID cannot be empty")]
    InvalidSessionId,
    #[error("JWT header is missing a key ID")]
    MissingKeyId,
    #[error("JWT key ID is not recognized")]
    UnknownKey,
    #[error("JWT algorithm is not allowed")]
    InvalidAlgorithm,
    #[error("JWT custom claims must serialize as an object")]
    CustomClaimsMustBeObject,
    #[error("JWT custom claims cannot redefine reserved claims")]
    ReservedClaim,
    #[error("JWT custom claim serialization failed")]
    CustomClaims(#[source] serde_json::Error),
    #[error("JWT processing failed")]
    Token(#[source] jsonwebtoken::errors::Error),
    #[error("the system clock is before the Unix epoch")]
    InvalidClock,
}

fn unix_timestamp() -> Result<u64, JwtError> {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_secs())
        .map_err(|_| JwtError::InvalidClock)
}

pub(crate) fn current_timestamp() -> Result<u64, JwtError> {
    unix_timestamp()
}

fn random_identifier() -> String {
    URL_SAFE_NO_PAD.encode(rand::random::<[u8; 16]>())
}

#[cfg(test)]
mod tests {
    use super::*;
    use phoenix_http::{Method, typed};
    use phoenix_routing::Routes;

    #[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
    struct UserClaims {
        role: String,
    }

    #[derive(Serialize)]
    struct ConflictingClaims {
        sub: String,
    }

    fn manager(key_id: &str, byte: u8) -> JwtManager {
        JwtManager::new(
            JwtKey::new(key_id, [byte; 32]).unwrap(),
            JwtConfig::new(Duration::from_mins(5))
                .leeway(Duration::ZERO)
                .issuer("phoenix.test")
                .audience("api.test"),
        )
        .unwrap()
    }

    #[test]
    fn round_trips_and_rotates_keys_without_exposing_secrets() {
        let old = JwtKey::new("old", [7_u8; 32]).unwrap();
        assert!(format!("{old:?}").contains("[REDACTED]"));
        let old_manager = JwtManager::new(old.clone(), JwtConfig::default()).unwrap();
        let token = old_manager
            .issue(
                "user-1",
                UserClaims {
                    role: "admin".to_owned(),
                },
            )
            .unwrap();
        let rotated = JwtManager::new(
            JwtKey::new("current", [9_u8; 32]).unwrap(),
            JwtConfig::default(),
        )
        .unwrap()
        .with_verification_key(old);

        let claims = rotated.verify::<UserClaims>(&token).unwrap();
        assert_eq!(claims.sub, "user-1");
        assert_eq!(claims.custom.role, "admin");
    }

    #[test]
    fn rejects_weak_unknown_and_expired_tokens() {
        assert!(matches!(
            JwtKey::new("weak", b"short"),
            Err(JwtError::WeakKey)
        ));
        let active_manager = manager("active", 3);
        let expired = active_manager
            .issue_at(
                "user-1".to_owned(),
                UserClaims {
                    role: "member".to_owned(),
                },
                1,
            )
            .unwrap();
        assert!(matches!(
            active_manager.verify::<UserClaims>(&expired),
            Err(JwtError::Token(_))
        ));

        let foreign = manager("foreign", 4)
            .issue(
                "user-1",
                UserClaims {
                    role: "member".to_owned(),
                },
            )
            .unwrap();
        assert!(matches!(
            active_manager.verify::<UserClaims>(&foreign),
            Err(JwtError::UnknownKey)
        ));
        assert!(matches!(
            active_manager.issue(
                "user-1",
                ConflictingClaims {
                    sub: "shadow".to_owned()
                }
            ),
            Err(JwtError::ReservedClaim)
        ));
        assert!(matches!(
            active_manager.issue(
                " ",
                UserClaims {
                    role: "member".to_owned(),
                }
            ),
            Err(JwtError::InvalidSubject)
        ));
        assert!(matches!(
            active_manager.issue_at_in_family(
                "user-1".to_owned(),
                UserClaims {
                    role: "member".to_owned(),
                },
                1,
                Some(String::new()),
            ),
            Err(JwtError::InvalidSessionId)
        ));
    }

    #[tokio::test]
    async fn bearer_middleware_inserts_typed_verified_claims() {
        let manager = manager("active", 5);
        let token = manager
            .issue(
                "user-7",
                UserClaims {
                    role: "editor".to_owned(),
                },
            )
            .unwrap();
        let handler = typed(|claims: Jwt<UserClaims>| async move {
            format!("{}:{}", claims.sub, claims.custom.role)
        });
        let router = Routes::new()
            .get("/private", handler)
            .with_middleware(JwtAuth::<UserClaims>::new(Arc::new(manager)))
            .build()
            .unwrap();

        let missing = router
            .handle(Request::new(Method::GET, "/private".parse().unwrap()))
            .await;
        assert_eq!(missing.status(), StatusCode::UNAUTHORIZED);
        assert_eq!(missing.headers()[header::WWW_AUTHENTICATE], "Bearer");

        let mut request = Request::new(Method::GET, "/private".parse().unwrap());
        request.headers_mut().insert(
            header::AUTHORIZATION,
            HeaderValue::from_str(&format!("Bearer {token}")).unwrap(),
        );
        let response = router.handle(request).await;
        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(response.body(), "user-7:editor");
    }
}