Skip to content

Commit

Permalink
add helper methods
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Tate <[email protected]>
  • Loading branch information
Ryanmtate committed Aug 22, 2024
1 parent 347259d commit 05d550e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
39 changes: 35 additions & 4 deletions src/core/metadata/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::{Deref, DerefMut};

use anyhow::Error;
use anyhow::{bail, Error, Result};
use parameters::wallet::{RequestObjectSigningAlgValuesSupported, ResponseTypesSupported};
use serde::{Deserialize, Serialize};
use ssi_jwk::Algorithm;
Expand Down Expand Up @@ -37,13 +37,32 @@ impl WalletMetadata {
&self.1
}

/// Return a reference to the vp formats supported.
pub fn vp_formats_supported(&self) -> &VpFormatsSupported {
&self.2
}

/// Returns whether the claim format is supported.
pub fn is_claim_format_supported(&self, designation: &ClaimFormatDesignation) -> bool {
self.vp_formats_supported().0.contains_key(designation)
/// Return a mutable reference to the vp formats supported.
pub fn vp_formats_supported_mut(&mut self) -> &mut VpFormatsSupported {
&mut self.2
}

// /// Returns whether the claim format is supported.
// pub fn is_claim_format_supported(&self, designation: &ClaimFormatDesignation) -> bool {
// self.vp_formats_supported()
// .is_claim_format_supported(designation)
// }

/// Adds a new algorithm to the list of supported request object signing algorithms.
pub fn add_request_object_signing_alg(&mut self, alg: String) -> Result<()> {
self.0
.get::<RequestObjectSigningAlgValuesSupported>()
.transpose()?
.map(|x| x.0)
.get_or_insert_with(Vec::new)
.push(alg);

Ok(())
}

/// The static wallet metadata bound to `openid4vp:`:
Expand Down Expand Up @@ -98,6 +117,18 @@ impl WalletMetadata {
// Unwrap safety: unit tested.
object.try_into().unwrap()
}

/// Return the `request_object_signing_alg_values_supported`
/// field from the wallet metadata.
pub fn request_object_signing_alg_values_supported(&self) -> Result<Vec<String>, Error> {
let Some(Ok(algs)) = self.get::<RequestObjectSigningAlgValuesSupported>() else {
bail!(
"Failed to parse request object signing algorithms supported from wallet metadata."
)
};

Ok(algs.0)
}
}

impl From<WalletMetadata> for UntypedObject {
Expand Down
10 changes: 8 additions & 2 deletions src/core/metadata/parameters/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
authorization_request::parameters::{ClientIdScheme, ResponseType},
object::TypedParameter,
},
presentation_exchange::ClaimFormatMap,
presentation_exchange::{ClaimFormatDesignation, ClaimFormatMap},
};
use anyhow::{bail, Error, Result};
use serde_json::Value as Json;
Expand Down Expand Up @@ -135,7 +135,7 @@ impl From<RequestObjectSigningAlgValuesSupported> for Json {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct VpFormatsSupported(pub ClaimFormatMap);

impl TypedParameter for VpFormatsSupported {
Expand All @@ -158,6 +158,12 @@ impl TryFrom<VpFormatsSupported> for Json {
}
}

impl VpFormatsSupported {
pub fn is_claim_format_supported(&self, designation: &ClaimFormatDesignation) -> bool {
self.0.contains_key(designation)
}
}

#[derive(Debug, Clone)]
pub struct AuthorizationEncryptionAlgValuesSupported(pub Vec<String>);

Expand Down
1 change: 1 addition & 0 deletions src/core/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) fn base_request() -> http::request::Builder {
}

#[cfg(feature = "reqwest")]
#[derive(Debug)]
pub struct ReqwestClient(reqwest::Client);

#[cfg(feature = "reqwest")]
Expand Down
26 changes: 26 additions & 0 deletions src/presentation_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,32 @@ pub enum ClaimFormatPayload {
Json(serde_json::Value),
}

impl ClaimFormatPayload {
/// Adds an algorithm value to the list of supported algorithms.
///
/// This method is a no-op if self is not of type `AlgValuesSupported` or `Alg`.
pub fn add_alg(&mut self, alg: String) {
match self {
Self::Alg(algs) | Self::AlgValuesSupported(algs) => {
algs.push(alg);
}
_ => {} // Noop
}
}

/// Adds a proof type to the list of supported proof types.
///
/// This method is a no-op if self is not of type `ProofType`.
pub fn add_proof_type(&mut self, proof_type: String) {
match self {
Self::ProofType(proof_types) => {
proof_types.push(proof_type);
}
_ => {} // Noop
}
}
}

/// The claim format designation type is used in the input description object to specify the format of the claim.
///
/// Registry of claim format type: https://identity.foundation/claim-format-registry/#registry
Expand Down

0 comments on commit 05d550e

Please sign in to comment.