rs-identity-model 0.6.1

Production-grade, RFC-compliant OIDC/OAuth2 client library for Rust
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
//! Async JWKS client: fetch, cache, and resolve JSON Web Keys by `kid`.

use std::time::Duration;

use reqwest::Client as HttpClient;
use reqwest::header::ACCEPT;

use crate::{IdentityError, Result};

use super::cache::Cache;
use super::key::{JsonWebKey, JsonWebKeySet};

/// Default lifetime of a cached JWK Set.
const DEFAULT_CACHE_TTL: Duration = Duration::from_secs(24 * 60 * 60);

/// Default per-request timeout so a hung server cannot block indefinitely.
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

/// Default minimum interval between automatic forced refreshes for a given
/// `jwks_uri`. Because `kid` is taken from an untrusted token header, an
/// attacker presenting tokens with random `kid` values would otherwise drive an
/// unbounded number of outbound JWKS fetches (request amplification / DoS).
/// Within the cooldown a `kid` miss returns [`IdentityError::KeyNotFound`]
/// without re-fetching. Mirrors `go/pkg/jwks` `defaultRefreshCooldown`.
const DEFAULT_REFRESH_COOLDOWN: Duration = Duration::from_secs(5);

/// Caps the JWKS response read into memory. A key set is a small JSON object;
/// this guards against a provider streaming an unbounded body
/// (memory-exhaustion DoS).
const MAX_BODY_BYTES: usize = 1 << 20; // 1 MiB

/// Default maximum number of distinct `jwks_uri` entries the cache retains. The
/// cache is keyed by `jwks_uri`; without a bound it grows one entry per distinct
/// URI forever, so a multi-tenant gateway or attacker-steered resolution could
/// accumulate entries until the process exhausts memory (unbounded-growth DoS).
/// 64 covers any realistic multi-IdP deployment while keeping worst-case memory
/// bounded. Mirrors the Python reference's `DEFAULT_MAX_CACHE_ENTRIES`.
const DEFAULT_MAX_CACHE_ENTRIES: usize = 64;

/// Environment variable overriding [`DEFAULT_MAX_CACHE_ENTRIES`]. A value of `0`
/// disables the bound (explicit unbounded escape hatch); garbage falls back to
/// the default.
const MAX_CACHE_ENTRIES_ENV: &str = "JWKS_CACHE_MAX_ENTRIES";

/// An async client that fetches, validates, caches, and resolves JSON Web Keys.
///
/// Construct one with [`JwksClient::new`] for the defaults (24h cache TTL, 30s
/// request timeout, HTTPS-only URIs) or [`JwksClient::builder`] to customise
/// them. A single client should be reused across calls so its cache and the
/// underlying connection pool are shared.
///
/// ```no_run
/// # async fn run() -> rs_identity_model::Result<()> {
/// use rs_identity_model::JwksClient;
///
/// let client = JwksClient::new();
/// let key_set = client.fetch("https://accounts.example.com/jwks").await?;
/// let key = key_set.resolve_key("rsa-sig-key")?;
/// println!("alg = {}", key.alg);
/// # Ok(())
/// # }
/// ```
pub struct JwksClient {
    http: HttpClient,
    cache: Cache,
    cache_ttl: Duration,
    timeout: Duration,
    allow_http: bool,
    refresh_cooldown: Duration,
}

impl JwksClient {
    /// Returns a client with the default configuration.
    pub fn new() -> Self {
        Self::builder().build()
    }

    /// Returns a builder for customising the client's cache TTL, request
    /// timeout, HTTP client, and HTTPS enforcement.
    pub fn builder() -> JwksClientBuilder {
        JwksClientBuilder::new()
    }

    /// Fetches, validates, and caches the JWK Set at `jwks_uri` (JWKS-001).
    ///
    /// A fresh cache entry is served without any HTTP request (JWKS-005); the
    /// entry is refreshed once its TTL elapses. The URI scheme must be `https`
    /// unless [`JwksClientBuilder::allow_http`] was set.
    ///
    /// # Errors
    ///
    /// - [`IdentityError::Validation`] — an empty or non-HTTPS `jwks_uri`, an
    ///   empty key set, or a key missing required parameters (JWKS-002/007).
    /// - [`IdentityError::Http`] — a transport failure or a non-2xx response.
    /// - [`IdentityError::Deserialization`] — a body that is not a valid JWK Set
    ///   (JWKS-007).
    pub async fn fetch(&self, jwks_uri: &str) -> Result<JsonWebKeySet> {
        let uri = jwks_uri.trim().to_string();
        if uri.is_empty() {
            return Err(IdentityError::Validation("jwks_uri is empty".to_string()));
        }

        // JWKS-005: a fresh cache entry is served without any HTTP request.
        if let Some(key_set) = self.cache.get(&uri).await {
            return Ok(key_set);
        }

        // Require an https URI unless http was explicitly allowed (for local
        // development / integration against non-TLS providers). URL schemes are
        // case-insensitive (RFC 3986 §3.1), so compare on a lowercased copy.
        let scheme = uri.to_ascii_lowercase();
        let is_https = scheme.starts_with("https://");
        let scheme_ok = is_https || (self.allow_http && scheme.starts_with("http://"));
        if !scheme_ok {
            return Err(IdentityError::Validation(format!(
                "jwks_uri {uri:?} must use https (enable allow_http for development)"
            )));
        }

        // Single-flight: concurrent misses for the same `jwks_uri` collapse to a
        // single outbound fetch instead of each firing its own (mirrors the Go
        // reference's `singleflight`). The first caller holds the per-URI gate
        // while it fetches and populates the cache; waiters then re-check under
        // the gate and observe the populated entry rather than re-fetching. On a
        // fetch error the gate is released without a cache write, so a waiter
        // retries rather than sharing a transient failure — no worse than the
        // pre-single-flight behaviour and safer than caching an error.
        // `flight` is an RAII guard: it releases the per-URI gate from the
        // in-flight map when it drops, including if this future is cancelled at
        // any `await` below. `flight` is declared before `_flight` so it outlives
        // the lock guard and releases the gate last.
        let flight = self.cache.flight_gate(&uri);
        let _flight = flight.gate().lock().await;
        // Re-check under the flight: a concurrent winner may have populated
        // the cache while we waited on the gate.
        if let Some(key_set) = self.cache.get(&uri).await {
            return Ok(key_set);
        }
        let key_set = self.fetch_and_parse(&uri).await?;
        self.cache
            .put(uri.clone(), key_set.clone(), self.cache_ttl)
            .await;
        Ok(key_set)
    }

    /// Invalidates the cached set for `jwks_uri` and re-fetches it from the
    /// provider (JWKS-006), returning the fresh set. Callers invoke it after a
    /// signature verification failure that may indicate key rotation.
    ///
    /// An explicit `force_refresh` is never throttled, but it starts the
    /// cooldown window that gates subsequent automatic refreshes in
    /// [`JwksClient::resolve_key`].
    ///
    /// # Errors
    ///
    /// Propagates the errors of [`JwksClient::fetch`].
    pub async fn force_refresh(&self, jwks_uri: &str) -> Result<JsonWebKeySet> {
        let uri = jwks_uri.trim().to_string();
        self.cache.invalidate(&uri).await;
        let key_set = self.fetch(&uri).await?;
        self.cache.mark_refresh(&uri).await;
        Ok(key_set)
    }

    /// Resolves the key with `kid` at `jwks_uri`, forcing one refresh and
    /// retrying on a miss before erroring (JWKS-003/004).
    ///
    /// This handles key rotation: a token signed with a freshly published key
    /// whose `kid` is not yet cached triggers a re-fetch. To resolve against an
    /// already-fetched set without a network round-trip, use
    /// [`JsonWebKeySet::resolve_key`].
    ///
    /// The automatic refresh is rate-limited per `jwks_uri` by the refresh
    /// cooldown (see [`JwksClientBuilder::refresh_cooldown`]): because `kid`
    /// comes from an untrusted token header, an attacker presenting tokens with
    /// random `kid` values would otherwise drive unbounded outbound fetches.
    /// Within the cooldown a miss returns [`IdentityError::KeyNotFound`] without
    /// re-fetching.
    ///
    /// # Errors
    ///
    /// - [`IdentityError::KeyNotFound`] — no key with `kid` even after a refresh.
    /// - The fetch errors of [`JwksClient::fetch`].
    pub async fn resolve_key(&self, jwks_uri: &str, kid: &str) -> Result<JsonWebKey> {
        let key_set = self.fetch(jwks_uri).await?;
        if let Some(key) = key_set.find(kid) {
            return Ok(key.clone());
        }
        // JWKS-004: the kid may belong to a freshly rotated key, so force one
        // refresh and retry. Throttle the automatic refresh per jwks_uri
        // (keyed on the trimmed URI, matching the cache key) so random unknown
        // kids cannot amplify traffic against the provider.
        let uri = jwks_uri.trim();
        if self
            .cache
            .refresh_throttled(uri, self.refresh_cooldown)
            .await
        {
            return Err(IdentityError::KeyNotFound(kid.to_string()));
        }
        let refreshed = self.force_refresh(uri).await?;
        refreshed.resolve_key(kid).cloned()
    }

    /// Performs the HTTP request and parses the body. Contains no caching logic.
    async fn fetch_and_parse(&self, uri: &str) -> Result<JsonWebKeySet> {
        let mut response = self
            .http
            .get(uri)
            .header(ACCEPT, "application/json")
            .timeout(self.timeout)
            .send()
            .await
            .map_err(|e| IdentityError::Http(format!("fetch {uri}: {e}")))?;

        // A non-2xx response is a transport error carrying the status.
        let status = response.status();
        if !status.is_success() {
            return Err(IdentityError::Http(format!(
                "unexpected HTTP status {} from {uri}",
                status.as_u16()
            )));
        }

        // Read the body in chunks so an oversized response is rejected before it
        // is fully buffered, bounding memory regardless of Content-Length.
        let mut body = Vec::new();
        while let Some(chunk) = response
            .chunk()
            .await
            .map_err(|e| IdentityError::Http(format!("read body from {uri}: {e}")))?
        {
            if body.len() + chunk.len() > MAX_BODY_BYTES {
                return Err(IdentityError::Deserialization(format!(
                    "JWK Set from {uri} exceeds {MAX_BODY_BYTES} bytes"
                )));
            }
            body.extend_from_slice(&chunk);
        }

        JsonWebKeySet::parse(&body)
    }
}

impl Default for JwksClient {
    fn default() -> Self {
        Self::new()
    }
}

/// Builder for [`JwksClient`]. Obtain one via [`JwksClient::builder`].
pub struct JwksClientBuilder {
    http: Option<HttpClient>,
    cache_ttl: Duration,
    timeout: Duration,
    allow_http: bool,
    refresh_cooldown: Duration,
    max_cache_entries: usize,
}

impl JwksClientBuilder {
    /// Returns a builder seeded with the default configuration. The cache bound
    /// defaults to [`DEFAULT_MAX_CACHE_ENTRIES`], overridable by the
    /// `JWKS_CACHE_MAX_ENTRIES` environment variable.
    fn new() -> Self {
        Self {
            http: None,
            cache_ttl: DEFAULT_CACHE_TTL,
            timeout: DEFAULT_TIMEOUT,
            allow_http: false,
            refresh_cooldown: DEFAULT_REFRESH_COOLDOWN,
            max_cache_entries: crate::env::max_cache_entries_from_env(
                MAX_CACHE_ENTRIES_ENV,
                DEFAULT_MAX_CACHE_ENTRIES,
            ),
        }
    }

    /// Sets how long a fetched key set is cached before the next call re-fetches
    /// it. A non-positive duration is ignored and the default (24h) is retained.
    pub fn cache_ttl(mut self, ttl: Duration) -> Self {
        if !ttl.is_zero() {
            self.cache_ttl = ttl;
        }
        self
    }

    /// Bounds each JWKS request with a per-request timeout. A non-positive
    /// duration is ignored and the default (30s) is retained.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        if !timeout.is_zero() {
            self.timeout = timeout;
        }
        self
    }

    /// Permits `http://` JWKS URIs, which are otherwise rejected. Intended for
    /// local development and integration tests against non-TLS providers; do not
    /// enable in production.
    pub fn allow_http(mut self, allow: bool) -> Self {
        self.allow_http = allow;
        self
    }

    /// Sets the minimum interval between automatic forced refreshes for a given
    /// `jwks_uri` in [`JwksClient::resolve_key`]. Within this window a `kid`
    /// miss returns [`IdentityError::KeyNotFound`] without a network re-fetch,
    /// bounding the rate at which an untrusted `kid` can trigger outbound
    /// traffic. A zero duration disables throttling (every miss refreshes);
    /// explicit [`JwksClient::force_refresh`] is never throttled.
    pub fn refresh_cooldown(mut self, cooldown: Duration) -> Self {
        self.refresh_cooldown = cooldown;
        self
    }

    /// Uses `client` for JWKS requests instead of a default [`reqwest::Client`],
    /// letting callers share a connection pool or supply custom transport
    /// configuration.
    pub fn http_client(mut self, client: HttpClient) -> Self {
        self.http = Some(client);
        self
    }

    /// Bounds the number of distinct `jwks_uri` entries the cache retains,
    /// evicting the least-recently-used entry once the bound is exceeded. The
    /// cache is keyed by `jwks_uri`, so without a bound it grows one entry per
    /// distinct URI forever (unbounded-growth DoS). Defaults to
    /// [`DEFAULT_MAX_CACHE_ENTRIES`] (overridable via `JWKS_CACHE_MAX_ENTRIES`).
    /// A value of `0` disables the bound (unbounded).
    pub fn max_cache_entries(mut self, max_entries: usize) -> Self {
        self.max_cache_entries = max_entries;
        self
    }

    /// Builds the [`JwksClient`].
    pub fn build(self) -> JwksClient {
        JwksClient {
            http: self.http.unwrap_or_else(crate::http::secure_client),
            cache: Cache::new(self.max_cache_entries),
            cache_ttl: self.cache_ttl,
            timeout: self.timeout,
            allow_http: self.allow_http,
            refresh_cooldown: self.refresh_cooldown,
        }
    }
}

impl Default for JwksClientBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    /// A JWK Set with one RSA and one EC signing key (mirrors
    /// `spec/test-fixtures/jwks/valid.json`).
    const VALID_SET: &str = r#"{
        "keys": [
            {"kty":"RSA","kid":"rsa-sig-key","use":"sig","alg":"RS256",
             "n":"0vx7agoebGcQ","e":"AQAB"},
            {"kty":"EC","kid":"ec-sig-key","use":"sig","alg":"ES256",
             "crv":"P-256","x":"f83OJ3D2xF1B","y":"x_FEzRu9m36H"}
        ]
    }"#;

    /// Mounts a single GET handler on `/jwks` returning `template`.
    async fn mount(server: &MockServer, template: ResponseTemplate) {
        Mock::given(method("GET"))
            .and(path("/jwks"))
            .respond_with(template)
            .mount(server)
            .await;
    }

    fn jwks_uri(server: &MockServer) -> String {
        format!("{}/jwks", server.uri())
    }

    // JWKS-001 / JWKS-002: fetch a valid set; both RSA and EC keys parse with
    // their key-type parameters intact.
    #[tokio::test]
    async fn fetches_and_parses_valid_set() {
        let server = MockServer::start().await;
        mount(
            &server,
            ResponseTemplate::new(200).set_body_string(VALID_SET),
        )
        .await;

        let client = JwksClient::builder().allow_http(true).build();
        let set = client.fetch(&jwks_uri(&server)).await.expect("fetch");

        assert_eq!(set.keys().len(), 2);
        let rsa = set.resolve_key("rsa-sig-key").expect("rsa present");
        assert_eq!(rsa.kty, "RSA");
        assert!(!rsa.n.is_empty() && !rsa.e.is_empty());
    }

    // JWKS-003 / AC6: an EC key resolves with kty == "EC" and curve parameters.
    #[tokio::test]
    async fn resolves_ec_key_by_kid() {
        let server = MockServer::start().await;
        mount(
            &server,
            ResponseTemplate::new(200).set_body_string(VALID_SET),
        )
        .await;

        let client = JwksClient::builder().allow_http(true).build();
        let key = client
            .resolve_key(&jwks_uri(&server), "ec-sig-key")
            .await
            .expect("ec key resolves");

        assert_eq!(key.kty, "EC");
        assert_eq!(key.crv, "P-256");
    }

    // JWKS-004: a kid absent from the cached set triggers one forced refresh; the
    // provider now serves the rotated key and resolution succeeds on retry.
    #[tokio::test]
    async fn resolve_with_refresh_retries() {
        let server = MockServer::start().await;
        // First response lacks the rotated kid; the second (post-refresh) has it.
        let without = r#"{ "keys": [ {"kty":"RSA","kid":"old","n":"a","e":"AQAB"} ] }"#;
        Mock::given(method("GET"))
            .and(path("/jwks"))
            .respond_with(ResponseTemplate::new(200).set_body_string(without))
            .up_to_n_times(1)
            .mount(&server)
            .await;
        Mock::given(method("GET"))
            .and(path("/jwks"))
            .respond_with(ResponseTemplate::new(200).set_body_string(VALID_SET))
            .mount(&server)
            .await;

        let client = JwksClient::builder().allow_http(true).build();
        let key = client
            .resolve_key(&jwks_uri(&server), "rsa-sig-key")
            .await
            .expect("rotated key resolves after refresh");

        assert_eq!(key.kid, "rsa-sig-key");
        let received = server.received_requests().await.unwrap();
        assert_eq!(received.len(), 2, "one initial fetch + one forced refresh");
    }

    // JWKS-004: a kid that never appears errors KeyNotFound after the refresh.
    #[tokio::test]
    async fn resolve_absent_kid_errors_after_refresh() {
        let server = MockServer::start().await;
        mount(
            &server,
            ResponseTemplate::new(200).set_body_string(VALID_SET),
        )
        .await;

        let client = JwksClient::builder().allow_http(true).build();
        let err = client
            .resolve_key(&jwks_uri(&server), "never")
            .await
            .expect_err("absent kid errors");

        assert!(
            matches!(&err, IdentityError::KeyNotFound(kid) if kid == "never"),
            "expected KeyNotFound(never), got {err:?}"
        );
    }

    // JWKS-004 (security): repeated unknown-kid resolutions are rate-limited by
    // the refresh cooldown, so a random kid cannot amplify traffic. The first
    // miss refreshes once (request 2); a second miss within the cooldown is
    // throttled and makes no further request. Mirrors go/pkg/jwks
    // TestResolveKeyWithRefresh_Throttled.
    #[tokio::test]
    async fn refresh_cooldown_throttles_repeated_misses() {
        let server = MockServer::start().await;
        mount(
            &server,
            ResponseTemplate::new(200).set_body_string(VALID_SET),
        )
        .await;

        let client = JwksClient::builder()
            .allow_http(true)
            .refresh_cooldown(Duration::from_secs(60))
            .build();
        let uri = jwks_uri(&server);

        // First unknown-kid resolution: cold fetch (req 1) + one forced refresh
        // (req 2), then KeyNotFound.
        client
            .resolve_key(&uri, "never")
            .await
            .expect_err("first miss errors");
        // Second unknown-kid resolution within the cooldown: throttled, no fetch.
        client
            .resolve_key(&uri, "never")
            .await
            .expect_err("second miss errors");

        let received = server.received_requests().await.unwrap();
        assert_eq!(
            received.len(),
            2,
            "second miss throttled within cooldown; no extra refresh"
        );
    }

    // A zero refresh_cooldown disables throttling: every unknown-kid miss forces
    // a refresh, so two misses issue two extra fetches.
    #[tokio::test]
    async fn zero_refresh_cooldown_refreshes_every_miss() {
        let server = MockServer::start().await;
        mount(
            &server,
            ResponseTemplate::new(200).set_body_string(VALID_SET),
        )
        .await;

        let client = JwksClient::builder()
            .allow_http(true)
            .refresh_cooldown(Duration::ZERO)
            .build();
        let uri = jwks_uri(&server);

        client
            .resolve_key(&uri, "never")
            .await
            .expect_err("first miss errors");
        client
            .resolve_key(&uri, "never")
            .await
            .expect_err("second miss errors");

        // req 1 = cold fetch; req 2 + req 3 = a forced refresh per miss.
        let received = server.received_requests().await.unwrap();
        assert_eq!(received.len(), 3, "no throttling: each miss refreshes");
    }

    // JWKS-005: a second fetch() within the TTL makes no HTTP request.
    #[tokio::test]
    async fn caches_within_ttl() {
        let server = MockServer::start().await;
        mount(
            &server,
            ResponseTemplate::new(200).set_body_string(VALID_SET),
        )
        .await;

        let client = JwksClient::builder().allow_http(true).build();
        let uri = jwks_uri(&server);
        client.fetch(&uri).await.expect("first fetch");
        client.fetch(&uri).await.expect("cache hit");

        let received = server.received_requests().await.unwrap();
        assert_eq!(received.len(), 1, "second call served from cache");
    }

    // Single-flight: several concurrent cache-misses for the SAME jwks_uri
    // collapse to exactly one outbound fetch. The response is delayed so the
    // requests genuinely overlap on the gate; without single-flight each miss
    // would fire its own request. Mirrors go/pkg/jwks TestFetchKeySet
    // singleflight coverage.
    #[tokio::test]
    async fn single_flight_dedupes_concurrent_misses() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/jwks"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_string(VALID_SET)
                    .set_delay(Duration::from_millis(200)),
            )
            .mount(&server)
            .await;

        let client = JwksClient::builder().allow_http(true).build();
        let uri = jwks_uri(&server);

        // Drive four fetches concurrently; the first holds the gate through the
        // delayed fetch while the rest wait, then observe the populated cache.
        let (a, b, c, d) = tokio::join!(
            client.fetch(&uri),
            client.fetch(&uri),
            client.fetch(&uri),
            client.fetch(&uri),
        );
        a.expect("fetch a");
        b.expect("fetch b");
        c.expect("fetch c");
        d.expect("fetch d");

        let received = server.received_requests().await.unwrap();
        assert_eq!(
            received.len(),
            1,
            "concurrent misses for one uri collapse to a single fetch"
        );
    }

    // A fetch cancelled at an await (here via a timeout against a slow server)
    // must release its single-flight gate; otherwise issuer/URI rotation plus
    // mid-fetch cancellation would grow the in-flight map without bound
    // (Finding 1 regression guard).
    #[tokio::test]
    async fn cancelled_fetch_releases_flight_gate() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/jwks"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_string(VALID_SET)
                    .set_delay(Duration::from_secs(5)),
            )
            .mount(&server)
            .await;

        let client = JwksClient::builder().allow_http(true).build();
        let uri = jwks_uri(&server);

        // Cancel the fetch long before the 5s response by racing it against a
        // short timeout; the fetch future is dropped mid-`fetch_and_parse`.
        let cancelled = tokio::time::timeout(Duration::from_millis(100), client.fetch(&uri)).await;
        assert!(
            cancelled.is_err(),
            "fetch should be cancelled by the timeout"
        );

        assert_eq!(
            client.cache.flight_gate_count(),
            0,
            "a cancelled fetch must not strand its single-flight gate"
        );
    }

    // JWKS-006: force_refresh() invalidates the cache and re-fetches.
    #[tokio::test]
    async fn force_refresh_refetches() {
        let server = MockServer::start().await;
        mount(
            &server,
            ResponseTemplate::new(200).set_body_string(VALID_SET),
        )
        .await;

        let client = JwksClient::builder().allow_http(true).build();
        let uri = jwks_uri(&server);
        client.fetch(&uri).await.expect("first fetch");
        let set = client.force_refresh(&uri).await.expect("force refresh");

        assert!(!set.keys().is_empty());
        let received = server.received_requests().await.unwrap();
        assert_eq!(received.len(), 2, "force_refresh bypasses the cache");
    }

    // JWKS-007: an empty key set from the endpoint is a validation error.
    #[tokio::test]
    async fn rejects_empty_key_set() {
        let server = MockServer::start().await;
        mount(
            &server,
            ResponseTemplate::new(200).set_body_string(r#"{ "keys": [] }"#),
        )
        .await;

        let client = JwksClient::builder().allow_http(true).build();
        let err = client
            .fetch(&jwks_uri(&server))
            .await
            .expect_err("empty errors");
        assert!(
            matches!(err, IdentityError::Validation(_)),
            "expected Validation, got {err:?}"
        );
    }

    // JWKS-007 / AC7: a non-JSON body is a deserialization error.
    #[tokio::test]
    async fn rejects_malformed_json() {
        let server = MockServer::start().await;
        mount(
            &server,
            ResponseTemplate::new(200).set_body_string("not-json"),
        )
        .await;

        let client = JwksClient::builder().allow_http(true).build();
        let err = client
            .fetch(&jwks_uri(&server))
            .await
            .expect_err("malformed body errors");
        assert!(
            matches!(err, IdentityError::Deserialization(_)),
            "expected Deserialization, got {err:?}"
        );
    }

    // AC7: a non-2xx response is an HTTP error carrying the status.
    #[tokio::test]
    async fn maps_http_status_error() {
        let server = MockServer::start().await;
        mount(&server, ResponseTemplate::new(500)).await;

        let client = JwksClient::builder().allow_http(true).build();
        let err = client
            .fetch(&jwks_uri(&server))
            .await
            .expect_err("500 errors");
        match err {
            IdentityError::Http(msg) => assert!(msg.contains("500"), "{msg}"),
            other => panic!("expected Http, got {other:?}"),
        }
    }

    // An http:// URI is rejected by default and no HTTP request is made.
    #[tokio::test]
    async fn requires_https_by_default() {
        let client = JwksClient::new();
        let err = client
            .fetch("http://insecure.example.com/jwks")
            .await
            .expect_err("http uri rejected");
        match err {
            IdentityError::Validation(msg) => assert!(msg.contains("https"), "{msg}"),
            other => panic!("expected Validation, got {other:?}"),
        }
    }

    // An empty jwks_uri is a validation error.
    #[tokio::test]
    async fn rejects_empty_uri() {
        let client = JwksClient::new();
        let err = client.fetch("   ").await.expect_err("empty uri rejected");
        assert!(
            matches!(err, IdentityError::Validation(_)),
            "expected Validation, got {err:?}"
        );
    }

    // A very large TTL (`Duration::MAX`) must not overflow `Instant` and panic;
    // the entry is stored and served from cache (parity with discovery).
    #[tokio::test]
    async fn handles_max_ttl_without_panicking() {
        let server = MockServer::start().await;
        mount(
            &server,
            ResponseTemplate::new(200).set_body_string(VALID_SET),
        )
        .await;

        let client = JwksClient::builder()
            .allow_http(true)
            .cache_ttl(Duration::MAX)
            .build();
        let uri = jwks_uri(&server);
        client.fetch(&uri).await.expect("first fetch");
        client.fetch(&uri).await.expect("cache hit");

        let received = server.received_requests().await.unwrap();
        assert_eq!(received.len(), 1, "max-ttl entry stays cached");
    }
}