Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/use option string as domain type #636

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/claims/crates/data-integrity/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod suite;
pub use decode::*;
use educe::Educe;
pub use options::ProofOptions;
pub use proof::value_or_array;
pub use proof::*;
use serde::Serialize;
use ssi_claims_core::{
Expand Down
7 changes: 6 additions & 1 deletion crates/claims/crates/data-integrity/core/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ pub struct ProofOptions<M, T> {
/// Example domain values include: `domain.example`` (DNS domain),
/// `https://domain.example:8443` (Web origin), `mycorp-intranet` (bespoke
/// text string), and `b31d37d4-dd59-47d3-9dd8-c973da43b63a` (UUID).
#[serde(default, skip_serializing_if = "Vec::is_empty", rename = "domain")]
#[serde(
default,
with = "crate::value_or_array",
skip_serializing_if = "Vec::is_empty",
rename = "domain"
)]
pub domains: Vec<String>,

/// Used to mitigate replay attacks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ pub struct ProofConfiguration<S: CryptographicSuite> {
/// Example domain values include: `domain.example`` (DNS domain),
/// `https://domain.example:8443` (Web origin), `mycorp-intranet` (bespoke
/// text string), and `b31d37d4-dd59-47d3-9dd8-c973da43b63a` (UUID).
#[serde(skip_serializing_if = "Vec::is_empty", rename = "domain")]
#[serde(
with = "crate::value_or_array",
skip_serializing_if = "Vec::is_empty",
rename = "domain"
)]
pub domains: Vec<String>,

/// Used to mitigate replay attacks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ pub struct ProofConfigurationRef<'a, S: CryptographicSuite> {
#[serde(skip_serializing_if = "Option::is_none")]
pub expires: Option<xsd_types::DateTimeStamp>,

#[serde(skip_serializing_if = "<[String]>::is_empty")]
#[serde(
with = "crate::value_or_array",
skip_serializing_if = "<[String]>::is_empty",
rename = "domain"
)]
pub domains: &'a [String],

#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -151,7 +155,11 @@ pub struct ProofConfigurationRefWithoutOptions<'a, S: CryptographicSuite> {
#[serde(skip_serializing_if = "Option::is_none")]
pub expires: Option<xsd_types::DateTimeStamp>,

#[serde(skip_serializing_if = "<[String]>::is_empty")]
#[serde(
with = "crate::value_or_array",
skip_serializing_if = "<[String]>::is_empty",
rename = "domain"
)]
pub domains: &'a [String],

#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
28 changes: 27 additions & 1 deletion crates/claims/crates/data-integrity/core/src/proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ pub struct Proof<S: CryptographicSuite> {
/// Example domain values include: `domain.example`` (DNS domain),
/// `https://domain.example:8443` (Web origin), `mycorp-intranet` (bespoke
/// text string), and `b31d37d4-dd59-47d3-9dd8-c973da43b63a` (UUID).
#[serde(skip_serializing_if = "Vec::is_empty", rename = "domain")]
#[serde(
with = "crate::value_or_array",
skip_serializing_if = "Vec::is_empty",
rename = "domain"
)]
pub domains: Vec<String>,

/// Used to mitigate replay attacks.
Expand Down Expand Up @@ -441,3 +445,25 @@ impl<'de, S: DeserializeCryptographicSuite<'de>> Deserialize<'de> for Proofs<S>
}
}
}

pub mod value_or_array {
use serde::{Deserialize, Serialize};
use ssi_core::OneOrMany;

pub fn serialize<T: Serialize, S>(value: &[T], serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match value.split_first() {
Some((first, [])) => first.serialize(serializer),
_ => value.serialize(serializer),
}
}

pub fn deserialize<'de, T: Deserialize<'de>, D>(deserializer: D) -> Result<Vec<T>, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(OneOrMany::deserialize(deserializer)?.into_vec())
}
}
Loading