forked from ucan-wg/rs-ucan
/
traits.rs
144 lines (128 loc) · 3.9 KB
/
traits.rs
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
use crate::{
ability::arguments::Named,
crypto::signature::Envelope,
crypto::varsig,
delegation::payload::Payload,
delegation::{policy::Predicate, Delegation},
did::Did,
};
use libipld_core::codec::Encode;
use libipld_core::ipld::Ipld;
use libipld_core::{cid::Cid, codec::Codec};
use nonempty::NonEmpty;
use std::{fmt::Debug, sync::Arc};
use thiserror::Error;
use web_time::SystemTime;
pub trait Store<DID: Did + Clone, V: varsig::Header<C> + Clone, C: Codec + TryFrom<u64> + Into<u64>>
where
Ipld: Encode<C>,
Payload<DID>: TryFrom<Named<Ipld>>,
Named<Ipld>: From<Payload<DID>>,
{
type DelegationStoreError: Debug;
fn get(
&self,
cid: &Cid,
) -> Result<Option<Arc<Delegation<DID, V, C>>>, Self::DelegationStoreError>;
fn insert(
&self,
delegation: Delegation<DID, V, C>,
) -> Result<(), CannotCidOr<Self::DelegationStoreError>> {
self.insert_keyed(delegation.cid()?, delegation)
.map_err(CannotCidOr::StoreError)
}
fn insert_keyed(
&self,
cid: Cid,
delegation: Delegation<DID, V, C>,
) -> Result<(), Self::DelegationStoreError>;
// FIXME validate invocation
// store invocation
// just... move to invocation
fn revoke(&self, cid: Cid) -> Result<(), Self::DelegationStoreError>;
fn get_chain(
&self,
audience: &DID,
subject: &DID,
command: String,
policy: Vec<Predicate>,
now: SystemTime,
) -> Result<Option<NonEmpty<(Cid, Arc<Delegation<DID, V, C>>)>>, Self::DelegationStoreError>;
fn get_chain_cids(
&self,
audience: &DID,
subject: &DID,
command: String,
policy: Vec<Predicate>,
now: SystemTime,
) -> Result<Option<NonEmpty<Cid>>, Self::DelegationStoreError> {
self.get_chain(audience, subject, command, policy, now)
.map(|chain| chain.map(|chain| chain.map(|(cid, _)| cid)))
}
fn can_delegate(
&self,
issuer: DID,
audience: &DID,
command: String,
policy: Vec<Predicate>,
now: SystemTime,
) -> Result<bool, Self::DelegationStoreError> {
self.get_chain(audience, &issuer, command, policy, now)
.map(|chain| chain.is_some())
}
fn get_many(
&self,
cids: &[Cid],
) -> Result<Vec<Option<Arc<Delegation<DID, V, C>>>>, Self::DelegationStoreError> {
cids.iter()
.map(|cid| self.get(cid))
.collect::<Result<_, Self::DelegationStoreError>>()
}
}
impl<
T: Store<DID, V, C>,
DID: Did + Clone,
V: varsig::Header<C> + Clone,
C: Codec + TryFrom<u64> + Into<u64>,
> Store<DID, V, C> for &T
where
Ipld: Encode<C>,
Payload<DID>: TryFrom<Named<Ipld>>,
Named<Ipld>: From<Payload<DID>>,
{
type DelegationStoreError = <T as Store<DID, V, C>>::DelegationStoreError;
fn get(
&self,
cid: &Cid,
) -> Result<Option<Arc<Delegation<DID, V, C>>>, Self::DelegationStoreError> {
(**self).get(cid)
}
fn insert_keyed(
&self,
cid: Cid,
delegation: Delegation<DID, V, C>,
) -> Result<(), Self::DelegationStoreError> {
(**self).insert_keyed(cid, delegation)
}
fn revoke(&self, cid: Cid) -> Result<(), Self::DelegationStoreError> {
(**self).revoke(cid)
}
fn get_chain(
&self,
audience: &DID,
subject: &DID,
command: String,
policy: Vec<Predicate>,
now: SystemTime,
) -> Result<Option<NonEmpty<(Cid, Arc<Delegation<DID, V, C>>)>>, Self::DelegationStoreError>
{
(**self).get_chain(audience, subject, command, policy, now)
}
}
#[derive(Debug, Error)]
pub enum CannotCidOr<E> {
#[error("Cannot make CID from delegation based on supplied Varsig")]
CannotMakeCid(#[from] libipld_core::error::Error),
#[error("Store error: {0}")]
StoreError(E),
}