day-part-auth 0.1.2

Sign-in for Day apps: OAuth 2.0 and OpenID Connect through the system's auth browser, Sign in with Apple, Google, and passkeys
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
// Copyright © The Daybrite Project
// SPDX-License-Identifier: MPL-2.0

//! JSON Web Signatures, JSON Web Keys and OpenID Connect ID-token claims (RFC 7515, RFC 7517,
//! OpenID Connect Core 1.0 §3.1.3.7).
//!
//! An ID token is accepted only when its signature verifies under one of the provider's
//! published keys (`jwks_uri`) with the algorithm the header names (RS256 or ES256; `none` and
//! everything else is refused) AND its claims pass [`validate`]: issuer, audience, expiry,
//! issue time, nonce, `azp` when several audiences, and `at_hash` when an access token was
//! issued alongside. Keys are matched by `kid` where the header carries one, else every key of
//! the right type is tried.

use std::time::{SystemTime, UNIX_EPOCH};

use serde::Deserialize;
use serde_json::Value;

use crate::AuthError;
use crate::crypto::{self, BigUint, P256, RsaPublicKey};

/// One key of a JWK set (RFC 7517 §4, RFC 7518 §6).
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
pub struct Jwk {
    pub kty: String,
    #[serde(default)]
    pub kid: Option<String>,
    #[serde(default)]
    pub alg: Option<String>,
    #[serde(default, rename = "use")]
    pub use_: Option<String>,
    /// RSA modulus, base64url.
    #[serde(default)]
    pub n: Option<String>,
    /// RSA exponent, base64url.
    #[serde(default)]
    pub e: Option<String>,
    /// EC curve name.
    #[serde(default)]
    pub crv: Option<String>,
    /// EC coordinates, base64url.
    #[serde(default)]
    pub x: Option<String>,
    #[serde(default)]
    pub y: Option<String>,
}

/// A JWK set, as a provider's `jwks_uri` serves it.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq)]
pub struct Jwks {
    #[serde(default)]
    pub keys: Vec<Jwk>,
}

impl Jwks {
    /// Parse a JWK set document.
    ///
    /// # Errors
    ///
    /// Returns [`AuthError::InvalidToken`] when `json` is not a JWK set.
    pub fn parse(json: &str) -> Result<Jwks, AuthError> {
        serde_json::from_str(json).map_err(|e| AuthError::InvalidToken(format!("JWK set: {e}")))
    }
}

/// A signing algorithm this crate verifies.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Algorithm {
    /// RSASSA-PKCS1-v1_5 with SHA-256.
    Rs256,
    /// ECDSA on P-256 with SHA-256.
    Es256,
}

impl Algorithm {
    fn parse(name: &str) -> Option<Algorithm> {
        match name {
            "RS256" => Some(Algorithm::Rs256),
            "ES256" => Some(Algorithm::Es256),
            _ => None,
        }
    }

    /// The JOSE name.
    pub fn name(self) -> &'static str {
        match self {
            Algorithm::Rs256 => "RS256",
            Algorithm::Es256 => "ES256",
        }
    }
}

/// A decoded compact JWS: the header and payload as JSON, and what the signature covers.
#[derive(Clone, Debug)]
pub struct Jws {
    pub header: Value,
    pub payload: Value,
    signing_input: String,
    signature: Vec<u8>,
}

impl Jws {
    /// Split and decode `header.payload.signature`. Nothing is verified yet.
    ///
    /// # Errors
    ///
    /// Returns [`AuthError::InvalidToken`] when `token` does not have three base64url parts or
    /// its header or payload is not JSON.
    pub fn decode(token: &str) -> Result<Jws, AuthError> {
        let mut parts = token.split('.');
        let (Some(h), Some(p), Some(s), None) =
            (parts.next(), parts.next(), parts.next(), parts.next())
        else {
            return Err(AuthError::InvalidToken("not a compact JWS".into()));
        };
        let header_bytes = crypto::base64url_decode(h)
            .ok_or_else(|| AuthError::InvalidToken("header is not base64url".into()))?;
        let payload_bytes = crypto::base64url_decode(p)
            .ok_or_else(|| AuthError::InvalidToken("payload is not base64url".into()))?;
        let signature = crypto::base64url_decode(s)
            .ok_or_else(|| AuthError::InvalidToken("signature is not base64url".into()))?;
        let header: Value = serde_json::from_slice(&header_bytes)
            .map_err(|e| AuthError::InvalidToken(format!("header: {e}")))?;
        let payload: Value = serde_json::from_slice(&payload_bytes)
            .map_err(|e| AuthError::InvalidToken(format!("payload: {e}")))?;
        if !header.is_object() || !payload.is_object() {
            return Err(AuthError::InvalidToken(
                "header and payload must be objects".into(),
            ));
        }
        Ok(Jws {
            header,
            payload,
            signing_input: format!("{h}.{p}"),
            signature,
        })
    }

    /// The header's `alg`, when it is one this crate verifies.
    ///
    /// # Errors
    ///
    /// Returns [`AuthError::InvalidToken`] for any other algorithm, `none` included.
    pub fn algorithm(&self) -> Result<Algorithm, AuthError> {
        let alg = self.header.get("alg").and_then(Value::as_str).unwrap_or("");
        Algorithm::parse(alg)
            .ok_or_else(|| AuthError::InvalidToken(format!("unsupported alg {alg:?}")))
    }

    /// The header's `kid`.
    pub fn key_id(&self) -> Option<&str> {
        self.header.get("kid").and_then(Value::as_str)
    }

    /// Verify the signature against `jwks`. The key is the one whose `kid` matches the
    /// header's; without a `kid`, every key of the algorithm's type is tried.
    ///
    /// # Errors
    ///
    /// Returns [`AuthError::InvalidToken`] when the algorithm is not supported, no key matches,
    /// or the signature does not verify.
    pub fn verify(&self, jwks: &Jwks) -> Result<(), AuthError> {
        let alg = self.algorithm()?;
        let kid = self.key_id();
        let candidates: Vec<&Jwk> = jwks
            .keys
            .iter()
            .filter(|k| match kid {
                Some(kid) => k.kid.as_deref() == Some(kid),
                None => true,
            })
            .filter(|k| match alg {
                Algorithm::Rs256 => k.kty == "RSA",
                Algorithm::Es256 => k.kty == "EC",
            })
            .filter(|k| k.alg.as_deref().is_none_or(|a| a == alg.name()))
            .filter(|k| k.use_.as_deref().is_none_or(|u| u == "sig"))
            .collect();
        if candidates.is_empty() {
            return Err(AuthError::InvalidToken(match kid {
                Some(kid) => format!("no {} key {kid:?} in the provider's key set", alg.name()),
                None => format!("no {} key in the provider's key set", alg.name()),
            }));
        }
        for key in candidates {
            if self.verify_with(alg, key) {
                return Ok(());
            }
        }
        Err(AuthError::InvalidToken("signature does not verify".into()))
    }

    fn verify_with(&self, alg: Algorithm, key: &Jwk) -> bool {
        let input = self.signing_input.as_bytes();
        match alg {
            Algorithm::Rs256 => {
                let (Some(n), Some(e)) = (
                    key.n.as_deref().and_then(crypto::base64url_decode),
                    key.e.as_deref().and_then(crypto::base64url_decode),
                ) else {
                    return false;
                };
                crypto::rsa_pkcs1_sha256_verify(&RsaPublicKey { n, e }, input, &self.signature)
            }
            Algorithm::Es256 => {
                if key.crv.as_deref() != Some("P-256") || self.signature.len() != 64 {
                    return false;
                }
                let (Some(x), Some(y)) = (
                    key.x.as_deref().and_then(crypto::base64url_decode),
                    key.y.as_deref().and_then(crypto::base64url_decode),
                ) else {
                    return false;
                };
                let curve = P256::new();
                let hash = crypto::sha256(input);
                curve.ecdsa_verify(
                    &BigUint::from_be_bytes(&x),
                    &BigUint::from_be_bytes(&y),
                    &hash,
                    &BigUint::from_be_bytes(&self.signature[..32]),
                    &BigUint::from_be_bytes(&self.signature[32..]),
                )
            }
        }
    }
}

/// The claims an ID token carries (OpenID Connect Core §2 and §5.1), plus the whole payload.
#[derive(Clone, Debug, PartialEq)]
pub struct Claims {
    pub issuer: Option<String>,
    pub subject: Option<String>,
    pub audience: Vec<String>,
    pub expires_at: Option<u64>,
    pub issued_at: Option<u64>,
    pub not_before: Option<u64>,
    pub auth_time: Option<u64>,
    pub nonce: Option<String>,
    pub authorized_party: Option<String>,
    pub access_token_hash: Option<String>,
    pub email: Option<String>,
    pub email_verified: Option<bool>,
    pub name: Option<String>,
    pub given_name: Option<String>,
    pub family_name: Option<String>,
    pub preferred_username: Option<String>,
    pub picture: Option<String>,
    /// Every claim, as the token carried it.
    pub raw: Value,
}

impl Claims {
    /// Read the claims out of a decoded payload.
    pub fn from_payload(payload: &Value) -> Claims {
        let s = |k: &str| payload.get(k).and_then(Value::as_str).map(str::to_string);
        let u = |k: &str| payload.get(k).and_then(Value::as_u64);
        let audience = match payload.get("aud") {
            Some(Value::String(a)) => vec![a.clone()],
            Some(Value::Array(items)) => items
                .iter()
                .filter_map(Value::as_str)
                .map(str::to_string)
                .collect(),
            _ => Vec::new(),
        };
        // Some providers send `email_verified` as the string "true".
        let email_verified = match payload.get("email_verified") {
            Some(Value::Bool(b)) => Some(*b),
            Some(Value::String(s)) => Some(s == "true"),
            _ => None,
        };
        Claims {
            issuer: s("iss"),
            subject: s("sub"),
            audience,
            expires_at: u("exp"),
            issued_at: u("iat"),
            not_before: u("nbf"),
            auth_time: u("auth_time"),
            nonce: s("nonce"),
            authorized_party: s("azp"),
            access_token_hash: s("at_hash"),
            email: s("email"),
            email_verified,
            name: s("name"),
            given_name: s("given_name"),
            family_name: s("family_name"),
            preferred_username: s("preferred_username"),
            picture: s("picture"),
            raw: payload.clone(),
        }
    }

    /// A claim by name, for anything not listed above.
    pub fn get(&self, name: &str) -> Option<&Value> {
        self.raw.get(name)
    }
}

/// What an ID token is checked against.
#[derive(Clone, Debug)]
pub struct Expected {
    /// The provider's issuer identifier; `None` skips the check (a provider with no discovery
    /// document).
    pub issuer: Option<String>,
    /// This client's id, which must be among the audiences.
    pub client_id: String,
    /// The nonce sent with the authorization request, which the token must echo.
    pub nonce: Option<String>,
    /// The access token issued alongside, for `at_hash`.
    pub access_token: Option<String>,
    /// Clock skew tolerated on `exp`, `iat` and `nbf`, in seconds.
    pub leeway_secs: u64,
    /// The time to check against, seconds since the epoch; `None` is now.
    pub now: Option<u64>,
}

impl Expected {
    /// The checks for one client, with sixty seconds of leeway.
    pub fn new(issuer: Option<String>, client_id: impl Into<String>) -> Expected {
        Expected {
            issuer,
            client_id: client_id.into(),
            nonce: None,
            access_token: None,
            leeway_secs: 60,
            now: None,
        }
    }
}

/// Seconds since the epoch.
pub fn now_secs() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |d| d.as_secs())
}

/// OpenID Connect Core §3.1.3.7, the client's part: steps 2 through 11, the ones that do not
/// depend on the signature already checked by [`Jws::verify`].
///
/// # Errors
///
/// Returns [`AuthError::InvalidToken`] naming the first claim that fails.
pub fn validate(claims: &Claims, alg: Algorithm, expected: &Expected) -> Result<(), AuthError> {
    let now = expected.now.unwrap_or_else(now_secs);
    let leeway = expected.leeway_secs;
    if let Some(issuer) = &expected.issuer {
        match &claims.issuer {
            Some(iss) if iss == issuer => {}
            Some(iss) => {
                return Err(AuthError::InvalidToken(format!(
                    "issuer {iss:?} is not {issuer:?}"
                )));
            }
            None => return Err(AuthError::InvalidToken("no issuer".into())),
        }
    }
    if claims.subject.as_deref().unwrap_or("").is_empty() {
        return Err(AuthError::InvalidToken("no subject".into()));
    }
    if !claims.audience.iter().any(|a| a == &expected.client_id) {
        return Err(AuthError::InvalidToken(
            "this client is not an audience".into(),
        ));
    }
    if claims.audience.len() > 1 {
        match &claims.authorized_party {
            Some(azp) if azp == &expected.client_id => {}
            Some(_) => return Err(AuthError::InvalidToken("azp is another client".into())),
            None => {
                return Err(AuthError::InvalidToken(
                    "several audiences and no azp".into(),
                ));
            }
        }
    } else if let Some(azp) = &claims.authorized_party
        && azp != &expected.client_id
    {
        return Err(AuthError::InvalidToken("azp is another client".into()));
    }
    match claims.expires_at {
        Some(exp) if exp + leeway > now => {}
        Some(_) => return Err(AuthError::InvalidToken("expired".into())),
        None => return Err(AuthError::InvalidToken("no expiry".into())),
    }
    match claims.issued_at {
        Some(iat) if iat <= now + leeway => {}
        Some(_) => return Err(AuthError::InvalidToken("issued in the future".into())),
        None => return Err(AuthError::InvalidToken("no issue time".into())),
    }
    if let Some(nbf) = claims.not_before
        && nbf > now + leeway
    {
        return Err(AuthError::InvalidToken("not yet valid".into()));
    }
    match (&expected.nonce, &claims.nonce) {
        (None, _) => {}
        (Some(want), Some(got)) if crypto::constant_time_eq(want.as_bytes(), got.as_bytes()) => {}
        (Some(_), Some(_)) => return Err(AuthError::InvalidToken("nonce does not match".into())),
        (Some(_), None) => return Err(AuthError::InvalidToken("no nonce".into())),
    }
    if let Some(access_token) = &expected.access_token
        && let Some(at_hash) = &claims.access_token_hash
    {
        let want = access_token_hash(access_token, alg);
        if !crypto::constant_time_eq(want.as_bytes(), at_hash.as_bytes()) {
            return Err(AuthError::InvalidToken(
                "at_hash does not match the access token".into(),
            ));
        }
    }
    Ok(())
}

/// The `at_hash` of an access token: the left half of its SHA-256, base64url (both RS256 and
/// ES256 hash with SHA-256).
pub fn access_token_hash(access_token: &str, _alg: Algorithm) -> String {
    let digest = crypto::sha256(access_token.as_bytes());
    crypto::base64url(&digest[..16])
}

/// Build a compact JWS signed with ES256: the demo provider's token mint.
///
/// # Errors
///
/// Returns [`AuthError::Platform`] when the random source fails or the key cannot sign.
pub fn sign_es256(
    header: &Value,
    payload: &Value,
    private_key: &BigUint,
) -> Result<String, AuthError> {
    let h = crypto::base64url(
        serde_json::to_string(header)
            .map_err(|e| AuthError::Platform(e.to_string()))?
            .as_bytes(),
    );
    let p = crypto::base64url(
        serde_json::to_string(payload)
            .map_err(|e| AuthError::Platform(e.to_string()))?
            .as_bytes(),
    );
    let input = format!("{h}.{p}");
    let curve = P256::new();
    let (r, s) = curve
        .ecdsa_sign(private_key, &crypto::sha256(input.as_bytes()))
        .ok_or_else(|| AuthError::Platform("ECDSA signing failed".into()))?;
    let mut sig = r.to_be_bytes(32);
    sig.extend(s.to_be_bytes(32));
    Ok(format!("{input}.{}", crypto::base64url(&sig)))
}

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

    fn demo_key() -> (BigUint, Jwks) {
        let curve = P256::new();
        let d = BigUint::from_be_bytes(
            &crypto::hex_decode("c9afa9d845ba75166b5c215767b1d6934e50c3db36e89b127b8a622b120f6721")
                .expect("hex"),
        );
        let (x, y) = curve.public_key(&d).expect("public key");
        let jwks = Jwks {
            keys: vec![Jwk {
                kty: "EC".into(),
                kid: Some("demo-1".into()),
                alg: Some("ES256".into()),
                use_: Some("sig".into()),
                n: None,
                e: None,
                crv: Some("P-256".into()),
                x: Some(crypto::base64url(&x.to_be_bytes(32))),
                y: Some(crypto::base64url(&y.to_be_bytes(32))),
            }],
        };
        (d, jwks)
    }

    #[test]
    fn es256_token_round_trips_through_sign_decode_verify() {
        let (d, jwks) = demo_key();
        let header = json!({"alg": "ES256", "typ": "JWT", "kid": "demo-1"});
        let payload = json!({"iss": "https://demo.example", "sub": "alice", "aud": "app", "exp": 4_000_000_000u64, "iat": 1_700_000_000u64, "nonce": "n1"});
        let token = sign_es256(&header, &payload, &d).expect("sign");
        let jws = Jws::decode(&token).expect("decode");
        assert_eq!(jws.algorithm(), Ok(Algorithm::Es256));
        assert_eq!(jws.key_id(), Some("demo-1"));
        jws.verify(&jwks).expect("verifies");
        // A tampered payload fails.
        let mut parts: Vec<&str> = token.split('.').collect();
        let forged = crypto::base64url(br#"{"iss":"https://demo.example","sub":"mallory","aud":"app","exp":4000000000,"iat":1700000000}"#);
        parts[1] = &forged;
        let forged_token = parts.join(".");
        assert!(
            Jws::decode(&forged_token)
                .expect("decode")
                .verify(&jwks)
                .is_err()
        );
        // Claims validate against what was requested.
        let claims = Claims::from_payload(&jws.payload);
        let mut expected = Expected::new(Some("https://demo.example".into()), "app");
        expected.nonce = Some("n1".into());
        expected.now = Some(1_700_000_010);
        validate(&claims, Algorithm::Es256, &expected).expect("valid");
        expected.nonce = Some("other".into());
        assert!(validate(&claims, Algorithm::Es256, &expected).is_err());
    }

    #[test]
    fn key_selection_follows_kid_alg_and_use() {
        let (d, mut jwks) = demo_key();
        let header = json!({"alg": "ES256", "kid": "missing"});
        let token = sign_es256(&header, &json!({"sub": "a"}), &d).expect("sign");
        assert!(Jws::decode(&token).expect("decode").verify(&jwks).is_err());
        // Without a kid every EC key is tried.
        let token = sign_es256(&json!({"alg": "ES256"}), &json!({"sub": "a"}), &d).expect("sign");
        Jws::decode(&token)
            .expect("decode")
            .verify(&jwks)
            .expect("any key");
        // An encryption key is never a signing key.
        jwks.keys[0].use_ = Some("enc".into());
        assert!(Jws::decode(&token).expect("decode").verify(&jwks).is_err());
    }

    #[test]
    fn none_and_unknown_algorithms_are_refused() {
        let h = crypto::base64url(br#"{"alg":"none"}"#);
        let p = crypto::base64url(br#"{"sub":"a"}"#);
        let jws = Jws::decode(&format!("{h}.{p}.")).expect("decode");
        assert!(jws.algorithm().is_err());
        assert!(jws.verify(&Jwks::default()).is_err());
        let h = crypto::base64url(br#"{"alg":"HS256"}"#);
        let jws = Jws::decode(&format!("{h}.{p}.AAAA")).expect("decode");
        assert!(jws.verify(&Jwks::default()).is_err());
        assert!(Jws::decode("a.b").is_err());
        assert!(Jws::decode("a.b.c.d").is_err());
    }

    #[test]
    fn claim_checks_cover_the_core_rules() {
        let payload = json!({"iss": "i", "sub": "s", "aud": ["app", "other"], "azp": "app", "exp": 2000, "iat": 1000, "nbf": 900, "email_verified": "true"});
        let claims = Claims::from_payload(&payload);
        assert_eq!(claims.email_verified, Some(true));
        let mut expected = Expected::new(Some("i".into()), "app");
        expected.now = Some(1500);
        expected.leeway_secs = 0;
        validate(&claims, Algorithm::Rs256, &expected).expect("valid");
        expected.now = Some(2001);
        assert!(validate(&claims, Algorithm::Rs256, &expected).is_err());
        expected.now = Some(999);
        assert!(validate(&claims, Algorithm::Rs256, &expected).is_err());
        expected.now = Some(1500);
        expected.client_id = "other".into();
        assert!(
            validate(&claims, Algorithm::Rs256, &expected).is_err(),
            "azp names another client"
        );
        expected.client_id = "app".into();
        expected.issuer = Some("j".into());
        assert!(validate(&claims, Algorithm::Rs256, &expected).is_err());
        expected.issuer = None;
        expected.access_token = Some("tok".into());
        let with_hash = Claims::from_payload(
            &json!({"iss": "i", "sub": "s", "aud": "app", "exp": 2000, "iat": 1000, "at_hash": access_token_hash("tok", Algorithm::Rs256)}),
        );
        validate(&with_hash, Algorithm::Rs256, &expected).expect("at_hash matches");
        expected.access_token = Some("other".into());
        assert!(validate(&with_hash, Algorithm::Rs256, &expected).is_err());
    }

    /// OpenID Connect Core §3.3.2.11's example: the `at_hash` of "jHkWEdUXMU1BwAsC4vtUsZwnNvTIxEl0z9K3vx5KF0Y".
    #[test]
    fn at_hash_matches_the_specification_example() {
        assert_eq!(
            access_token_hash(
                "jHkWEdUXMU1BwAsC4vtUsZwnNvTIxEl0z9K3vx5KF0Y",
                Algorithm::Rs256
            ),
            "77QmUPtjPfzWtF2AnpK9RQ"
        );
    }

    #[test]
    fn jwks_parse_tolerates_unknown_fields() {
        let jwks = Jwks::parse(r#"{"keys":[{"kty":"RSA","kid":"a","n":"AQAB","e":"AQAB","x5c":["..."]},{"kty":"OKP","crv":"Ed25519","x":"AA"}]}"#).expect("parse");
        assert_eq!(jwks.keys.len(), 2);
        assert!(Jwks::parse("{").is_err());
    }
}