krabka-security 0.8.0

TLS, SASL, SCRAM, OAuth, and Kerberos security utilities for Krabka
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
770
771
772
773
774
775
776
777
//! JSON Web Key Set (RFC 7517) parsing + JWS signature verification.
//!
//! Pure logic, no I/O: this module parses a JWKS *string* and verifies a
//! token's signature against an in-memory [`Jwks`]. The broker fetches the key
//! set over the network and feeds it in through a [`JwksHandle`]. Supported key
//! types / algorithms:
//!
//! - `RS256`: RSASSA-PKCS1-v1_5 + SHA-256 over an `RSA` key (`n`, `e`).
//! - `ES256`: ECDSA P-256 + SHA-256 over an `EC` key (`crv:P-256`, `x`, `y`).
//!
//! These cover most OAuth 2.0 identity providers. This module delegates
//! verification to `ring`, the same crypto backend that the rest of
//! `crates/security` uses, and does not pull in a JWT crate. That keeps the
//! dependency surface small and keeps the temporal checks (`exp` / `iat` /
//! `nbf`) injectable for deterministic tests.

use std::{
    collections::HashMap,
    sync::{
        Arc,
        atomic::{AtomicI64, Ordering},
    },
};

use arc_swap::ArcSwap;
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD as B64URL};
use ring::signature;
use serde_json::Value;
use tokio::sync::mpsc;

use crate::AuthError;

/// A single supported public key from a JWKS, keyed externally by `kid`.
#[derive(Debug, Clone, PartialEq, Eq)]
enum JwkKey {
    /// RSA public key: big-endian modulus + exponent, as decoded from the JWK
    /// `n` / `e` members. This module passes them verbatim to `ring`'s
    /// `RsaPublicKeyComponents`.
    Rsa { n: Vec<u8>, e: Vec<u8> },
    /// EC P-256 public key: the SEC1 uncompressed point `0x04 ‖ x ‖ y`, ready
    /// for `ring`'s `UnparsedPublicKey`.
    EcP256 { point: Vec<u8> },
}

/// A parsed JWK key set.
///
/// Lookups are by `kid`. This module stores a key without a `kid` under the
/// empty string, and uses that key when a token header omits `kid` and the set
/// holds exactly one key.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Jwks {
    keys: HashMap<String, JwkKey>,
}

impl Jwks {
    /// An empty key set. Nothing validates until the broker's refresher fills
    /// it.
    #[must_use]
    // cargo-mutants: trivial constructor.
    #[cfg_attr(test, mutants::skip)]
    pub fn empty() -> Self {
        Self {
            keys: HashMap::new(),
        }
    }

    /// Whether the key set has no usable keys.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.keys.is_empty()
    }

    /// Number of usable keys. The count skips unsupported `kty` / `crv`.
    #[must_use]
    pub fn len(&self) -> usize {
        self.keys.len()
    }

    /// Parse an RFC 7517 JWKS document.
    ///
    /// This function skips keys with an unsupported `kty` / `crv` / `alg`. A
    /// skipped key is not an error, because identity providers publish
    /// encryption keys and other algs next to the signing keys. A document that
    /// is not valid JSON, or that has no `keys` array, is an error.
    ///
    /// When `ignore_key_use` is `false`, which is the default, this function
    /// removes keys with `use=enc`. Encryption-only keys are unsuitable for
    /// signature verification. When `ignore_key_use` is `true`, this function
    /// keeps all keys regardless of `use`. Strimzi exposes the same toggle.
    /// Some identity providers serve signing keys with `use:"enc"` by mistake,
    /// and operators sometimes need to override.
    ///
    /// # Errors
    ///
    /// [`AuthError::MalformedMessage`] when the document is not valid JSON or
    /// has no `keys` array.
    // JWKS key-set load/reload. skip_all keeps the raw JWKS document `s` out of
    // span fields (it is not secret, but is large); only the non-sensitive
    // `ignore_key_use` toggle is recorded. `err` surfaces a parse failure.
    #[tracing::instrument(level = "info", skip_all, fields(ignore_key_use = ignore_key_use), err)]
    pub fn from_json(s: &str, ignore_key_use: bool) -> Result<Self, AuthError> {
        let doc: Value = serde_json::from_str(s).map_err(|_| AuthError::MalformedMessage)?;
        let arr = doc
            .get("keys")
            .and_then(Value::as_array)
            .ok_or(AuthError::MalformedMessage)?;
        let mut keys = HashMap::new();
        for jwk in arr {
            let Some((kid, key)) = parse_one_jwk(jwk, ignore_key_use) else {
                continue;
            };
            keys.insert(kid, key);
        }
        Ok(Self { keys })
    }

    /// Whether this key set holds an entry for `kid`. Convenience for callers
    /// that want to assert on individual keys without exposing the internal
    /// map type.
    #[must_use]
    pub fn contains_kid(&self, kid: &str) -> bool {
        self.keys.contains_key(kid)
    }

    /// Verify a JWS `signature` over `signing_input` with the key that `kid` /
    /// `alg` select.
    ///
    /// `signing_input` is the ASCII `header_b64 "." payload_b64`.
    ///
    /// `kid` is the token header `kid` when the header has one. When `kid` is
    /// `None`, the set must hold exactly one key. `alg` must be `RS256` or
    /// `ES256` and must match the selected key's type.
    ///
    /// # Errors
    ///
    /// [`AuthError::InvalidToken`] for an unknown / ambiguous key, an
    /// alg/key-type mismatch, or a bad signature.
    // Per-token JWS signature verification. skip_all keeps `signing_input` +
    // `signature` bytes out of span fields; only the non-sensitive `alg` and
    // `kid` (a key identifier, not a secret) are recorded. `err` surfaces a
    // verification failure (Debug).
    #[tracing::instrument(
        level = "debug",
        skip_all,
        fields(alg = %alg, kid = kid.unwrap_or("<none>")),
        err
    )]
    pub fn verify(
        &self,
        kid: Option<&str>,
        alg: &str,
        signing_input: &[u8],
        signature: &[u8],
    ) -> Result<(), AuthError> {
        let key = self.select_key(kid)?;
        match (alg, key) {
            ("RS256", JwkKey::Rsa { n, e }) => {
                let pk = signature::RsaPublicKeyComponents { n, e };
                pk.verify(
                    &signature::RSA_PKCS1_2048_8192_SHA256,
                    signing_input,
                    signature,
                )
                .map_err(|_| AuthError::InvalidToken)
            }
            ("ES256", JwkKey::EcP256 { point }) => {
                let pk =
                    signature::UnparsedPublicKey::new(&signature::ECDSA_P256_SHA256_FIXED, point);
                pk.verify(signing_input, signature)
                    .map_err(|_| AuthError::InvalidToken)
            }
            // alg present but no key of the matching type, or unsupported alg.
            _ => Err(AuthError::InvalidToken),
        }
    }

    /// Pick the key for `kid`. With no `kid`, succeed only when the set holds a
    /// single key. Any other count makes the selection ambiguous.
    fn select_key(&self, kid: Option<&str>) -> Result<&JwkKey, AuthError> {
        kid.map_or_else(
            || {
                if self.keys.len() == 1 {
                    self.keys.values().next().ok_or(AuthError::InvalidToken)
                } else {
                    Err(AuthError::InvalidToken)
                }
            },
            |kid| self.keys.get(kid).ok_or(AuthError::InvalidToken),
        )
    }
}

/// Parse a single JWK object into a `(kid, key)` pair.
///
/// This function returns `None` for an unsupported or malformed key. Such a key
/// is skipped and is not fatal. The function honors `use` when the key has it: it
/// skips a key marked for encryption with `use: "enc"` unless `ignore_key_use`
/// is true.
fn parse_one_jwk(jwk: &Value, ignore_key_use: bool) -> Option<(String, JwkKey)> {
    if !ignore_key_use && jwk.get("use").and_then(Value::as_str) == Some("enc") {
        return None;
    }
    let kid = jwk
        .get("kid")
        .and_then(Value::as_str)
        .unwrap_or("")
        .to_owned();
    match jwk.get("kty").and_then(Value::as_str)? {
        "RSA" => {
            let n = b64url_field(jwk, "n")?;
            let e = b64url_field(jwk, "e")?;
            Some((kid, JwkKey::Rsa { n, e }))
        }
        "EC" => {
            if jwk.get("crv").and_then(Value::as_str)? != "P-256" {
                return None;
            }
            // P-256 coordinates are 32 bytes; JWK base64url may drop leading
            // zero bytes, so left-pad before assembling the SEC1 point.
            let x = left_pad_32(&b64url_field(jwk, "x")?)?;
            let y = left_pad_32(&b64url_field(jwk, "y")?)?;
            let mut point = Vec::with_capacity(65);
            point.push(0x04);
            point.extend_from_slice(&x);
            point.extend_from_slice(&y);
            Some((kid, JwkKey::EcP256 { point }))
        }
        _ => None,
    }
}

/// base64url-decode a string-valued JWK member.
fn b64url_field(jwk: &Value, key: &str) -> Option<Vec<u8>> {
    let s = jwk.get(key).and_then(Value::as_str)?;
    B64URL.decode(s).ok()
}

/// Left-pad a big-endian coordinate to exactly 32 bytes.
///
/// This function returns `None` if the input is already longer than 32 bytes,
/// which is a malformed P-256 coordinate.
fn left_pad_32(bytes: &[u8]) -> Option<[u8; 32]> {
    let start = 32usize.checked_sub(bytes.len())?;
    let mut out = [0u8; 32];
    out.get_mut(start..)?.copy_from_slice(bytes);
    Some(out)
}

/// A cheaply-clonable, atomically-swappable holder for the live [`Jwks`].
///
/// Mirrors `DynamicServerConfig`: the broker's background JWKS
/// refresher [`store`](JwksHandle::store)s a freshly-fetched key set while
/// validators [`load`](JwksHandle::load) the current one with no lock. A clone
/// of a handle shares the same underlying cell.
///
/// A handle paired with a refresher also carries a shared
/// `last_successful_fetch_ms` counter for the hard cache-expiry checks in the
/// signed validator. It carries a `signal_tx` mpsc sender as well, for
/// fire-and-forget on-demand refresh requests when a validator meets an
/// unknown-kid or bad-signature token. A default-constructed handle carries no
/// signal sender, and `signal_refresh()` is a silent no-op on those.
#[derive(Debug, Clone)]
pub struct JwksHandle {
    keys: Arc<ArcSwap<Jwks>>,
    /// Epoch ms of the last successful refresh. Validators
    /// check this against `expiry_ms` to fail closed on a stale cache.
    /// The `0` sentinel means never successfully fetched, the initial state.
    last_successful_fetch_ms: Arc<AtomicI64>,
    /// Fire-and-forget signal sender to the refresher.
    /// The validator calls `signal_refresh()` on a verify failure, which is an
    /// unknown kid or a bad signature. `None` when the validator is not paired
    /// with a refresher, such as a default-constructed `JwksHandle` in a
    /// non-signed validator or in the pre-`apply_to` state.
    signal_tx: Option<mpsc::Sender<()>>,
}

impl JwksHandle {
    /// Wrap an initial key set with NO refresher coordination.
    ///
    /// The initial key set is often [`Jwks::empty`] at startup.
    /// Default-constructed handles use this constructor, as do tests that do
    /// not need the signal channel. `signal_refresh()` on these is a silent
    /// no-op.
    #[must_use]
    pub fn new(jwks: Jwks) -> Self {
        Self {
            keys: Arc::new(ArcSwap::from_pointee(jwks)),
            last_successful_fetch_ms: Arc::new(AtomicI64::new(0)),
            signal_tx: None,
        }
    }

    /// Wrap an initial key set WITH the shared timestamp counter
    /// and signal sender pre-wired.
    ///
    /// The refresher constructs its own `(signal_tx, signal_rx)` pair and
    /// passes `signal_tx` here. The refresher keeps `signal_rx` and a clone of
    /// the shared `Arc<AtomicI64>` for the timestamp updates.
    #[must_use]
    pub fn new_with_refresher_handles(
        jwks: Jwks,
        last_successful_fetch_ms: Arc<AtomicI64>,
        signal_tx: mpsc::Sender<()>,
    ) -> Self {
        Self {
            keys: Arc::new(ArcSwap::from_pointee(jwks)),
            last_successful_fetch_ms,
            signal_tx: Some(signal_tx),
        }
    }

    /// Atomically replace the key set.
    ///
    /// The refresher calls this after a successful fetch. The swap is
    /// lock-free. A concurrent `load` sees either the old set or the new set,
    /// never a torn one.
    pub fn store(&self, jwks: Jwks) {
        self.keys.store(Arc::new(jwks));
    }

    /// Load the current key set. This costs one `Arc` clone.
    #[must_use]
    pub fn load(&self) -> Arc<Jwks> {
        self.keys.load_full()
    }

    /// Epoch-ms timestamp of the last successful JWKS fetch.
    ///
    /// The value is `0` if no fetch has succeeded yet, the initial state.
    /// Validators compare it against `now_ms - expiry_ms` to enforce hard cache
    /// expiry.
    #[must_use]
    pub fn last_successful_fetch_ms(&self) -> i64 {
        self.last_successful_fetch_ms.load(Ordering::Relaxed)
    }

    /// Fire-and-forget signal to the refresher that asks for an on-demand
    /// refresh, for example after an unknown-kid token.
    ///
    /// This call does not block. It drops the signal silently if the channel is
    /// full, because signals coalesce and one is enough. It is a no-op when
    /// `signal_tx` is `None`, which is the case for default-constructed
    /// handles.
    pub fn signal_refresh(&self) {
        if let Some(tx) = &self.signal_tx {
            let _ = tx.try_send(());
        }
    }
}

impl Default for JwksHandle {
    fn default() -> Self {
        Self::new(Jwks::empty())
    }
}

// Token-minting helpers the sibling `oauthbearer` tests reuse. Declared before
// the test module so clippy's `items_after_test_module` stays quiet.
#[cfg(test)]
pub use tests::{mint_es256, mint_rs256, mint_rs256_with_header};

#[cfg(test)]
mod tests {
    use assert2::check;
    use ring::{
        rand::SystemRandom,
        signature::{EcdsaKeyPair, KeyPair, RsaKeyPair},
    };

    use super::*;

    /// A static RSA-2048 PKCS#8 key, generated with `openssl genpkey`.
    ///
    /// `ring` cannot generate RSA keys, so tests mint RS256 tokens from this
    /// fixed key. Production never sees a private key. It reads `n`/`e` from
    /// JWKS.
    const RSA_PKCS8_B64: &str = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC1Ekoc++7sSsH55QXBCq/aj71helk6ZCTkzYxfLRZXbox0FcV7vOkLNodetJLY7nAUekZLltQ7Q6FJ42geqGV+vgttF63Ue9OP24mPmn/OiFqVYhBaJDRI5BMBLCqZbUfpNBDh7ZOCczwlX8Z5FQS0QJBA4F26H9AKzFRvofwHFk1wxqiGdgwDyClgi+eDnhEGGhBEHuTl1edvTRif88rLDfPHKG1TRqKC6LMXCZQdNy7lrDEGPKHqfW4mb2mq7Vj6h2Jjv+1SpsSxdqX8Tsua4/LrAKvFIXfoZAnjzhACbhXqf1DdSdInZ0i1adY8JpgJQ+WtJ0i9aIOnnmDYwgMvAgMBAAECggEAHqBqUr62Kdd3Odpn/7/cAL7hTHSSVRMNPnoZ7RtGNSGothXcolJQpKnjebxXPkQORxhrfWuUmDWXOVUyjkTzbd2dNyWTLGaJYULD4LtENN3RXIUKuQR4p3+US1V6Gxtl12cMF/rEQYNWQAgUHPTWJ9rny2Fn2Qx6dukauwsOAvCU47fL873sm06SYgPJsLm7MKVeifl8dDudgpURxeC9z37cm9kjjE6n6aiBTNAuBEkMaAbcfgJ0RZfzaMo7IpsOeyOwp932JDlKROpQWKA+lz08YzhkU81qHJYOS/js2F0jxzFz31D9IN+OLu7vRCANFLJl/qnin1JEgVPh7gxSKQKBgQDfrQEsutvH1746ytfE+4jUXyv7Fuaz9MML8uaJbC4hMFdCJuMuLBY07bDE23+4byuWY7JHrgsLRaZ+qpNGWs3LH2x6xsHiK8Ivpuy8TVUJ6hgkPK1cr8yUJxaDcyV8tJAZ+mFmyyWx7wUdlgJFCa2MQF1HnrlBKZvSLWV4CjctZQKBgQDPPR2wLwyk6JlyapsVnCpNBGcXqbJxPh1TM7uPqlODxTzegUK+TMJDZ840u2aBNXf2D5WIJMl+/ohYefOOqK9z2OJUGObnJMgGusH04rdbBoDCdBwfwjiluU7vxbuQKBu8JNXzeb7HJhmgxtXWdJuFYcYbmGu8leFvmUxZTPRfAwKBgQCm6Gpf/m/SiGMjbAnmq+xGzV38V/J/hr2lRPRSx68EhRYX/vy3j55ikJu/yitcbViROIPoiS8kkizTiGWtskSuthw04ev74btd46n0OaCjbVPmdoDHEUgPpbtfC6WFkReWyweztRPD2yBuG2pGKhqe9cilkQOcZHgqNkXpdXYHIQKBgQCO0BQkdNfm0O/l3DdRdhPkjVMqCGSTC3YT/0OS5pK07PhccYF4ONdqsh91UWt7QUiRBf5LGubMoEV/i1LfjbmTQPP/dkWxJjS+Bndg9dfbX6jd2DwFWsfE1OXj8ESoPCuYxV23cr+Y59WjaUK1jhgam9106N3d0P/Q8zidFZ4V1wKBgQDFvIqMLnpaInWhb7kP+X6o0tPQSg+6odMWPnjhwnpSIiUjPUTZV4ijc/d1tPsUemFQxDe+ZreQXDMVGcAVldFnoEMyL8iAtMAHtsSmq2E80RNZfc6nUgy5esQ9rJeX2pH9aZCVvKv6iVTeUtAxS+ltjmEG9BSEI2WQI1WDzPbKiA==";

    fn rsa_pkcs8() -> Vec<u8> {
        use base64::engine::general_purpose::STANDARD;
        STANDARD.decode(RSA_PKCS8_B64).unwrap()
    }

    /// Split the two big-endian INTEGERs, modulus and publicExponent, out of a
    /// PKCS#1 `RSAPublicKey` DER.
    ///
    /// This helper is test-only. Production reads `n`/`e` straight from JWKS
    /// JSON.
    fn split_pkcs1_public(der: &[u8]) -> (Vec<u8>, Vec<u8>) {
        // SEQUENCE { INTEGER n, INTEGER e }
        let mut p = 0usize;
        assert2::assert!(der[p] == 0x30);
        p += 1;
        let (_seq_len, adv) = read_der_len(&der[p..]);
        p += adv;
        let n = read_der_integer(der, &mut p);
        let e = read_der_integer(der, &mut p);
        (n, e)
    }

    fn read_der_len(b: &[u8]) -> (usize, usize) {
        if b[0] & 0x80 == 0 {
            (b[0] as usize, 1)
        } else {
            let nbytes = (b[0] & 0x7f) as usize;
            let mut len = 0usize;
            for i in 0..nbytes {
                len = (len << 8) | b[1 + i] as usize;
            }
            (len, 1 + nbytes)
        }
    }

    fn read_der_integer(der: &[u8], p: &mut usize) -> Vec<u8> {
        assert2::assert!(der[*p] == 0x02);
        *p += 1;
        let (len, adv) = read_der_len(&der[*p..]);
        *p += adv;
        let mut bytes = der[*p..*p + len].to_vec();
        *p += len;
        // Strip the leading zero a DER INTEGER prepends to keep the high bit
        // clear; JWK `n`/`e` are unsigned big-endian.
        if bytes.first() == Some(&0) {
            bytes.remove(0);
        }
        bytes
    }

    fn b64(b: &[u8]) -> String {
        B64URL.encode(b)
    }

    /// Mint an RS256 token signed by the static RSA key and return
    /// `(token, jwks_json)`.
    ///
    /// The JWKS advertises the matching public key under `kid`.
    fn rs256(kid: &str, claims: &str) -> (String, String) {
        let der = rsa_pkcs8();
        let kp = RsaKeyPair::from_pkcs8(&der).unwrap();
        let header = format!("{{\"alg\":\"RS256\",\"kid\":\"{kid}\"}}");
        let signing_input = format!("{}.{}", b64(header.as_bytes()), b64(claims.as_bytes()));
        let mut sig = vec![0u8; kp.public().modulus_len()];
        kp.sign(
            &signature::RSA_PKCS1_SHA256,
            &SystemRandom::new(),
            signing_input.as_bytes(),
            &mut sig,
        )
        .unwrap();
        let token = format!("{signing_input}.{}", b64(&sig));

        let pkcs1 = kp.public().as_ref();
        let (n, e) = split_pkcs1_public(pkcs1);
        let jwks = format!(
            "{{\"keys\":[{{\"kty\":\"RSA\",\"kid\":\"{kid}\",\"n\":\"{}\",\"e\":\"{}\"}}]}}",
            b64(&n),
            b64(&e),
        );
        (token, jwks)
    }

    /// Generate a fresh ES256 key pair and return `(key_pair, jwks_json)`.
    fn es256_key(kid: &str) -> (EcdsaKeyPair, String) {
        let rng = SystemRandom::new();
        let pkcs8 = EcdsaKeyPair::generate_pkcs8(&signature::ECDSA_P256_SHA256_FIXED_SIGNING, &rng)
            .unwrap();
        let kp = EcdsaKeyPair::from_pkcs8(
            &signature::ECDSA_P256_SHA256_FIXED_SIGNING,
            pkcs8.as_ref(),
            &rng,
        )
        .unwrap();
        let point = kp.public_key().as_ref(); // 0x04 || x || y
        let x = &point[1..33];
        let y = &point[33..65];
        let jwks = format!(
            "{{\"keys\":[{{\"kty\":\"EC\",\"crv\":\"P-256\",\"kid\":\"{kid}\",\"x\":\"{}\",\"y\":\"{}\"}}]}}",
            b64(x),
            b64(y),
        );
        (kp, jwks)
    }

    fn es256_token(kp: &EcdsaKeyPair, kid: &str, claims: &str) -> String {
        let header = format!("{{\"alg\":\"ES256\",\"kid\":\"{kid}\"}}");
        let signing_input = format!("{}.{}", b64(header.as_bytes()), b64(claims.as_bytes()));
        let sig = kp
            .sign(&SystemRandom::new(), signing_input.as_bytes())
            .unwrap();
        format!("{signing_input}.{}", b64(sig.as_ref()))
    }

    /// Pull `(kid, alg, signing_input, sig)` out of a compact JWS for the
    /// verify-level tests.
    fn parts(token: &str) -> (Option<String>, String, Vec<u8>, Vec<u8>) {
        let segs: Vec<&str> = token.split('.').collect();
        let header: Value = serde_json::from_slice(&B64URL.decode(segs[0]).unwrap()).unwrap();
        let kid = header.get("kid").and_then(Value::as_str).map(str::to_owned);
        let alg = header
            .get("alg")
            .and_then(Value::as_str)
            .unwrap()
            .to_owned();
        let signing_input = format!("{}.{}", segs[0], segs[1]).into_bytes();
        let sig = B64URL.decode(segs[2]).unwrap();
        (kid, alg, signing_input, sig)
    }

    #[test]
    fn empty_jwks_has_no_keys() {
        let jwks = Jwks::empty();
        check!((jwks.is_empty(), jwks.len(), jwks.contains_kid("k1")) == (true, 0, false));
    }

    #[test]
    fn jwks_with_key_is_not_empty() {
        let (_token, jwks_json) = rs256("k1", "{\"sub\":\"alice\",\"exp\":9999999999}");
        let jwks = Jwks::from_json(&jwks_json, false).expect("parse jwks");
        check!((jwks.is_empty(), jwks.len(), jwks.contains_kid("k1")) == (false, 1, true));
    }

    #[test]
    fn left_pad_32_accepts_short_and_exact_coordinates_only() {
        assert2::assert!(left_pad_32(&[0xAA; 33]).is_none());

        let short = left_pad_32(&[0x01, 0x02]).expect("short coordinate padded");
        assert2::assert!(&short[..30] == &[0u8; 30][..]);
        assert2::assert!(&short[30..] == &[0x01, 0x02][..]);

        let exact = [0x7Fu8; 32];
        assert2::assert!(left_pad_32(&exact).expect("exact coordinate") == exact);
    }

    #[test]
    fn parses_mixed_rsa_and_ec_set() {
        let (_kp, ec_jwks) = es256_key("ec1");
        let (_t, rsa_jwks) = rs256("rsa1", "{\"sub\":\"a\",\"exp\":9999999999}");
        // Merge the two single-key documents into one set.
        let ec: Value = serde_json::from_str(&ec_jwks).unwrap();
        let rsa: Value = serde_json::from_str(&rsa_jwks).unwrap();
        let mut keys = ec["keys"].as_array().unwrap().clone();
        keys.extend(rsa["keys"].as_array().unwrap().clone());
        let merged = serde_json::json!({ "keys": keys }).to_string();
        let jwks = Jwks::from_json(&merged, false).unwrap();
        assert2::assert!(jwks.len() == 2);
    }

    #[test]
    fn skips_unsupported_and_enc_keys() {
        let jwks = Jwks::from_json(
            r#"{"keys":[
                {"kty":"oct","k":"AAAA","kid":"sym"},
                {"kty":"EC","crv":"P-384","kid":"big"},
                {"kty":"RSA","use":"enc","kid":"enc1","n":"AQAB","e":"AQAB"}
            ]}"#,
            false,
        )
        .unwrap();
        assert2::assert!(jwks.is_empty());
    }

    #[test]
    fn rejects_non_json_and_missing_keys_array() {
        for (_name, input) in [("invalid JSON", "not json"), ("missing keys array", "{}")] {
            assert2::assert!(Jwks::from_json(input, false) == Err(AuthError::MalformedMessage));
        }
    }

    #[test]
    fn verifies_valid_rs256() {
        let (token, jwks_json) = rs256("rsa1", "{\"sub\":\"admin\",\"exp\":9999999999}");
        let jwks = Jwks::from_json(&jwks_json, false).unwrap();
        let (kid, alg, si, sig) = parts(&token);
        assert2::assert!(jwks.verify(kid.as_deref(), &alg, &si, &sig).is_ok());
    }

    #[test]
    fn verifies_valid_es256() {
        let (kp, jwks_json) = es256_key("ec1");
        let token = es256_token(&kp, "ec1", "{\"sub\":\"admin\",\"exp\":9999999999}");
        let jwks = Jwks::from_json(&jwks_json, false).unwrap();
        let (kid, alg, si, sig) = parts(&token);
        assert2::assert!(jwks.verify(kid.as_deref(), &alg, &si, &sig).is_ok());
    }

    #[test]
    fn rejects_tampered_rs256_signature() {
        let (token, jwks_json) = rs256("rsa1", "{\"sub\":\"admin\",\"exp\":9999999999}");
        let jwks = Jwks::from_json(&jwks_json, false).unwrap();
        let (kid, alg, si, mut sig) = parts(&token);
        sig[0] ^= 0xff;
        assert2::assert!(
            jwks.verify(kid.as_deref(), &alg, &si, &sig) == Err(AuthError::InvalidToken)
        );
    }

    #[test]
    fn rejects_unknown_kid() {
        let (token, jwks_json) = rs256("rsa1", "{\"sub\":\"a\",\"exp\":9999999999}");
        let jwks = Jwks::from_json(&jwks_json, false).unwrap();
        let (_kid, alg, si, sig) = parts(&token);
        assert2::assert!(
            jwks.verify(Some("other"), &alg, &si, &sig) == Err(AuthError::InvalidToken)
        );
    }

    #[test]
    fn rejects_missing_kid_when_set_has_multiple_keys() {
        let (kp, ec_jwks) = es256_key("ec1");
        let (_t, rsa_jwks) = rs256("rsa1", "{\"sub\":\"a\",\"exp\":9999999999}");
        let ec: Value = serde_json::from_str(&ec_jwks).unwrap();
        let rsa: Value = serde_json::from_str(&rsa_jwks).unwrap();
        let mut keys = ec["keys"].as_array().unwrap().clone();
        keys.extend(rsa["keys"].as_array().unwrap().clone());
        let merged = serde_json::json!({ "keys": keys }).to_string();
        let jwks = Jwks::from_json(&merged, false).unwrap();
        // Token minted without a kid in the header.
        let header = b64(b"{\"alg\":\"ES256\"}");
        let payload = b64(b"{\"sub\":\"a\",\"exp\":9999999999}");
        let signing_input = format!("{header}.{payload}");
        let sig = kp
            .sign(&SystemRandom::new(), signing_input.as_bytes())
            .unwrap();
        assert2::assert!(
            jwks.verify(None, "ES256", signing_input.as_bytes(), sig.as_ref())
                == Err(AuthError::InvalidToken)
        );
    }

    #[test]
    fn accepts_missing_kid_when_set_has_single_key() {
        let (token, jwks_json) =
            mint_rs256_with_header("{\"alg\":\"RS256\"}", "{\"sub\":\"a\",\"exp\":9999999999}");
        let jwks = Jwks::from_json(&jwks_json, false).unwrap();
        let (kid, alg, si, sig) = parts(&token);
        assert2::assert!(kid.is_none());
        jwks.verify(kid.as_deref(), &alg, &si, &sig)
            .expect("single-key JWKS should verify a token without kid");
    }

    #[test]
    fn rejects_es256_signed_by_wrong_key() {
        let (kp_a, _jwks_a) = es256_key("ec1");
        let (_kp_b, jwks_b) = es256_key("ec1"); // same kid, different key
        let token = es256_token(&kp_a, "ec1", "{\"sub\":\"a\",\"exp\":9999999999}");
        let jwks = Jwks::from_json(&jwks_b, false).unwrap();
        let (kid, alg, si, sig) = parts(&token);
        assert2::assert!(
            jwks.verify(kid.as_deref(), &alg, &si, &sig) == Err(AuthError::InvalidToken)
        );
    }

    #[test]
    fn rejects_alg_key_type_mismatch() {
        // RSA key set, but ask it to verify with ES256.
        let (token, jwks_json) = rs256("rsa1", "{\"sub\":\"a\",\"exp\":9999999999}");
        let jwks = Jwks::from_json(&jwks_json, false).unwrap();
        let (kid, _alg, si, sig) = parts(&token);
        assert2::assert!(
            jwks.verify(kid.as_deref(), "ES256", &si, &sig) == Err(AuthError::InvalidToken)
        );
    }

    #[test]
    fn handle_store_and_load_round_trips() {
        let h = JwksHandle::default();
        assert2::assert!(h.load().is_empty());
        let (_t, jwks_json) = rs256("rsa1", "{\"sub\":\"a\",\"exp\":9999999999}");
        h.store(Jwks::from_json(&jwks_json, false).unwrap());
        assert2::assert!(h.load().len() == 1);
    }

    // ---- ignore_key_use filter + handle helpers ------------------

    #[test]
    fn parse_jwks_filters_use_enc_by_default() {
        let json = r#"{
            "keys": [
                {"kty":"RSA","kid":"sig-key","use":"sig","n":"AQAB","e":"AQAB"},
                {"kty":"RSA","kid":"enc-key","use":"enc","n":"AQAB","e":"AQAB"}
            ]
        }"#;
        let jwks = Jwks::from_json(json, false).expect("parses");
        assert2::assert!(
            (jwks.contains_kid("sig-key"), jwks.contains_kid("enc-key")) == (true, false)
        );
    }

    #[test]
    fn parse_jwks_keeps_use_enc_when_ignore_key_use_true() {
        let json = r#"{
            "keys": [
                {"kty":"RSA","kid":"sig-key","use":"sig","n":"AQAB","e":"AQAB"},
                {"kty":"RSA","kid":"enc-key","use":"enc","n":"AQAB","e":"AQAB"}
            ]
        }"#;
        let jwks = Jwks::from_json(json, true).expect("parses");
        assert2::assert!(
            (jwks.contains_kid("sig-key"), jwks.contains_kid("enc-key")) == (true, true)
        );
    }

    #[test]
    fn parse_jwks_keeps_keys_with_absent_use_field_regardless() {
        let json = r#"{
            "keys": [
                {"kty":"RSA","kid":"no-use","n":"AQAB","e":"AQAB"}
            ]
        }"#;
        assert2::assert!(Jwks::from_json(json, false).unwrap().contains_kid("no-use"));
        assert2::assert!(Jwks::from_json(json, true).unwrap().contains_kid("no-use"));
    }

    #[test]
    fn default_handle_signal_refresh_is_silent_noop() {
        // A handle constructed without refresher wiring must not panic on
        // signal_refresh(); validators may share the same code path whether
        // or not a refresher is paired.
        let h = JwksHandle::default();
        h.signal_refresh();
        assert2::assert!(h.last_successful_fetch_ms() == 0);
    }

    #[tokio::test(flavor = "current_thread")]
    async fn paired_handle_signal_refresh_delivers_to_receiver() {
        let (tx, mut rx) = mpsc::channel::<()>(1);
        let ts = Arc::new(AtomicI64::new(0));
        let h = JwksHandle::new_with_refresher_handles(Jwks::empty(), ts, tx);
        h.signal_refresh();
        assert2::assert!(rx.try_recv().is_ok());
    }

    // Expose minting helpers to the sibling `oauthbearer` tests.
    pub fn mint_rs256(kid: &str, claims: &str) -> (String, String) {
        rs256(kid, claims)
    }

    /// Mint an RS256 token with a fully-controlled header JSON, so the caller
    /// can drive `typ` / `kid` / extra header fields.
    ///
    /// This helper returns `(token, jwks_json)` like the other minters.
    pub fn mint_rs256_with_header(header_json: &str, claims: &str) -> (String, String) {
        let der = rsa_pkcs8();
        let kp = RsaKeyPair::from_pkcs8(&der).unwrap();
        let signing_input = format!("{}.{}", b64(header_json.as_bytes()), b64(claims.as_bytes()));
        let mut sig = vec![0u8; kp.public().modulus_len()];
        kp.sign(
            &signature::RSA_PKCS1_SHA256,
            &SystemRandom::new(),
            signing_input.as_bytes(),
            &mut sig,
        )
        .unwrap();
        let token = format!("{signing_input}.{}", b64(&sig));

        // Echo the JWKS used by `rs256` so the validator can verify the
        // signature. Default kid="k1" so tests don't have to thread it.
        let pkcs1 = kp.public().as_ref();
        let (n, e) = split_pkcs1_public(pkcs1);
        let jwks = format!(
            "{{\"keys\":[{{\"kty\":\"RSA\",\"kid\":\"k1\",\"n\":\"{}\",\"e\":\"{}\"}}]}}",
            b64(&n),
            b64(&e),
        );
        (token, jwks)
    }

    /// Mint an ES256 token under a *fresh* key and return `(token, jwks_json)`.
    ///
    /// Each call generates a new key pair, so two calls give independent keys.
    /// That helps key-rotation tests, where RS256's fixed key cannot differ.
    pub fn mint_es256(kid: &str, claims: &str) -> (String, String) {
        let (kp, jwks) = es256_key(kid);
        let token = es256_token(&kp, kid, claims);
        (token, jwks)
    }
}