plakous-auth 0.0.1

Provider-neutral secure authentication foundations for Plakous
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
//! JWKS fetch/cache/single-flight orchestrator for subscription OIDC.
//!
//! [`JwksCache`] holds the async mutex across the fetch (single-flight) and
//! bounds both the mutex wait and each fetch by the caller's deadline; dialects
//! supply fetch + crypto validation. Shared ladder: purge negatives → fresh hit
//! → negative miss → fetch rounds → install → hit or record negative.

use std::collections::BTreeMap;
use std::future::Future;
use std::time::{Duration, Instant};

/// In-process JWKS snapshot + negative cache.
#[derive(Clone, Debug)]
pub(crate) struct JwksCacheState<K> {
    /// Validated signing keys keyed by `kid`.
    pub keys: BTreeMap<String, K>,
    /// Positive snapshot expiry (`None` = empty).
    pub expires_at: Option<Instant>,
    /// Negative cache: `kid` → expiry.
    pub negative: BTreeMap<String, Instant>,
    /// Cap on negative entries (oldest expiry evicted first).
    pub max_negative: usize,
}

impl<K> Default for JwksCacheState<K> {
    fn default() -> Self {
        Self {
            keys: BTreeMap::new(),
            expires_at: None,
            negative: BTreeMap::new(),
            max_negative: 64,
        }
    }
}

impl<K> JwksCacheState<K> {
    /// Empty cache with a negative-entry cap.
    #[must_use]
    pub fn with_max_negative(max_negative: usize) -> Self {
        Self {
            max_negative,
            ..Self::default()
        }
    }

    /// Non-empty snapshot still within positive TTL.
    #[must_use]
    pub fn is_fresh(&self, now: Instant) -> bool {
        self.expires_at.is_some_and(|expires_at| expires_at > now) && !self.keys.is_empty()
    }

    /// Drop expired negatives.
    pub fn purge_negatives(&mut self, now: Instant) {
        self.negative.retain(|_, expires_at| *expires_at > now);
    }

    /// Install a newly fetched positive snapshot.
    pub fn install_snapshot(
        &mut self,
        keys: BTreeMap<String, K>,
        expires_at: Instant,
        clear_negatives: bool,
    ) {
        self.keys = keys;
        self.expires_at = Some(expires_at);
        if clear_negatives {
            self.negative.clear();
        }
    }

    /// Record a kid miss.
    pub fn record_negative(&mut self, kid: &str, expires_at: Instant) {
        if self.max_negative == 0 {
            return;
        }
        if self.negative.len() >= self.max_negative
            && !self.negative.contains_key(kid)
            && let Some(oldest) = self
                .negative
                .iter()
                .min_by_key(|(_, expiry)| **expiry)
                .map(|(key, _)| key.clone())
        {
            self.negative.remove(&oldest);
        }
        self.negative.insert(kid.to_owned(), expires_at);
    }

    /// `kid` is negatively cached and not expired.
    #[must_use]
    pub fn is_negative(&self, kid: &str, now: Instant) -> bool {
        self.negative
            .get(kid)
            .is_some_and(|expires_at| *expires_at > now)
    }
}

/// Validated JWKS snapshot from a dialect fetch.
#[derive(Clone, Debug)]
pub struct FetchedJwks<K> {
    /// Accepted keys keyed by `kid`.
    pub keys: BTreeMap<String, K>,
    /// Absolute positive-cache expiry.
    pub expires_at: Instant,
}

/// Dialect policy for the shared JWKS resolve ladder.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct JwksResolvePolicy {
    /// Fetch rounds when no fresh snapshot exists.
    pub rounds_when_stale: u32,
    /// Fetch rounds when a fresh snapshot lacks the kid.
    pub rounds_when_fresh_miss: u32,
    /// Clear negatives on every successful fetch (xAI generation semantics).
    pub clear_negatives_on_fetch: bool,
    /// When true, negatives apply only while a fresh snapshot exists.
    pub negative_requires_fresh_snapshot: bool,
    /// Negative-cache TTL after an unresolved kid.
    pub negative_ttl: Duration,
}

impl JwksResolvePolicy {
    /// Codex-style: time-based negatives that survive fetches; 2 stale rounds.
    #[must_use]
    pub const fn codex() -> Self {
        Self {
            rounds_when_stale: 2,
            rounds_when_fresh_miss: 1,
            clear_negatives_on_fetch: false,
            negative_requires_fresh_snapshot: false,
            negative_ttl: Duration::from_secs(30),
        }
    }

    /// SuperGrok-style: clear negatives on fetch; single fetch round.
    #[must_use]
    pub const fn xai() -> Self {
        Self {
            rounds_when_stale: 1,
            rounds_when_fresh_miss: 1,
            clear_negatives_on_fetch: true,
            negative_requires_fresh_snapshot: true,
            // Matches positive TTL so a generation miss dies with the snapshot.
            negative_ttl: Duration::from_secs(600),
        }
    }
}

/// Resolve one signing key by `kid` against a locked cache state.
///
/// `fetch` runs while [`JwksCache`] still holds the cache mutex (single-flight).
///
/// # Errors
///
/// Propagates `fetch` errors and maps miss/negative through the provided closures.
pub(crate) async fn resolve_signing_key<K, E, F, Fut>(
    state: &mut JwksCacheState<K>,
    kid: &str,
    policy: &JwksResolvePolicy,
    negative_error: impl FnOnce() -> E,
    miss_error: impl FnOnce() -> E,
    mut fetch: F,
) -> Result<K, E>
where
    K: Clone,
    F: FnMut() -> Fut,
    Fut: Future<Output = Result<FetchedJwks<K>, E>>,
{
    let now = Instant::now();
    state.purge_negatives(now);
    let fresh = state.is_fresh(now);
    if fresh && let Some(key) = state.keys.get(kid).cloned() {
        return Ok(key);
    }
    let honor_negative =
        state.is_negative(kid, now) && (fresh || !policy.negative_requires_fresh_snapshot);
    if honor_negative {
        return Err(negative_error());
    }

    let rounds = if fresh {
        policy.rounds_when_fresh_miss
    } else {
        policy.rounds_when_stale
    }
    .max(1);
    for _ in 0..rounds {
        let fetched = fetch().await?;
        state.install_snapshot(
            fetched.keys,
            fetched.expires_at,
            policy.clear_negatives_on_fetch,
        );
        if let Some(key) = state.keys.get(kid).cloned() {
            state.negative.remove(kid);
            return Ok(key);
        }
    }

    state.record_negative(kid, Instant::now() + policy.negative_ttl);
    Err(miss_error())
}

/// Single-flight JWKS cache shared by every subscription OIDC dialect.
///
/// The cancellation-safe async mutex is held across the fetch, so concurrent
/// resolvers of a missing key wait for one fetch instead of issuing their own.
/// Cancellation drops the guard, so no stuck in-flight state can strand later
/// callers.
#[derive(Debug)]
pub struct JwksCache<K> {
    state: tokio::sync::Mutex<JwksCacheState<K>>,
    policy: JwksResolvePolicy,
}

impl<K: Clone> JwksCache<K> {
    /// Empty cache that keeps at most `max_negative` unresolved key ids.
    #[must_use]
    pub fn new(policy: JwksResolvePolicy, max_negative: usize) -> Self {
        Self {
            state: tokio::sync::Mutex::new(JwksCacheState::with_max_negative(max_negative)),
            policy,
        }
    }

    /// Resolves `kid`, fetching under the single flight when needed.
    ///
    /// With a `deadline`, the wait for an in-flight fetch and every fetch
    /// round start only while time remains; the wait itself is bounded by the
    /// remaining time. `expired` names the failure once the deadline passed.
    /// A negative-cached or still-unresolved `kid` fails with `unresolved`.
    ///
    /// # Errors
    ///
    /// `expired`, `unresolved`, or the error `fetch` returns.
    pub async fn resolve<E, F, Fut>(
        &self,
        kid: &str,
        deadline: Option<Instant>,
        expired: impl Fn() -> E,
        unresolved: impl Fn() -> E,
        mut fetch: F,
    ) -> Result<K, E>
    where
        F: FnMut() -> Fut,
        Fut: Future<Output = Result<FetchedJwks<K>, E>>,
    {
        let remaining = || match deadline {
            None => Ok(None),
            Some(deadline) => crate::oauth_workflow::remaining_until(deadline, Instant::now())
                .map(Some)
                .ok_or_else(&expired),
        };
        let mut state = match remaining()? {
            None => self.state.lock().await,
            Some(wait) => tokio::time::timeout(wait, self.state.lock())
                .await
                .map_err(|_| expired())?,
        };
        remaining()?;
        resolve_signing_key(
            &mut state,
            kid,
            &self.policy,
            &unresolved,
            &unresolved,
            || {
                let started = remaining();
                let fetched = fetch();
                async move {
                    started?;
                    fetched.await
                }
            },
        )
        .await
    }
}

/// Parse `Cache-Control: max-age=` (first matching directive).
#[must_use]
pub fn parse_cache_control_max_age(value: &str) -> Option<Duration> {
    value.split(',').find_map(|directive| {
        let (name, value) = directive.trim().split_once('=')?;
        name.eq_ignore_ascii_case("max-age")
            .then(|| value.trim_matches('"').parse::<u64>().ok())
            .flatten()
            .map(Duration::from_secs)
    })
}

#[cfg(test)]
#[allow(
    clippy::expect_used,
    clippy::unwrap_used,
    reason = "unit tests may assert with expect/unwrap"
)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[derive(Clone, Debug, PartialEq, Eq)]
    struct TestKey(u8);

    #[tokio::test]
    async fn fresh_hit_avoids_fetch() {
        let mut state = JwksCacheState::default();
        state.install_snapshot(
            BTreeMap::from([("k1".into(), TestKey(1))]),
            Instant::now() + Duration::from_secs(60),
            true,
        );
        let fetches = AtomicUsize::new(0);
        let key = resolve_signing_key(
            &mut state,
            "k1",
            &JwksResolvePolicy::codex(),
            || "negative",
            || "miss",
            || async {
                fetches.fetch_add(1, Ordering::SeqCst);
                Err("fetch")
            },
        )
        .await
        .unwrap();
        assert_eq!(key, TestKey(1));
        assert_eq!(fetches.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn miss_records_negative_and_second_call_short_circuits() {
        let mut state = JwksCacheState::default();
        let fetches = AtomicUsize::new(0);
        let policy = JwksResolvePolicy::xai();
        assert_eq!(
            resolve_signing_key(
                &mut state,
                "missing",
                &policy,
                || "negative",
                || "miss",
                || async {
                    fetches.fetch_add(1, Ordering::SeqCst);
                    Ok(FetchedJwks {
                        keys: BTreeMap::from([("other".into(), TestKey(9))]),
                        expires_at: Instant::now() + Duration::from_secs(60),
                    })
                },
            )
            .await,
            Err("miss")
        );
        assert!(state.is_negative("missing", Instant::now()));
        assert_eq!(
            resolve_signing_key(
                &mut state,
                "missing",
                &policy,
                || "negative",
                || "miss",
                || async {
                    fetches.fetch_add(1, Ordering::SeqCst);
                    Err("fetch")
                },
            )
            .await,
            Err("negative")
        );
        assert_eq!(fetches.load(Ordering::SeqCst), 1);
    }

    fn snapshot(kid: &str, key: u8) -> FetchedJwks<TestKey> {
        FetchedJwks {
            keys: BTreeMap::from([(kid.into(), TestKey(key))]),
            expires_at: Instant::now() + Duration::from_secs(60),
        }
    }

    #[tokio::test]
    async fn deadline_bounds_the_wait_behind_an_in_flight_fetch() {
        let cache = std::sync::Arc::new(JwksCache::new(JwksResolvePolicy::xai(), 8));
        let (fetch_started, started) = tokio::sync::oneshot::channel();
        let (release_fetch, release) = tokio::sync::oneshot::channel::<()>();
        let holder = tokio::spawn({
            let cache = std::sync::Arc::clone(&cache);
            async move {
                let mut signal = Some((fetch_started, release));
                cache
                    .resolve(
                        "k1",
                        None,
                        || "expired",
                        || "unresolved",
                        || {
                            let (fetch_started, release) = signal.take().unwrap();
                            async move {
                                fetch_started.send(()).unwrap();
                                release.await.unwrap();
                                Ok(snapshot("k1", 1))
                            }
                        },
                    )
                    .await
            }
        });
        started.await.unwrap();
        let begun = Instant::now();
        let waited = cache
            .resolve(
                "k1",
                Some(Instant::now() + Duration::from_millis(50)),
                || "expired",
                || "unresolved",
                || async { Err("fetch") },
            )
            .await;
        assert_eq!(waited, Err("expired"));
        assert!(begun.elapsed() < Duration::from_secs(5));
        release_fetch.send(()).unwrap();
        assert_eq!(holder.await.unwrap(), Ok(TestKey(1)));
    }

    #[tokio::test]
    async fn expired_deadline_refuses_before_fetching() {
        let cache = JwksCache::new(JwksResolvePolicy::codex(), 8);
        let fetches = AtomicUsize::new(0);
        let result = cache
            .resolve(
                "k1",
                Some(Instant::now()),
                || "expired",
                || "unresolved",
                || async {
                    fetches.fetch_add(1, Ordering::SeqCst);
                    Ok(snapshot("k1", 1))
                },
            )
            .await;
        assert_eq!(result, Err("expired"));
        assert_eq!(fetches.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn concurrent_misses_share_one_fetch() {
        let cache = JwksCache::new(JwksResolvePolicy::codex(), 8);
        let fetches = AtomicUsize::new(0);
        let resolve = || {
            cache.resolve(
                "k1",
                Some(Instant::now() + Duration::from_secs(30)),
                || "expired",
                || "unresolved",
                || async {
                    fetches.fetch_add(1, Ordering::SeqCst);
                    tokio::task::yield_now().await;
                    Ok(snapshot("k1", 1))
                },
            )
        };
        let (first, second) = tokio::join!(resolve(), resolve());
        assert_eq!(first, Ok(TestKey(1)));
        assert_eq!(second, Ok(TestKey(1)));
        assert_eq!(fetches.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn parse_max_age_reads_first_directive() {
        assert_eq!(
            parse_cache_control_max_age("public, max-age=120, must-revalidate"),
            Some(Duration::from_secs(120))
        );
        assert_eq!(parse_cache_control_max_age("no-store"), None);
    }
}