pensieve-server 0.1.0

HTTP + gRPC query API, auth stub, health, observability.
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
//! Generic OIDC bearer-token validation. Configured with one or more trusted
//! issuers; JWKS are fetched via OIDC discovery and cached, refreshing when
//! an unknown `kid` is seen (rate-limited). Tokens that do not look like JWTs
//! fall through to the wrapped inner backend, so native Pensieve tokens keep
//! working unchanged.

use super::backend::{AuthBackend, AuthError, Principal, Role};
use async_trait::async_trait;
use jsonwebtoken::{
    decode, decode_header,
    jwk::{Jwk, JwkSet},
    Algorithm, DecodingKey, Validation,
};
use std::{
    collections::HashMap,
    sync::Arc,
    time::{Duration, Instant},
};
use tokio::sync::RwLock;

/// Config parsed from env (see `from_env`).
#[derive(Debug, Clone)]
pub struct OidcConfig {
    pub issuers: Vec<String>,
    pub audience: String,
    pub role_claim: String,      // default "pensieve_role"
    pub subject_claim: String,   // default "sub"
    pub databases_claim: String, // default "pensieve_databases"
    pub realms_claim: String,    // default "pensieve_realms"
}

impl OidcConfig {
    /// `None` when `PENSIEVE_OIDC_ISSUERS` is unset/empty (OIDC disabled).
    pub fn from_env() -> Option<Self> {
        let issuers: Vec<String> = std::env::var("PENSIEVE_OIDC_ISSUERS")
            .ok()?
            .split(',')
            .map(|s| s.trim().trim_end_matches('/').to_owned())
            .filter(|s| !s.is_empty())
            .collect();
        if issuers.is_empty() {
            return None;
        }
        let audience = std::env::var("PENSIEVE_OIDC_AUDIENCE").unwrap_or_else(|_| "pensieve".into());
        Some(Self {
            issuers,
            audience,
            role_claim: std::env::var("PENSIEVE_OIDC_ROLE_CLAIM")
                .unwrap_or_else(|_| "pensieve_role".into()),
            subject_claim: std::env::var("PENSIEVE_OIDC_SUBJECT_CLAIM")
                .unwrap_or_else(|_| "sub".into()),
            databases_claim: std::env::var("PENSIEVE_OIDC_DATABASES_CLAIM")
                .unwrap_or_else(|_| "pensieve_databases".into()),
            realms_claim: std::env::var("PENSIEVE_OIDC_REALMS_CLAIM")
                .unwrap_or_else(|_| "pensieve_realms".into()),
        })
    }
}

struct CachedJwks {
    set: JwkSet,
    fetched_at: Instant,
    /// Last time we attempted a refresh for an unknown kid, to rate-limit.
    last_refresh_attempt: Option<Instant>,
}

/// Validates JWTs from configured OIDC issuers; delegates non-JWT tokens to
/// `inner`. Auth is "enabled" if OIDC is configured (inner may add native tokens).
pub struct OidcAuthBackend {
    cfg: OidcConfig,
    inner: Arc<dyn AuthBackend>,
    http: reqwest::Client,
    /// issuer -> cached JWKS
    jwks: RwLock<HashMap<String, CachedJwks>>,
    /// Minimum time between forced JWKS refreshes for an unknown kid.
    /// Defaults to [`JWKS_MIN_REFRESH`]. Override via [`Self::with_min_refresh`]
    /// in tests to bypass the 30-second throttle.
    min_refresh: Duration,
}

const JWKS_TTL: Duration = Duration::from_secs(3600);
const JWKS_MIN_REFRESH: Duration = Duration::from_secs(30);

impl OidcAuthBackend {
    /// Create a new backend wrapping `inner` for non-JWT tokens.
    pub fn new(cfg: OidcConfig, inner: Arc<dyn AuthBackend>) -> Self {
        Self {
            cfg,
            inner,
            http: reqwest::Client::new(),
            jwks: RwLock::new(HashMap::new()),
            min_refresh: JWKS_MIN_REFRESH,
        }
    }

    /// Override the minimum time between forced JWKS refreshes.
    ///
    /// Intended for integration tests that need to exercise key rotation without
    /// waiting 30 seconds. Pass `Duration::ZERO` to disable throttling entirely.
    #[doc(hidden)]
    pub fn with_min_refresh(mut self, d: Duration) -> Self {
        self.min_refresh = d;
        self
    }

    /// Returns true if the string looks like a three-segment base64url JWT.
    fn looks_like_jwt(token: &str) -> bool {
        token.split('.').count() == 3
    }

    /// Returns an error if `url` uses http:// with a non-loopback host.
    fn require_https_for_non_loopback(url: &str) -> Result<(), AuthError> {
        // Only enforce on http:// URLs.
        if !url.starts_with("http://") {
            return Ok(());
        }
        // Extract the host portion from http://host[:port]/...
        let rest = &url["http://".len()..];
        let host = rest.split('/').next().unwrap_or("").split(':').next().unwrap_or("");
        let loopback = matches!(host, "localhost" | "127.0.0.1" | "::1");
        if loopback {
            return Ok(());
        }
        Err(AuthError::Backend(format!(
            "oidc issuer must use https (got http for non-loopback host: {host})"
        )))
    }

    /// Fetch and cache the JWKS for `issuer`.
    ///
    /// If `force` is true, a refresh is attempted even if the TTL has not
    /// expired, subject to `JWKS_MIN_REFRESH` rate-limiting.
    async fn jwks_for(
        &self,
        issuer: &str,
        force: bool,
    ) -> Result<JwkSet, AuthError> {
        // Enforce HTTPS for non-loopback issuers before any network call.
        Self::require_https_for_non_loopback(issuer)?;

        // --- fast path: read lock ---
        {
            let guard = self.jwks.read().await;
            if let Some(cached) = guard.get(issuer) {
                let expired = cached.fetched_at.elapsed() > JWKS_TTL;
                if !expired && !force {
                    return Ok(cached.set.clone());
                }
                // Rate-limit forced refreshes.
                if force {
                    if let Some(last) = cached.last_refresh_attempt {
                        if last.elapsed() < self.min_refresh {
                            // Return stale rather than hammering the IdP.
                            return Ok(cached.set.clone());
                        }
                    }
                }
            }
        }

        // --- slow path: fetch then write lock ---
        let discovery_url = format!("{}/.well-known/openid-configuration", issuer);
        let discovery: serde_json::Value = self
            .http
            .get(&discovery_url)
            .send()
            .await
            .map_err(|e| AuthError::Backend(format!("OIDC discovery fetch failed: {e}")))?
            .json()
            .await
            .map_err(|e| AuthError::Backend(format!("OIDC discovery parse failed: {e}")))?;

        let jwks_uri = discovery
            .get("jwks_uri")
            .and_then(|v| v.as_str())
            .ok_or_else(|| AuthError::Backend("OIDC discovery missing jwks_uri".into()))?
            .to_owned();

        // Also enforce HTTPS for the jwks_uri from the discovery document.
        Self::require_https_for_non_loopback(&jwks_uri)?;

        let set: JwkSet = self
            .http
            .get(&jwks_uri)
            .send()
            .await
            .map_err(|e| AuthError::Backend(format!("JWKS fetch failed: {e}")))?
            .json()
            .await
            .map_err(|e| AuthError::Backend(format!("JWKS parse failed: {e}")))?;

        let mut guard = self.jwks.write().await;
        guard.insert(
            issuer.to_owned(),
            CachedJwks {
                set: set.clone(),
                fetched_at: Instant::now(),
                last_refresh_attempt: if force { Some(Instant::now()) } else { None },
            },
        );
        Ok(set)
    }

    /// Validate a JWT string and map claims to a [`Principal`].
    async fn validate_jwt(&self, token: &str) -> Result<Principal, AuthError> {
        // Step 1: decode the header to get `kid` and `alg`.
        let header = decode_header(token).map_err(|_| AuthError::UnknownToken)?;

        // Step 2: peek at the payload (second segment) to find `iss` without
        // full validation yet — we need `iss` to select the right JWKS.
        let parts: Vec<&str> = token.split('.').collect();
        if parts.len() != 3 {
            return Err(AuthError::UnknownToken);
        }
        let payload_bytes =
            base64_url_decode(parts[1]).map_err(|_| AuthError::UnknownToken)?;
        let payload: serde_json::Value =
            serde_json::from_slice(&payload_bytes).map_err(|_| AuthError::UnknownToken)?;

        let raw_iss = payload
            .get("iss")
            .and_then(|v| v.as_str())
            .ok_or(AuthError::UnknownToken)?;

        // Normalize for the allowlist check (strip trailing slash), but keep
        // the raw value to use in set_issuer so jsonwebtoken's literal
        // comparison matches what the token actually carries.
        let normalized_iss = raw_iss.trim_end_matches('/').to_owned();
        if !self.cfg.issuers.iter().any(|i| *i == normalized_iss) {
            return Err(AuthError::UnknownToken);
        }
        // The JWKS cache is keyed by normalized issuer.
        let iss = normalized_iss;

        // Step 3: find the signing key in the JWKS, refreshing once if kid is
        // unknown.
        let kid = header.kid.as_deref().unwrap_or("");
        let jwk = self.find_jwk(&iss, kid, false).await?;

        // Step 4: build the decoding key and validate.
        let decoding_key = DecodingKey::from_jwk(&jwk)
            .map_err(|e| AuthError::Backend(format!("DecodingKey from JWK: {e}")))?;

        // Use the algorithm from the token header, but only allow asymmetric
        // algorithms (RSA / EC families). Symmetric algs (HS*) are rejected
        // because they require sharing the secret and are not used by OIDC IdPs.
        let alg = header.alg;
        let allowed = [
            Algorithm::RS256,
            Algorithm::RS384,
            Algorithm::RS512,
            Algorithm::ES256,
            Algorithm::ES384,
        ];
        if !allowed.contains(&alg) {
            return Err(AuthError::UnknownToken);
        }

        let mut validation = Validation::new(alg);
        validation.set_audience(&[&self.cfg.audience]);
        // Use the token's literal (un-normalized) iss so that an Auth0-style
        // trailing slash in the token doesn't cause a spurious mismatch.
        // The security boundary is the allowlist check above (normalized).
        validation.set_issuer(&[raw_iss]);
        // jsonwebtoken defaults validate_nbf=false; we enforce it.
        validation.validate_nbf = true;

        let token_data =
            decode::<serde_json::Value>(token, &decoding_key, &validation)
                .map_err(|_| AuthError::UnknownToken)?;

        let claims = token_data.claims;

        // Step 5: map claims -> Principal.
        let role = claims
            .get(&self.cfg.role_claim)
            .and_then(|v| v.as_str())
            .and_then(Role::parse)
            .unwrap_or(Role::Read);

        let subject = claims
            .get(&self.cfg.subject_claim)
            .and_then(|v| v.as_str())
            .map(str::to_owned);

        let allowed_databases = claims
            .get(&self.cfg.databases_claim)
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|x| x.as_str().map(str::to_owned))
                    .collect::<Vec<_>>()
            });

        // Realms claim: absent → None (unrestricted). Present but malformed
        // (not a JSON array, or any element not a non-empty string) → REJECT
        // the token (fail closed). This deliberately differs from the
        // databases claim above, which silently maps a non-array to None — a
        // fail-open footgun we do not replicate for a security boundary.
        let allowed_realms = match claims.get(&self.cfg.realms_claim) {
            None => None,
            Some(serde_json::Value::Array(arr)) => {
                let mut out = Vec::with_capacity(arr.len());
                for x in arr {
                    match x.as_str().map(str::trim) {
                        Some(s) if !s.is_empty() => out.push(s.to_owned()),
                        _ => {
                            tracing::warn!(
                                claim = %self.cfg.realms_claim,
                                "rejecting token: realms claim element is not a non-empty string"
                            );
                            return Err(AuthError::UnknownToken);
                        }
                    }
                }
                Some(out)
            }
            Some(_) => {
                tracing::warn!(
                    claim = %self.cfg.realms_claim,
                    "rejecting token: realms claim is not a JSON array"
                );
                return Err(AuthError::UnknownToken);
            }
        };

        // NOTE: multi-tenant claim mapping (e.g. a `pensieve_tenant` claim that
        // picks the TenantId) is a cloud-track follow-up. Self-hosted always
        // uses DEFAULT_TENANT.
        Ok(Principal {
            tenant: pensieve_core::tenant::DEFAULT_TENANT,
            role,
            subject,
            allowed_databases,
            allowed_realms,
        })
    }

    /// Find the JWK by kid, optionally forcing a cache refresh on first miss.
    async fn find_jwk(
        &self,
        issuer: &str,
        kid: &str,
        already_refreshed: bool,
    ) -> Result<Jwk, AuthError> {
        let set = self.jwks_for(issuer, already_refreshed).await?;
        let found = set.keys.iter().find(|k| {
            k.common
                .key_id
                .as_deref()
                .map_or(kid.is_empty(), |id| id == kid)
        });
        match found {
            Some(k) => Ok(k.clone()),
            None if !already_refreshed => {
                // Unknown kid — try a forced refresh once.
                let set2 = self.jwks_for(issuer, true).await?;
                set2.keys
                    .into_iter()
                    .find(|k| {
                        k.common
                            .key_id
                            .as_deref()
                            .map_or(kid.is_empty(), |id| id == kid)
                    })
                    .ok_or(AuthError::UnknownToken)
            }
            None => Err(AuthError::UnknownToken),
        }
    }

    /// Test helper: inject a pre-built JwkSet into the cache, bypassing network.
    #[cfg(test)]
    pub(crate) async fn inject_jwks(&self, issuer: &str, set: JwkSet) {
        let mut guard = self.jwks.write().await;
        guard.insert(
            issuer.to_owned(),
            CachedJwks {
                set,
                fetched_at: Instant::now(),
                last_refresh_attempt: None,
            },
        );
    }
}

/// Decode a base64url segment (no padding required).
fn base64_url_decode(input: &str) -> Result<Vec<u8>, base64::DecodeError> {
    use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
    URL_SAFE_NO_PAD.decode(input)
}

#[async_trait]
impl AuthBackend for OidcAuthBackend {
    fn enabled(&self) -> bool {
        true
    }

    async fn authenticate(&self, token: &str) -> Result<Principal, AuthError> {
        if Self::looks_like_jwt(token) {
            match self.validate_jwt(token).await {
                Ok(p) => return Ok(p),
                Err(e) => {
                    // Only fall through to inner if inner is enabled.
                    if self.inner.enabled() {
                        return self.inner.authenticate(token).await;
                    }
                    return Err(e);
                }
            }
        }
        // Non-JWT: delegate to inner.
        self.inner.authenticate(token).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use jsonwebtoken::{encode, EncodingKey, Header};
    use rsa::{
        pkcs1::EncodeRsaPrivateKey,
        traits::PublicKeyParts as _,
        RsaPrivateKey,
    };
    use std::time::{SystemTime, UNIX_EPOCH};

    // ---------------------------------------------------------------------------
    // Test key material (generated once per test run)
    // ---------------------------------------------------------------------------

    fn make_rsa_key() -> RsaPrivateKey {
        let mut rng = rand::thread_rng();
        RsaPrivateKey::new(&mut rng, 2048).expect("generate RSA key")
    }

    /// Build a JwkSet from an RSA private key's public components.
    fn rsa_jwk_set(priv_key: &RsaPrivateKey, kid: &str) -> JwkSet {
        let pub_key = priv_key.to_public_key();
        let n = pub_key.n().to_bytes_be();
        let e = pub_key.e().to_bytes_be();

        use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
        let n_b64 = URL_SAFE_NO_PAD.encode(&n);
        let e_b64 = URL_SAFE_NO_PAD.encode(&e);

        let jwk_json = serde_json::json!({
            "keys": [{
                "kty": "RSA",
                "use": "sig",
                "kid": kid,
                "alg": "RS256",
                "n": n_b64,
                "e": e_b64,
            }]
        });
        serde_json::from_value(jwk_json).expect("build JwkSet")
    }

    fn encoding_key(priv_key: &RsaPrivateKey) -> EncodingKey {
        let pem = priv_key
            .to_pkcs1_pem(rsa::pkcs1::LineEnding::LF)
            .expect("to PEM");
        EncodingKey::from_rsa_pem(pem.as_bytes()).expect("EncodingKey")
    }

    fn now_secs() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs()
    }

    fn make_cfg(issuer: &str) -> OidcConfig {
        OidcConfig {
            issuers: vec![issuer.to_owned()],
            audience: "pensieve".into(),
            role_claim: "pensieve_role".into(),
            subject_claim: "sub".into(),
            databases_claim: "pensieve_databases".into(),
            realms_claim: "pensieve_realms".into(),
        }
    }

    /// Stub inner backend that recognises exactly one opaque token.
    struct StubInner {
        enabled: bool,
        token: String,
        principal: Principal,
    }

    #[async_trait]
    impl AuthBackend for StubInner {
        fn enabled(&self) -> bool {
            self.enabled
        }
        async fn authenticate(&self, token: &str) -> Result<Principal, AuthError> {
            if token == self.token {
                Ok(self.principal.clone())
            } else {
                Err(AuthError::UnknownToken)
            }
        }
    }

    fn stub_inner(enabled: bool) -> Arc<dyn AuthBackend> {
        Arc::new(StubInner {
            enabled,
            token: "opaque-1".into(),
            principal: Principal {
                tenant: pensieve_core::tenant::DEFAULT_TENANT,
                role: Role::Read,
                subject: Some("stub-subject".into()),
                allowed_databases: None,
                allowed_realms: None,
            },
        })
    }

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

    #[tokio::test]
    async fn validates_rs256_jwt_and_maps_claims() {
        let priv_key = make_rsa_key();
        let issuer = "https://test.example.com";
        let cfg = make_cfg(issuer);
        let backend = OidcAuthBackend::new(cfg, stub_inner(false));
        backend
            .inject_jwks(issuer, rsa_jwk_set(&priv_key, "test-key-1"))
            .await;

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some("test-key-1".into());

        let claims = serde_json::json!({
            "iss": issuer,
            "aud": "pensieve",
            "exp": now_secs() + 3600,
            "sub": "user-1",
            "pensieve_role": "write",
            "pensieve_databases": ["prod"],
        });

        let token = encode(&header, &claims, &encoding_key(&priv_key)).unwrap();
        let p = backend.authenticate(&token).await.unwrap();

        assert_eq!(p.role, Role::Write);
        assert_eq!(p.subject, Some("user-1".into()));
        assert_eq!(p.allowed_databases, Some(vec!["prod".into()]));
        assert_eq!(p.tenant, pensieve_core::tenant::DEFAULT_TENANT);
    }

    #[tokio::test]
    async fn missing_role_claim_defaults_to_read() {
        let priv_key = make_rsa_key();
        let issuer = "https://test.example.com";
        let cfg = make_cfg(issuer);
        let backend = OidcAuthBackend::new(cfg, stub_inner(false));
        backend
            .inject_jwks(issuer, rsa_jwk_set(&priv_key, "test-key-1"))
            .await;

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some("test-key-1".into());

        let claims = serde_json::json!({
            "iss": issuer,
            "aud": "pensieve",
            "exp": now_secs() + 3600,
            "sub": "user-2",
            // no pensieve_role
        });

        let token = encode(&header, &claims, &encoding_key(&priv_key)).unwrap();
        let p = backend.authenticate(&token).await.unwrap();
        assert_eq!(p.role, Role::Read);
    }

    #[tokio::test]
    async fn missing_databases_claim_means_unrestricted() {
        let priv_key = make_rsa_key();
        let issuer = "https://test.example.com";
        let cfg = make_cfg(issuer);
        let backend = OidcAuthBackend::new(cfg, stub_inner(false));
        backend
            .inject_jwks(issuer, rsa_jwk_set(&priv_key, "test-key-1"))
            .await;

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some("test-key-1".into());

        let claims = serde_json::json!({
            "iss": issuer,
            "aud": "pensieve",
            "exp": now_secs() + 3600,
            "sub": "user-3",
            // no pensieve_databases
        });

        let token = encode(&header, &claims, &encoding_key(&priv_key)).unwrap();
        let p = backend.authenticate(&token).await.unwrap();
        assert!(p.allowed_databases.is_none(), "should be unrestricted");
    }

    // -----------------------------------------------------------------------
    // Fix 1: nbf validation
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn rejects_not_yet_valid() {
        let priv_key = make_rsa_key();
        let issuer = "https://test.example.com";
        let cfg = make_cfg(issuer);
        let backend = OidcAuthBackend::new(cfg, stub_inner(false));
        backend
            .inject_jwks(issuer, rsa_jwk_set(&priv_key, "test-key-1"))
            .await;

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some("test-key-1".into());

        let claims = serde_json::json!({
            "iss": issuer,
            "aud": "pensieve",
            "exp": now_secs() + 7200,
            "nbf": now_secs() + 3600, // not valid yet
            "sub": "future-user",
        });

        let token = encode(&header, &claims, &encoding_key(&priv_key)).unwrap();
        let err = backend.authenticate(&token).await.unwrap_err();
        assert!(
            matches!(err, AuthError::UnknownToken),
            "expected UnknownToken for nbf in future, got {err:?}"
        );
    }

    // -----------------------------------------------------------------------
    // Fix 2: Issuer trailing-slash interop
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn accepts_issuer_with_trailing_slash_in_token() {
        // Config has no trailing slash; token iss has trailing slash.
        let priv_key = make_rsa_key();
        let issuer_config = "https://issuer.test";
        let issuer_token = "https://issuer.test/"; // trailing slash in token
        let cfg = make_cfg(issuer_config);
        let backend = OidcAuthBackend::new(cfg, stub_inner(false));
        // Cache under the normalized (no slash) key because from_env / make_cfg normalizes.
        backend
            .inject_jwks(issuer_config, rsa_jwk_set(&priv_key, "key-1"))
            .await;

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some("key-1".into());

        let claims = serde_json::json!({
            "iss": issuer_token,
            "aud": "pensieve",
            "exp": now_secs() + 3600,
            "sub": "slash-user",
        });

        let token = encode(&header, &claims, &encoding_key(&priv_key)).unwrap();
        let p = backend.authenticate(&token).await.unwrap();
        assert_eq!(p.subject, Some("slash-user".into()));
    }

    #[tokio::test]
    async fn accepts_trailing_slash_in_config() {
        // Regression guard: even if config had trailing slash (from_env strips it),
        // token without slash must still be accepted.
        let priv_key = make_rsa_key();
        // Build OidcConfig manually with already-stripped issuer (mirrors from_env behavior).
        let issuer_config = "https://issuer.test";
        let issuer_token = "https://issuer.test"; // no trailing slash
        let cfg = make_cfg(issuer_config);
        let backend = OidcAuthBackend::new(cfg, stub_inner(false));
        backend
            .inject_jwks(issuer_config, rsa_jwk_set(&priv_key, "key-2"))
            .await;

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some("key-2".into());

        let claims = serde_json::json!({
            "iss": issuer_token,
            "aud": "pensieve",
            "exp": now_secs() + 3600,
            "sub": "noslash-user",
        });

        let token = encode(&header, &claims, &encoding_key(&priv_key)).unwrap();
        let p = backend.authenticate(&token).await.unwrap();
        assert_eq!(p.subject, Some("noslash-user".into()));
    }

    // -----------------------------------------------------------------------
    // Fix 3: HTTPS enforcement for non-loopback JWKS
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn rejects_http_issuer_for_non_loopback() {
        // http:// with a non-loopback host must fail before any network call.
        let priv_key = make_rsa_key();
        let issuer = "http://idp.example.com";
        let cfg = make_cfg(issuer);
        let backend = OidcAuthBackend::new(cfg, stub_inner(false));
        // Do NOT inject JWKS — the scheme check should fire before any fetch.
        backend
            .inject_jwks(issuer, rsa_jwk_set(&priv_key, "key-1"))
            .await;

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some("key-1".into());

        let claims = serde_json::json!({
            "iss": issuer,
            "aud": "pensieve",
            "exp": now_secs() + 3600,
            "sub": "http-user",
        });

        let token = encode(&header, &claims, &encoding_key(&priv_key)).unwrap();
        let err = backend.authenticate(&token).await.unwrap_err();
        assert!(
            matches!(err, AuthError::Backend(ref msg) if msg.contains("https")),
            "expected Backend error mentioning https, got {err:?}"
        );
    }

    // -----------------------------------------------------------------------
    // Fix 4: Bad-signature test
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn rejects_bad_signature() {
        // Sign token with key_a but serve JwkSet with key_b under the same kid.
        let key_a = make_rsa_key();
        let key_b = make_rsa_key();
        let issuer = "https://test.example.com";
        let cfg = make_cfg(issuer);
        let backend = OidcAuthBackend::new(cfg, stub_inner(false));
        // Inject key_b's public key under kid "shared-kid"
        backend
            .inject_jwks(issuer, rsa_jwk_set(&key_b, "shared-kid"))
            .await;

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some("shared-kid".into());

        let claims = serde_json::json!({
            "iss": issuer,
            "aud": "pensieve",
            "exp": now_secs() + 3600,
            "sub": "bad-sig-user",
        });

        // Sign with key_a — JWKS has key_b → signature mismatch
        let token = encode(&header, &claims, &encoding_key(&key_a)).unwrap();
        let err = backend.authenticate(&token).await.unwrap_err();
        assert!(
            matches!(err, AuthError::UnknownToken),
            "expected UnknownToken for bad signature, got {err:?}"
        );
    }

    #[tokio::test]
    async fn rejects_expired() {
        let priv_key = make_rsa_key();
        let issuer = "https://test.example.com";
        let cfg = make_cfg(issuer);
        let backend = OidcAuthBackend::new(cfg, stub_inner(false));
        backend
            .inject_jwks(issuer, rsa_jwk_set(&priv_key, "test-key-1"))
            .await;

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some("test-key-1".into());

        let claims = serde_json::json!({
            "iss": issuer,
            "aud": "pensieve",
            "exp": now_secs() - 7200,  // expired well past any leeway
            "sub": "user-expired",
        });

        let token = encode(&header, &claims, &encoding_key(&priv_key)).unwrap();
        let err = backend.authenticate(&token).await.unwrap_err();
        assert!(
            matches!(err, AuthError::UnknownToken),
            "expected UnknownToken, got {err:?}"
        );
    }

    #[tokio::test]
    async fn rejects_wrong_audience() {
        let priv_key = make_rsa_key();
        let issuer = "https://test.example.com";
        let cfg = make_cfg(issuer);
        let backend = OidcAuthBackend::new(cfg, stub_inner(false));
        backend
            .inject_jwks(issuer, rsa_jwk_set(&priv_key, "test-key-1"))
            .await;

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some("test-key-1".into());

        let claims = serde_json::json!({
            "iss": issuer,
            "aud": "wrong-audience",
            "exp": now_secs() + 3600,
            "sub": "user-aud",
        });

        let token = encode(&header, &claims, &encoding_key(&priv_key)).unwrap();
        let err = backend.authenticate(&token).await.unwrap_err();
        assert!(matches!(err, AuthError::UnknownToken));
    }

    #[tokio::test]
    async fn rejects_wrong_issuer() {
        let priv_key = make_rsa_key();
        // Configure backend to trust "https://trusted.example.com" but token
        // carries "https://evil.example.com".
        let trusted = "https://trusted.example.com";
        let evil = "https://evil.example.com";
        let cfg = make_cfg(trusted);
        let backend = OidcAuthBackend::new(cfg, stub_inner(false));
        // Only inject JWKS for trusted — evil never gets to network lookup.
        backend
            .inject_jwks(trusted, rsa_jwk_set(&priv_key, "test-key-1"))
            .await;

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some("test-key-1".into());

        let claims = serde_json::json!({
            "iss": evil,
            "aud": "pensieve",
            "exp": now_secs() + 3600,
            "sub": "evil-user",
        });

        let token = encode(&header, &claims, &encoding_key(&priv_key)).unwrap();
        // The iss check happens before any JWKS/network call.
        let err = backend.authenticate(&token).await.unwrap_err();
        assert!(matches!(err, AuthError::UnknownToken));
    }

    #[tokio::test]
    async fn rejects_unknown_kid() {
        // Token signed with kid "other" while JWKS only has "test-key-1".
        // The force-refresh path will attempt network to an unreachable address,
        // which will map to AuthError::Backend or AuthError::UnknownToken.
        // We assert only that the result is an error.
        let priv_key = make_rsa_key();
        // Use 127.0.0.1:1 — TCP connect will fail immediately (no listener).
        let issuer = "http://127.0.0.1:1";
        let cfg = make_cfg(issuer);
        let backend = OidcAuthBackend::new(cfg, stub_inner(false));
        // Inject a JWK with kid "test-key-1" only.
        backend
            .inject_jwks(issuer, rsa_jwk_set(&priv_key, "test-key-1"))
            .await;

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some("other".into()); // not in JWKS

        let claims = serde_json::json!({
            "iss": issuer,
            "aud": "pensieve",
            "exp": now_secs() + 3600,
            "sub": "some-user",
        });

        let token = encode(&header, &claims, &encoding_key(&priv_key)).unwrap();
        // The force-refresh will try http://127.0.0.1:1/.well-known/... which
        // fails fast with a connection error → Backend error, or the stale
        // cache is returned and kid is still missing → UnknownToken.
        let err = backend.authenticate(&token).await.unwrap_err();
        assert!(
            matches!(err, AuthError::UnknownToken | AuthError::Backend(_)),
            "expected error for unknown kid, got {err:?}"
        );
    }

    #[tokio::test]
    async fn non_jwt_token_falls_through_to_inner() {
        let issuer = "https://test.example.com";
        let cfg = make_cfg(issuer);
        let backend = OidcAuthBackend::new(cfg, stub_inner(true));

        // "opaque-1" has no dots → not a JWT → delegated to inner
        let p = backend.authenticate("opaque-1").await.unwrap();
        assert_eq!(p.subject, Some("stub-subject".into()));
    }

    #[tokio::test]
    async fn jwt_failing_oidc_falls_through_when_inner_enabled() {
        // A valid-looking JWT from an untrusted issuer; inner is enabled.
        // The OIDC validation returns UnknownToken; we fall through to inner,
        // which also rejects it (unknown token).
        let priv_key = make_rsa_key();
        let evil = "https://evil.example.com";
        let trusted = "https://trusted.example.com";
        let cfg = make_cfg(trusted);
        let backend = OidcAuthBackend::new(cfg, stub_inner(true));

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some("test-key-1".into());

        let claims = serde_json::json!({
            "iss": evil,
            "aud": "pensieve",
            "exp": now_secs() + 3600,
            "sub": "evil",
        });
        let token = encode(&header, &claims, &encoding_key(&priv_key)).unwrap();

        // JWT with wrong issuer → falls through to inner; inner rejects it.
        let err = backend.authenticate(&token).await.unwrap_err();
        assert!(matches!(err, AuthError::UnknownToken));
    }

    #[tokio::test]
    async fn jwt_failing_oidc_errors_when_inner_disabled() {
        let priv_key = make_rsa_key();
        let evil = "https://evil.example.com";
        let trusted = "https://trusted.example.com";
        let cfg = make_cfg(trusted);
        let backend = OidcAuthBackend::new(cfg, stub_inner(false));

        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some("test-key-1".into());

        let claims = serde_json::json!({
            "iss": evil,
            "aud": "pensieve",
            "exp": now_secs() + 3600,
            "sub": "evil",
        });
        let token = encode(&header, &claims, &encoding_key(&priv_key)).unwrap();

        // JWT with wrong issuer, inner disabled → direct error (no fallback).
        let err = backend.authenticate(&token).await.unwrap_err();
        assert!(matches!(err, AuthError::UnknownToken));
    }
}