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
use std::convert::{TryFrom, TryInto};

use ct_codecs::{Base64UrlSafeNoPadding, Encoder};
use p256::ecdsa::{self, signature::DigestVerifier as _, signature::RandomizedDigestSigner as _};
use p256::elliptic_curve::Generate as _;
use p256::pkcs8::{DecodePrivateKey, DecodePublicKey, EncodePrivateKey, EncodePublicKey};
use p256::NonZeroScalar;
use serde::{de::DeserializeOwned, Serialize};

use crate::algorithms::jwk::{
    decode_secret, ec_export, ec_point, ec_point_matches, ec_thumbprint, Jwk, JwkDescriptor,
    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::*;

const ES256_PUBLIC_JWK: JwkDescriptor = JwkDescriptor::new(
    KeyType::Ec,
    Some("P-256"),
    "ES256",
    KeyRole::SignaturePublic,
);
const ES256_PRIVATE_JWK: JwkDescriptor = ES256_PUBLIC_JWK.with_role(KeyRole::SignaturePrivate);

#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct P256PublicKey(ecdsa::VerifyingKey);

impl AsRef<ecdsa::VerifyingKey> for P256PublicKey {
    fn as_ref(&self) -> &ecdsa::VerifyingKey {
        &self.0
    }
}

impl P256PublicKey {
    pub(crate) fn from_jwk(jwk: &str) -> Result<(Self, Option<String>), Error> {
        let jwk = Jwk::parse(jwk, &ES256_PUBLIC_JWK)?;
        let point = ec_point(&jwk, 32).ok_or(JWTError::InvalidPublicKey)?;
        let pk = Self::from_bytes(&point)?;
        Ok((pk, jwk.kid().map(Into::into)))
    }

    pub(crate) fn to_jwk(&self, key_id: Option<&str>) -> String {
        ec_export(
            &ES256_PUBLIC_JWK,
            &self.to_bytes_uncompressed(),
            None,
            key_id,
        )
    }

    pub(crate) fn jwk_thumbprint(&self) -> String {
        ec_thumbprint("P-256", &self.to_bytes_uncompressed())
    }

    pub fn from_bytes(raw: &[u8]) -> Result<Self, Error> {
        let p256_pk =
            ecdsa::VerifyingKey::from_sec1_bytes(raw).map_err(|_| JWTError::InvalidPublicKey)?;
        Ok(P256PublicKey(p256_pk))
    }

    pub fn from_der(der: &[u8]) -> Result<Self, Error> {
        let p256_pk = ecdsa::VerifyingKey::from_public_key_der(der)
            .map_err(|_| JWTError::InvalidPublicKey)?;
        Ok(P256PublicKey(p256_pk))
    }

    pub fn from_pem(pem: &str) -> Result<Self, Error> {
        let p256_pk = ecdsa::VerifyingKey::from_public_key_pem(pem)
            .map_err(|_| JWTError::InvalidPublicKey)?;
        Ok(P256PublicKey(p256_pk))
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        self.0.to_sec1_point(true).as_bytes().to_vec()
    }

    pub fn to_bytes_uncompressed(&self) -> Vec<u8> {
        self.0.to_sec1_point(false).as_bytes().to_vec()
    }

    pub fn to_der(&self) -> Result<Vec<u8>, Error> {
        let p256_pk = p256::PublicKey::from(self.0);
        Ok(p256_pk
            .to_public_key_der()
            .map_err(|_| JWTError::InvalidPublicKey)?
            .as_ref()
            .to_vec())
    }

    pub fn to_pem(&self) -> Result<String, Error> {
        let p256_pk = p256::PublicKey::from(self.0);
        Ok(p256_pk
            .to_public_key_pem(Default::default())
            .map_err(|_| JWTError::InvalidPublicKey)?)
    }
}

#[doc(hidden)]
pub struct P256KeyPair {
    p256_sk: ecdsa::SigningKey,
    metadata: Option<KeyMetadata>,
}

impl std::fmt::Debug for P256KeyPair {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "EcKey")
    }
}

impl AsRef<ecdsa::SigningKey> for P256KeyPair {
    fn as_ref(&self) -> &ecdsa::SigningKey {
        &self.p256_sk
    }
}

impl P256KeyPair {
    pub(crate) fn from_jwk(jwk: &str) -> Result<(Self, Option<String>), Error> {
        let jwk = Jwk::parse(jwk, &ES256_PRIVATE_JWK)?;
        let d = decode_secret(jwk.d(), 32).ok_or(JWTError::InvalidKeyPair)?;
        let key_pair = Self::from_bytes(&d)?;
        ensure!(
            ec_point_matches(&jwk, &key_pair.public_key().to_bytes_uncompressed()),
            JWTError::InvalidKeyPair
        );
        Ok((key_pair, jwk.kid().map(Into::into)))
    }

    pub(crate) fn to_jwk(&self, key_id: Option<&str>) -> String {
        let point = self.public_key().to_bytes_uncompressed();
        let d = zeroize::Zeroizing::new(self.p256_sk.to_bytes().to_vec());
        ec_export(&ES256_PRIVATE_JWK, &point, Some(&d), key_id)
    }

    pub fn from_bytes(raw: &[u8]) -> Result<Self, Error> {
        let raw: &p256::FieldBytes = raw.try_into().map_err(|_| JWTError::InvalidKeyPair)?;
        let p256_sk = ecdsa::SigningKey::from_bytes(raw).map_err(|_| JWTError::InvalidKeyPair)?;
        Ok(P256KeyPair {
            p256_sk,
            metadata: None,
        })
    }

    pub fn from_der(der: &[u8]) -> Result<Self, Error> {
        let p256_sk =
            ecdsa::SigningKey::from_pkcs8_der(der).map_err(|_| JWTError::InvalidKeyPair)?;
        Ok(P256KeyPair {
            p256_sk,
            metadata: None,
        })
    }

    pub fn from_pem(pem: &str) -> Result<Self, Error> {
        let p256_sk =
            ecdsa::SigningKey::from_pkcs8_pem(pem).map_err(|_| JWTError::InvalidKeyPair)?;
        Ok(P256KeyPair {
            p256_sk,
            metadata: None,
        })
    }

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

    pub fn to_der(&self) -> Result<Vec<u8>, Error> {
        let scalar = NonZeroScalar::from_repr(self.p256_sk.to_bytes());
        if bool::from(scalar.is_none()) {
            return Err(JWTError::InvalidKeyPair.into());
        }
        let p256_sk =
            p256::SecretKey::from(NonZeroScalar::from_repr(scalar.unwrap().into()).unwrap());
        Ok(p256_sk
            .to_pkcs8_der()
            .map_err(|_| JWTError::InvalidKeyPair)?
            .as_bytes()
            .to_vec())
    }

    pub fn to_pem(&self) -> Result<String, Error> {
        let scalar = NonZeroScalar::from_repr(self.p256_sk.to_bytes());
        if bool::from(scalar.is_none()) {
            return Err(JWTError::InvalidKeyPair.into());
        }
        let p256_sk =
            p256::SecretKey::from(NonZeroScalar::from_repr(scalar.unwrap().into()).unwrap());
        Ok(p256_sk
            .to_pkcs8_pem(Default::default())
            .map_err(|_| JWTError::InvalidKeyPair)?
            .to_string())
    }

    pub fn public_key(&self) -> P256PublicKey {
        let p256_pk = self.p256_sk.verifying_key();
        P256PublicKey(*p256_pk)
    }

    pub fn generate() -> Self {
        let mut rng = rand::rng();
        let p256_sk = ecdsa::SigningKey::generate_from_rng(&mut rng);
        P256KeyPair {
            p256_sk,
            metadata: None,
        }
    }
}

pub trait ECDSAP256KeyPairLike {
    fn jwt_alg_name() -> &'static str;
    fn key_pair(&self) -> &P256KeyPair;
    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 mut rng = rand::rng();
            let signature: ecdsa::Signature = self
                .key_pair()
                .as_ref()
                .sign_digest_with_rng(&mut rng, |digest: &mut hmac_sha256::Hash| {
                    digest.update(authenticated.as_bytes())
                });
            Ok(signature.to_vec())
        })
    }
}

pub trait ECDSAP256PublicKeyLike {
    fn jwt_alg_name() -> &'static str;
    fn public_key(&self) -> &P256PublicKey;
    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| {
                let ecdsa_signature = ecdsa::Signature::try_from(signature)
                    .map_err(|_| JWTError::InvalidSignature)?;
                self.public_key()
                    .as_ref()
                    .verify_digest(
                        |digest: &mut hmac_sha256::Hash| {
                            digest.update(authenticated.as_bytes());
                            Ok(())
                        },
                        &ecdsa_signature,
                    )
                    .map_err(|_| JWTError::InvalidSignature)?;
                Ok(())
            },
            |_salt: Option<&[u8]>| Ok(()),
        )
    }

    #[cfg(feature = "cwt")]
    fn verify_cwt_token<CustomClaims: DeserializeOwned>(
        &self,
        token: &str,
        options: Option<VerificationOptions>,
    ) -> Result<JWTClaims<NoCustomClaims>, Error> {
        CWTToken::verify(
            Self::jwt_alg_name(),
            token,
            options,
            |authenticated, signature| {
                let ecdsa_signature = ecdsa::Signature::try_from(signature)
                    .map_err(|_| JWTError::InvalidSignature)?;
                self.public_key()
                    .as_ref()
                    .verify_digest(
                        |digest: &mut hmac_sha256::Hash| {
                            digest.update(authenticated.as_bytes());
                            Ok(())
                        },
                        &ecdsa_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()
    }
}

pub struct ES256KeyPair {
    key_pair: P256KeyPair,
    key_id: Option<String>,
}

impl std::fmt::Debug for ES256KeyPair {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "EcKey")
    }
}

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

impl ECDSAP256KeyPairLike for ES256KeyPair {
    fn jwt_alg_name() -> &'static str {
        "ES256"
    }

    fn key_pair(&self) -> &P256KeyPair {
        &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 ES256KeyPair {
    pub fn from_bytes(raw: &[u8]) -> Result<Self, Error> {
        Ok(ES256KeyPair {
            key_pair: P256KeyPair::from_bytes(raw)?,
            key_id: None,
        })
    }

    pub fn from_der(der: &[u8]) -> Result<Self, Error> {
        Ok(ES256KeyPair {
            key_pair: P256KeyPair::from_der(der)?,
            key_id: None,
        })
    }

    pub fn from_pem(pem: &str) -> Result<Self, Error> {
        Ok(ES256KeyPair {
            key_pair: P256KeyPair::from_pem(pem)?,
            key_id: None,
        })
    }

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

    pub fn to_der(&self) -> Result<Vec<u8>, Error> {
        self.key_pair.to_der()
    }

    pub fn to_pem(&self) -> Result<String, Error> {
        self.key_pair.to_pem()
    }

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

    pub fn generate() -> Self {
        ES256KeyPair {
            key_pair: P256KeyPair::generate(),
            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 JWK (`kty: EC`, `crv: P-256`).
    ///
    /// See [strict JWK validation](crate#strict-jwk-validation) for the rules.
    pub fn from_jwk(jwk: &str) -> Result<Self, Error> {
        let (key_pair, key_id) = P256KeyPair::from_jwk(jwk)?;
        Ok(ES256KeyPair { 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.key_id.as_deref())
    }

    /// The JWK thumbprint of the public key.
    pub fn jwk_thumbprint(&self) -> String {
        self.key_pair.public_key().jwk_thumbprint()
    }
}

impl ECDSAP256PublicKeyLike for ES256PublicKey {
    fn jwt_alg_name() -> &'static str {
        "ES256"
    }

    fn public_key(&self) -> &P256PublicKey {
        &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 ES256PublicKey {
    pub fn from_bytes(raw: &[u8]) -> Result<Self, Error> {
        Ok(ES256PublicKey {
            pk: P256PublicKey::from_bytes(raw)?,
            key_id: None,
        })
    }

    pub fn from_der(der: &[u8]) -> Result<Self, Error> {
        Ok(ES256PublicKey {
            pk: P256PublicKey::from_der(der)?,
            key_id: None,
        })
    }

    pub fn from_pem(pem: &str) -> Result<Self, Error> {
        Ok(ES256PublicKey {
            pk: P256PublicKey::from_pem(pem)?,
            key_id: None,
        })
    }

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

    pub fn to_der(&self) -> Result<Vec<u8>, Error> {
        self.pk.to_der()
    }

    pub fn to_pem(&self) -> Result<String, Error> {
        self.pk.to_pem()
    }

    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 a JWK (`kty: EC`, `crv: P-256`).
    ///
    /// See [strict JWK validation](crate#strict-jwk-validation) for the rules.
    pub fn from_jwk(jwk: &str) -> Result<Self, Error> {
        let (pk, key_id) = P256PublicKey::from_jwk(jwk)?;
        Ok(ES256PublicKey { pk, key_id })
    }

    /// Export the public key as a JWK, with `alg: ES256` and `use: sig`.
    pub fn to_jwk(&self) -> String {
        self.pk.to_jwk(self.key_id.as_deref())
    }

    /// The JWK thumbprint of the key.
    pub fn jwk_thumbprint(&self) -> String {
        self.pk.jwk_thumbprint()
    }
}

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

    // Thumbprints computed with jwcrypto 1.5.6.
    const P256: &str = r#"{"kty":"EC","crv":"P-256",
        "x":"MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4",
        "y":"4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM",
        "d":"870MB6gfuTJ4HtUnUvYMyJpr5eUZNP4Bk43bVdj3eAE"}"#;
    const P256_THUMBPRINT: &str = "cn-I_WNMClehiVp51i_0VpOENW1upEerA8sEam5hn-s";
    const P384: &str = r#"{"kty":"EC","crv":"P-384",
        "x":"LUwGxVN6rAonzPWy9u0PjghkPwE4CHit0QJ5kGzDqg8oGYav-Ln8veWuBTIz0h-e",
        "y":"U-AWY87B3rceQqi6QYg8-RYj4LZsejs1NdqTD3ueSanvFGtPpBVpURzfFFhNstIt",
        "d":"t8OYG13e1MHzM_oUKHTPZtgc0Z8WHi95AB2XVcF3N27UelbAVnqKxERaNbsir7ec"}"#;
    const P384_THUMBPRINT: &str = "X87vqkTYo4hKc59UwU-zy0NKzDsrgMRum4flPbRwXVs";
    const K256: &str = r#"{"kty":"EC","crv":"secp256k1",
        "x":"EyvzgldJCeQIOPfRJ9oZO3rVVzS8ZlFtgToMR2OWSuE",
        "y":"JlvzSwhNHfvBwDiRfgsJuT1zjRSTDzZpASPlUSmi8tA",
        "d":"hwiWAz2LjbQMp9MEzs9YzNvmcPkvJt5i-fc7kgceObk"}"#;
    const K256_THUMBPRINT: &str = "sCqxz4FJp22HsNYDd_gawjMoBLsFAaQixbb8JSoXRrA";

    #[test]
    fn independent_thumbprints() {
        let key_pair = ES256KeyPair::from_jwk(P256).unwrap();
        assert_eq!(key_pair.jwk_thumbprint(), P256_THUMBPRINT);
        let public = ES256PublicKey::from_jwk(&without(P256, &["d"])).unwrap();
        assert_eq!(public.jwk_thumbprint(), P256_THUMBPRINT);

        let key_pair = ES384KeyPair::from_jwk(P384).unwrap();
        assert_eq!(key_pair.jwk_thumbprint(), P384_THUMBPRINT);
        assert_eq!(key_pair.public_key().jwk_thumbprint(), P384_THUMBPRINT);

        let key_pair = ES256kKeyPair::from_jwk(K256).unwrap();
        assert_eq!(key_pair.jwk_thumbprint(), K256_THUMBPRINT);
        assert_eq!(key_pair.public_key().jwk_thumbprint(), K256_THUMBPRINT);
    }

    macro_rules! round_trip {
        ($key_pair:ident, $public_key:ident, $crv:expr, $alg:expr, $size:expr) => {{
            let key_pair = $key_pair::generate().with_key_id("k1");
            let private_jwk = key_pair.to_jwk();
            let public_jwk = key_pair.public_key().to_jwk();
            let exported = json(&public_jwk);
            assert_eq!(exported["kty"], "EC");
            assert_eq!(exported["crv"], $crv);
            assert_eq!(exported["alg"], $alg);
            assert_eq!(exported["use"], "sig");
            assert_eq!(exported["kid"], "k1");
            assert!(exported.get("d").is_none());
            assert_eq!(member_bytes(&public_jwk, "x").len(), $size);
            assert_eq!(member_bytes(&public_jwk, "y").len(), $size);
            assert_eq!(member_bytes(&private_jwk, "d").len(), $size);

            let restored = $key_pair::from_jwk(&private_jwk).unwrap();
            let public_key = $public_key::from_jwk(&public_jwk).unwrap();
            assert_eq!(restored.to_bytes(), key_pair.to_bytes());
            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(Duration::from_hours(1)))
                .unwrap();
            let options = VerificationOptions {
                required_key_id: Some("k1".into()),
                ..Default::default()
            };
            public_key
                .verify_token::<NoCustomClaims>(&token, Some(options))
                .unwrap();
        }};
    }

    #[test]
    fn round_trips() {
        round_trip!(ES256KeyPair, ES256PublicKey, "P-256", "ES256", 32);
        round_trip!(ES384KeyPair, ES384PublicKey, "P-384", "ES384", 48);
        round_trip!(ES256kKeyPair, ES256kPublicKey, "secp256k1", "ES256K", 32);
    }

    #[test]
    fn coordinates_are_full_size_and_checked_one_by_one() {
        let key_pair = (0..10_000)
            .map(|_| ES256KeyPair::generate())
            .find(|kp| kp.public_key().public_key().to_bytes_uncompressed()[1] == 0)
            .unwrap();
        let public_jwk = key_pair.public_key().to_jwk();
        let x = member_bytes(&public_jwk, "x");
        assert_eq!(x.len(), 32);
        assert_eq!(x[0], 0);
        ES256PublicKey::from_jwk(&public_jwk).unwrap();
        ES256KeyPair::from_jwk(&key_pair.to_jwk()).unwrap();

        let short = with(&public_jwk, "x", b64(&x[1..]).into());
        assert!(matches!(
            error_of(ES256PublicKey::from_jwk(&short)),
            JWTError::InvalidPublicKey
        ));

        let public_jwk = without(P256, &["d"]);
        let x = member_bytes(P256, "x");
        let y = member_bytes(P256, "y");
        let xy = [&x[..], &y[..]].concat();
        // A valid point must still have two full-length coordinates.
        for split in [31, 33] {
            let jwk = with(&public_jwk, "x", b64(&xy[..split]).into());
            let jwk = with(&jwk, "y", b64(&xy[split..]).into());
            assert!(
                matches!(
                    error_of(ES256PublicKey::from_jwk(&jwk)),
                    JWTError::InvalidPublicKey
                ),
                "{}",
                split
            );
        }
        let mut off_curve = y.clone();
        off_curve[31] ^= 1;
        let jwk = with(&public_jwk, "y", b64(&off_curve).into());
        assert!(ES256PublicKey::from_jwk(&jwk).is_err());
        assert!(ES256PublicKey::from_jwk(&without(&public_jwk, &["y"])).is_err());
    }

    #[test]
    fn private_scalar_must_be_valid_and_match() {
        let order = "_____wAAAAD__________7zm-q2nF56E87nKwvxjJVE";
        assert_eq!(unb64(order).len(), 32);
        let other = json(&ES256KeyPair::generate().to_jwk());
        for (case, d) in [
            ("zero", b64(&[0u8; 32])),
            ("group order", order.to_string()),
            ("other key", other["d"].as_str().unwrap().to_string()),
            ("short", b64(&member_bytes(P256, "d")[1..])),
        ] {
            let jwk = with(P256, "d", d.into());
            assert!(
                matches!(
                    error_of(ES256KeyPair::from_jwk(&jwk)),
                    JWTError::InvalidKeyPair
                ),
                "{}",
                case
            );
        }
        assert!(ES256KeyPair::from_jwk(&without(P256, &["y"])).is_err());
    }

    #[test]
    fn keys_only_load_into_their_own_type() {
        let p256_public = without(P256, &["d"]);
        assert!(ES384PublicKey::from_jwk(&p256_public).is_err());
        assert!(ES256kPublicKey::from_jwk(&p256_public).is_err());
        assert!(ES256KeyPair::from_jwk(&without(P384, &["d"])).is_err());

        ES256PublicKey::from_jwk(&with(&p256_public, "alg", "ES256".into())).unwrap();
        let mut rejected = vec![
            with(&p256_public, "crv", "secp256k1".into()),
            with(&p256_public, "use", "enc".into()),
            with(&p256_public, "key_ops", serde_json::json!(["deriveBits"])),
        ];
        for alg in [
            "ES384",
            "ES256K",
            "ECDH-ES+A256KW",
            "ECDH",
            "EdDSA",
            "RS256",
        ] {
            rejected.push(with(&p256_public, "alg", alg.into()));
        }
        for jwk in rejected {
            assert!(ES256PublicKey::from_jwk(&jwk).is_err(), "{}", jwk);
        }
    }

    #[test]
    fn webcrypto_exports() {
        for export in jwk_test_vectors::ALL {
            let export = json(export);
            let entry = &export["keys"]["ES256"];
            let key_pair = ES256KeyPair::from_jwk(&entry["private"].to_string()).unwrap();
            let public_key = ES256PublicKey::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();

            let entry = &export["keys"]["ES384"];
            let key_pair = ES384KeyPair::from_jwk(&entry["private"].to_string()).unwrap();
            let public_key = ES384PublicKey::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();

            // Without usage information, ECDH public keys look like signing keys.
            let ecdh = &export["keys"]["ECDH-P256"];
            let labeled = ecdh["public"].get("alg").is_some();
            assert_eq!(
                ES256PublicKey::from_jwk(&ecdh["public"].to_string()).is_err(),
                labeled
            );
            assert!(ES256KeyPair::from_jwk(&ecdh["private"].to_string()).is_err());
        }
    }
}