mockforge-http 0.3.116

HTTP/REST protocol support for MockForge
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
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
//! JWKS key conversion utilities
//!
//! This module provides functions to convert PEM-encoded keys to JWK format
//! for the JWKS endpoint. It includes a lightweight DER/ASN.1 parser to
//! extract RSA modulus/exponent and EC point coordinates from SPKI-encoded
//! public keys.

use base64::{engine::general_purpose, Engine as _};
use mockforge_core::Error;

use super::oidc::{JwkKey, JwkPublicKey};

// ---------------------------------------------------------------------------
// Lightweight DER / ASN.1 helpers
// ---------------------------------------------------------------------------

/// ASN.1 tag constants
const TAG_INTEGER: u8 = 0x02;
const TAG_BIT_STRING: u8 = 0x03;
const TAG_SEQUENCE: u8 = 0x30;

/// Read a DER tag + length, returning (tag, length, rest).
fn read_der_tl(data: &[u8]) -> Result<(u8, usize, &[u8]), Error> {
    if data.is_empty() {
        return Err(Error::internal("DER: unexpected end of data"));
    }
    let tag = data[0];
    if data.len() < 2 {
        return Err(Error::internal("DER: truncated length"));
    }
    let (len, header_len) = if data[1] & 0x80 == 0 {
        (data[1] as usize, 2)
    } else {
        let num_bytes = (data[1] & 0x7f) as usize;
        if num_bytes == 0 || num_bytes > 4 || data.len() < 2 + num_bytes {
            return Err(Error::internal("DER: invalid length encoding"));
        }
        let mut len: usize = 0;
        for &b in &data[2..2 + num_bytes] {
            len = len.checked_shl(8).ok_or_else(|| Error::internal("DER: length overflow"))?
                | b as usize;
        }
        (len, 2 + num_bytes)
    };
    if data.len() < header_len + len {
        return Err(Error::internal("DER: content exceeds buffer"));
    }
    Ok((tag, len, &data[header_len..]))
}

/// Expect a specific tag and return (content, rest_after).
fn expect_tag(data: &[u8], expected: u8) -> Result<(&[u8], &[u8]), Error> {
    let (tag, len, rest) = read_der_tl(data)?;
    if tag != expected {
        return Err(Error::internal(format!(
            "DER: expected tag 0x{expected:02x}, got 0x{tag:02x}"
        )));
    }
    Ok((&rest[..len], &rest[len..]))
}

/// Read an ASN.1 INTEGER, stripping any leading zero-padding byte, and return
/// (integer_bytes, rest_after).
fn read_integer(data: &[u8]) -> Result<(&[u8], &[u8]), Error> {
    let (content, rest) = expect_tag(data, TAG_INTEGER)?;
    // ASN.1 integers are signed; a leading 0x00 byte is padding for unsigned values.
    let trimmed = if content.len() > 1 && content[0] == 0x00 {
        &content[1..]
    } else {
        content
    };
    Ok((trimmed, rest))
}

/// Base64url-encode bytes (no padding) per RFC 7515.
fn base64url_encode(bytes: &[u8]) -> String {
    general_purpose::URL_SAFE_NO_PAD.encode(bytes)
}

// ---------------------------------------------------------------------------
// PEM helpers
// ---------------------------------------------------------------------------

/// Decode a PEM block into raw DER bytes.
fn decode_pem(pem: &str) -> Result<Vec<u8>, Error> {
    let pem = pem.trim();

    // Validate that it has PEM markers
    if !pem.starts_with("-----BEGIN") || !pem.contains("-----END") {
        return Err(Error::internal("Invalid PEM format: missing BEGIN/END markers"));
    }

    // Strip header and footer lines, join base64 body
    let b64: String = pem.lines().filter(|l| !l.starts_with("-----")).collect::<Vec<_>>().join("");

    general_purpose::STANDARD
        .decode(b64)
        .map_err(|e| Error::internal(format!("PEM base64 decode error: {e}")))
}

// ---------------------------------------------------------------------------
// RSA PEM → JWK
// ---------------------------------------------------------------------------

/// Parse an SPKI (or PKCS#1) DER RSA public key and return (n, e) as
/// base64url-encoded strings.
fn parse_rsa_public_key_der(der: &[u8]) -> Result<(String, String), Error> {
    // Try SPKI first: SEQUENCE { AlgorithmIdentifier, BIT STRING { PKCS1Key } }
    let (outer_content, _) = expect_tag(der, TAG_SEQUENCE)?;

    // Peek at the first element — if it's a SEQUENCE it's SPKI, if INTEGER it's PKCS#1.
    let (first_tag, _, _) = read_der_tl(outer_content)?;

    if first_tag == TAG_SEQUENCE {
        // SPKI format
        let (_alg_id, after_alg) = expect_tag(outer_content, TAG_SEQUENCE)?;
        let (bit_string, _) = expect_tag(after_alg, TAG_BIT_STRING)?;

        // BIT STRING has a leading "unused bits" byte (should be 0)
        if bit_string.is_empty() {
            return Err(Error::internal("DER: empty BIT STRING"));
        }
        let pkcs1_der = &bit_string[1..]; // skip unused-bits byte
        return parse_rsa_pkcs1(pkcs1_der);
    }

    // PKCS#1 format (SEQUENCE already consumed above — re-parse from `der`)
    parse_rsa_pkcs1(der)
}

/// Parse PKCS#1 RSAPublicKey: SEQUENCE { INTEGER(n), INTEGER(e) }
fn parse_rsa_pkcs1(der: &[u8]) -> Result<(String, String), Error> {
    let (content, _) = expect_tag(der, TAG_SEQUENCE)?;
    let (n_bytes, rest) = read_integer(content)?;
    let (e_bytes, _) = read_integer(rest)?;
    Ok((base64url_encode(n_bytes), base64url_encode(e_bytes)))
}

/// Convert a PEM-encoded RSA public key to JWK format.
///
/// Supports both SPKI (`BEGIN PUBLIC KEY`) and PKCS#1 (`BEGIN RSA PUBLIC KEY`)
/// PEM formats. Parses the ASN.1 DER structure to extract the modulus (n) and
/// exponent (e).
pub fn rsa_pem_to_jwk(pem: &str, kid: &str, alg: &str) -> Result<JwkPublicKey, Error> {
    let der = decode_pem(pem)?;
    let (n, e) = parse_rsa_public_key_der(&der)?;

    Ok(JwkPublicKey {
        kid: kid.to_string(),
        kty: "RSA".to_string(),
        alg: alg.to_string(),
        use_: "sig".to_string(),
        n: Some(n),
        e: Some(e),
        crv: None,
        x: None,
        y: None,
    })
}

// ---------------------------------------------------------------------------
// EC PEM → JWK
// ---------------------------------------------------------------------------

/// Well-known OID byte representations for named curves.
const OID_P256: &[u8] = &[0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07];
const OID_P384: &[u8] = &[0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22];
const OID_P521: &[u8] = &[0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x23];

/// Expected uncompressed point coordinate size for each curve (bytes per coordinate).
fn ec_coord_size(crv: &str) -> usize {
    match crv {
        "P-256" => 32,
        "P-384" => 48,
        "P-521" => 66,
        _ => 32,
    }
}

/// Identify the curve name from the AlgorithmIdentifier SEQUENCE bytes.
fn identify_ec_curve(alg_id_bytes: &[u8]) -> Result<&'static str, Error> {
    // The AlgorithmIdentifier SEQUENCE contains: OID(ecPublicKey), OID(namedCurve)
    // We scan for the curve OID.
    if contains_oid(alg_id_bytes, OID_P256) {
        Ok("P-256")
    } else if contains_oid(alg_id_bytes, OID_P384) {
        Ok("P-384")
    } else if contains_oid(alg_id_bytes, OID_P521) {
        Ok("P-521")
    } else {
        Err(Error::internal("Unsupported EC curve OID in SPKI AlgorithmIdentifier"))
    }
}

/// Check whether `haystack` contains the byte pattern `needle`.
fn contains_oid(haystack: &[u8], needle: &[u8]) -> bool {
    haystack.windows(needle.len()).any(|window| window == needle)
}

/// Parse an SPKI-encoded EC public key. Returns (curve, x, y) where x and y
/// are base64url-encoded.
fn parse_ec_public_key_der(der: &[u8]) -> Result<(String, String, String), Error> {
    let (outer_content, _) = expect_tag(der, TAG_SEQUENCE)?;

    // AlgorithmIdentifier SEQUENCE
    let (alg_id_bytes, after_alg) = expect_tag(outer_content, TAG_SEQUENCE)?;
    let crv = identify_ec_curve(alg_id_bytes)?;

    // BIT STRING containing the uncompressed EC point
    let (bit_string, _) = expect_tag(after_alg, TAG_BIT_STRING)?;
    if bit_string.is_empty() {
        return Err(Error::internal("DER: empty BIT STRING in EC key"));
    }

    let point = &bit_string[1..]; // skip unused-bits byte
    if point.is_empty() || point[0] != 0x04 {
        return Err(Error::internal(
            "EC public key is not in uncompressed point format (expected 0x04 prefix)",
        ));
    }

    let coord_bytes = &point[1..]; // skip 0x04
    let coord_size = ec_coord_size(crv);
    if coord_bytes.len() < coord_size * 2 {
        return Err(Error::internal(format!(
            "EC point too short for {crv}: expected {expected} bytes, got {got}",
            expected = coord_size * 2,
            got = coord_bytes.len()
        )));
    }

    let x = base64url_encode(&coord_bytes[..coord_size]);
    let y = base64url_encode(&coord_bytes[coord_size..coord_size * 2]);

    Ok((crv.to_string(), x, y))
}

/// Convert a PEM-encoded ECDSA public key to JWK format.
///
/// Parses the SPKI DER structure to identify the curve and extract the
/// uncompressed point coordinates (x, y).
pub fn ecdsa_pem_to_jwk(pem: &str, kid: &str, alg: &str) -> Result<JwkPublicKey, Error> {
    let der = decode_pem(pem)?;
    let (crv, x, y) = parse_ec_public_key_der(&der)?;

    Ok(JwkPublicKey {
        kid: kid.to_string(),
        kty: "EC".to_string(),
        alg: alg.to_string(),
        use_: "sig".to_string(),
        n: None,
        e: None,
        crv: Some(crv),
        x: Some(x),
        y: Some(y),
    })
}

// ---------------------------------------------------------------------------
// HMAC → JWK (unchanged — HMAC secrets must not be exposed in JWKS)
// ---------------------------------------------------------------------------

/// Convert HMAC secret to JWK format (oct key type)
///
/// HMAC keys are symmetric secrets and MUST NOT be exposed in JWKS endpoints.
/// JWKS is designed for public key discovery only. This returns an `oct` key
/// entry with the key ID and algorithm for identification purposes, but
/// intentionally omits the secret material (`k` field).
pub fn hmac_to_jwk(_secret: &str, kid: &str, alg: &str) -> Result<JwkPublicKey, Error> {
    Ok(JwkPublicKey {
        kid: kid.to_string(),
        kty: "oct".to_string(),
        alg: alg.to_string(),
        use_: "sig".to_string(),
        n: None,
        e: None,
        crv: None,
        x: None,
        y: None,
    })
}

// ---------------------------------------------------------------------------
// High-level converters
// ---------------------------------------------------------------------------

/// Convert JwkKey to JwkPublicKey for JWKS endpoint
pub fn jwk_key_to_public(jwk_key: &JwkKey) -> Result<JwkPublicKey, Error> {
    match jwk_key.kty.as_str() {
        "RSA" => rsa_pem_to_jwk(&jwk_key.public_key, &jwk_key.kid, &jwk_key.alg),
        "EC" => ecdsa_pem_to_jwk(&jwk_key.public_key, &jwk_key.kid, &jwk_key.alg),
        "oct" => hmac_to_jwk(&jwk_key.public_key, &jwk_key.kid, &jwk_key.alg),
        _ => Err(Error::internal(format!("Unsupported key type: {}", jwk_key.kty))),
    }
}

/// Simplified JWK conversion that first tries to parse `public_key` as JSON
/// (JWK-format), then falls back to PEM parsing with full ASN.1 extraction.
pub fn convert_jwk_key_simple(jwk_key: &JwkKey) -> Result<JwkPublicKey, Error> {
    // Check if public_key is already in JWK format (JSON)
    if let Ok(jwk_json) = serde_json::from_str::<serde_json::Value>(&jwk_key.public_key) {
        if let Some(n) = jwk_json.get("n").and_then(|v| v.as_str()) {
            // It's an RSA key in JWK format
            return Ok(JwkPublicKey {
                kid: jwk_key.kid.clone(),
                kty: "RSA".to_string(),
                alg: jwk_key.alg.clone(),
                use_: jwk_key.use_.clone(),
                n: Some(n.to_string()),
                e: jwk_json.get("e").and_then(|v| v.as_str()).map(|s| s.to_string()),
                crv: None,
                x: None,
                y: None,
            });
        }
        if let Some(x) = jwk_json.get("x").and_then(|v| v.as_str()) {
            // It's an EC key in JWK format
            return Ok(JwkPublicKey {
                kid: jwk_key.kid.clone(),
                kty: "EC".to_string(),
                alg: jwk_key.alg.clone(),
                use_: jwk_key.use_.clone(),
                n: None,
                e: None,
                crv: jwk_json.get("crv").and_then(|v| v.as_str()).map(|s| s.to_string()),
                x: Some(x.to_string()),
                y: jwk_json.get("y").and_then(|v| v.as_str()).map(|s| s.to_string()),
            });
        }
    }

    // Not JSON — treat as PEM and parse with full ASN.1 extraction.
    match jwk_key.kty.as_str() {
        "RSA" => rsa_pem_to_jwk(&jwk_key.public_key, &jwk_key.kid, &jwk_key.alg),
        "EC" => ecdsa_pem_to_jwk(&jwk_key.public_key, &jwk_key.kid, &jwk_key.alg),
        "oct" => hmac_to_jwk(&jwk_key.public_key, &jwk_key.kid, &jwk_key.alg),
        _ => Err(Error::internal(format!("Unsupported key type: {}", jwk_key.kty))),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // -----------------------------------------------------------------------
    // Helper: build a minimal SPKI PEM for RSA from known n/e
    // -----------------------------------------------------------------------

    /// Build a DER-encoded ASN.1 INTEGER (with leading 0x00 padding if high bit set).
    fn der_integer(bytes: &[u8]) -> Vec<u8> {
        let needs_pad = !bytes.is_empty() && bytes[0] & 0x80 != 0;
        let mut out = vec![TAG_INTEGER];
        let len = bytes.len() + if needs_pad { 1 } else { 0 };
        encode_der_length(&mut out, len);
        if needs_pad {
            out.push(0x00);
        }
        out.extend_from_slice(bytes);
        out
    }

    fn encode_der_length(out: &mut Vec<u8>, len: usize) {
        if len < 0x80 {
            out.push(len as u8);
        } else if len < 0x100 {
            out.push(0x81);
            out.push(len as u8);
        } else {
            out.push(0x82);
            out.push((len >> 8) as u8);
            out.push(len as u8);
        }
    }

    fn der_sequence(contents: &[u8]) -> Vec<u8> {
        let mut out = vec![TAG_SEQUENCE];
        encode_der_length(&mut out, contents.len());
        out.extend_from_slice(contents);
        out
    }

    fn der_bit_string(contents: &[u8]) -> Vec<u8> {
        let mut out = vec![TAG_BIT_STRING];
        encode_der_length(&mut out, contents.len() + 1);
        out.push(0x00); // unused bits
        out.extend_from_slice(contents);
        out
    }

    /// Wrap DER bytes in PEM markers.
    fn to_pem(der: &[u8], label: &str) -> String {
        let b64 = general_purpose::STANDARD.encode(der);
        let mut pem = format!("-----BEGIN {label}-----\n");
        for chunk in b64.as_bytes().chunks(64) {
            pem.push_str(std::str::from_utf8(chunk).unwrap());
            pem.push('\n');
        }
        pem.push_str(&format!("-----END {label}-----"));
        pem
    }

    /// Build a test RSA SPKI PEM with the given modulus and exponent bytes.
    fn build_rsa_spki_pem(n_bytes: &[u8], e_bytes: &[u8]) -> String {
        // RSA OID: 1.2.840.113549.1.1.1
        let rsa_oid: Vec<u8> = vec![
            0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01,
        ];
        let null = vec![0x05, 0x00];
        let mut alg_id_inner = Vec::new();
        alg_id_inner.extend_from_slice(&rsa_oid);
        alg_id_inner.extend_from_slice(&null);
        let alg_id = der_sequence(&alg_id_inner);

        let n_int = der_integer(n_bytes);
        let e_int = der_integer(e_bytes);
        let mut pkcs1_inner = Vec::new();
        pkcs1_inner.extend_from_slice(&n_int);
        pkcs1_inner.extend_from_slice(&e_int);
        let pkcs1 = der_sequence(&pkcs1_inner);
        let bit_string = der_bit_string(&pkcs1);

        let mut spki_inner = Vec::new();
        spki_inner.extend_from_slice(&alg_id);
        spki_inner.extend_from_slice(&bit_string);
        let spki = der_sequence(&spki_inner);

        to_pem(&spki, "PUBLIC KEY")
    }

    /// Build a test EC SPKI PEM with the given curve OID and point coordinates.
    fn build_ec_spki_pem(curve_oid: &[u8], x_bytes: &[u8], y_bytes: &[u8]) -> String {
        // ecPublicKey OID: 1.2.840.10045.2.1
        let ec_oid: Vec<u8> = vec![0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01];
        let mut alg_id_inner = Vec::new();
        alg_id_inner.extend_from_slice(&ec_oid);
        alg_id_inner.extend_from_slice(curve_oid);
        let alg_id = der_sequence(&alg_id_inner);

        // Uncompressed point: 0x04 || x || y
        let mut point = vec![0x04];
        point.extend_from_slice(x_bytes);
        point.extend_from_slice(y_bytes);
        let bit_string = der_bit_string(&point);

        let mut spki_inner = Vec::new();
        spki_inner.extend_from_slice(&alg_id);
        spki_inner.extend_from_slice(&bit_string);
        let spki = der_sequence(&spki_inner);

        to_pem(&spki, "PUBLIC KEY")
    }

    // -----------------------------------------------------------------------
    // RSA tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_rsa_pem_to_jwk_extracts_n_and_e() {
        let n_bytes = vec![0x01; 32]; // 256-bit modulus
        let e_bytes = vec![0x01, 0x00, 0x01]; // 65537
        let pem = build_rsa_spki_pem(&n_bytes, &e_bytes);

        let jwk = rsa_pem_to_jwk(&pem, "test-kid", "RS256").unwrap();
        assert_eq!(jwk.kid, "test-kid");
        assert_eq!(jwk.kty, "RSA");
        assert_eq!(jwk.alg, "RS256");
        assert_eq!(jwk.use_, "sig");
        assert!(jwk.n.is_some(), "n should be extracted from PEM");
        assert!(jwk.e.is_some(), "e should be extracted from PEM");

        // Decode and verify round-trip
        let decoded_n = general_purpose::URL_SAFE_NO_PAD.decode(jwk.n.unwrap()).unwrap();
        assert_eq!(decoded_n, n_bytes);

        let decoded_e = general_purpose::URL_SAFE_NO_PAD.decode(jwk.e.unwrap()).unwrap();
        assert_eq!(decoded_e, e_bytes);
    }

    #[test]
    fn test_rsa_pem_to_jwk_with_high_bit_modulus() {
        // Modulus with high bit set — ASN.1 will add a leading 0x00, which we must strip
        let n_bytes = vec![0xFF; 32];
        let e_bytes = vec![0x01, 0x00, 0x01];
        let pem = build_rsa_spki_pem(&n_bytes, &e_bytes);

        let jwk = rsa_pem_to_jwk(&pem, "kid-highbit", "RS384").unwrap();
        let decoded_n = general_purpose::URL_SAFE_NO_PAD.decode(jwk.n.unwrap()).unwrap();
        assert_eq!(decoded_n, n_bytes);
    }

    #[test]
    fn test_rsa_pem_invalid_format() {
        let result = rsa_pem_to_jwk("not-a-pem", "kid", "RS256");
        assert!(result.is_err());
    }

    #[test]
    fn test_rsa_pem_multiple_algorithms() {
        let n = vec![0x42; 16];
        let e = vec![0x01, 0x00, 0x01];
        let pem = build_rsa_spki_pem(&n, &e);

        for alg in &["RS256", "RS384", "RS512"] {
            let jwk = rsa_pem_to_jwk(&pem, "kid", alg).unwrap();
            assert_eq!(jwk.alg, *alg);
            assert_eq!(jwk.kty, "RSA");
            assert!(jwk.n.is_some());
            assert!(jwk.e.is_some());
        }
    }

    // -----------------------------------------------------------------------
    // EC tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_ecdsa_pem_to_jwk_p256() {
        let x = vec![0xAA; 32];
        let y = vec![0xBB; 32];
        let pem = build_ec_spki_pem(OID_P256, &x, &y);

        let jwk = ecdsa_pem_to_jwk(&pem, "ec-kid", "ES256").unwrap();
        assert_eq!(jwk.kid, "ec-kid");
        assert_eq!(jwk.kty, "EC");
        assert_eq!(jwk.alg, "ES256");
        assert_eq!(jwk.crv, Some("P-256".to_string()));
        assert!(jwk.x.is_some());
        assert!(jwk.y.is_some());

        let decoded_x = general_purpose::URL_SAFE_NO_PAD.decode(jwk.x.unwrap()).unwrap();
        assert_eq!(decoded_x, x);
        let decoded_y = general_purpose::URL_SAFE_NO_PAD.decode(jwk.y.unwrap()).unwrap();
        assert_eq!(decoded_y, y);
    }

    #[test]
    fn test_ecdsa_pem_to_jwk_p384() {
        let x = vec![0xCC; 48];
        let y = vec![0xDD; 48];
        let pem = build_ec_spki_pem(OID_P384, &x, &y);

        let jwk = ecdsa_pem_to_jwk(&pem, "ec384-kid", "ES384").unwrap();
        assert_eq!(jwk.crv, Some("P-384".to_string()));
        assert!(jwk.x.is_some());
        assert!(jwk.y.is_some());
    }

    #[test]
    fn test_ecdsa_pem_to_jwk_p521() {
        let x = vec![0xEE; 66];
        let y = vec![0xFF; 66];
        let pem = build_ec_spki_pem(OID_P521, &x, &y);

        let jwk = ecdsa_pem_to_jwk(&pem, "ec521-kid", "ES512").unwrap();
        assert_eq!(jwk.crv, Some("P-521".to_string()));
        assert!(jwk.x.is_some());
        assert!(jwk.y.is_some());
    }

    #[test]
    fn test_ecdsa_pem_invalid_format() {
        let result = ecdsa_pem_to_jwk("not-a-pem", "kid", "ES256");
        assert!(result.is_err());
    }

    // -----------------------------------------------------------------------
    // HMAC tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_hmac_to_jwk() {
        let jwk = hmac_to_jwk("test-secret", "test-kid", "HS256").unwrap();
        assert_eq!(jwk.kid, "test-kid");
        assert_eq!(jwk.kty, "oct");
        assert_eq!(jwk.alg, "HS256");
        assert_eq!(jwk.use_, "sig");
        assert!(jwk.n.is_none());
        assert!(jwk.e.is_none());
        assert!(jwk.crv.is_none());
        assert!(jwk.x.is_none());
        assert!(jwk.y.is_none());
    }

    #[test]
    fn test_hmac_multiple_algorithms() {
        for alg in &["HS256", "HS384", "HS512"] {
            let jwk = hmac_to_jwk("secret", "kid", alg).unwrap();
            assert_eq!(jwk.alg, *alg);
            assert_eq!(jwk.kty, "oct");
        }
    }

    // -----------------------------------------------------------------------
    // jwk_key_to_public tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_jwk_key_to_public_rsa() {
        let n = vec![0x42; 16];
        let e = vec![0x01, 0x00, 0x01];
        let pem = build_rsa_spki_pem(&n, &e);

        let jwk_key = JwkKey {
            kid: "rsa-key".to_string(),
            alg: "RS256".to_string(),
            public_key: pem,
            private_key: Some("private-key".to_string()),
            kty: "RSA".to_string(),
            use_: "sig".to_string(),
        };

        let public_key = jwk_key_to_public(&jwk_key).unwrap();
        assert_eq!(public_key.kid, "rsa-key");
        assert_eq!(public_key.kty, "RSA");
        assert_eq!(public_key.alg, "RS256");
        assert!(public_key.n.is_some());
        assert!(public_key.e.is_some());
    }

    #[test]
    fn test_jwk_key_to_public_ec() {
        let x = vec![0xAA; 32];
        let y = vec![0xBB; 32];
        let pem = build_ec_spki_pem(OID_P256, &x, &y);

        let jwk_key = JwkKey {
            kid: "ec-key".to_string(),
            alg: "ES256".to_string(),
            public_key: pem,
            private_key: Some("private-key".to_string()),
            kty: "EC".to_string(),
            use_: "sig".to_string(),
        };

        let public_key = jwk_key_to_public(&jwk_key).unwrap();
        assert_eq!(public_key.kid, "ec-key");
        assert_eq!(public_key.kty, "EC");
        assert_eq!(public_key.alg, "ES256");
        assert_eq!(public_key.crv, Some("P-256".to_string()));
        assert!(public_key.x.is_some());
        assert!(public_key.y.is_some());
    }

    #[test]
    fn test_jwk_key_to_public_oct() {
        let jwk_key = JwkKey {
            kid: "hmac-key".to_string(),
            alg: "HS256".to_string(),
            public_key: "secret".to_string(),
            private_key: Some("secret".to_string()),
            kty: "oct".to_string(),
            use_: "sig".to_string(),
        };

        let public_key = jwk_key_to_public(&jwk_key).unwrap();
        assert_eq!(public_key.kid, "hmac-key");
        assert_eq!(public_key.kty, "oct");
        assert_eq!(public_key.alg, "HS256");
    }

    #[test]
    fn test_jwk_key_to_public_unsupported() {
        let jwk_key = JwkKey {
            kid: "unknown-key".to_string(),
            alg: "UNKNOWN".to_string(),
            public_key: "data".to_string(),
            private_key: None,
            kty: "UNSUPPORTED".to_string(),
            use_: "sig".to_string(),
        };

        assert!(jwk_key_to_public(&jwk_key).is_err());
    }

    // -----------------------------------------------------------------------
    // convert_jwk_key_simple tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_convert_jwk_key_simple_rsa_json() {
        let jwk_json = r#"{"n": "modulus-value", "e": "exponent-value"}"#;
        let jwk_key = JwkKey {
            kid: "rsa-json-key".to_string(),
            alg: "RS256".to_string(),
            public_key: jwk_json.to_string(),
            private_key: None,
            kty: "RSA".to_string(),
            use_: "sig".to_string(),
        };

        let public_key = convert_jwk_key_simple(&jwk_key).unwrap();
        assert_eq!(public_key.kid, "rsa-json-key");
        assert_eq!(public_key.kty, "RSA");
        assert_eq!(public_key.n, Some("modulus-value".to_string()));
        assert_eq!(public_key.e, Some("exponent-value".to_string()));
    }

    #[test]
    fn test_convert_jwk_key_simple_ec_json() {
        let jwk_json = r#"{"x": "x-coordinate", "y": "y-coordinate", "crv": "P-256"}"#;
        let jwk_key = JwkKey {
            kid: "ec-json-key".to_string(),
            alg: "ES256".to_string(),
            public_key: jwk_json.to_string(),
            private_key: None,
            kty: "EC".to_string(),
            use_: "sig".to_string(),
        };

        let public_key = convert_jwk_key_simple(&jwk_key).unwrap();
        assert_eq!(public_key.kid, "ec-json-key");
        assert_eq!(public_key.kty, "EC");
        assert_eq!(public_key.x, Some("x-coordinate".to_string()));
        assert_eq!(public_key.y, Some("y-coordinate".to_string()));
        assert_eq!(public_key.crv, Some("P-256".to_string()));
    }

    #[test]
    fn test_convert_jwk_key_simple_rsa_pem_extracts_components() {
        let n = vec![0x42; 16];
        let e = vec![0x01, 0x00, 0x01];
        let pem = build_rsa_spki_pem(&n, &e);

        let jwk_key = JwkKey {
            kid: "rsa-pem-key".to_string(),
            alg: "RS256".to_string(),
            public_key: pem,
            private_key: None,
            kty: "RSA".to_string(),
            use_: "sig".to_string(),
        };

        let public_key = convert_jwk_key_simple(&jwk_key).unwrap();
        assert_eq!(public_key.kid, "rsa-pem-key");
        assert_eq!(public_key.kty, "RSA");
        assert!(public_key.n.is_some(), "n should now be extracted from PEM");
        assert!(public_key.e.is_some(), "e should now be extracted from PEM");
    }

    #[test]
    fn test_convert_jwk_key_simple_ec_pem_extracts_coordinates() {
        let x = vec![0xAA; 32];
        let y = vec![0xBB; 32];
        let pem = build_ec_spki_pem(OID_P256, &x, &y);

        let jwk_key = JwkKey {
            kid: "ec-pem-key".to_string(),
            alg: "ES256".to_string(),
            public_key: pem,
            private_key: None,
            kty: "EC".to_string(),
            use_: "sig".to_string(),
        };

        let public_key = convert_jwk_key_simple(&jwk_key).unwrap();
        assert_eq!(public_key.kid, "ec-pem-key");
        assert_eq!(public_key.kty, "EC");
        assert_eq!(public_key.crv, Some("P-256".to_string()));
        assert!(public_key.x.is_some(), "x should now be extracted from PEM");
        assert!(public_key.y.is_some(), "y should now be extracted from PEM");
    }

    #[test]
    fn test_convert_jwk_key_simple_oct() {
        let jwk_key = JwkKey {
            kid: "oct-key".to_string(),
            alg: "HS256".to_string(),
            public_key: "secret-data".to_string(),
            private_key: Some("secret-data".to_string()),
            kty: "oct".to_string(),
            use_: "sig".to_string(),
        };

        let public_key = convert_jwk_key_simple(&jwk_key).unwrap();
        assert_eq!(public_key.kid, "oct-key");
        assert_eq!(public_key.kty, "oct");
        assert_eq!(public_key.alg, "HS256");
    }

    #[test]
    fn test_convert_jwk_key_simple_unsupported() {
        let jwk_key = JwkKey {
            kid: "unsupported-key".to_string(),
            alg: "UNKNOWN".to_string(),
            public_key: "data".to_string(),
            private_key: None,
            kty: "UNSUPPORTED".to_string(),
            use_: "sig".to_string(),
        };

        assert!(convert_jwk_key_simple(&jwk_key).is_err());
    }

    #[test]
    fn test_convert_jwk_key_simple_rsa_json_without_exponent() {
        let jwk_json = r#"{"n": "modulus-only"}"#;
        let jwk_key = JwkKey {
            kid: "rsa-no-e".to_string(),
            alg: "RS256".to_string(),
            public_key: jwk_json.to_string(),
            private_key: None,
            kty: "RSA".to_string(),
            use_: "sig".to_string(),
        };

        let public_key = convert_jwk_key_simple(&jwk_key).unwrap();
        assert_eq!(public_key.n, Some("modulus-only".to_string()));
        assert!(public_key.e.is_none());
    }

    #[test]
    fn test_convert_jwk_key_simple_ec_json_without_y() {
        let jwk_json = r#"{"x": "x-only"}"#;
        let jwk_key = JwkKey {
            kid: "ec-no-y".to_string(),
            alg: "ES256".to_string(),
            public_key: jwk_json.to_string(),
            private_key: None,
            kty: "EC".to_string(),
            use_: "sig".to_string(),
        };

        let public_key = convert_jwk_key_simple(&jwk_key).unwrap();
        assert_eq!(public_key.x, Some("x-only".to_string()));
        assert!(public_key.y.is_none());
    }
}