Skip to main content

SdJwt

Struct SdJwt 

Source
pub struct SdJwt { /* private fields */ }
Available on crate feature sd-jwt only.
Expand description

An SD-JWT: an issuer-signed JWT, its disclosures and, in an SD-JWT+KB, a key binding JWT, as the compact serialization <JWT>~<Disclosure 1>~…~<Disclosure N>~[<KB-JWT>].

Parsing checks the structure only. SdJwt::process matches the disclosures to the digests and yields the processed payload; the signature is the validator’s business (SdJwtCredentialValidator for an SD-JWT VC).

§Examples

The presentation of RFC 9901 §5.2, processed.

use identity_kit::sd_jwt_vc::SdJwt;
use serde_json::json;

let presentation = "eyJhbGciOiAiRVMyNTYiLCAidHlwIjogImV4YW1wbGUrc2Qtand0In0.eyJfc2QiOiBbIkNyUWU3UzVr\
    cUJBSHQtbk1ZWGdjNmJkdDJTSDVhVFkxc1VfTS1QZ2tqUEkiLCAiSnpZakg0c3ZsaUgwUjNQeUVNZmVa\
    dTZKdDY5dTVxZWhabzdGN0VQWWxTRSIsICJQb3JGYnBLdVZ1Nnh5bUphZ3ZrRnNGWEFiUm9jMkpHbEFV\
    QTJCQTRvN2NJIiwgIlRHZjRvTGJnd2Q1SlFhSHlLVlFaVTlVZEdFMHc1cnREc3JaemZVYW9tTG8iLCAi\
    WFFfM2tQS3QxWHlYN0tBTmtxVlI2eVoyVmE1TnJQSXZQWWJ5TXZSS0JNTSIsICJYekZyendzY002R242\
    Q0pEYzZ2Vks4QmtNbmZHOHZPU0tmcFBJWmRBZmRFIiwgImdiT3NJNEVkcTJ4Mkt3LXc1d1BFemFrb2I5\
    aFYxY1JEMEFUTjNvUUw5Sk0iLCAianN1OXlWdWx3UVFsaEZsTV8zSmx6TWFTRnpnbGhRRzBEcGZheVF3\
    TFVLNCJdLCAiaXNzIjogImh0dHBzOi8vaXNzdWVyLmV4YW1wbGUuY29tIiwgImlhdCI6IDE2ODMwMDAw\
    MDAsICJleHAiOiAxODgzMDAwMDAwLCAic3ViIjogInVzZXJfNDIiLCAibmF0aW9uYWxpdGllcyI6IFt7\
    Ii4u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xWEUifQ.76YniH11s4EDLnqNXKzYtIU1yXEBNzY8wfTd1QkpKzcJ0JY_KXbURL8qQBJA7xpZiA8\
    vXYrlpPpd4yfHJTfhQA";
let sd_jwt: SdJwt = presentation.parse()?;
assert_eq!(sd_jwt.disclosures().len(), 4);
assert!(sd_jwt.key_binding_jwt().is_some());

let processed = sd_jwt.process()?;
assert_eq!(processed.payload["given_name"], "John");
assert_eq!(processed.payload["nationalities"], json!(["US"]));
assert!(!processed.payload.contains_key("email"));

Implementations§

Source§

impl SdJwt

Source

pub fn new(jwt: Jwt, disclosures: Vec<Disclosure>) -> Self

An SD-JWT of jwt and disclosures, without key binding.

Source

pub fn parse(s: &str) -> Result<Self, SdJwtError>

Parses the compact serialization.

§Errors

SdJwtError::Format without a ~ or with an empty disclosure, SdJwtError::Jws when the JWT or the key binding JWT does not decode, SdJwtError::InvalidDisclosure and SdJwtError::DuplicateDisclosure.

Source

pub fn jwt(&self) -> &Jwt

The issuer-signed JWT.

Source

pub fn disclosures(&self) -> &[Disclosure]

The disclosures, in order.

Source

pub fn key_binding_jwt(&self) -> Option<&Jwt>

The key binding JWT, when this is an SD-JWT+KB.

Source

pub fn with_key_binding(self, key_binding: Jwt) -> Self

This SD-JWT with key_binding as its key binding JWT.

Source

pub fn without_key_binding(self) -> Self

This SD-JWT without its key binding JWT.

Source

pub fn payload(&self) -> Result<Map<String, Value>, SdJwtError>

The issuer-signed JWT payload as issued: digests in place, nothing disclosed, nothing verified.

§Errors

SdJwtError::Payload when the payload is not a JSON object.

Source

pub fn hash_algorithm(&self) -> Result<HashAlgorithm, SdJwtError>

The hash algorithm, from _sd_alg; SHA-256 when absent.

§Errors

SdJwtError::Payload when _sd_alg is not a string, SdJwtError::UnsupportedHashAlgorithm when it names an algorithm this crate does not implement.

Source

pub fn to_string_without_key_binding(&self) -> String

The compact serialization without the key binding JWT: the input of sd_hash.

Source

pub fn sd_hash(&self, alg: HashAlgorithm) -> String

The sd_hash a key binding JWT over this SD-JWT carries (RFC 9901 §4.3.1): the digest under alg of the serialization without the key binding JWT.

Source

pub fn process(&self) -> Result<Processed, SdJwtError>

Processes the disclosures into the payload (RFC 9901 §7.1, steps 3 to 5): every disclosure must match a digest, no digest may appear twice, no disclosure may name a reserved or existing claim.

The claims may nest at most SdJwtBuilder::MAX_DEPTH containers deep, counting the payload and those a disclosure’s value adds alike, so a chain of disclosures, each referencing the next, cannot nest them without bound. Whatever the builder issues stays within the limit.

§Errors

SdJwtError::Payload, SdJwtError::UnsupportedHashAlgorithm, SdJwtError::DuplicateDigest, SdJwtError::UnusedDisclosure, SdJwtError::WrongDisclosureKind, SdJwtError::ReservedClaimName, SdJwtError::DuplicateClaim or SdJwtError::TooDeep.

Source

pub fn select( &self, keep: impl FnMut(&Disclosure) -> bool, ) -> Result<Self, SdJwtError>

This SD-JWT with only the disclosures keep selects and no key binding JWT, for presentation (RFC 9901 §7.2). A kept disclosure nested in another needs that one kept too.

§Errors

As process, plus SdJwtError::MissingParentDisclosure.

Source

pub fn disclose<'a>( &self, paths: impl IntoIterator<Item = &'a ClaimPath>, ) -> Result<Self, SdJwtError>

This SD-JWT with only the disclosures at paths and beneath them, plus the disclosures they are nested in, and no key binding JWT. A path may use null for every element.

§Errors

As process, plus SdJwtError::NoSuchClaim for a path selecting no disclosure.

Source

pub async fn sign_key_binding( self, binding: &KeyBinding, signer: &dyn Signer, ) -> Result<Self, SdJwtError>

This SD-JWT as an SD-JWT+KB: binding signed by the holder with signer over the sd_hash of this SD-JWT (RFC 9901 §4.3), replacing any key binding JWT present.

§Errors

As hash_algorithm, or SdJwtError::Jws when signing fails.

Trait Implementations§

Source§

impl Clone for SdJwt

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SdJwt

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for SdJwt

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for SdJwt

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for SdJwt

Source§

impl FromStr for SdJwt

Source§

type Err = SdJwtError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for SdJwt

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for SdJwt

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for SdJwt

Auto Trait Implementations§

§

impl Freeze for SdJwt

§

impl RefUnwindSafe for SdJwt

§

impl Send for SdJwt

§

impl Sync for SdJwt

§

impl Unpin for SdJwt

§

impl UnsafeUnpin for SdJwt

§

impl UnwindSafe for SdJwt

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more