chronix-security 0.1.0

Security layer for Chronix โ€” authentication, Cedar authorization, audit trail, and multi-tenancy
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
//! OIDC discovery and JWKS caching.
//!
//! Fetches JSON Web Key Sets from `OpenID` Connect providers and caches
//! the parsed decoding keys with a configurable TTL.
//!
//! # TLS Trust Model
//!
//! OIDC/JWKS requests use the system's default TLS trust store (via
//! `reqwest` / `rustls` / `native-tls`) rather than certificate pinning.
//! This is the industry-standard approach for OIDC because:
//!
//! - Identity providers rotate TLS certificates independently of the
//!   relying party, making static pins fragile and operationally risky.
//! - The security guarantee comes from the **signed JWT** itself (RS256/
//!   ES256 signature verified against JWKS keys), not from the transport
//!   that delivered the JWKS.
//! - Certificate Transparency and CAA records provide equivalent
//!   protection against mis-issuance without the brittleness of pinning.
//!
//! Operators who require additional transport-layer assurance can
//! configure a custom `reqwest::Client` with certificate pinning or
//! mutual TLS outside this crate.
//!
//! # Example
//!
//! ```no_run
//! use chronix_security::auth::oidc::JwksCache;
//! use std::time::Duration;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let cache = JwksCache::from_oidc_issuer(
//!     "https://accounts.google.com",
//!     Duration::from_secs(3600),
//! ).await?;
//!
//! let key = cache.get_key("my-kid").await?;
//! # Ok(())
//! # }
//! ```

use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};

use jsonwebtoken::jwk::{Jwk, JwkSet};
use jsonwebtoken::{Algorithm, DecodingKey};
use parking_lot::RwLock;
use serde::Deserialize;
use tokio::sync::Mutex as AsyncMutex;

use crate::auth::error::AuthError;

/// Default HTTP timeout for OIDC/JWKS requests.
const HTTP_TIMEOUT: Duration = Duration::from_secs(10);

// ---------------------------------------------------------------------------
// OIDC discovery
// ---------------------------------------------------------------------------

/// Subset of the `OpenID` Connect discovery document we care about.
#[derive(Debug, Deserialize)]
struct OidcDiscoveryDocument {
    /// The JWKS endpoint URL.
    jwks_uri: String,
    /// The issuer identifier โ€” validated per OIDC Discovery ยง4.3.
    issuer: String,
}

/// The TLS configuration for this crate's outbound HTTPS requests.
///
/// **This is a library, so it does not install a process-global provider.**
/// `CryptoProvider::install_default` sets process-wide state and silently
/// loses to whoever called it first: a crate that installs `ring` on its first
/// webhook would make a later `install_default(aws_lc_rs)` in the embedding
/// application fail, and the application would get a provider it did not
/// choose with no error to read. `chronix` is published for other people to
/// embed, so that is not a theoretical objection.
///
/// Instead the provider the application installed is *used* if there is one,
/// and `ring` is the fallback only when nobody has chosen. `chronixd`
/// installs one at startup, so under the server this resolves to the server's
/// choice.
///
/// Trust anchors come from the platform verifier, matching what `reqwest`
/// would have built on its own.
fn tls_config() -> Result<rustls::ClientConfig, rustls::Error> {
    use rustls_platform_verifier::BuilderVerifierExt;

    let provider = rustls::crypto::CryptoProvider::get_default()
        .cloned()
        .unwrap_or_else(|| std::sync::Arc::new(rustls::crypto::aws_lc_rs::default_provider()));

    Ok(rustls::ClientConfig::builder_with_provider(provider)
        .with_safe_default_protocol_versions()?
        .with_platform_verifier()?
        .with_no_client_auth())
}

/// Build the HTTP client used for OIDC discovery and JWKS fetching.
///
/// **Trust anchors come from the operating system.** `reqwest` 0.13 removed
/// its bundled-roots feature and verifies with `rustls-platform-verifier`, so
/// the host's CA store decides which identity providers are reachable. For
/// this client that is the better default โ€” a corporate IdP behind an internal
/// CA works without configuration โ€” but it is an environment dependency, and
/// a container image with no `ca-certificates` package will fail every
/// discovery and JWKS fetch with a TLS error rather than a configuration one.
///
/// This is server-side only: the embedded library authenticates nothing and
/// never reaches here.
fn http_client() -> Result<reqwest::Client, AuthError> {
    let tls = tls_config()
        .map_err(|e| AuthError::Config(format!("failed to build TLS configuration: {e}")))?;
    reqwest::Client::builder()
        .timeout(HTTP_TIMEOUT)
        .use_preconfigured_tls(tls)
        .build()
        .map_err(|e| AuthError::Config(format!("failed to build HTTP client: {e}")))
}

/// Fetch the JWKS URI from an OIDC issuer's discovery endpoint.
///
/// Performs `GET {issuer}/.well-known/openid-configuration` and extracts
/// the `jwks_uri` field.
pub async fn discover_jwks_url(issuer: &str) -> Result<String, AuthError> {
    let url = format!(
        "{}/.well-known/openid-configuration",
        issuer.trim_end_matches('/')
    );

    let client = http_client()?;

    let resp =
        client.get(&url).send().await.map_err(|e| {
            AuthError::Config(format!("OIDC discovery request failed for {url}: {e}"))
        })?;

    if !resp.status().is_success() {
        return Err(AuthError::Config(format!(
            "OIDC discovery returned HTTP {}",
            resp.status()
        )));
    }

    let doc: OidcDiscoveryDocument = resp
        .json()
        .await
        .map_err(|e| AuthError::Config(format!("failed to parse OIDC discovery document: {e}")))?;

    // OIDC Discovery ยง4.3: issuer in the document MUST match the expected
    // issuer URL (with trailing-slash normalization).
    let expected = issuer.trim_end_matches('/');
    let actual = doc.issuer.trim_end_matches('/');
    if expected != actual {
        return Err(AuthError::Config(format!(
            "OIDC issuer mismatch: expected {expected}, document reports {actual}"
        )));
    }

    Ok(doc.jwks_uri)
}

// ---------------------------------------------------------------------------
// JWKS fetching & parsing
// ---------------------------------------------------------------------------

/// Fetch a [`JwkSet`] from a URL.
pub async fn fetch_jwks(url: &str) -> Result<JwkSet, AuthError> {
    let client = http_client()?;

    let resp = client
        .get(url)
        .send()
        .await
        .map_err(|e| AuthError::Config(format!("JWKS fetch failed for {url}: {e}")))?;

    if !resp.status().is_success() {
        return Err(AuthError::Config(format!(
            "JWKS endpoint returned HTTP {}",
            resp.status()
        )));
    }

    resp.json::<JwkSet>()
        .await
        .map_err(|e| AuthError::Config(format!("failed to parse JWKS: {e}")))
}

/// Convert a [`Jwk`] into a [`DecodingKey`].
fn decoding_key_from_jwk(jwk: &Jwk) -> Result<DecodingKey, AuthError> {
    DecodingKey::from_jwk(jwk)
        .map_err(|e| AuthError::Config(format!("failed to create decoding key from JWK: {e}")))
}

/// Determine the [`Algorithm`] for a JWK, defaulting to RS256.
fn algorithm_for_jwk(jwk: &Jwk) -> Algorithm {
    jwk.common
        .key_algorithm
        .map_or(Algorithm::RS256, |ka| match ka {
            jsonwebtoken::jwk::KeyAlgorithm::RS256 => Algorithm::RS256,
            jsonwebtoken::jwk::KeyAlgorithm::RS384 => Algorithm::RS384,
            jsonwebtoken::jwk::KeyAlgorithm::RS512 => Algorithm::RS512,
            jsonwebtoken::jwk::KeyAlgorithm::ES256 => Algorithm::ES256,
            jsonwebtoken::jwk::KeyAlgorithm::ES384 => Algorithm::ES384,
            _ => Algorithm::RS256,
        })
}

// ---------------------------------------------------------------------------
// Cached key entry
// ---------------------------------------------------------------------------

/// A single cached key with its algorithm.
#[derive(Clone)]
pub struct CachedKey {
    /// The decoding key.
    pub key: DecodingKey,
    /// The algorithm associated with this key.
    pub algorithm: Algorithm,
}

impl std::fmt::Debug for CachedKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CachedKey")
            .field("algorithm", &self.algorithm)
            .finish_non_exhaustive()
    }
}

// ---------------------------------------------------------------------------
// JwksCache
// ---------------------------------------------------------------------------

/// Inner cache state protected by a lock.
struct CacheInner {
    /// `kid` โ†’ cached key mapping.
    keys: HashMap<String, CachedKey>,
    /// When the cache was last populated, `None` if never fetched.
    fetched_at: Option<Instant>,
}

/// Caches JWKS decoding keys with a configurable TTL.
///
/// Thread-safe: all access goes through a [`parking_lot::RwLock`].
/// When the cache expires, the next call to [`get_key`](JwksCache::get_key)
/// triggers a background-safe refresh. A `tokio::sync::Mutex` serialises
/// concurrent refresh attempts to prevent thundering-herd behaviour.
pub struct JwksCache {
    /// The URL from which to fetch the JWKS.
    jwks_url: String,
    /// How long fetched keys remain valid.
    ttl: Duration,
    /// Maximum duration stale keys may be served after a failed refresh.
    max_stale: Duration,
    /// The cached keys.
    inner: Arc<RwLock<CacheInner>>,
    /// Serialises concurrent refresh attempts.
    refresh_mutex: Arc<AsyncMutex<()>>,
}

impl std::fmt::Debug for JwksCache {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("JwksCache")
            .field("jwks_url", &self.jwks_url)
            .field("ttl", &self.ttl)
            .field("cached_keys", &self.inner.read().keys.len())
            .finish()
    }
}

impl JwksCache {
    /// Create a new cache that will fetch keys from `jwks_url`.
    ///
    /// Does **not** fetch keys eagerly โ€” the first call to
    /// [`get_key`](Self::get_key) or [`refresh`](Self::refresh) will
    /// populate the cache.
    #[must_use]
    pub fn new(jwks_url: String, ttl: Duration) -> Self {
        Self {
            jwks_url,
            ttl,
            max_stale: Duration::from_secs(24 * 3600), // 24 hours
            inner: Arc::new(RwLock::new(CacheInner {
                keys: HashMap::new(),
                fetched_at: None,
            })),
            refresh_mutex: Arc::new(AsyncMutex::new(())),
        }
    }

    /// Create a cache by first performing OIDC discovery on the given issuer
    /// URL, then eagerly fetching the JWKS.
    pub async fn from_oidc_issuer(issuer: &str, ttl: Duration) -> Result<Self, AuthError> {
        let jwks_url = discover_jwks_url(issuer).await?;
        let cache = Self::new(jwks_url, ttl);
        cache.refresh().await?;
        Ok(cache)
    }

    /// Create a cache pre-populated with the given keys (useful for testing).
    #[must_use]
    pub fn from_keys(keys: HashMap<String, CachedKey>) -> Self {
        Self {
            jwks_url: String::new(),
            ttl: Duration::from_secs(3600),
            max_stale: Duration::from_secs(24 * 3600),
            inner: Arc::new(RwLock::new(CacheInner {
                keys,
                fetched_at: Some(Instant::now()),
            })),
            refresh_mutex: Arc::new(AsyncMutex::new(())),
        }
    }

    /// Whether the cached keys have expired (or were never fetched).
    #[must_use]
    pub fn is_expired(&self) -> bool {
        let inner = self.inner.read();
        match inner.fetched_at {
            None => true,
            Some(t) => t.elapsed() > self.ttl,
        }
    }

    /// Number of keys currently cached.
    #[must_use]
    pub fn len(&self) -> usize {
        self.inner.read().keys.len()
    }

    /// Whether the cache is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.inner.read().keys.is_empty()
    }

    /// Fetch the JWKS from the configured URL and update the cache.
    ///
    /// On failure, previously cached keys are preserved (stale-on-error)
    /// so that transient network failures don't immediately break JWT
    /// validation for all requests.
    pub async fn refresh(&self) -> Result<(), AuthError> {
        if self.jwks_url.is_empty() {
            return Err(AuthError::Config(
                "cannot refresh: no JWKS URL configured (cache was created via from_keys)".into(),
            ));
        }

        let jwk_set = match fetch_jwks(&self.jwks_url).await {
            Ok(set) => set,
            Err(e) => {
                // If we have cached keys and they haven't exceeded max_stale,
                // log the error but keep serving stale.
                let inner = self.inner.read();
                if !inner.keys.is_empty() {
                    let too_old = inner
                        .fetched_at
                        .is_none_or(|t| t.elapsed() > self.max_stale);
                    if too_old {
                        tracing::error!(
                            url = %self.jwks_url,
                            error = %e,
                            max_stale_secs = self.max_stale.as_secs(),
                            "JWKS refresh failed and stale keys exceeded max_stale โ€” rejecting"
                        );
                        return Err(e);
                    }
                    tracing::warn!(
                        url = %self.jwks_url,
                        error = %e,
                        "JWKS refresh failed, serving stale keys"
                    );
                    return Ok(());
                }
                return Err(e);
            }
        };

        let mut new_keys = HashMap::new();
        for jwk in &jwk_set.keys {
            let kid = jwk
                .common
                .key_id
                .clone()
                .unwrap_or_else(|| "default".to_string());
            let dk = decoding_key_from_jwk(jwk)?;
            let alg = algorithm_for_jwk(jwk);
            new_keys.insert(
                kid,
                CachedKey {
                    key: dk,
                    algorithm: alg,
                },
            );
        }

        let mut inner = self.inner.write();
        inner.keys = new_keys;
        inner.fetched_at = Some(Instant::now());

        tracing::debug!(
            keys = inner.keys.len(),
            url = %self.jwks_url,
            "JWKS cache refreshed"
        );

        Ok(())
    }

    /// Ensure the cache is fresh. Serialises concurrent refresh attempts
    /// so only one HTTP request is made even under high concurrency.
    async fn ensure_fresh(&self) -> Result<(), AuthError> {
        if !self.is_expired() {
            return Ok(());
        }
        // Serialise refreshes โ€” only one task fetches at a time.
        let _guard = self.refresh_mutex.lock().await;
        // Double-check after acquiring the lock โ€” another task may have
        // already refreshed while we were waiting.
        if !self.is_expired() {
            return Ok(());
        }
        self.refresh().await
    }

    /// Get a cached key by its `kid` (key ID).
    ///
    /// If the cache has expired, it is refreshed first. Concurrent callers
    /// share a single refresh request (thundering-herd protection).
    pub async fn get_key(&self, kid: &str) -> Result<CachedKey, AuthError> {
        self.ensure_fresh().await?;

        let inner = self.inner.read();
        inner
            .keys
            .get(kid)
            .cloned()
            .ok_or_else(|| AuthError::Config(format!("no JWKS key with kid={kid}")))
    }

    /// Get all cached keys (useful when the JWT has no `kid` header).
    ///
    /// If the cache has expired, it is refreshed first. Concurrent callers
    /// share a single refresh request (thundering-herd protection).
    pub async fn get_all_keys(&self) -> Result<Vec<CachedKey>, AuthError> {
        self.ensure_fresh().await?;

        let inner = self.inner.read();
        Ok(inner.keys.values().cloned().collect())
    }

    /// Returns the JWKS URL this cache is configured to fetch from.
    #[must_use]
    pub fn jwks_url(&self) -> &str {
        &self.jwks_url
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn cache_from_keys_not_expired() {
        let mut keys = HashMap::new();
        keys.insert(
            "kid-1".to_string(),
            CachedKey {
                key: DecodingKey::from_secret(b"test"),
                algorithm: Algorithm::HS256,
            },
        );
        let cache = JwksCache::from_keys(keys);

        assert!(!cache.is_expired());
        assert_eq!(cache.len(), 1);
        assert!(!cache.is_empty());
    }

    #[test]
    fn empty_cache_is_expired() {
        let cache = JwksCache::new("https://example.com/jwks".into(), Duration::from_secs(60));
        assert!(cache.is_expired());
        assert!(cache.is_empty());
    }

    #[test]
    fn cache_expires_after_ttl() {
        let cache = JwksCache {
            jwks_url: String::new(),
            ttl: Duration::from_millis(0), // immediate expiry
            max_stale: Duration::from_secs(24 * 3600),
            inner: Arc::new(RwLock::new(CacheInner {
                keys: HashMap::new(),
                fetched_at: Some(Instant::now().checked_sub(Duration::from_secs(1)).unwrap()),
            })),
            refresh_mutex: Arc::new(AsyncMutex::new(())),
        };
        assert!(cache.is_expired());
    }

    #[tokio::test]
    async fn get_key_from_prepopulated_cache() {
        let mut keys = HashMap::new();
        keys.insert(
            "my-kid".to_string(),
            CachedKey {
                key: DecodingKey::from_secret(b"secret"),
                algorithm: Algorithm::HS256,
            },
        );
        let cache = JwksCache::from_keys(keys);

        let k = cache.get_key("my-kid").await.unwrap();
        assert_eq!(k.algorithm, Algorithm::HS256);
    }

    #[tokio::test]
    async fn get_key_missing_kid_returns_error() {
        let cache = JwksCache::from_keys(HashMap::new());
        let err = cache.get_key("nonexistent").await.unwrap_err();
        assert!(matches!(err, AuthError::Config(_)));
    }

    #[tokio::test]
    async fn get_all_keys_from_prepopulated_cache() {
        let mut keys = HashMap::new();
        keys.insert(
            "kid-a".to_string(),
            CachedKey {
                key: DecodingKey::from_secret(b"a"),
                algorithm: Algorithm::RS256,
            },
        );
        keys.insert(
            "kid-b".to_string(),
            CachedKey {
                key: DecodingKey::from_secret(b"b"),
                algorithm: Algorithm::RS256,
            },
        );
        let cache = JwksCache::from_keys(keys);

        let all = cache.get_all_keys().await.unwrap();
        assert_eq!(all.len(), 2);
    }

    #[test]
    fn discover_url_construction() {
        // Verify the URL we would construct for OIDC discovery
        let issuer = "https://accounts.google.com";
        let url = format!(
            "{}/.well-known/openid-configuration",
            issuer.trim_end_matches('/')
        );
        assert_eq!(
            url,
            "https://accounts.google.com/.well-known/openid-configuration"
        );

        // With trailing slash
        let issuer = "https://example.com/";
        let url = format!(
            "{}/.well-known/openid-configuration",
            issuer.trim_end_matches('/')
        );
        assert_eq!(url, "https://example.com/.well-known/openid-configuration");
    }

    #[test]
    fn algorithm_for_jwk_defaults_to_rs256() {
        let jwk: Jwk = serde_json::from_value(serde_json::json!({
            "kty": "RSA",
            "n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw",
            "e": "AQAB",
            "kid": "test-kid"
        }))
        .unwrap();

        let alg = algorithm_for_jwk(&jwk);
        assert_eq!(alg, Algorithm::RS256);
    }

    #[test]
    fn decoding_key_from_valid_jwk() {
        let jwk: Jwk = serde_json::from_value(serde_json::json!({
            "kty": "RSA",
            "n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw",
            "e": "AQAB",
            "kid": "test-kid"
        }))
        .unwrap();

        let result = decoding_key_from_jwk(&jwk);
        assert!(result.is_ok());
    }
}