identity-kit 0.2.1

DID, key management and verifiable credential primitives
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
//! Validation of credential and presentation JWTs: signature, binding to the issuer or holder,
//! validity period, audience, nonce and status.
//!
//! The validators take two extension points: a [`KeyResolver`] (from
//! [`resolver`](crate::resolver), re-exported here), which turns the [`KeyReference`] of a JWT
//! into the public key of its signer, and a [`JwsVerifier`](crate::jose::JwsVerifier), which
//! checks the signature and is [`DefaultVerifier`](crate::jose::DefaultVerifier) unless an
//! application substitutes its own. The crate's [`Resolver`](crate::resolver::Resolver)
//! resolves DID URLs; a [`CompositeKeyResolver`](crate::resolver::CompositeKeyResolver) adds
//! keys by value ([`JwkResolver`](crate::resolver::JwkResolver)) or the key sets of HTTPS
//! issuers (`JwksResolver`, feature `jwks`). A [`StatusCheck`] is a third, optional extension
//! point, for `credentialStatus` and the `status` claim, shared with the SD-JWT validators.
//!
//! # What is checked
//!
//! For a credential ([`JwtCredentialValidator`]):
//!
//! 1. The token is a compact JWS whose header names its key
//!    ([`KeyReference::from_jws`], with the issuer as `iss`): a `kid` that is a DID URL of the
//!    form `did:…#fragment`, a `#fragment` relative to a DID issuer, a `kid` into the key set of
//!    an HTTPS issuer, or a `jwk`, `x5c` or `jku` header. A path or DID parameters such as
//!    `versionId` in a DID URL are refused, so the signer cannot pick the document version its
//!    key is looked up in (ADR 0004). When `typ` is present it names the profile: `JWT` for
//!    VCDM 1.1, `vc+jwt` for VCDM 2.0, or one of the values the options add
//!    (`CredentialValidationOptions::additional_typ`).
//! 2. The payload is a claims set that maps to a [`Credential`](crate::vc::Credential) (or a
//!    [`CredentialV2`](crate::vc::CredentialV2)), with its registered claims agreeing with the
//!    credential. A payload with `_sd` or `_sd_alg` as a member name anywhere, or an array
//!    element `{"...": "<digest>"}`, is the issuer-signed JWT of an SD-JWT and is refused
//!    ([`ValidationError::SdJwtPayload`]), typed or not: read as a plain JWT, its digests would
//!    be properties and its concealed claims would look absent. The `typ` check alone does not
//!    catch it, since an untyped token is accepted; `SdJwtCredentialValidator` of `sd_jwt_vc`
//!    validates it.
//! 3. The key the reference names is resolved for [`KeyRole::Issuer`] (`assertionMethod` in a
//!    DID document) and verifies the signature.
//! 4. The key belongs to the issuer ([`KeyReference::binds_to`]): the issuer is the DID of the
//!    DID URL, the `did:jwk` or thumbprint of a key by value, or the HTTPS issuer of the key
//!    set.
//! 5. `nbf`, `exp` and `iat`, and the credential's own validity period, hold at the validation
//!    time, within the leeway.
//! 6. `credentialStatus` passes the [`StatusCheck`], per the [`StatusPolicy`], and so does a
//!    `status` claim, which the Token Status List defines for any JWT (§6.1), as
//!    `CredentialStatus::SdJwt` after the `credentialStatus` entries. With the `sd-jwt`
//!    feature a `status` claim that does not parse, or names no status mechanism, is
//!    refused; without it the claim cannot be checked, so it fails under
//!    [`StatusPolicy::Strict`] and is ignored otherwise.
//!
//! For a presentation ([`JwtPresentationValidator`]): the same for the presentation JWT, with
//! the key resolved for [`KeyRole::Subject`] (`authentication`) and bound to the holder, plus the expected
//! audience and nonce; then every credential it carries is validated as above, and its subject
//! is checked against the holder per the [`SubjectHolderRelationship`]. A VCDM 1.1
//! presentation (`typ` `JWT`, [`validate`](JwtPresentationValidator::validate)) carries
//! credential JWTs in its `vp` claim; a VCDM 2.0 one (`typ` `vp+jwt`,
//! [`validate_v2`](JwtPresentationValidator::validate_v2)) is the payload itself and envelopes
//! `vc+jwt` credentials and, with the `sd-jwt` feature, `vc+sd-jwt` ones (validated as
//! `SdJwtCredentialValidator::validate_v2` of `sd_jwt_vc` does, with the key binding the
//! presentation options require); an envelope of another media type fails for that
//! credential alone.
//!
//! Everything a verifier decides by policy (which issuers to trust, which credential types to
//! accept, what the claims must say) is left to the application, which gets the decoded
//! credential back. [`decode_credential`], [`decode_credential_v2`], [`decode_presentation`]
//! and [`decode_presentation_v2`] decode without verifying, for a look at a token before
//! deciding how to validate it, and refuse an SD-JWT payload as the validators do; [`extract_issuer`] and [`extract_holder`] read just the signer's DID.

use core::time::Duration;
use std::collections::BTreeSet;
use std::sync::Arc;

use chrono::{DateTime, TimeDelta, Utc};

use crate::did::Did;
pub use crate::resolver::{
    KeyReference, KeyReferenceError, KeyResolutionError, KeyResolver, KeyRole,
};
use crate::vc::{Jwt, JwtClaims, TypedObject, claims};

mod credential;
mod error;
mod presentation;

pub use credential::{
    DecodedCredential, DecodedCredentialV2, JwtCredentialValidator, decode_credential,
    decode_credential_v2,
};
pub use error::{PresentationValidationError, StatusError, ValidationError};
#[cfg(feature = "linked-vp")]
pub(crate) use presentation::decode_presentation_with;
pub use presentation::{
    DecodedPresentation, DecodedPresentationV2, JwtPresentationValidator, PresentedCredentialV2,
    decode_presentation, decode_presentation_v2,
};

/// A status entry of a credential, of either credential format, for a [`StatusCheck`].
///
/// # Examples
///
/// ```
/// use identity_kit::vc::TypedObject;
/// use identity_kit::vc::validation::CredentialStatus;
///
/// let entry: TypedObject = serde_json::from_value(serde_json::json!({
///     "id": "https://example.com/credentials/status/3#94567",
///     "type": "BitstringStatusListEntry",
/// }))?;
/// assert_eq!(CredentialStatus::Vcdm(&entry).mechanism(), "BitstringStatusListEntry");
/// # Ok::<(), serde_json::Error>(())
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum CredentialStatus<'a> {
    /// A `credentialStatus` entry of a VCDM credential.
    Vcdm(&'a TypedObject),
    /// The `status` claim of an SD-JWT VC, of a VCDM 2.0 credential secured with SD-JWT, or of
    /// a JWT credential of either profile (IETF Token Status List §6.1 and other mechanisms).
    #[cfg(feature = "sd-jwt")]
    #[cfg_attr(docsrs, doc(cfg(feature = "sd-jwt")))]
    SdJwt(&'a crate::sd_jwt_vc::Status),
}

impl CredentialStatus<'_> {
    /// What a check reports as [`StatusError::Unsupported`] for this entry: the `type` of a
    /// `credentialStatus` entry, or the names of the mechanisms in a `status` claim
    /// (`status_list`, and the members of other mechanisms), comma separated.
    pub fn mechanism(&self) -> String {
        match self {
            Self::Vcdm(entry) => entry.object_type().to_owned(),
            #[cfg(feature = "sd-jwt")]
            Self::SdJwt(status) => status
                .mechanisms()
                .map(|mechanism| match mechanism {
                    crate::sd_jwt_vc::StatusMechanism::StatusList(_) => "status_list",
                    crate::sd_jwt_vc::StatusMechanism::Other { name, .. } => name,
                })
                .collect::<Vec<_>>()
                .join(", "),
        }
    }
}

/// Checks the status of a credential: fetches the status list, bitmap or registry an entry
/// points to and reports whether the credential is still good.
///
/// One check serves both credential formats: the validators hand it every `credentialStatus`
/// entry of a VCDM credential as [`CredentialStatus::Vcdm`] and the `status` claim of an
/// SD-JWT credential, or of a JWT credential, as `CredentialStatus::SdJwt` (feature
/// `sd-jwt`), so an application implements status checking once. A check that does not
/// understand an entry reports [`StatusError::Unsupported`], which the [`StatusPolicy`]
/// decides about. `StatusListCheck` of `sd_jwt_vc::validation` (features `sd-jwt` and
/// `did-web`) checks the Token Status List and reports every VCDM entry as unsupported.
///
/// # Examples
///
/// A check that knows only the Token Status List, and only the list it was given:
///
/// ```
/// use chrono::{DateTime, Utc};
/// use identity_kit::vc::validation::{CredentialStatus, StatusCheck, StatusError};
///
/// struct Revoked;
///
/// #[async_trait::async_trait]
/// impl StatusCheck for Revoked {
///     async fn check(&self, status: CredentialStatus<'_>, _: DateTime<Utc>) -> Result<(), StatusError> {
///         match status {
///             #[cfg(feature = "sd-jwt")]
///             CredentialStatus::SdJwt(status) if status.status_list.is_some() => {
///                 Err(StatusError::Invalid("revoked".to_owned()))
///             }
///             other => Err(StatusError::Unsupported(other.mechanism())),
///         }
///     }
/// }
/// ```
#[async_trait::async_trait]
pub trait StatusCheck: Send + Sync {
    /// Checks `status` at the validation time `now`, against which a check judges what it
    /// fetched (the expiry of a status list token, for instance).
    ///
    /// # Errors
    ///
    /// A [`StatusError`]: [`Unsupported`](StatusError::Unsupported) for an entry or mechanism
    /// the check does not know, [`Unavailable`](StatusError::Unavailable) when the status
    /// could not be obtained, [`Invalid`](StatusError::Invalid) when the credential is revoked
    /// or suspended.
    async fn check(
        &self,
        status: CredentialStatus<'_>,
        now: DateTime<Utc>,
    ) -> Result<(), StatusError>;
}

macro_rules! forward_status_check {
    ($($ty:ty),* $(,)?) => {
        $(
            #[async_trait::async_trait]
            impl<T: StatusCheck + ?Sized> StatusCheck for $ty {
                async fn check(
                    &self,
                    status: CredentialStatus<'_>,
                    now: DateTime<Utc>,
                ) -> Result<(), StatusError> {
                    (**self).check(status, now).await
                }
            }
        )*
    };
}

forward_status_check!(&T, Box<T>, Arc<T>);

/// Checks every entry of `statuses` with `check` per `policy`, at `now`.
pub(crate) async fn check_statuses<'a>(
    check: Option<&dyn StatusCheck>,
    statuses: impl IntoIterator<Item = CredentialStatus<'a>>,
    policy: StatusPolicy,
    now: DateTime<Utc>,
) -> Result<(), ValidationError> {
    if policy == StatusPolicy::SkipAll {
        return Ok(());
    }
    for status in statuses {
        let result = match check {
            Some(check) => check.check(status, now).await,
            None => Err(StatusError::Unsupported(status.mechanism())),
        };
        match result {
            Ok(()) => {}
            Err(StatusError::Unsupported(_)) if policy == StatusPolicy::SkipUnsupported => {}
            Err(StatusError::Unsupported(mechanism)) => {
                return Err(ValidationError::StatusUnchecked(mechanism));
            }
            Err(error) => return Err(ValidationError::Status(error)),
        }
    }
    Ok(())
}

/// The `status` claim of a JWT credential, which the Token Status List defines for any JWT
/// (§6.1): a `sd_jwt_vc::Status` with the `sd-jwt` feature, uninhabited without it.
#[cfg(feature = "sd-jwt")]
pub(super) type StatusClaim = crate::sd_jwt_vc::Status;

/// The `status` claim of a JWT credential, which the Token Status List defines for any JWT
/// (§6.1): a `sd_jwt_vc::Status` with the `sd-jwt` feature, uninhabited without it.
#[cfg(not(feature = "sd-jwt"))]
pub(super) enum StatusClaim {}

/// The `status` claim of `claims`, for the [`StatusCheck`] beside the `credentialStatus`
/// entries.
///
/// With the `sd-jwt` feature the claim is parsed, and one that does not parse, or names no
/// status mechanism, is an [`InvalidClaim`](crate::vc::CredentialError::InvalidClaim)
/// whatever the `policy`. Without it the claim cannot be handed to a check, so it fails under
/// [`StatusPolicy::Strict`] as an unchecked entry and is ignored otherwise.
pub(super) fn status_claim(
    claims: &JwtClaims,
    policy: StatusPolicy,
) -> Result<Option<StatusClaim>, ValidationError> {
    let Some(status) = claims.additional.get("status") else {
        return Ok(None);
    };
    #[cfg(feature = "sd-jwt")]
    {
        let _ = policy;
        let invalid = |reason: String| crate::vc::CredentialError::InvalidClaim {
            claim: "status",
            reason,
        };
        let status: StatusClaim =
            serde_json::from_value(status.clone()).map_err(|e| invalid(e.to_string()))?;
        // The claim references at least one status mechanism (Token Status List §6.1).
        if status.is_empty() {
            return Err(invalid("names no status mechanism".to_owned()).into());
        }
        Ok(Some(status))
    }
    #[cfg(not(feature = "sd-jwt"))]
    {
        let _ = status;
        match policy {
            StatusPolicy::Strict => Err(ValidationError::StatusUnchecked("status".to_owned())),
            _ => Ok(None),
        }
    }
}

/// `status` as the entry a [`StatusCheck`] is handed.
pub(super) fn status_claim_entry(status: &StatusClaim) -> CredentialStatus<'_> {
    #[cfg(feature = "sd-jwt")]
    {
        CredentialStatus::SdJwt(status)
    }
    #[cfg(not(feature = "sd-jwt"))]
    {
        match *status {}
    }
}

/// What to do about a status entry: a `credentialStatus` entry, or the `status` claim of an
/// SD-JWT credential or of a JWT credential.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum StatusPolicy {
    /// Every entry must pass the validator's [`StatusCheck`]; without one, or for a type it
    /// does not support, validation fails.
    #[default]
    Strict,
    /// Entries of a type the [`StatusCheck`] does not support, or any entry when there is no
    /// check, are ignored.
    SkipUnsupported,
    /// Every status entry is ignored: `credentialStatus` and the `status` claim.
    SkipAll,
}

/// Whether to stop at the first failing credential of a presentation.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FailFast {
    /// Stop at the first credential that fails.
    #[default]
    FirstError,
    /// Validate every credential and report all failures.
    AllErrors,
}

/// How the subject of a presented credential must relate to the holder presenting it.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SubjectHolderRelationship {
    /// Every subject of every credential must be the holder. A bearer credential fails.
    #[default]
    AlwaysSubject,
    /// As [`AlwaysSubject`](Self::AlwaysSubject), but only for credentials whose
    /// `nonTransferable` property is `true`.
    SubjectOnNonTransferable,
    /// Not checked.
    Any,
}

/// The validation time and the clock skew allowed around it.
///
/// # Examples
///
/// ```
/// use std::time::Duration;
///
/// use chrono::{TimeZone as _, Utc};
/// use identity_kit::vc::validation::TimeOptions;
///
/// let fixed = TimeOptions::at(Utc.with_ymd_and_hms(2019, 11, 6, 0, 0, 0).unwrap())
///     .with_leeway(Duration::from_secs(60));
/// assert_eq!(fixed.now().to_rfc3339(), "2019-11-06T00:00:00+00:00");
/// assert_eq!(fixed.leeway, Duration::from_secs(60));
/// assert!(TimeOptions::default().now() > fixed.now());
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct TimeOptions {
    /// The validation time; the current time when `None`.
    pub now: Option<DateTime<Utc>>,
    /// How far `nbf`, `exp` and `iat` may miss the validation time and still pass; zero by
    /// default.
    pub leeway: Duration,
}

impl TimeOptions {
    /// Options validating at `now`.
    pub fn at(now: DateTime<Utc>) -> Self {
        Self {
            now: Some(now),
            leeway: Duration::ZERO,
        }
    }

    /// These options with `leeway`.
    #[must_use]
    pub fn with_leeway(mut self, leeway: Duration) -> Self {
        self.leeway = leeway;
        self
    }

    /// The validation time.
    pub fn now(&self) -> DateTime<Utc> {
        self.now.unwrap_or_else(Utc::now)
    }

    /// The leeway as a `chrono` delta, saturated.
    fn delta(&self) -> TimeDelta {
        TimeDelta::from_std(self.leeway).unwrap_or(TimeDelta::MAX)
    }

    /// Checks that `now` is within `[not_before, expires)` with the leeway, and that
    /// `issued_at` is not in the future.
    ///
    /// # Errors
    ///
    /// [`ValidationError::NotYetValid`], [`ValidationError::Expired`] or
    /// [`ValidationError::IssuedInFuture`].
    pub fn check(
        &self,
        not_before: Option<DateTime<Utc>>,
        expires: Option<DateTime<Utc>>,
        issued_at: Option<DateTime<Utc>>,
    ) -> Result<(), ValidationError> {
        let now = self.now();
        let delta = self.delta();
        // A leeway past the range `chrono` represents saturates rather than overflowing.
        let latest = now
            .checked_add_signed(delta)
            .unwrap_or(DateTime::<Utc>::MAX_UTC);
        let earliest = now
            .checked_sub_signed(delta)
            .unwrap_or(DateTime::<Utc>::MIN_UTC);
        if let Some(not_before) = not_before
            && not_before > latest
        {
            return Err(ValidationError::NotYetValid { not_before, now });
        }
        if let Some(expired_at) = expires
            && expired_at <= earliest
        {
            return Err(ValidationError::Expired { expired_at, now });
        }
        if let Some(issued_at) = issued_at
            && issued_at > latest
        {
            return Err(ValidationError::IssuedInFuture { issued_at, now });
        }
        Ok(())
    }

    /// Checks the registered time claims of `claims`: `nbf`, `exp` and `iat`.
    ///
    /// # Errors
    ///
    /// As [`check`](Self::check), or [`ValidationError::Credential`] for a claim out of
    /// range.
    pub fn check_claims(&self, claims: &JwtClaims) -> Result<(), ValidationError> {
        self.check(
            timestamp(claims.nbf, "nbf")?,
            timestamp(claims.exp, "exp")?,
            timestamp(claims.iat, "iat")?,
        )
    }
}

/// The time a `NumericDate` claim names.
fn timestamp(
    value: Option<i64>,
    claim: &'static str,
) -> Result<Option<DateTime<Utc>>, ValidationError> {
    value
        .map(|value| {
            JwtClaims::timestamp(value).ok_or_else(|| {
                ValidationError::Credential(crate::vc::CredentialError::InvalidClaim {
                    claim,
                    reason: format!("{value} is out of range"),
                })
            })
        })
        .transpose()
}

/// Options of [`JwtCredentialValidator`].
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct CredentialValidationOptions {
    /// The validation time and leeway.
    pub time: TimeOptions,
    /// What to do about `credentialStatus`.
    pub status: StatusPolicy,
    /// Header `typ` values accepted besides the profile's own (`JWT` for VCDM 1.1, `vc+jwt`
    /// for VCDM 2.0); none by default. A token without `typ` is always accepted.
    pub additional_typ: BTreeSet<String>,
}

impl CredentialValidationOptions {
    /// Default options: validate now without leeway, [`StatusPolicy::Strict`].
    pub fn new() -> Self {
        Self::default()
    }

    /// These options with `time`.
    #[must_use]
    pub fn with_time(mut self, time: TimeOptions) -> Self {
        self.time = time;
        self
    }

    /// These options with `status`.
    #[must_use]
    pub fn with_status(mut self, status: StatusPolicy) -> Self {
        self.status = status;
        self
    }

    /// These options also accepting a credential whose header `typ` is `typ`.
    #[must_use]
    pub fn with_additional_typ(mut self, typ: impl Into<String>) -> Self {
        self.additional_typ.insert(typ.into());
        self
    }
}

/// Options of [`JwtPresentationValidator`].
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PresentationValidationOptions {
    /// The validation time and leeway of the presentation JWT.
    pub time: TimeOptions,
    /// The audience the presentation must name in `aud`, if any is required.
    pub audience: Option<String>,
    /// The `nonce` the presentation must carry, if one is required.
    pub nonce: Option<String>,
    /// How credential subjects must relate to the holder.
    pub subject_holder: SubjectHolderRelationship,
    /// The options every presented credential is validated with.
    pub credentials: CredentialValidationOptions,
    /// Whether to stop at the first failing credential.
    pub fail_fast: FailFast,
    /// Header `typ` values accepted for the presentation JWT besides the profile's own (`JWT`
    /// for VCDM 1.1, `vp+jwt` for VCDM 2.0), by both profiles; none by default. A token
    /// without `typ` is always accepted. The credentials' own are
    /// [`CredentialValidationOptions::additional_typ`].
    pub additional_typ: BTreeSet<String>,
    /// The key binding every enveloped credential secured with SD-JWT must prove, if any
    /// is required; none by default, since the presentation JWT already proves holder
    /// control. See [`JwtPresentationValidator::validate_v2`].
    #[cfg(feature = "sd-jwt")]
    #[cfg_attr(docsrs, doc(cfg(feature = "sd-jwt")))]
    pub key_binding: Option<crate::sd_jwt_vc::KeyBindingOptions>,
}

impl PresentationValidationOptions {
    /// Default options: validate now without leeway, no audience or nonce required,
    /// [`SubjectHolderRelationship::AlwaysSubject`], default credential options,
    /// [`FailFast::FirstError`].
    pub fn new() -> Self {
        Self::default()
    }

    /// These options with `time`, for the presentation JWT only.
    #[must_use]
    pub fn with_time(mut self, time: TimeOptions) -> Self {
        self.time = time;
        self
    }

    /// These options requiring `audience`.
    #[must_use]
    pub fn with_audience(mut self, audience: impl Into<String>) -> Self {
        self.audience = Some(audience.into());
        self
    }

    /// These options requiring `nonce`.
    #[must_use]
    pub fn with_nonce(mut self, nonce: impl Into<String>) -> Self {
        self.nonce = Some(nonce.into());
        self
    }

    /// These options with `relationship`.
    #[must_use]
    pub fn with_subject_holder(mut self, relationship: SubjectHolderRelationship) -> Self {
        self.subject_holder = relationship;
        self
    }

    /// These options validating credentials with `credentials`.
    #[must_use]
    pub fn with_credentials(mut self, credentials: CredentialValidationOptions) -> Self {
        self.credentials = credentials;
        self
    }

    /// These options with `fail_fast`.
    #[must_use]
    pub fn with_fail_fast(mut self, fail_fast: FailFast) -> Self {
        self.fail_fast = fail_fast;
        self
    }

    /// These options also accepting a presentation whose header `typ` is `typ`.
    #[must_use]
    pub fn with_additional_typ(mut self, typ: impl Into<String>) -> Self {
        self.additional_typ.insert(typ.into());
        self
    }

    /// These options requiring `key_binding` of every enveloped credential secured with
    /// SD-JWT.
    #[cfg(feature = "sd-jwt")]
    #[cfg_attr(docsrs, doc(cfg(feature = "sd-jwt")))]
    #[must_use]
    pub fn with_key_binding(mut self, key_binding: crate::sd_jwt_vc::KeyBindingOptions) -> Self {
        self.key_binding = Some(key_binding);
        self
    }
}

/// The DID of the issuer of a credential JWT, read without verifying anything: `iss`, else
/// the `issuer` of the `vc` claim (VCDM 1.1) or of the payload (VCDM 2.0).
///
/// # Errors
///
/// [`ValidationError::Jws`] or [`ValidationError::Payload`] when the token does not decode,
/// [`ValidationError::Credential`] when it names no issuer, and [`ValidationError::NotDid`]
/// when the issuer is not a DID.
pub fn extract_issuer(jwt: &Jwt) -> Result<Did, ValidationError> {
    let claims = decode_claims(jwt)?;
    let issuer = claims
        .iss
        .clone()
        .or_else(|| property_id(&claims, "vc", "issuer"))
        .ok_or(crate::vc::CredentialError::MissingClaim("iss"))?;
    Did::parse(&issuer).map_err(|_| ValidationError::NotDid {
        role: "issuer",
        id: issuer,
    })
}

/// The DID of the holder of a presentation JWT, read without verifying anything: `iss`, else
/// the `holder` of the `vp` claim (VCDM 1.1) or of the payload (VCDM 2.0).
///
/// # Errors
///
/// As [`extract_issuer`], for the holder.
pub fn extract_holder(jwt: &Jwt) -> Result<Did, ValidationError> {
    let claims = decode_claims(jwt)?;
    let holder = claims
        .iss
        .clone()
        .or_else(|| property_id(&claims, "vp", "holder"))
        .ok_or(crate::vc::CredentialError::MissingClaim("iss"))?;
    Did::parse(&holder).map_err(|_| ValidationError::NotDid {
        role: "holder",
        id: holder,
    })
}

/// The identifier of `property` in the `container` claim (VCDM 1.1), or in the payload.
fn property_id(claims: &JwtClaims, container: &str, property: &str) -> Option<String> {
    let value = match claims.additional.get(container) {
        Some(container) => container.get(property)?,
        None => claims.additional.get(property)?,
    };
    claims::id_of(value).map(str::to_owned)
}

/// The claims set of `jwt`, unverified.
pub(super) fn decode_claims(jwt: &Jwt) -> Result<JwtClaims, ValidationError> {
    let jws = jwt.decode()?;
    serde_json::from_slice(jws.payload()).map_err(|e| ValidationError::Payload(e.to_string()))
}