forked from ucan-wg/rs-ucan
/
agent.rs
146 lines (128 loc) · 4.2 KB
/
agent.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
145
146
use super::{payload::Payload, policy::Predicate, store::Store, Delegation};
use crate::{
ability::arguments::Named,
crypto::{signature::Envelope, varsig, Nonce},
did,
did::Did,
time::Timestamp,
};
use libipld_core::{
cid::Cid,
codec::{Codec, Encode},
ipld::Ipld,
};
use std::{collections::BTreeMap, marker::PhantomData};
use thiserror::Error;
use web_time::SystemTime;
/// A stateful agent capable of delegating to others, and being delegated to.
///
/// This is helpful for sessions where more than one delegation will be made.
#[derive(Debug)]
pub struct Agent<
S: Store<DID, V, C>,
DID: Did + Clone = did::preset::Verifier,
V: varsig::Header<C> + Clone = varsig::header::Preset,
C: Codec = varsig::encoding::Preset,
> where
Ipld: Encode<C>,
Payload<DID>: TryFrom<Named<Ipld>>,
Named<Ipld>: From<Payload<DID>>,
{
/// The [`Did`][Did] of the agent.
pub did: DID,
/// The attached [`deleagtion::Store`][super::store::Store].
pub store: S,
signer: <DID as Did>::Signer,
_marker: PhantomData<(V, C)>,
}
impl<S: Store<DID, V, C> + Clone, DID: Did + Clone, V: varsig::Header<C> + Clone, C: Codec>
Agent<S, DID, V, C>
where
Ipld: Encode<C>,
Payload<DID>: TryFrom<Named<Ipld>>,
Named<Ipld>: From<Payload<DID>>,
{
pub fn new(did: DID, signer: <DID as Did>::Signer, store: S) -> Self {
Self {
did,
store,
signer,
_marker: PhantomData,
}
}
pub fn delegate(
&self,
audience: DID,
subject: Option<&DID>,
via: Option<DID>,
command: String,
new_policy: Vec<Predicate>,
metadata: BTreeMap<String, Ipld>,
expiration: Option<Timestamp>,
not_before: Option<Timestamp>,
now: SystemTime,
varsig_header: V,
) -> Result<Delegation<DID, V, C>, DelegateError<S::Error>> {
let nonce = Nonce::generate_16();
let (subject, policy) = match subject {
Some(subject) if *subject == self.did => (Some(subject.clone()), new_policy),
None => (None, new_policy),
Some(subject) => {
let proofs = &self
.store
.get_chain(&self.did, &subject, &command, vec![], now)
.map_err(DelegateError::StoreError)?
.ok_or(DelegateError::ProofsNotFound)?;
let to_delegate = proofs.first().1.payload();
let mut policy = to_delegate.policy.clone();
policy.extend(new_policy);
(Some(subject.clone()), policy)
}
};
let payload: Payload<DID> = Payload {
issuer: self.did.clone(),
audience,
subject,
via,
command,
metadata,
nonce,
expiration,
not_before,
policy,
};
Ok(Delegation::try_sign(&self.signer, varsig_header, payload).expect("FIXME"))
}
pub fn receive(
&self,
cid: Cid, // FIXME remove and generate from the capsule header?
delegation: Delegation<DID, V, C>,
) -> Result<(), ReceiveError<S::Error, DID>> {
if self.store.get(&cid).is_ok() {
return Ok(());
}
if delegation.audience() != &self.did {
return Err(ReceiveError::WrongAudience(delegation.audience().clone()));
}
delegation
.validate_signature()
.map_err(|_| ReceiveError::InvalidSignature(cid))?;
self.store.insert_keyed(cid, delegation).map_err(Into::into)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Error)]
pub enum DelegateError<StoreErr> {
#[error("The current agent does not have the necessary proofs to delegate.")]
ProofsNotFound,
#[error(transparent)]
StoreError(#[from] StoreErr),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Error)]
pub enum ReceiveError<StoreErr, DID> {
#[error("The current agent ({0}) is not the intended audience of the delegation.")]
WrongAudience(DID),
#[error("Signature for UCAN with CID {0} is invalid.")]
InvalidSignature(Cid),
#[error(transparent)]
StoreError(#[from] StoreErr),
}