Skip to content

Commit

Permalink
add builders
Browse files Browse the repository at this point in the history
  • Loading branch information
chunningham committed Sep 27, 2023
1 parent fbfd79f commit a68b398
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 25 deletions.
36 changes: 17 additions & 19 deletions src/v3/payload.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use super::Cacao;
use libipld::{cid::Cid, Ipld};
use libipld::cid::Cid;
use multidid::MultiDid;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use ucan_capabilities_object::Capabilities;
use varsig::VarSig;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Eq, Hash)]
#[serde(deny_unknown_fields)]
pub struct Payload<V, F, NB = Ipld> {
pub struct Payload<V, F, NB> {
#[serde(rename = "iss")]
pub issuer: MultiDid,
#[serde(rename = "aud")]
Expand Down Expand Up @@ -36,6 +35,21 @@ pub struct Payload<V, F, NB = Ipld> {
}

impl<V, F, NB> Payload<V, F, NB> {
pub(crate) fn new(issuer: MultiDid, audience: MultiDid, version: V) -> Self {
Self {
issuer,
audience,
version,
attenuations: Capabilities::default(),
nonce: None,
proof: None,
issued_at: None,
not_before: None,
expiration: None,
facts: None,
}
}

pub fn capabilities(&mut self) -> &mut Capabilities<NB> {
&mut self.attenuations
}
Expand Down Expand Up @@ -69,22 +83,6 @@ impl<V, F, NB> Payload<V, F, NB> {
self.facts = Some(facts);
self
}

pub fn sign<S>(self, sig: S) -> Cacao<V, S, F, NB> {
Cacao {
issuer: self.issuer,
audience: self.audience,
version: self.version,
attenuations: self.attenuations,
nonce: self.nonce,
proof: self.proof,
issued_at: self.issued_at,
not_before: self.not_before,
expiration: self.expiration,
facts: self.facts,
signature: VarSig::new(sig),
}
}
}

#[derive(Debug, Clone, PartialEq, Serialize, Eq, Hash)]
Expand Down
26 changes: 25 additions & 1 deletion src/v3/recap_cacao.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Cacao, CacaoVerifier};
use super::{payload::Payload, Cacao, CacaoVerifier};
use async_trait::async_trait;
use http::uri::Authority;
use iri_string::types::UriString;
Expand Down Expand Up @@ -214,6 +214,30 @@ where
}
}

impl<NB> RecapCacao<NB> {
pub fn builder(iss: MultiDid, aud: MultiDid) -> Payload<SiweVersion, RecapFacts, NB> {
Payload::new(iss, aud, SiweVersion::V1)
}
}

impl<NB> Payload<SiweVersion, RecapFacts, NB> {
pub fn sign(self, sig: RecapSignature) -> RecapCacao<NB> {
Cacao {
issuer: self.issuer,
audience: self.audience,
version: SiweVersion::V1,
attenuations: self.attenuations,
nonce: self.nonce,
proof: self.proof,
issued_at: self.issued_at,
not_before: self.not_before,
expiration: self.expiration,
facts: self.facts,
signature: VarSig::new(sig),
}
}
}

pub mod version {
use serde::{de::Deserializer, ser::Serializer, Deserialize, Serialize};

Expand Down
30 changes: 27 additions & 3 deletions src/v3/ucan_cacao.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Cacao, CacaoVerifier};
use super::{payload::Payload, Cacao, CacaoVerifier};
use async_trait::async_trait;
use multidid::MultiDid;
use serde::{Deserialize, Serialize};
Expand All @@ -8,7 +8,7 @@ use ssi_ucan::{
jose::{self, Signature, VerificationError},
jwt,
version::SemanticVersion,
Payload, Ucan,
Payload as UcanPayload, Ucan,
};
use std::collections::BTreeMap;
use std::str::FromStr;
Expand Down Expand Up @@ -89,7 +89,7 @@ impl<F, NB> From<UcanCacao<F, NB>> for Ucan<F, NB, Signature> {
JoseSig::Rsa256(s) => Signature::RS256(s.into_inner()),
JoseSig::Rsa512(s) => Signature::RS512(s.into_inner()),
};
let mut payload = Payload::new(cacao.issuer.to_string(), cacao.audience.to_string());
let mut payload = UcanPayload::new(cacao.issuer.to_string(), cacao.audience.to_string());
payload.capabilities = cacao.attenuations;
payload.nonce = cacao.nonce;
payload.proof = cacao.proof;
Expand All @@ -100,3 +100,27 @@ impl<F, NB> From<UcanCacao<F, NB>> for Ucan<F, NB, Signature> {
payload.sign(signature)
}
}

impl<F, NB> UcanCacao<F, NB> {
pub fn builder(iss: MultiDid, aud: MultiDid) -> Payload<SemanticVersion, F, NB> {
Payload::new(iss, aud, SemanticVersion)
}
}

impl<F, NB> Payload<SemanticVersion, UcanFacts<F>, NB> {
pub fn sign(self, sig: UcanSignature) -> UcanCacao<F, NB> {
Cacao {
issuer: self.issuer,
audience: self.audience,
version: SemanticVersion,
attenuations: self.attenuations,
nonce: self.nonce,
proof: self.proof,
issued_at: self.issued_at,
not_before: self.not_before,
expiration: self.expiration,
facts: self.facts,
signature: VarSig::new(sig),
}
}
}
4 changes: 2 additions & 2 deletions src/v3/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use serde::{de::Deserializer, ser::Serializer, Deserialize, Serialize};
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Default)]
pub struct Version3;

impl Serialize for SiweVersion {
impl Serialize for Version3 {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str("3")
}
}

impl<'de> Deserialize<'de> for SiweVersion {
impl<'de> Deserialize<'de> for Version3 {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
if s == "3" {
Expand Down

0 comments on commit a68b398

Please sign in to comment.