solo-api 0.11.4

Solo: MCP and HTTP transports
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
// SPDX-License-Identifier: Apache-2.0

//! OIDC validation: discovery URL → JWKS → signature + claim checks.
//!
//! On the first request that arrives in OIDC mode, `OidcValidator`:
//!   1. Decodes the JWT header (no signature check) to extract `alg + kid`.
//!   2. Looks up the signing key in its in-memory JWKS cache. On cache
//!      miss (cold or unknown `kid` — key rotation), fetches the
//!      provider's discovery document, follows `jwks_uri`, and rebuilds
//!      the cache. The cache TTL respects `Cache-Control: max-age=N`
//!      from the discovery response, falling back to 1 hour if absent.
//!   3. Validates the signature + `aud` + `exp` via the `jsonwebtoken`
//!      crate.
//!   4. Extracts the configured tenant claim (default `solo_tenant`),
//!      validates it through `TenantId::new`, and packages everything
//!      into an [`AuthenticatedPrincipal`].
//!
//! Failure modes (mapped to HTTP status in `middleware.rs`):
//!   * Missing/malformed Authorization header → 401 ([`AuthError::MissingAuthHeader`] /
//!     [`AuthError::MalformedAuthHeader`]).
//!   * Token rejected (bad signature, expired, wrong audience, unknown kid post-refetch)
//!     → 401 ([`AuthError::InvalidOidcToken`]).
//!   * Token valid but tenant claim missing/invalid → 403
//!     ([`AuthError::MissingTenantClaim`] / [`AuthError::InvalidTenantClaim`]).
//!   * Upstream IdP unreachable → 500 ([`AuthError::Discovery`] / [`AuthError::Jwks`]).

use super::{AuthError, AuthenticatedPrincipal};
use jsonwebtoken::jwk::{Jwk, JwkSet};
use jsonwebtoken::{Algorithm, DecodingKey, TokenData, Validation, decode, decode_header};
use serde_json::Value;
use solo_core::TenantId;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

/// Per-instance configuration for an OIDC validator. Mirrors the
/// `AuthConfig::Oidc { ... }` variant in `auth/mod.rs`.
#[derive(Debug, Clone)]
pub struct OidcConfig {
    pub discovery_url: String,
    pub audience: String,
    pub tenant_claim_name: String,
}

/// Cached JWKS keys, indexed by `kid`. `fetched_at + ttl` together
/// govern eviction; a cache miss on an unknown `kid` triggers a
/// refetch regardless of TTL to handle IdP key rotation.
struct CachedJwks {
    keys: HashMap<String, KeyEntry>,
    fetched_at: Instant,
    ttl: Duration,
}

/// One JWKS entry. We retain the algorithm so we can pick the right
/// `Validation::new(Algorithm::*)` per token (each `kid` is bound to a
/// single algorithm by RFC 7517; trusting the token header's `alg`
/// without cross-checking against the key's `alg` is the
/// confused-deputy hole the validator below closes by always preferring
/// the JWK-side algorithm).
struct KeyEntry {
    key: DecodingKey,
    algorithm: Algorithm,
}

/// OIDC validator. Cheap-to-clone (one `Arc<RwLock<…>>` for the JWKS
/// cache; everything else is small).
#[derive(Clone)]
pub struct OidcValidator {
    config: OidcConfig,
    http_client: reqwest::Client,
    jwks_cache: Arc<RwLock<Option<CachedJwks>>>,
}

impl std::fmt::Debug for OidcValidator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OidcValidator")
            .field("config", &self.config)
            .field("jwks_cache", &"<RwLock>")
            .finish()
    }
}

impl OidcValidator {
    pub fn new(config: OidcConfig) -> Self {
        let http_client = reqwest::Client::builder()
            .timeout(Duration::from_secs(10))
            .build()
            .expect("reqwest client");
        Self {
            config,
            http_client,
            jwks_cache: Arc::new(RwLock::new(None)),
        }
    }

    /// Construct with an explicit reqwest client. Used by tests so the
    /// timeout can be shorter / so the client is shared across the
    /// fake IdP fixture.
    #[cfg(test)]
    pub fn with_http_client(config: OidcConfig, http_client: reqwest::Client) -> Self {
        Self {
            config,
            http_client,
            jwks_cache: Arc::new(RwLock::new(None)),
        }
    }

    /// Validate the value of an Authorization header.
    pub async fn validate(
        &self,
        header: Option<&str>,
    ) -> Result<AuthenticatedPrincipal, AuthError> {
        let header = header.ok_or(AuthError::MissingAuthHeader)?;
        let token = header
            .strip_prefix("Bearer ")
            .ok_or(AuthError::MalformedAuthHeader)?;

        // Decode the header to discover `kid + alg`. No signature check yet.
        let jwt_header = decode_header(token).map_err(|e| AuthError::InvalidOidcToken {
            reason: format!("decode header: {e}"),
        })?;
        let kid = jwt_header
            .kid
            .clone()
            .ok_or_else(|| AuthError::InvalidOidcToken {
                reason: "missing kid in token header".to_string(),
            })?;

        // Look up the key (refresh cache on miss).
        let entry = self.get_key(&kid).await?;

        // Cross-check: the JWK's algorithm must match the token's `alg`.
        // Honoring the token-side `alg` alone would let an attacker
        // present an HS256 token signed with the public-key bytes of an
        // RS256 JWK. We pin to the JWK-side algorithm.
        if entry.algorithm != jwt_header.alg {
            return Err(AuthError::InvalidOidcToken {
                reason: format!(
                    "token alg {:?} does not match JWK alg {:?}",
                    jwt_header.alg, entry.algorithm
                ),
            });
        }

        let mut validation = Validation::new(entry.algorithm);
        validation.set_audience(&[&self.config.audience]);
        let token_data: TokenData<Value> =
            decode(token, &entry.key, &validation).map_err(|e| AuthError::InvalidOidcToken {
                reason: format!("{e}"),
            })?;

        let subject = token_data
            .claims
            .get("sub")
            .and_then(|v| v.as_str())
            .unwrap_or("unknown")
            .to_string();

        let tenant_claim_str = token_data
            .claims
            .get(&self.config.tenant_claim_name)
            .and_then(|v| v.as_str())
            .ok_or_else(|| AuthError::MissingTenantClaim {
                claim_name: self.config.tenant_claim_name.clone(),
            })?;
        let tenant_claim = TenantId::new(tenant_claim_str.to_string())?;

        let scopes = token_data
            .claims
            .get("scope")
            .and_then(|v| v.as_str())
            .map(|s| s.split_whitespace().map(String::from).collect())
            .unwrap_or_default();

        Ok(AuthenticatedPrincipal {
            subject,
            tenant_claim: Some(tenant_claim),
            scopes,
            claims: token_data.claims,
        })
    }

    /// Look up a JWKS key by `kid`. Returns a clone of the cached entry's
    /// decoding key + algorithm; refetches on cache miss (cold or
    /// unknown-kid).
    async fn get_key(&self, kid: &str) -> Result<KeyEntry, AuthError> {
        // Fast path: cache hit + within TTL + kid present.
        {
            let cache = self.jwks_cache.read().await;
            if let Some(c) = cache.as_ref()
                && c.fetched_at.elapsed() < c.ttl
                && let Some(entry) = c.keys.get(kid)
            {
                return Ok(KeyEntry {
                    key: entry.key.clone(),
                    algorithm: entry.algorithm,
                });
            }
            // Drop the read lock before taking write lock.
        }

        // Slow path: refetch + retry.
        self.refresh_cache().await?;
        let cache = self.jwks_cache.read().await;
        cache
            .as_ref()
            .and_then(|c| c.keys.get(kid))
            .map(|entry| KeyEntry {
                key: entry.key.clone(),
                algorithm: entry.algorithm,
            })
            .ok_or_else(|| AuthError::Jwks(format!("kid '{kid}' not found in JWKS")))
    }

    /// Fetch the OIDC discovery document, follow `jwks_uri`, rebuild the
    /// JWKS cache. TTL is sourced from `Cache-Control: max-age=N` on
    /// the discovery response (fallback 1 hour).
    async fn refresh_cache(&self) -> Result<(), AuthError> {
        let discovery_resp = self
            .http_client
            .get(&self.config.discovery_url)
            .send()
            .await
            .map_err(|e| AuthError::Discovery(format!("{e}")))?
            .error_for_status()
            .map_err(|e| AuthError::Discovery(format!("{e}")))?;

        // Determine TTL from Cache-Control max-age BEFORE consuming
        // the body (we lose access to headers after `.json()`).
        let ttl = parse_max_age(
            discovery_resp
                .headers()
                .get("cache-control")
                .and_then(|h| h.to_str().ok()),
        )
        .unwrap_or(Duration::from_secs(3600));

        let body: Value = discovery_resp
            .json()
            .await
            .map_err(|e| AuthError::Discovery(format!("{e}")))?;
        let jwks_uri = body
            .get("jwks_uri")
            .and_then(|v| v.as_str())
            .ok_or_else(|| AuthError::Discovery("discovery missing jwks_uri".to_string()))?;

        let jwks: JwkSet = self
            .http_client
            .get(jwks_uri)
            .send()
            .await
            .map_err(|e| AuthError::Jwks(format!("{e}")))?
            .error_for_status()
            .map_err(|e| AuthError::Jwks(format!("{e}")))?
            .json()
            .await
            .map_err(|e| AuthError::Jwks(format!("{e}")))?;

        let mut keys = HashMap::new();
        for jwk in jwks.keys.iter() {
            let Some(kid) = jwk.common.key_id.as_deref() else {
                continue;
            };
            let Some(algorithm) = jwk_algorithm(jwk) else {
                continue;
            };
            let key = match DecodingKey::from_jwk(jwk) {
                Ok(k) => k,
                Err(_) => continue,
            };
            keys.insert(kid.to_string(), KeyEntry { key, algorithm });
        }

        let mut cache = self.jwks_cache.write().await;
        *cache = Some(CachedJwks {
            keys,
            fetched_at: Instant::now(),
            ttl,
        });
        Ok(())
    }
}

/// Read the `alg` from the JWK's CommonParameters. The `key_algorithm`
/// field is the JWK side of the contract; we use that rather than the
/// token-header `alg` to avoid the confused-deputy hole described in
/// `OidcValidator::validate`.
fn jwk_algorithm(jwk: &Jwk) -> Option<Algorithm> {
    use jsonwebtoken::jwk::KeyAlgorithm;
    match jwk.common.key_algorithm? {
        KeyAlgorithm::HS256 => Some(Algorithm::HS256),
        KeyAlgorithm::HS384 => Some(Algorithm::HS384),
        KeyAlgorithm::HS512 => Some(Algorithm::HS512),
        KeyAlgorithm::RS256 => Some(Algorithm::RS256),
        KeyAlgorithm::RS384 => Some(Algorithm::RS384),
        KeyAlgorithm::RS512 => Some(Algorithm::RS512),
        KeyAlgorithm::PS256 => Some(Algorithm::PS256),
        KeyAlgorithm::PS384 => Some(Algorithm::PS384),
        KeyAlgorithm::PS512 => Some(Algorithm::PS512),
        KeyAlgorithm::ES256 => Some(Algorithm::ES256),
        KeyAlgorithm::ES384 => Some(Algorithm::ES384),
        KeyAlgorithm::EdDSA => Some(Algorithm::EdDSA),
        // Other JWK algorithms (RSA-OAEP, etc.) are key-wrap, not JWT-sign.
        _ => None,
    }
}

/// Parse `max-age=N` (in seconds) out of a Cache-Control header. Returns
/// `None` if the header is absent / malformed / lacks `max-age`.
fn parse_max_age(header: Option<&str>) -> Option<Duration> {
    let h = header?;
    for part in h.split(',').map(str::trim) {
        if let Some(rest) = part.strip_prefix("max-age=")
            && let Ok(n) = rest.parse::<u64>()
        {
            return Some(Duration::from_secs(n));
        }
    }
    None
}

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

    // ----------------------------------------------------------------------
    // Fake-IdP fixture (banked-lesson #26 candidate).
    //
    // We sign tokens with HMAC-SHA256 — both because it sidesteps RSA
    // key-generation in the test path (no `rsa` crate dep, no PEM
    // wrangling) and because the validator's contract is unchanged by
    // algorithm choice: the same `decode` + JWKS lookup path runs for
    // HMAC and RSA. The shared HMAC secret IS published in the JWK set
    // as an `oct` key; in production OIDC nobody would publish HMAC
    // secrets, but for in-process tests it's the cleanest path.
    // ----------------------------------------------------------------------

    /// Test IdP that stands up a wiremock server with an OIDC discovery
    /// document + a JWKS endpoint, holding an HMAC signing secret so
    /// tests can mint tokens.
    struct FakeIdp {
        server: MockServer,
        signing_secret: Vec<u8>,
        signing_kid: String,
    }

    impl FakeIdp {
        /// Stand up a fake IdP. `extra_response` lets a test override
        /// the Cache-Control on the discovery doc (returning `Some(secs)`
        /// adds `max-age=<secs>`; `None` omits the header).
        async fn start(signing_kid: &str, cache_max_age_secs: Option<u64>) -> Self {
            let server = MockServer::start().await;
            let secret = b"fixture-secret-bytes-for-hmac-tests".to_vec();
            let kid = signing_kid.to_string();

            let discovery_body = json!({
                "issuer": server.uri(),
                "jwks_uri": format!("{}/jwks", server.uri()),
            });
            let mut discovery_resp = ResponseTemplate::new(200).set_body_json(discovery_body);
            if let Some(secs) = cache_max_age_secs {
                discovery_resp = discovery_resp
                    .insert_header("cache-control", format!("max-age={secs}").as_str());
            }
            Mock::given(method("GET"))
                .and(path("/.well-known/openid-configuration"))
                .respond_with(discovery_resp)
                .mount(&server)
                .await;

            let jwks_body = json!({
                "keys": [
                    {
                        "kty": "oct",
                        "kid": &kid,
                        "alg": "HS256",
                        "k": base64_url(&secret),
                    }
                ]
            });
            Mock::given(method("GET"))
                .and(path("/jwks"))
                .respond_with(ResponseTemplate::new(200).set_body_json(jwks_body))
                .mount(&server)
                .await;

            Self {
                server,
                signing_secret: secret,
                signing_kid: kid,
            }
        }

        /// Replace the JWKS endpoint with one that publishes a *new* kid.
        /// Used by `oidc_key_rotation` to simulate the IdP rotating keys
        /// faster than the cache TTL. The existing mock is left in place
        /// (wiremock falls through to the most recently mounted matcher).
        async fn rotate_to(&mut self, new_kid: &str, new_secret: &[u8]) {
            let jwks_body = json!({
                "keys": [
                    {
                        "kty": "oct",
                        "kid": new_kid,
                        "alg": "HS256",
                        "k": base64_url(new_secret),
                    }
                ]
            });
            self.server.reset().await;
            // Re-mount discovery (reset clears everything).
            let discovery_body = json!({
                "issuer": self.server.uri(),
                "jwks_uri": format!("{}/jwks", self.server.uri()),
            });
            Mock::given(method("GET"))
                .and(path("/.well-known/openid-configuration"))
                .respond_with(ResponseTemplate::new(200).set_body_json(discovery_body))
                .mount(&self.server)
                .await;
            Mock::given(method("GET"))
                .and(path("/jwks"))
                .respond_with(ResponseTemplate::new(200).set_body_json(jwks_body))
                .mount(&self.server)
                .await;
            self.signing_secret = new_secret.to_vec();
            self.signing_kid = new_kid.to_string();
        }

        /// Mint a token signed by this fake IdP. `extra_claims` are
        /// merged into the claim set after the defaults so tests can
        /// override `exp`, `aud`, `solo_tenant`, etc.
        fn mint(&self, claims_override: Value) -> String {
            self.mint_with_kid(&self.signing_kid, &self.signing_secret, claims_override)
        }

        /// Mint a token signed by an explicit `kid`/secret pair —
        /// lets tests sign with an old key after the JWKS has rotated.
        fn mint_with_kid(&self, kid: &str, secret: &[u8], claims_override: Value) -> String {
            let mut header = Header::new(Algorithm::HS256);
            header.kid = Some(kid.to_string());
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs();
            let mut claims = json!({
                "iss": self.server.uri(),
                "sub": "test-subject",
                "aud": "test-audience",
                "exp": now + 600,
                "iat": now,
                "solo_tenant": "default",
            });
            if let (Value::Object(c), Value::Object(o)) = (&mut claims, claims_override) {
                for (k, v) in o {
                    c.insert(k, v);
                }
            }
            jsonwebtoken::encode(&header, &claims, &EncodingKey::from_secret(secret))
                .expect("encode")
        }
    }

    /// Base64-URL no-pad (matches JWK spec for the `k` parameter).
    fn base64_url(bytes: &[u8]) -> String {
        use base64::Engine;
        base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes)
    }

    fn make_validator(server_uri: &str) -> OidcValidator {
        OidcValidator::with_http_client(
            OidcConfig {
                discovery_url: format!("{server_uri}/.well-known/openid-configuration"),
                audience: "test-audience".to_string(),
                tenant_claim_name: "solo_tenant".to_string(),
            },
            reqwest::Client::builder()
                .timeout(Duration::from_secs(2))
                .build()
                .unwrap(),
        )
    }

    #[tokio::test]
    async fn oidc_happy_path() {
        let idp = FakeIdp::start("test-kid-1", None).await;
        let validator = make_validator(&idp.server.uri());

        let token = idp.mint(json!({ "solo_tenant": "tenant-a" }));
        let principal = validator
            .validate(Some(&format!("Bearer {token}")))
            .await
            .expect("validate");
        assert_eq!(principal.subject, "test-subject");
        assert_eq!(
            principal.tenant_claim,
            Some(TenantId::new("tenant-a").unwrap())
        );
    }

    #[tokio::test]
    async fn oidc_key_rotation() {
        let mut idp = FakeIdp::start("old-kid", None).await;
        let validator = make_validator(&idp.server.uri());

        // Warm the cache with the old kid.
        let warmup_token = idp.mint(json!({}));
        let _ = validator
            .validate(Some(&format!("Bearer {warmup_token}")))
            .await
            .expect("warmup");

        // IdP rotates. Mint a token under the NEW kid.
        let new_secret = b"new-rotated-secret-32-bytes--here".to_vec();
        idp.rotate_to("new-kid", &new_secret).await;
        let token = idp.mint(json!({}));

        // Validator's cache has old-kid only — should refetch on
        // unknown-kid miss + accept the new token.
        let principal = validator
            .validate(Some(&format!("Bearer {token}")))
            .await
            .expect("post-rotation");
        assert_eq!(principal.subject, "test-subject");
    }

    #[tokio::test]
    async fn oidc_invalid_audience() {
        let idp = FakeIdp::start("kid-aud", None).await;
        let validator = make_validator(&idp.server.uri());

        let token = idp.mint(json!({ "aud": "wrong-audience" }));
        let err = validator
            .validate(Some(&format!("Bearer {token}")))
            .await
            .unwrap_err();
        assert!(
            matches!(err, AuthError::InvalidOidcToken { .. }),
            "got {err:?}"
        );
    }

    #[tokio::test]
    async fn oidc_expired_token() {
        let idp = FakeIdp::start("kid-exp", None).await;
        let validator = make_validator(&idp.server.uri());

        // exp = 5 min in the past, well outside the default 60s leeway.
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let token = idp.mint(json!({ "exp": now - 300, "iat": now - 600 }));
        let err = validator
            .validate(Some(&format!("Bearer {token}")))
            .await
            .unwrap_err();
        assert!(
            matches!(err, AuthError::InvalidOidcToken { .. }),
            "got {err:?}"
        );
    }

    #[tokio::test]
    async fn oidc_missing_tenant_claim() {
        let idp = FakeIdp::start("kid-no-tenant", None).await;
        let validator = make_validator(&idp.server.uri());

        let token = idp.mint(json!({ "solo_tenant": null }));
        let err = validator
            .validate(Some(&format!("Bearer {token}")))
            .await
            .unwrap_err();
        assert!(
            matches!(err, AuthError::MissingTenantClaim { ref claim_name } if claim_name == "solo_tenant"),
            "got {err:?}"
        );
    }

    #[tokio::test]
    async fn oidc_invalid_tenant_claim_format() {
        let idp = FakeIdp::start("kid-bad-tenant", None).await;
        let validator = make_validator(&idp.server.uri());

        // TenantId rejects uppercase + spaces.
        let token = idp.mint(json!({ "solo_tenant": "INVALID UPPERCASE" }));
        let err = validator
            .validate(Some(&format!("Bearer {token}")))
            .await
            .unwrap_err();
        assert!(
            matches!(err, AuthError::InvalidTenantClaim(_)),
            "got {err:?}"
        );
    }

    #[tokio::test]
    async fn oidc_jwks_cache_within_ttl_no_refetch() {
        // No max-age = 1hr fallback TTL → second validate should NOT
        // re-hit discovery / jwks.
        let server = MockServer::start().await;
        let secret = b"counted-secret-32-bytes--padding".to_vec();
        let kid = "counted-kid";

        let discovery_body = json!({
            "issuer": server.uri(),
            "jwks_uri": format!("{}/jwks", server.uri()),
        });
        Mock::given(method("GET"))
            .and(path("/.well-known/openid-configuration"))
            .respond_with(ResponseTemplate::new(200).set_body_json(discovery_body))
            .expect(1) // hit exactly once across both validate() calls
            .mount(&server)
            .await;
        let jwks_body = json!({
            "keys": [
                {
                    "kty": "oct",
                    "kid": kid,
                    "alg": "HS256",
                    "k": base64_url(&secret),
                }
            ]
        });
        Mock::given(method("GET"))
            .and(path("/jwks"))
            .respond_with(ResponseTemplate::new(200).set_body_json(jwks_body))
            .expect(1) // also exactly once
            .mount(&server)
            .await;

        let validator = make_validator(&server.uri());

        // Mint two tokens with the same kid (would normally each cost a
        // network round-trip if uncached).
        let mut header = Header::new(Algorithm::HS256);
        header.kid = Some(kid.to_string());
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let claims = json!({
            "iss": server.uri(),
            "sub": "subj",
            "aud": "test-audience",
            "exp": now + 600,
            "iat": now,
            "solo_tenant": "default",
        });
        let token =
            jsonwebtoken::encode(&header, &claims, &EncodingKey::from_secret(&secret)).unwrap();

        let _ = validator
            .validate(Some(&format!("Bearer {token}")))
            .await
            .expect("first");
        let _ = validator
            .validate(Some(&format!("Bearer {token}")))
            .await
            .expect("second");
        // Drop the validator; wiremock asserts the .expect(1) counts on drop.
    }

    #[tokio::test]
    async fn oidc_jwks_cache_respects_cache_control_max_age() {
        // Cache-Control: max-age=0 → second validate MUST refetch.
        let server = MockServer::start().await;
        let secret = b"max-age-secret-bytes-for-tests--".to_vec();
        let kid = "max-age-kid";

        let discovery_body = json!({
            "issuer": server.uri(),
            "jwks_uri": format!("{}/jwks", server.uri()),
        });
        Mock::given(method("GET"))
            .and(path("/.well-known/openid-configuration"))
            .respond_with(
                ResponseTemplate::new(200)
                    .insert_header("cache-control", "max-age=0")
                    .set_body_json(discovery_body),
            )
            .expect(2) // two validate() calls each trigger a refetch
            .mount(&server)
            .await;
        let jwks_body = json!({
            "keys": [
                {
                    "kty": "oct",
                    "kid": kid,
                    "alg": "HS256",
                    "k": base64_url(&secret),
                }
            ]
        });
        Mock::given(method("GET"))
            .and(path("/jwks"))
            .respond_with(ResponseTemplate::new(200).set_body_json(jwks_body))
            .expect(2)
            .mount(&server)
            .await;

        let validator = make_validator(&server.uri());

        let mut header = Header::new(Algorithm::HS256);
        header.kid = Some(kid.to_string());
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let claims = json!({
            "iss": server.uri(),
            "sub": "subj",
            "aud": "test-audience",
            "exp": now + 600,
            "iat": now,
            "solo_tenant": "default",
        });
        let token =
            jsonwebtoken::encode(&header, &claims, &EncodingKey::from_secret(&secret)).unwrap();

        let _ = validator
            .validate(Some(&format!("Bearer {token}")))
            .await
            .expect("first");
        let _ = validator
            .validate(Some(&format!("Bearer {token}")))
            .await
            .expect("second");
    }

    #[test]
    fn parse_max_age_handles_typical_headers() {
        // bare max-age
        assert_eq!(
            parse_max_age(Some("max-age=300")),
            Some(Duration::from_secs(300))
        );
        // combined directives, whitespace-tolerant
        assert_eq!(
            parse_max_age(Some("public, max-age=86400, must-revalidate")),
            Some(Duration::from_secs(86400))
        );
        // missing → None → caller uses fallback
        assert_eq!(parse_max_age(Some("no-cache, no-store")), None);
        assert_eq!(parse_max_age(None), None);
    }
}