From a68b398c5cc89e20d42c9d9f2120d2ff27312d81 Mon Sep 17 00:00:00 2001 From: chunningham Date: Wed, 27 Sep 2023 16:20:01 +0200 Subject: [PATCH] add builders --- src/v3/payload.rs | 36 +++++++++++++++++------------------- src/v3/recap_cacao.rs | 26 +++++++++++++++++++++++++- src/v3/ucan_cacao.rs | 30 +++++++++++++++++++++++++++--- src/v3/version.rs | 4 ++-- 4 files changed, 71 insertions(+), 25 deletions(-) diff --git a/src/v3/payload.rs b/src/v3/payload.rs index 0ed41b1..2596ea5 100644 --- a/src/v3/payload.rs +++ b/src/v3/payload.rs @@ -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 { +pub struct Payload { #[serde(rename = "iss")] pub issuer: MultiDid, #[serde(rename = "aud")] @@ -36,6 +35,21 @@ pub struct Payload { } impl Payload { + 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 { &mut self.attenuations } @@ -69,22 +83,6 @@ impl Payload { self.facts = Some(facts); self } - - pub fn sign(self, sig: S) -> Cacao { - 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)] diff --git a/src/v3/recap_cacao.rs b/src/v3/recap_cacao.rs index aa992f2..401a442 100644 --- a/src/v3/recap_cacao.rs +++ b/src/v3/recap_cacao.rs @@ -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; @@ -214,6 +214,30 @@ where } } +impl RecapCacao { + pub fn builder(iss: MultiDid, aud: MultiDid) -> Payload { + Payload::new(iss, aud, SiweVersion::V1) + } +} + +impl Payload { + pub fn sign(self, sig: RecapSignature) -> RecapCacao { + 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}; diff --git a/src/v3/ucan_cacao.rs b/src/v3/ucan_cacao.rs index 1a83da0..4351d33 100644 --- a/src/v3/ucan_cacao.rs +++ b/src/v3/ucan_cacao.rs @@ -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}; @@ -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; @@ -89,7 +89,7 @@ impl From> for Ucan { 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; @@ -100,3 +100,27 @@ impl From> for Ucan { payload.sign(signature) } } + +impl UcanCacao { + pub fn builder(iss: MultiDid, aud: MultiDid) -> Payload { + Payload::new(iss, aud, SemanticVersion) + } +} + +impl Payload, NB> { + pub fn sign(self, sig: UcanSignature) -> UcanCacao { + 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), + } + } +} diff --git a/src/v3/version.rs b/src/v3/version.rs index 38c4547..9662f1f 100644 --- a/src/v3/version.rs +++ b/src/v3/version.rs @@ -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(&self, serializer: S) -> Result { serializer.serialize_str("3") } } -impl<'de> Deserialize<'de> for SiweVersion { +impl<'de> Deserialize<'de> for Version3 { fn deserialize>(deserializer: D) -> Result { let s = String::deserialize(deserializer)?; if s == "3" {