1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use anyhow::Result;
use cid::Cid;
use libipld_cbor::DagCborCodec;
use std::hash::Hash;
use ucan::{crypto::KeyMaterial, store::UcanJwtStore, Ucan};
use noosphere_storage::{
encoding::{base64_decode, base64_encode},
interface::BlockStore,
ucan::UcanStore,
};
use serde::{Deserialize, Serialize};
use crate::data::{CidKey, VersionedMapIpld};
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub struct AuthorityIpld {
pub allowed: Cid,
pub revoked: Cid,
}
impl AuthorityIpld {
pub async fn try_empty<S: BlockStore>(store: &mut S) -> Result<Self> {
let allowed_ipld = AllowedIpld::try_empty(store).await?;
let allowed = store.save::<DagCborCodec, _>(allowed_ipld).await?;
let revoked_ipld = RevokedIpld::try_empty(store).await?;
let revoked = store.save::<DagCborCodec, _>(revoked_ipld).await?;
Ok(AuthorityIpld { allowed, revoked })
}
}
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize, Hash)]
pub struct DelegationIpld {
pub name: String,
pub jwt: Cid,
}
impl DelegationIpld {
pub async fn try_register<S: BlockStore>(name: &str, jwt: &str, store: &S) -> Result<Self> {
let mut store = UcanStore(store.clone());
let cid = store.write_token(jwt).await?;
Ok(DelegationIpld {
name: name.to_string(),
jwt: cid,
})
}
pub async fn resolve_ucan<S: BlockStore>(&self, store: &S) -> Result<Ucan> {
let store = UcanStore(store.clone());
let jwt = store.require_token(&self.jwt).await?;
Ucan::try_from_token_string(&jwt)
}
}
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize, Hash)]
pub struct RevocationIpld {
pub iss: String,
pub revoke: String,
pub challenge: String,
}
impl RevocationIpld {
pub async fn try_revoke<K: KeyMaterial>(cid: &Cid, issuer: &K) -> Result<Self> {
Ok(RevocationIpld {
iss: issuer.get_did().await?,
revoke: cid.to_string(),
challenge: base64_encode(&issuer.sign(&Self::make_challenge_payload(cid)).await?)?,
})
}
pub async fn try_verify<K: KeyMaterial + ?Sized>(&self, claimed_issuer: &K) -> Result<()> {
let cid = Cid::try_from(self.revoke.as_str())?;
let challenge_payload = Self::make_challenge_payload(&cid);
let signature = base64_decode(&self.challenge)?;
claimed_issuer
.verify(&challenge_payload, &signature)
.await?;
Ok(())
}
fn make_challenge_payload(cid: &Cid) -> Vec<u8> {
format!("REVOKE:{cid}").as_bytes().to_vec()
}
}
pub type AllowedIpld = VersionedMapIpld<CidKey, DelegationIpld>;
pub type RevokedIpld = VersionedMapIpld<CidKey, RevocationIpld>;
#[cfg(test)]
mod tests {
use noosphere_storage::{memory::MemoryStore, ucan::UcanStore};
use ucan::{builder::UcanBuilder, crypto::KeyMaterial, store::UcanJwtStore};
use crate::authority::generate_ed25519_key;
use super::{DelegationIpld, RevocationIpld};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test;
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn it_stores_a_registerd_jwt() {
let store = MemoryStore::default();
let key = generate_ed25519_key();
let ucan_jwt = UcanBuilder::default()
.issued_by(&key)
.for_audience(&key.get_did().await.unwrap())
.with_lifetime(128)
.build()
.unwrap()
.sign()
.await
.unwrap()
.encode()
.unwrap();
let delegation = DelegationIpld::try_register("foobar", &ucan_jwt, &store)
.await
.unwrap();
assert_eq!(
UcanStore(store).read_token(&delegation.jwt).await.unwrap(),
Some(ucan_jwt)
);
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn it_can_verify_that_a_key_issued_a_revocation() {
let store = MemoryStore::default();
let key = generate_ed25519_key();
let other_key = generate_ed25519_key();
let ucan_jwt = UcanBuilder::default()
.issued_by(&key)
.for_audience(&key.get_did().await.unwrap())
.with_lifetime(128)
.build()
.unwrap()
.sign()
.await
.unwrap()
.encode()
.unwrap();
let delegation = DelegationIpld::try_register("foobar", &ucan_jwt, &store)
.await
.unwrap();
let revocation = RevocationIpld::try_revoke(&delegation.jwt, &key)
.await
.unwrap();
assert!(revocation.try_verify(&key).await.is_ok());
assert!(revocation.try_verify(&other_key).await.is_err());
}
}