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

add Ipld de/encoding for CACAOs #7

Merged
merged 10 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ thiserror = "1.0"
url = "2.2"
async-trait = "0.1"
serde = "1.0"
libipld = "0.12"
serde_with = "1.11"
http = "0.2.5"
hex = { version = "0.4", optional = true }
Expand Down
12 changes: 8 additions & 4 deletions src/generic.rs
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use super::{Payload, SignatureScheme, VerificationError};
use async_trait::async_trait;
use libipld::{
cbor::DagCborCodec,
codec::{Decode, Encode},
};
use std::marker::PhantomData;

#[derive(Default)]
Expand Down Expand Up @@ -28,11 +32,11 @@ where
return Err(VerificationError::NotCurrentlyValid);
};
S::verify(
&R::serialize(&payload)
&R::serialize(payload)
.map_err(|_| VerificationError::Serialization)?
.into(),
&S::get_vmat(&payload).ok_or(VerificationError::MissingVerificationMaterial)?,
&sig,
&S::get_vmat(payload).ok_or(VerificationError::MissingVerificationMaterial)?,
sig,
)
.await
.map_err(|_| VerificationError::Crypto)?;
Expand All @@ -55,7 +59,7 @@ pub trait Parse: Representation {
#[async_trait]
pub trait SignatureType {
const ID: &'static str;
type Signature;
type Signature: Encode<DagCborCodec> + Decode<DagCborCodec>;
type Payload;
type VerificationMaterial;
type Output;
Expand Down
199 changes: 186 additions & 13 deletions src/lib.rs
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use http::uri::Authority;
use iri_string::types::{UriAbsoluteString, UriString};
use http::uri::{Authority, InvalidUri};
use iri_string::{
types::{UriAbsoluteString, UriString},
validate::Error as URIStringError,
};
use libipld::{
cbor::{DagCbor, DagCborCodec},
codec::{Decode, Encode},
DagCbor,
};
pub use siwe::TimeStamp;
use thiserror::Error;

pub mod generic;

pub mod siwe_cacao;

#[derive(DagCbor)]
pub struct CACAO<S>
where
S: SignatureScheme,
S::Signature: DagCbor,
{
h: Header,
p: Payload,
Expand All @@ -21,6 +30,7 @@ where
impl<S> CACAO<S>
where
S: SignatureScheme,
S::Signature: DagCbor,
{
pub fn new(p: Payload, s: S::Signature) -> Self {
Self {
Expand All @@ -47,10 +57,11 @@ where
S: Send + Sync,
S::Signature: Send + Sync,
{
S::verify_cacao(&self).await
S::verify_cacao(self).await
}
}

#[derive(DagCbor)]
pub struct Header {
t: String,
}
Expand All @@ -75,7 +86,7 @@ pub trait SignatureScheme {
async fn verify_cacao(cacao: &CACAO<Self>) -> Result<(), VerificationError>
where
Self: Sized,
Self::Signature: Send + Sync,
Self::Signature: Send + Sync + DagCbor,
{
Self::verify(cacao.payload(), cacao.signature()).await
}
Expand All @@ -93,17 +104,27 @@ pub enum VerificationError {
NotCurrentlyValid,
}

pub struct BasicSignature<S: AsRef<[u8]> + TryFrom<Vec<u8>>> {
#[derive(DagCbor)]
pub struct BasicSignature<S>
where
S: DagCbor + AsRef<[u8]> + TryFrom<Vec<u8>>,
{
pub s: S,
}

impl<S: AsRef<[u8]> + TryFrom<Vec<u8>>> AsRef<[u8]> for BasicSignature<S> {
impl<S> AsRef<[u8]> for BasicSignature<S>
where
S: DagCbor + AsRef<[u8]> + TryFrom<Vec<u8>>,
{
fn as_ref(&self) -> &[u8] {
self.s.as_ref()
}
}

impl<S: AsRef<[u8]> + TryFrom<Vec<u8>>> TryFrom<Vec<u8>> for BasicSignature<S> {
impl<S> TryFrom<Vec<u8>> for BasicSignature<S>
where
S: DagCbor + AsRef<[u8]> + TryFrom<Vec<u8>>,
{
type Error = <S as TryFrom<Vec<u8>>>::Error;
fn try_from(s: Vec<u8>) -> Result<Self, Self::Error> {
Ok(Self { s: s.try_into()? })
Expand All @@ -129,9 +150,11 @@ pub struct Payload {
pub request_id: Option<String>,
pub resources: Vec<UriString>,
}

impl Payload {
pub fn sign<S: SignatureScheme>(self, s: S::Signature) -> CACAO<S> {
pub fn sign<S: SignatureScheme>(self, s: S::Signature) -> CACAO<S>
where
S::Signature: DagCbor,
{
CACAO {
h: S::header(),
p: self,
Expand All @@ -147,11 +170,11 @@ impl Payload {
S: Send + Sync,
S::Signature: Send + Sync,
{
S::verify(&self, s).await
S::verify(self, s).await
}

pub fn iss<'a>(&'a self) -> &'a str {
&self.iss.as_str()
pub fn iss(&self) -> &str {
self.iss.as_str()
}

pub fn valid_at(&self, t: &DateTime<Utc>) -> bool {
Expand All @@ -163,3 +186,153 @@ impl Payload {
self.valid_at(&Utc::now())
}
}

mod payload_ipld {
use super::*;
use libipld::error::Error as IpldError;
use std::io::{Read, Seek, Write};

#[derive(Clone, DagCbor)]
struct TmpPayload {
domain: String,
iss: String,
statement: Option<String>,
aud: String,
version: String,
nonce: String,
iat: String,
exp: Option<String>,
nbf: Option<String>,
#[ipld(rename = "requestId")]
request_id: Option<String>,
resources: Vec<String>,
}

impl From<&Payload> for TmpPayload {
fn from(p: &Payload) -> Self {
Self {
domain: p.domain.to_string(),
iss: p.iss.to_string(),
statement: p.statement.as_ref().map(|e| e.to_string()),
aud: p.aud.to_string(),
version: (p.version as u64).to_string(),
nonce: p.nonce.to_string(),
iat: p.iat.to_string(),
exp: p.exp.as_ref().map(|e| e.to_string()),
nbf: p.nbf.as_ref().map(|e| e.to_string()),
request_id: p.request_id.clone(),
resources: p.resources.iter().map(|r| r.to_string()).collect(),
}
}
}

#[derive(Error, Debug)]
pub enum PayloadIpldParseError {
#[error(transparent)]
Domain(#[from] InvalidUri),
#[error(transparent)]
Uri(#[from] URIStringError),
#[error(transparent)]
TimeStamp(#[from] chrono::format::ParseError),
}

impl TryFrom<TmpPayload> for Payload {
type Error = PayloadIpldParseError;
fn try_from(p: TmpPayload) -> Result<Self, Self::Error> {
Ok(Self {
domain: p.domain.parse()?,
iss: p.iss.parse()?,
statement: p.statement,
aud: p.aud.parse()?,
version: Version::V1,
nonce: p.nonce,
iat: p.iat.parse()?,
exp: p.exp.map(|s| s.parse()).transpose()?,
nbf: p.nbf.map(|s| s.parse()).transpose()?,
request_id: p.request_id,
resources: p
.resources
.iter()
.map(|r| r.parse())
.collect::<Result<Vec<UriString>, URIStringError>>()?,
})
}
}

impl Encode<DagCborCodec> for Payload {
fn encode<W>(&self, c: DagCborCodec, w: &mut W) -> Result<(), IpldError>
where
W: Write,
{
TmpPayload::from(self).encode(c, w)
}
}

impl Decode<DagCborCodec> for Payload {
fn decode<R>(c: DagCborCodec, r: &mut R) -> Result<Self, IpldError>
where
R: Read + Seek,
{
TmpPayload::decode(c, r).and_then(|t| Ok(t.try_into()?))
}
}
}

#[cfg(test)]
pub mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn test_ipld() {
let cacao = Payload::decode(
DagCborCodec,
&mut Cursor::new([
163u8, 97u8, 104u8, 161u8, 97u8, 116u8, 103u8, 101u8, 105u8, 112u8, 52u8, 51u8,
54u8, 49u8, 97u8, 112u8, 168u8, 99u8, 97u8, 117u8, 100u8, 120u8, 56u8, 100u8,
105u8, 100u8, 58u8, 107u8, 101u8, 121u8, 58u8, 122u8, 54u8, 77u8, 107u8, 114u8,
66u8, 100u8, 78u8, 100u8, 119u8, 85u8, 80u8, 110u8, 88u8, 68u8, 86u8, 68u8, 49u8,
68u8, 67u8, 120u8, 101u8, 100u8, 122u8, 86u8, 86u8, 66u8, 112u8, 97u8, 71u8, 105u8,
56u8, 97u8, 83u8, 109u8, 111u8, 88u8, 70u8, 65u8, 101u8, 75u8, 78u8, 103u8, 116u8,
65u8, 101u8, 114u8, 56u8, 99u8, 105u8, 97u8, 116u8, 120u8, 24u8, 50u8, 48u8, 50u8,
49u8, 45u8, 48u8, 57u8, 45u8, 51u8, 48u8, 84u8, 49u8, 54u8, 58u8, 50u8, 53u8, 58u8,
50u8, 52u8, 46u8, 48u8, 48u8, 48u8, 90u8, 99u8, 105u8, 115u8, 115u8, 120u8, 59u8,
100u8, 105u8, 100u8, 58u8, 112u8, 107u8, 104u8, 58u8, 101u8, 105u8, 112u8, 49u8,
53u8, 53u8, 58u8, 49u8, 58u8, 48u8, 120u8, 66u8, 100u8, 57u8, 68u8, 57u8, 99u8,
55u8, 68u8, 67u8, 51u8, 56u8, 57u8, 55u8, 49u8, 53u8, 97u8, 56u8, 57u8, 102u8,
67u8, 56u8, 49u8, 52u8, 57u8, 69u8, 52u8, 97u8, 53u8, 66u8, 101u8, 57u8, 49u8,
51u8, 51u8, 54u8, 66u8, 50u8, 55u8, 57u8, 54u8, 101u8, 110u8, 111u8, 110u8, 99u8,
101u8, 104u8, 51u8, 50u8, 56u8, 57u8, 49u8, 55u8, 53u8, 55u8, 102u8, 100u8, 111u8,
109u8, 97u8, 105u8, 110u8, 107u8, 115u8, 101u8, 114u8, 118u8, 105u8, 99u8, 101u8,
46u8, 111u8, 114u8, 103u8, 103u8, 118u8, 101u8, 114u8, 115u8, 105u8, 111u8, 110u8,
97u8, 49u8, 105u8, 114u8, 101u8, 115u8, 111u8, 117u8, 114u8, 99u8, 101u8, 115u8,
130u8, 120u8, 53u8, 105u8, 112u8, 102u8, 115u8, 58u8, 47u8, 47u8, 81u8, 109u8,
101u8, 55u8, 115u8, 115u8, 51u8, 65u8, 82u8, 86u8, 103u8, 120u8, 118u8, 54u8,
114u8, 88u8, 113u8, 86u8, 80u8, 105u8, 105u8, 107u8, 77u8, 74u8, 56u8, 117u8, 50u8,
78u8, 76u8, 103u8, 109u8, 103u8, 115u8, 122u8, 103u8, 49u8, 51u8, 112u8, 89u8,
114u8, 68u8, 75u8, 69u8, 111u8, 105u8, 117u8, 120u8, 38u8, 104u8, 116u8, 116u8,
112u8, 115u8, 58u8, 47u8, 47u8, 101u8, 120u8, 97u8, 109u8, 112u8, 108u8, 101u8,
46u8, 99u8, 111u8, 109u8, 47u8, 109u8, 121u8, 45u8, 119u8, 101u8, 98u8, 50u8, 45u8,
99u8, 108u8, 97u8, 105u8, 109u8, 46u8, 106u8, 115u8, 111u8, 110u8, 105u8, 115u8,
116u8, 97u8, 116u8, 101u8, 109u8, 101u8, 110u8, 116u8, 120u8, 65u8, 73u8, 32u8,
97u8, 99u8, 99u8, 101u8, 112u8, 116u8, 32u8, 116u8, 104u8, 101u8, 32u8, 83u8,
101u8, 114u8, 118u8, 105u8, 99u8, 101u8, 79u8, 114u8, 103u8, 32u8, 84u8, 101u8,
114u8, 109u8, 115u8, 32u8, 111u8, 102u8, 32u8, 83u8, 101u8, 114u8, 118u8, 105u8,
99u8, 101u8, 58u8, 32u8, 104u8, 116u8, 116u8, 112u8, 115u8, 58u8, 47u8, 47u8,
115u8, 101u8, 114u8, 118u8, 105u8, 99u8, 101u8, 46u8, 111u8, 114u8, 103u8, 47u8,
116u8, 111u8, 115u8, 97u8, 115u8, 162u8, 97u8, 115u8, 120u8, 132u8, 48u8, 120u8,
49u8, 48u8, 57u8, 51u8, 49u8, 51u8, 101u8, 55u8, 53u8, 50u8, 53u8, 100u8, 101u8,
97u8, 53u8, 53u8, 101u8, 99u8, 57u8, 97u8, 51u8, 99u8, 99u8, 98u8, 98u8, 54u8,
51u8, 101u8, 97u8, 56u8, 100u8, 54u8, 56u8, 52u8, 48u8, 54u8, 51u8, 54u8, 54u8,
50u8, 53u8, 48u8, 99u8, 102u8, 48u8, 56u8, 56u8, 48u8, 100u8, 54u8, 55u8, 48u8,
51u8, 50u8, 98u8, 52u8, 53u8, 55u8, 97u8, 98u8, 51u8, 51u8, 99u8, 57u8, 50u8, 54u8,
99u8, 54u8, 55u8, 102u8, 102u8, 51u8, 102u8, 99u8, 99u8, 54u8, 54u8, 97u8, 99u8,
51u8, 49u8, 98u8, 97u8, 97u8, 54u8, 56u8, 54u8, 56u8, 97u8, 56u8, 48u8, 97u8, 49u8,
50u8, 102u8, 98u8, 101u8, 54u8, 98u8, 55u8, 54u8, 51u8, 56u8, 97u8, 56u8, 57u8,
102u8, 52u8, 102u8, 54u8, 100u8, 53u8, 49u8, 97u8, 48u8, 50u8, 50u8, 57u8, 53u8,
57u8, 48u8, 99u8, 102u8, 54u8, 54u8, 55u8, 54u8, 102u8, 49u8, 99u8, 97u8, 116u8,
102u8, 101u8, 105u8, 112u8, 49u8, 57u8, 49u8,
]),
)
.unwrap();
}
}
Loading