forked from ucan-wg/rs-ucan
/
traits.rs
105 lines (92 loc) · 2.97 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
use crate::{
crypto::varsig,
delegation::{policy::Predicate, Delegation},
did::Did,
};
use libipld_core::{cid::Cid, codec::Codec};
use nonempty::NonEmpty;
use std::fmt::Debug;
use web_time::SystemTime;
pub trait Store<DID: Did, V: varsig::Header<Enc>, Enc: Codec + TryFrom<u64> + Into<u64>> {
type DelegationStoreError: Debug;
fn get(
&self,
cid: &Cid,
) -> Result<Option<&Delegation<DID, V, Enc>>, Self::DelegationStoreError>;
fn insert(
&mut self,
cid: Cid,
delegation: Delegation<DID, V, Enc>,
) -> Result<(), Self::DelegationStoreError>;
// FIXME validate invocation
// store invocation
// just... move to invocation
fn revoke(&mut self, cid: Cid) -> Result<(), Self::DelegationStoreError>;
fn get_chain(
&self,
audience: &DID,
subject: &Option<DID>,
command: String,
policy: Vec<Predicate>,
now: SystemTime,
) -> Result<Option<NonEmpty<(Cid, &Delegation<DID, V, Enc>)>>, Self::DelegationStoreError>;
fn get_chain_cids(
&self,
audience: &DID,
subject: &Option<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, &Some(issuer), command, policy, now)
.map(|chain| chain.is_some())
}
fn get_many(
&self,
cids: &[Cid],
) -> Result<Vec<Option<&Delegation<DID, V, Enc>>>, Self::DelegationStoreError> {
cids.iter().try_fold(vec![], |mut acc, cid| {
acc.push(self.get(cid)?);
Ok(acc)
})
}
}
impl<T: Store<DID, V, C>, DID: Did, V: varsig::Header<C>, C: Codec + TryFrom<u64> + Into<u64>>
Store<DID, V, C> for &mut T
{
type DelegationStoreError = <T as Store<DID, V, C>>::DelegationStoreError;
fn get(&self, cid: &Cid) -> Result<Option<&Delegation<DID, V, C>>, Self::DelegationStoreError> {
(**self).get(cid)
}
fn insert(
&mut self,
cid: Cid,
delegation: Delegation<DID, V, C>,
) -> Result<(), Self::DelegationStoreError> {
(**self).insert(cid, delegation)
}
fn revoke(&mut self, cid: Cid) -> Result<(), Self::DelegationStoreError> {
(**self).revoke(cid)
}
fn get_chain(
&self,
audience: &DID,
subject: &Option<DID>,
command: String,
policy: Vec<Predicate>,
now: SystemTime,
) -> Result<Option<NonEmpty<(Cid, &Delegation<DID, V, C>)>>, Self::DelegationStoreError> {
(**self).get_chain(audience, subject, command, policy, now)
}
}