jwt-simple 0.15.0

JWT authentication with JSON Web Token signing and verification (JWS), encryption (JWE), and custom claims. Supports RSA, ECDSA, Ed25519, and ML-DSA.
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
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
use std::convert::TryInto;

use ct_codecs::{Base64UrlSafeNoPadding, Encoder};
use serde::{de::DeserializeOwned, Serialize};
use superboring::mldsa::{Algorithm, MlDsaPrivateKey, MlDsaPublicKey};

use crate::algorithms::jwk::{
    decode_fixed, decode_secret, encode, encode_secret, is_encoding_of, thumbprint, Jwk,
    JwkDescriptor, JwkMembers, KeyRole, KeyType,
};
use crate::claims::*;
use crate::common::*;
#[cfg(feature = "cwt")]
use crate::cwt_token::*;
use crate::error::*;
use crate::jwt_header::*;
use crate::token::*;

fn mldsa_jwk_descriptor(alg: &'static str, role: KeyRole) -> JwkDescriptor {
    JwkDescriptor::new(KeyType::Akp, None, alg, role)
}

#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct MLDSAPublicKey(MlDsaPublicKey);

impl AsRef<MlDsaPublicKey> for MLDSAPublicKey {
    fn as_ref(&self) -> &MlDsaPublicKey {
        &self.0
    }
}

impl MLDSAPublicKey {
    pub fn from_bytes(algorithm: Algorithm, raw: &[u8]) -> Result<Self, Error> {
        let mldsa_pk = MlDsaPublicKey::from_slice(algorithm, raw);
        Ok(MLDSAPublicKey(
            mldsa_pk.map_err(|_| JWTError::InvalidPublicKey)?,
        ))
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        self.0.to_bytes().expect("failed to serialize public key")
    }

    pub(crate) fn from_jwk(
        jwk: &str,
        alg: &'static str,
        algorithm: Algorithm,
    ) -> Result<(Self, Option<String>), Error> {
        let jwk = Jwk::parse(jwk, &mldsa_jwk_descriptor(alg, KeyRole::SignaturePublic))?;
        let mut raw = vec![0u8; algorithm.public_key_bytes()];
        ensure!(
            decode_fixed(jwk.public(), &mut raw),
            JWTError::InvalidPublicKey
        );
        Ok((
            Self::from_bytes(algorithm, &raw)?,
            jwk.kid().map(Into::into),
        ))
    }

    pub(crate) fn to_jwk(&self, alg: &'static str, key_id: Option<&str>) -> String {
        let public = encode(&self.to_bytes());
        let members = JwkMembers {
            public: Some(&public),
            ..Default::default()
        };
        mldsa_jwk_descriptor(alg, KeyRole::SignaturePublic).export(members, key_id)
    }

    pub(crate) fn jwk_thumbprint(&self, alg: &str) -> String {
        thumbprint(&[
            ("alg", alg),
            ("kty", "AKP"),
            ("pub", &encode(&self.to_bytes())),
        ])
    }
}

#[doc(hidden)]
#[derive(Clone)]
pub struct MLDSAKeyPair {
    mldsa_sk: MlDsaPrivateKey,
    metadata: Option<KeyMetadata>,
}

impl std::fmt::Debug for MLDSAKeyPair {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PKey")
            .field("algorithm", &"ML-DSA")
            .finish()
    }
}

impl AsRef<MlDsaPrivateKey> for MLDSAKeyPair {
    fn as_ref(&self) -> &MlDsaPrivateKey {
        &self.mldsa_sk
    }
}

impl MLDSAKeyPair {
    /// The raw representation of an ML-DSA key pair is its 32-byte seed, as
    /// mandated for JOSE and COSE.
    pub fn from_bytes(algorithm: Algorithm, raw: &[u8]) -> Result<Self, Error> {
        let seed = raw.try_into().map_err(|_| JWTError::InvalidKeyPair)?;
        let mldsa_sk =
            MlDsaPrivateKey::from_seed(algorithm, &seed).map_err(|_| JWTError::InvalidKeyPair)?;
        Ok(MLDSAKeyPair {
            mldsa_sk,
            metadata: None,
        })
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        self.mldsa_sk.seed_bytes().to_vec()
    }

    pub fn public_key(&self) -> MLDSAPublicKey {
        let mldsa_pk = self
            .mldsa_sk
            .public_key()
            .expect("failed to create public key");
        MLDSAPublicKey(mldsa_pk)
    }

    pub fn generate(algorithm: Algorithm) -> Self {
        let (_, mldsa_sk) =
            MlDsaPrivateKey::generate(algorithm).expect("failed to generate key pair");
        MLDSAKeyPair {
            mldsa_sk,
            metadata: None,
        }
    }

    pub(crate) fn from_jwk(
        jwk: &str,
        alg: &'static str,
        algorithm: Algorithm,
    ) -> Result<(Self, Option<String>), Error> {
        let jwk = Jwk::parse(jwk, &mldsa_jwk_descriptor(alg, KeyRole::SignaturePrivate))?;
        let seed = decode_secret(jwk.private(), 32).ok_or(JWTError::InvalidKeyPair)?;
        let key_pair = Self::from_bytes(algorithm, &seed)?;
        ensure!(
            is_encoding_of(jwk.public(), &key_pair.public_key().to_bytes()),
            JWTError::InvalidKeyPair
        );
        Ok((key_pair, jwk.kid().map(Into::into)))
    }

    pub(crate) fn to_jwk(&self, alg: &'static str, key_id: Option<&str>) -> String {
        let public = encode(&self.public_key().to_bytes());
        let private = encode_secret(self.mldsa_sk.seed_bytes());
        let members = JwkMembers {
            public: Some(&public),
            private: Some(&private),
            ..Default::default()
        };
        mldsa_jwk_descriptor(alg, KeyRole::SignaturePrivate).export(members, key_id)
    }
}

pub trait MLDSAKeyPairLike {
    fn jwt_alg_name() -> &'static str;
    fn key_pair(&self) -> &MLDSAKeyPair;
    fn key_id(&self) -> &Option<String>;
    fn metadata(&self) -> &Option<KeyMetadata>;
    fn attach_metadata(&mut self, metadata: KeyMetadata) -> Result<(), Error>;

    fn sign<CustomClaims: Serialize>(
        &self,
        claims: JWTClaims<CustomClaims>,
    ) -> Result<String, Error> {
        self.sign_with_options(claims, &Default::default())
    }

    fn sign_with_options<CustomClaims: Serialize>(
        &self,
        claims: JWTClaims<CustomClaims>,
        opts: &HeaderOptions,
    ) -> Result<String, Error> {
        let jwt_header = JWTHeader::new(Self::jwt_alg_name().to_string(), self.key_id().clone())
            .with_key_metadata(self.metadata())
            .with_options(opts);
        Token::build(&jwt_header, claims, |authenticated| {
            let signature = self.key_pair().as_ref().sign(authenticated.as_bytes())?;
            Ok(signature)
        })
    }
}

pub trait MLDSAPublicKeyLike {
    fn jwt_alg_name() -> &'static str;
    fn public_key(&self) -> &MLDSAPublicKey;
    fn key_id(&self) -> &Option<String>;
    fn set_key_id(&mut self, key_id: String);

    fn verify_token<CustomClaims: DeserializeOwned>(
        &self,
        token: &str,
        options: Option<VerificationOptions>,
    ) -> Result<JWTClaims<CustomClaims>, Error> {
        Token::verify(
            Self::jwt_alg_name(),
            token,
            options,
            |authenticated, signature| {
                self.public_key()
                    .as_ref()
                    .verify(authenticated.as_bytes(), signature)
                    .map_err(|_| JWTError::InvalidSignature)?;
                Ok(())
            },
            |_salt: Option<&[u8]>| Ok(()),
        )
    }

    #[cfg(feature = "cwt")]
    fn verify_cwt_token<CustomClaims: DeserializeOwned>(
        &self,
        token: &[u8],
        options: Option<VerificationOptions>,
    ) -> Result<JWTClaims<NoCustomClaims>, Error> {
        CWTToken::verify(
            Self::jwt_alg_name(),
            token,
            options,
            |authenticated, signature| {
                self.public_key()
                    .as_ref()
                    .verify(authenticated.as_bytes(), signature)
                    .map_err(|_| JWTError::InvalidSignature)?;
                Ok(())
            },
        )
    }

    /// Decode CWT token metadata that can be useful prior to signature/tag verification
    #[cfg(feature = "cwt")]
    fn decode_cwt_metadata(&self, token: impl AsRef<[u8]>) -> Result<TokenMetadata, Error> {
        CWTToken::decode_metadata(token)
    }

    fn create_key_id(&mut self) -> &str {
        self.set_key_id(
            Base64UrlSafeNoPadding::encode_to_string(hmac_sha256::Hash::hash(
                &self.public_key().to_bytes(),
            ))
            .unwrap(),
        );
        self.key_id().as_ref().map(|x| x.as_str()).unwrap()
    }
}

#[derive(Clone)]
pub struct MLDSA44KeyPair {
    key_pair: MLDSAKeyPair,
    key_id: Option<String>,
}

impl std::fmt::Debug for MLDSA44KeyPair {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PKey")
            .field("algorithm", &"ML-DSA-44")
            .finish()
    }
}

#[derive(Debug, Clone)]
pub struct MLDSA44PublicKey {
    pk: MLDSAPublicKey,
    key_id: Option<String>,
}

impl MLDSAKeyPairLike for MLDSA44KeyPair {
    fn jwt_alg_name() -> &'static str {
        "ML-DSA-44"
    }

    fn key_pair(&self) -> &MLDSAKeyPair {
        &self.key_pair
    }

    fn key_id(&self) -> &Option<String> {
        &self.key_id
    }

    fn metadata(&self) -> &Option<KeyMetadata> {
        &self.key_pair.metadata
    }

    fn attach_metadata(&mut self, metadata: KeyMetadata) -> Result<(), Error> {
        self.key_pair.metadata = Some(metadata);
        Ok(())
    }
}

impl MLDSA44KeyPair {
    pub fn from_bytes(raw: &[u8]) -> Result<Self, Error> {
        Ok(MLDSA44KeyPair {
            key_pair: MLDSAKeyPair::from_bytes(Algorithm::MlDsa44, raw)?,
            key_id: None,
        })
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        self.key_pair.to_bytes()
    }

    pub fn public_key(&self) -> MLDSA44PublicKey {
        MLDSA44PublicKey {
            pk: self.key_pair.public_key(),
            key_id: self.key_id.clone(),
        }
    }

    pub fn generate() -> Self {
        MLDSA44KeyPair {
            key_pair: MLDSAKeyPair::generate(Algorithm::MlDsa44),
            key_id: None,
        }
    }

    pub fn with_key_id(mut self, key_id: &str) -> Self {
        self.key_id = Some(key_id.to_string());
        self
    }

    /// Import a key pair from a private AKP JWK, with `alg: ML-DSA-44`.
    ///
    /// `priv` must contain the 32-byte seed used by `from_bytes()`.
    /// See [strict JWK validation](crate#strict-jwk-validation) for the other rules.
    pub fn from_jwk(jwk: &str) -> Result<Self, Error> {
        let (key_pair, key_id) =
            MLDSAKeyPair::from_jwk(jwk, Self::jwt_alg_name(), Algorithm::MlDsa44)?;
        Ok(MLDSA44KeyPair { key_pair, key_id })
    }

    /// Export the key pair as a JWK, private key included.
    ///
    /// Use `public_key().to_jwk()` to share the public key.
    pub fn to_jwk(&self) -> String {
        self.key_pair
            .to_jwk(Self::jwt_alg_name(), self.key_id.as_deref())
    }

    /// The JWK thumbprint of the public key, which includes its algorithm.
    pub fn jwk_thumbprint(&self) -> String {
        self.key_pair
            .public_key()
            .jwk_thumbprint(Self::jwt_alg_name())
    }
}

impl MLDSAPublicKeyLike for MLDSA44PublicKey {
    fn jwt_alg_name() -> &'static str {
        "ML-DSA-44"
    }

    fn public_key(&self) -> &MLDSAPublicKey {
        &self.pk
    }

    fn key_id(&self) -> &Option<String> {
        &self.key_id
    }

    fn set_key_id(&mut self, key_id: String) {
        self.key_id = Some(key_id);
    }
}

impl MLDSA44PublicKey {
    pub fn from_bytes(raw: &[u8]) -> Result<Self, Error> {
        Ok(MLDSA44PublicKey {
            pk: MLDSAPublicKey::from_bytes(Algorithm::MlDsa44, raw)?,
            key_id: None,
        })
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        self.pk.to_bytes()
    }

    pub fn with_key_id(mut self, key_id: &str) -> Self {
        self.key_id = Some(key_id.to_string());
        self
    }

    /// Import a public key from an AKP JWK, with `alg: ML-DSA-44`.
    ///
    /// See [strict JWK validation](crate#strict-jwk-validation) for the rules.
    pub fn from_jwk(jwk: &str) -> Result<Self, Error> {
        let (pk, key_id) = MLDSAPublicKey::from_jwk(jwk, Self::jwt_alg_name(), Algorithm::MlDsa44)?;
        Ok(MLDSA44PublicKey { pk, key_id })
    }

    /// Export the public key as an AKP JWK, with `alg: ML-DSA-44` and `use: sig`.
    pub fn to_jwk(&self) -> String {
        self.pk.to_jwk(Self::jwt_alg_name(), self.key_id.as_deref())
    }

    /// The JWK thumbprint of the key, which includes its algorithm.
    pub fn jwk_thumbprint(&self) -> String {
        self.pk.jwk_thumbprint(Self::jwt_alg_name())
    }
}

//

#[derive(Clone)]
pub struct MLDSA65KeyPair {
    key_pair: MLDSAKeyPair,
    key_id: Option<String>,
}

impl std::fmt::Debug for MLDSA65KeyPair {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PKey")
            .field("algorithm", &"ML-DSA-65")
            .finish()
    }
}

#[derive(Debug, Clone)]
pub struct MLDSA65PublicKey {
    pk: MLDSAPublicKey,
    key_id: Option<String>,
}

impl MLDSAKeyPairLike for MLDSA65KeyPair {
    fn jwt_alg_name() -> &'static str {
        "ML-DSA-65"
    }

    fn key_pair(&self) -> &MLDSAKeyPair {
        &self.key_pair
    }

    fn key_id(&self) -> &Option<String> {
        &self.key_id
    }

    fn metadata(&self) -> &Option<KeyMetadata> {
        &self.key_pair.metadata
    }

    fn attach_metadata(&mut self, metadata: KeyMetadata) -> Result<(), Error> {
        self.key_pair.metadata = Some(metadata);
        Ok(())
    }
}

impl MLDSA65KeyPair {
    pub fn from_bytes(raw: &[u8]) -> Result<Self, Error> {
        Ok(MLDSA65KeyPair {
            key_pair: MLDSAKeyPair::from_bytes(Algorithm::MlDsa65, raw)?,
            key_id: None,
        })
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        self.key_pair.to_bytes()
    }

    pub fn public_key(&self) -> MLDSA65PublicKey {
        MLDSA65PublicKey {
            pk: self.key_pair.public_key(),
            key_id: self.key_id.clone(),
        }
    }

    pub fn generate() -> Self {
        MLDSA65KeyPair {
            key_pair: MLDSAKeyPair::generate(Algorithm::MlDsa65),
            key_id: None,
        }
    }

    pub fn with_key_id(mut self, key_id: &str) -> Self {
        self.key_id = Some(key_id.to_string());
        self
    }

    /// Import a key pair from a private AKP JWK, with `alg: ML-DSA-65`.
    ///
    /// `priv` must contain the 32-byte seed used by `from_bytes()`.
    /// See [strict JWK validation](crate#strict-jwk-validation) for the other rules.
    pub fn from_jwk(jwk: &str) -> Result<Self, Error> {
        let (key_pair, key_id) =
            MLDSAKeyPair::from_jwk(jwk, Self::jwt_alg_name(), Algorithm::MlDsa65)?;
        Ok(MLDSA65KeyPair { key_pair, key_id })
    }

    /// Export the key pair as a JWK, private key included.
    ///
    /// Use `public_key().to_jwk()` to share the public key.
    pub fn to_jwk(&self) -> String {
        self.key_pair
            .to_jwk(Self::jwt_alg_name(), self.key_id.as_deref())
    }

    /// The JWK thumbprint of the public key, which includes its algorithm.
    pub fn jwk_thumbprint(&self) -> String {
        self.key_pair
            .public_key()
            .jwk_thumbprint(Self::jwt_alg_name())
    }
}

impl MLDSAPublicKeyLike for MLDSA65PublicKey {
    fn jwt_alg_name() -> &'static str {
        "ML-DSA-65"
    }

    fn public_key(&self) -> &MLDSAPublicKey {
        &self.pk
    }

    fn key_id(&self) -> &Option<String> {
        &self.key_id
    }

    fn set_key_id(&mut self, key_id: String) {
        self.key_id = Some(key_id);
    }
}

impl MLDSA65PublicKey {
    pub fn from_bytes(raw: &[u8]) -> Result<Self, Error> {
        Ok(MLDSA65PublicKey {
            pk: MLDSAPublicKey::from_bytes(Algorithm::MlDsa65, raw)?,
            key_id: None,
        })
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        self.pk.to_bytes()
    }

    pub fn with_key_id(mut self, key_id: &str) -> Self {
        self.key_id = Some(key_id.to_string());
        self
    }

    /// Import a public key from an AKP JWK, with `alg: ML-DSA-65`.
    ///
    /// See [strict JWK validation](crate#strict-jwk-validation) for the rules.
    pub fn from_jwk(jwk: &str) -> Result<Self, Error> {
        let (pk, key_id) = MLDSAPublicKey::from_jwk(jwk, Self::jwt_alg_name(), Algorithm::MlDsa65)?;
        Ok(MLDSA65PublicKey { pk, key_id })
    }

    /// Export the public key as an AKP JWK, with `alg: ML-DSA-65` and `use: sig`.
    pub fn to_jwk(&self) -> String {
        self.pk.to_jwk(Self::jwt_alg_name(), self.key_id.as_deref())
    }

    /// The JWK thumbprint of the key, which includes its algorithm.
    pub fn jwk_thumbprint(&self) -> String {
        self.pk.jwk_thumbprint(Self::jwt_alg_name())
    }
}

//

#[derive(Clone)]
pub struct MLDSA87KeyPair {
    key_pair: MLDSAKeyPair,
    key_id: Option<String>,
}

impl std::fmt::Debug for MLDSA87KeyPair {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PKey")
            .field("algorithm", &"ML-DSA-87")
            .finish()
    }
}

#[derive(Debug, Clone)]
pub struct MLDSA87PublicKey {
    pk: MLDSAPublicKey,
    key_id: Option<String>,
}

impl MLDSAKeyPairLike for MLDSA87KeyPair {
    fn jwt_alg_name() -> &'static str {
        "ML-DSA-87"
    }

    fn key_pair(&self) -> &MLDSAKeyPair {
        &self.key_pair
    }

    fn key_id(&self) -> &Option<String> {
        &self.key_id
    }

    fn metadata(&self) -> &Option<KeyMetadata> {
        &self.key_pair.metadata
    }

    fn attach_metadata(&mut self, metadata: KeyMetadata) -> Result<(), Error> {
        self.key_pair.metadata = Some(metadata);
        Ok(())
    }
}

impl MLDSA87KeyPair {
    pub fn from_bytes(raw: &[u8]) -> Result<Self, Error> {
        Ok(MLDSA87KeyPair {
            key_pair: MLDSAKeyPair::from_bytes(Algorithm::MlDsa87, raw)?,
            key_id: None,
        })
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        self.key_pair.to_bytes()
    }

    pub fn public_key(&self) -> MLDSA87PublicKey {
        MLDSA87PublicKey {
            pk: self.key_pair.public_key(),
            key_id: self.key_id.clone(),
        }
    }

    pub fn generate() -> Self {
        MLDSA87KeyPair {
            key_pair: MLDSAKeyPair::generate(Algorithm::MlDsa87),
            key_id: None,
        }
    }

    pub fn with_key_id(mut self, key_id: &str) -> Self {
        self.key_id = Some(key_id.to_string());
        self
    }

    /// Import a key pair from a private AKP JWK, with `alg: ML-DSA-87`.
    ///
    /// `priv` must contain the 32-byte seed used by `from_bytes()`.
    /// See [strict JWK validation](crate#strict-jwk-validation) for the other rules.
    pub fn from_jwk(jwk: &str) -> Result<Self, Error> {
        let (key_pair, key_id) =
            MLDSAKeyPair::from_jwk(jwk, Self::jwt_alg_name(), Algorithm::MlDsa87)?;
        Ok(MLDSA87KeyPair { key_pair, key_id })
    }

    /// Export the key pair as a JWK, private key included.
    ///
    /// Use `public_key().to_jwk()` to share the public key.
    pub fn to_jwk(&self) -> String {
        self.key_pair
            .to_jwk(Self::jwt_alg_name(), self.key_id.as_deref())
    }

    /// The JWK thumbprint of the public key, which includes its algorithm.
    pub fn jwk_thumbprint(&self) -> String {
        self.key_pair
            .public_key()
            .jwk_thumbprint(Self::jwt_alg_name())
    }
}

impl MLDSAPublicKeyLike for MLDSA87PublicKey {
    fn jwt_alg_name() -> &'static str {
        "ML-DSA-87"
    }

    fn public_key(&self) -> &MLDSAPublicKey {
        &self.pk
    }

    fn key_id(&self) -> &Option<String> {
        &self.key_id
    }

    fn set_key_id(&mut self, key_id: String) {
        self.key_id = Some(key_id);
    }
}

impl MLDSA87PublicKey {
    pub fn from_bytes(raw: &[u8]) -> Result<Self, Error> {
        Ok(MLDSA87PublicKey {
            pk: MLDSAPublicKey::from_bytes(Algorithm::MlDsa87, raw)?,
            key_id: None,
        })
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        self.pk.to_bytes()
    }

    pub fn with_key_id(mut self, key_id: &str) -> Self {
        self.key_id = Some(key_id.to_string());
        self
    }

    /// Import a public key from an AKP JWK, with `alg: ML-DSA-87`.
    ///
    /// See [strict JWK validation](crate#strict-jwk-validation) for the rules.
    pub fn from_jwk(jwk: &str) -> Result<Self, Error> {
        let (pk, key_id) = MLDSAPublicKey::from_jwk(jwk, Self::jwt_alg_name(), Algorithm::MlDsa87)?;
        Ok(MLDSA87PublicKey { pk, key_id })
    }

    /// Export the public key as an AKP JWK, with `alg: ML-DSA-87` and `use: sig`.
    pub fn to_jwk(&self) -> String {
        self.pk.to_jwk(Self::jwt_alg_name(), self.key_id.as_deref())
    }

    /// The JWK thumbprint of the key, which includes its algorithm.
    pub fn jwk_thumbprint(&self) -> String {
        self.pk.jwk_thumbprint(Self::jwt_alg_name())
    }
}

#[cfg(test)]
mod jwk_tests {
    use super::*;
    use crate::algorithms::jwk_test_vectors;
    use crate::algorithms::test_util::{b64, error_of, json as value, unb64, with, without};

    macro_rules! rfc9964_vector {
        ($vector:expr, $key_pair:ident, $public_key:ident) => {{
            let vector = $vector;
            let private_jwk = vector["jwk"].to_string();
            let kid = vector["jwk"]["kid"].as_str().unwrap();
            let key_pair = $key_pair::from_jwk(&private_jwk).unwrap();
            assert_eq!(key_pair.key_id.as_deref(), Some(kid));
            assert_eq!(key_pair.to_bytes(), [0u8; 32]);
            assert_eq!(key_pair.jwk_thumbprint(), kid);
            assert_eq!(value(&key_pair.to_jwk())["pub"], vector["jwk"]["pub"]);

            let public_key = $public_key::from_jwk(&without(&private_jwk, &["priv"])).unwrap();
            assert_eq!(public_key.jwk_thumbprint(), kid);
            assert_eq!(key_pair.public_key().to_bytes(), public_key.to_bytes());

            // The example has no claims, so check its signature directly.
            let jws = vector["jws"].as_str().unwrap();
            let (signed, signature) = jws.rsplit_once('.').unwrap();
            public_key
                .public_key()
                .as_ref()
                .verify(signed.as_bytes(), &unb64(signature))
                .unwrap();
            private_jwk
        }};
    }

    #[test]
    fn rfc9964_vectors() {
        let vectors = value(jwk_test_vectors::RFC9964_JOSE);
        let jwk44 = rfc9964_vector!(&vectors[0], MLDSA44KeyPair, MLDSA44PublicKey);
        let jwk65 = rfc9964_vector!(&vectors[1], MLDSA65KeyPair, MLDSA65PublicKey);
        let jwk87 = rfc9964_vector!(&vectors[2], MLDSA87KeyPair, MLDSA87PublicKey);

        // Changing `alg` must not let a key load as a different type.
        assert!(MLDSA65KeyPair::from_jwk(&jwk44).is_err());
        assert!(MLDSA44KeyPair::from_jwk(&jwk65).is_err());
        assert!(MLDSA44PublicKey::from_jwk(&without(&jwk87, &["priv"])).is_err());
        let relabeled = with(&jwk44, "alg", "ML-DSA-65".into());
        assert!(MLDSA65KeyPair::from_jwk(&relabeled).is_err());
        assert!(MLDSA44KeyPair::from_jwk(&relabeled).is_err());
    }

    #[test]
    fn akp_keys_require_their_algorithm() {
        let key_pair = MLDSA44KeyPair::generate();
        let private_jwk = key_pair.to_jwk();
        let public_jwk = key_pair.public_key().to_jwk();
        for jwk in [
            without(&public_jwk, &["alg"]),
            with(&public_jwk, "alg", "ML-DSA-44 ".into()),
            with(&public_jwk, "alg", serde_json::Value::Null),
            with(&public_jwk, "crv", "Ed25519".into()),
            with(&public_jwk, "x", "AAAA".into()),
            with(&public_jwk, "priv", serde_json::Value::Null),
            with(&public_jwk, "use", "enc".into()),
            with(&public_jwk, "kty", "OKP".into()),
        ] {
            assert!(
                matches!(
                    error_of(MLDSA44PublicKey::from_jwk(&jwk)),
                    JWTError::InvalidPublicKey
                ),
                "{}",
                jwk
            );
        }
        assert!(matches!(
            error_of(MLDSA44KeyPair::from_jwk(&without(&private_jwk, &["alg"]))),
            JWTError::InvalidKeyPair
        ));
    }

    #[test]
    fn private_key_is_the_seed() {
        let key_pair = MLDSA44KeyPair::generate();
        let other = MLDSA44KeyPair::generate();
        let private_jwk = key_pair.to_jwk();
        let seed = key_pair.to_bytes();
        let with_private = |private: &[u8]| with(&private_jwk, "priv", b64(private).into());
        let other_public = value(&other.to_jwk())["pub"].clone();
        for (case, jwk) in [
            ("short seed", with_private(&seed[..31])),
            ("long seed", with_private(&[&seed[..], &[0]].concat())),
            // ML-DSA-44 expanded keys are 2560 bytes.
            ("expanded key", with_private(&[0x5au8; 2560])),
            ("empty", with_private(&[])),
            ("other seed", with_private(&other.to_bytes())),
            ("other public key", with(&private_jwk, "pub", other_public)),
            ("no public key", without(&private_jwk, &["pub"])),
        ] {
            assert!(
                matches!(
                    error_of(MLDSA44KeyPair::from_jwk(&jwk)),
                    JWTError::InvalidKeyPair
                ),
                "{}",
                case
            );
        }
    }

    #[test]
    fn round_trips() {
        let key_pair = MLDSA65KeyPair::generate().with_key_id("pq");
        let private_jwk = key_pair.to_jwk();
        let public_jwk = key_pair.public_key().to_jwk();
        let exported = value(&public_jwk);
        assert_eq!(exported["kty"], "AKP");
        assert_eq!(exported["alg"], "ML-DSA-65");
        assert_eq!(exported["use"], "sig");
        assert_eq!(exported["kid"], "pq");
        assert!(exported.get("priv").is_none());

        let restored = MLDSA65KeyPair::from_jwk(&private_jwk).unwrap();
        let public_key = MLDSA65PublicKey::from_jwk(&public_jwk).unwrap();
        assert_eq!(restored.to_jwk(), private_jwk);
        assert_eq!(public_key.to_jwk(), public_jwk);
        assert_eq!(restored.jwk_thumbprint(), public_key.jwk_thumbprint());
        let token = restored
            .sign(Claims::create(coarsetime::Duration::from_hours(1)))
            .unwrap();
        public_key
            .verify_token::<NoCustomClaims>(&token, None)
            .unwrap();

        let mut public_key = public_key;
        let key_id = public_key.create_key_id().to_string();
        let raw_hash = hmac_sha256::Hash::hash(&public_key.to_bytes());
        assert_eq!(key_id, b64(&raw_hash));
    }

    #[test]
    fn webcrypto_exports() {
        for export in jwk_test_vectors::ALL {
            let export = value(export);
            let keys = &export["keys"];
            if let Some(entry) = keys.get("ML-DSA-44") {
                let key_pair = MLDSA44KeyPair::from_jwk(&entry["private"].to_string()).unwrap();
                let public_key = MLDSA44PublicKey::from_jwk(&entry["public"].to_string()).unwrap();
                assert_eq!(key_pair.jwk_thumbprint(), public_key.jwk_thumbprint());
                public_key
                    .verify_token::<NoCustomClaims>(entry["jwt"].as_str().unwrap(), None)
                    .unwrap();
            }
            if let Some(entry) = keys.get("ML-DSA-65") {
                let key_pair = MLDSA65KeyPair::from_jwk(&entry["private"].to_string()).unwrap();
                let public_key = MLDSA65PublicKey::from_jwk(&entry["public"].to_string()).unwrap();
                assert_eq!(key_pair.jwk_thumbprint(), public_key.jwk_thumbprint());
            }
            if let Some(entry) = keys.get("ML-DSA-87") {
                let key_pair = MLDSA87KeyPair::from_jwk(&entry["private"].to_string()).unwrap();
                let public_key = MLDSA87PublicKey::from_jwk(&entry["public"].to_string()).unwrap();
                assert_eq!(key_pair.jwk_thumbprint(), public_key.jwk_thumbprint());
            }
        }
    }
}