forked from ucan-wg/rs-ucan
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegation.rs
190 lines (165 loc) · 5.08 KB
/
delegation.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
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
183
184
185
186
187
188
189
190
//! A [`Delegation`] is the way to grant someone else the use of [`Ability`][crate::ability].
//!
//! ## Data
//!
//! - [`Delegation`] is the top-level, signed data struture.
//! - [`Payload`] is the fields unique to an invocation.
//! - [`Preset`] is an [`Delegation`] preloaded with this library's [preset abilities](crate::ability::preset::Ready).
//! - [`Predicate`]s are syntactically-driven validation rules for [`Delegation`]s.
//!
//! ## Stateful Helpers
//!
//! - [`Agent`] is a high-level interface for sessions that will involve more than one invoctaion.
//! - [`store`] is an interface for caching [`Delegation`]s.
pub mod policy;
pub mod store;
mod agent;
mod payload;
pub use agent::Agent;
pub use payload::*;
use crate::ability::arguments::Named;
use crate::{
capsule::Capsule,
crypto::{signature::Envelope, varsig, Nonce},
did::{self, Did},
time::{TimeBoundError, Timestamp},
};
use libipld_core::link::Link;
use libipld_core::{codec::Codec, ipld::Ipld};
use policy::Predicate;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use web_time::SystemTime;
/// A [`Delegation`] is a signed delegation [`Payload`]
///
/// A [`Payload`] on its own is not a valid [`Delegation`], as it must be signed by the issuer.
#[derive(Clone, Debug, PartialEq)]
pub struct Delegation<
DID: Did = did::preset::Verifier,
V: varsig::Header<C> = varsig::header::Preset,
C: Codec = varsig::encoding::Preset,
> {
pub varsig_header: V,
pub payload: Payload<DID>,
pub signature: DID::Signature,
_marker: std::marker::PhantomData<C>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Proof<
DID: Did = did::preset::Verifier,
V: varsig::Header<C> = varsig::header::Preset,
C: Codec = varsig::encoding::Preset,
> {
pub prf: Vec<Link<Delegation<DID, V, C>>>,
}
impl<DID: Did, V: varsig::Header<C>, C: Codec> Capsule for Proof<DID, V, C> {
const TAG: &'static str = "ucan/prf";
}
impl<DID: Did, V: varsig::Header<C>, C: Codec> Delegation<DID, V, C> {
pub fn new(
varsig_header: V,
signature: DID::Signature,
payload: Payload<DID>,
) -> Delegation<DID, V, C> {
Delegation {
varsig_header,
payload,
signature,
_marker: std::marker::PhantomData,
}
}
/// Retrive the `issuer` of a [`Delegation`]
pub fn issuer(&self) -> &DID {
&self.payload.issuer
}
/// Retrive the `subject` of a [`Delegation`]
pub fn subject(&self) -> &Option<DID> {
&self.payload.subject
}
/// Retrive the `audience` of a [`Delegation`]
pub fn audience(&self) -> &DID {
&self.payload.audience
}
/// Retrive the `policy` of a [`Delegation`]
pub fn policy(&self) -> &Vec<Predicate> {
&self.payload.policy
}
/// Retrive the `metadata` of a [`Delegation`]
pub fn metadata(&self) -> &BTreeMap<String, Ipld> {
&self.payload.metadata
}
/// Retrive the `nonce` of a [`Delegation`]
pub fn nonce(&self) -> &Nonce {
&self.payload.nonce
}
/// Retrive the `not_before` of a [`Delegation`]
pub fn not_before(&self) -> Option<&Timestamp> {
self.payload.not_before.as_ref()
}
/// Retrive the `expiration` of a [`Delegation`]
pub fn expiration(&self) -> &Timestamp {
&self.payload.expiration
}
pub fn check_time(&self, now: SystemTime) -> Result<(), TimeBoundError> {
self.payload.check_time(now)
}
}
impl<DID: Did + Clone, V: varsig::Header<C> + Clone, C: Codec> Envelope for Delegation<DID, V, C>
where
Payload<DID>: TryFrom<Named<Ipld>>,
Named<Ipld>: From<Payload<DID>>,
{
type DID = DID;
type Payload = Payload<DID>;
type VarsigHeader = V;
type Encoder = C;
fn construct(
varsig_header: V,
signature: DID::Signature,
payload: Payload<DID>,
) -> Delegation<DID, V, C> {
Delegation {
varsig_header,
payload,
signature,
_marker: std::marker::PhantomData,
}
}
fn varsig_header(&self) -> &V {
&self.varsig_header
}
fn payload(&self) -> &Payload<DID> {
&self.payload
}
fn signature(&self) -> &DID::Signature {
&self.signature
}
fn verifier(&self) -> &DID {
&self.payload.issuer
}
}
impl<DID: Did + Clone, V: varsig::Header<C> + Clone, C: Codec> Serialize for Delegation<DID, V, C>
where
Payload<DID>: TryFrom<Named<Ipld>>,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.to_ipld_envelope().serialize(serializer)
}
}
impl<'de, DID: Did + Clone, V: varsig::Header<C> + Clone, C: Codec> Deserialize<'de>
for Delegation<DID, V, C>
where
Payload<DID>: TryFrom<Named<Ipld>>,
<Payload<DID> as TryFrom<Named<Ipld>>>::Error: std::fmt::Display,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let ipld = Ipld::deserialize(deserializer)?;
Self::try_from_ipld_envelope(ipld).map_err(serde::de::Error::custom)
}
}