-
Notifications
You must be signed in to change notification settings - Fork 12
/
attenuation.test.ts
205 lines (183 loc) · 5.43 KB
/
attenuation.test.ts
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { Chained } from "../src/chained"
import * as token from "../src/token"
import { alice, bob, mallory } from "./fixtures"
import { emailCapabilities } from "./emailCapabilities"
import { maxNbf } from "./utils"
describe("attenuation.emailCapabilities", () => {
it("works with a simple example", async () => {
// alice -> bob, bob -> mallory
// alice delegates access to sending email as her to bob
// and bob delegates it further to mallory
const leafUcan = await token.build({
issuer: alice,
audience: bob.did(),
capabilities: [{
email: "alice@email.com",
cap: "SEND",
}]
})
const ucan = await token.build({
issuer: bob,
audience: mallory.did(),
capabilities: [{
email: "alice@email.com",
cap: "SEND",
}],
proofs: [token.encode(leafUcan)]
})
const emailCaps = Array.from(emailCapabilities(await Chained.fromToken(token.encode(ucan))))
expect(emailCaps).toEqual([{
info: {
originator: alice.did(),
expiresAt: Math.min(leafUcan.payload.exp, ucan.payload.exp),
notBefore: maxNbf(leafUcan.payload.nbf, ucan.payload.nbf),
},
capability: {
email: "alice@email.com",
cap: "SEND"
}
}])
})
it("reports the first issuer in the chain as originator", async () => {
// alice -> bob, bob -> mallory
// alice delegates nothing to bob
// and bob delegates his email to mallory
const leafUcan = await token.build({
issuer: alice,
audience: bob.did(),
})
const ucan = await token.build({
issuer: bob,
audience: mallory.did(),
capabilities: [{
email: "bob@email.com",
cap: "SEND",
}],
proofs: [token.encode(leafUcan)]
})
// we implicitly expect the originator to become bob
expect(Array.from(emailCapabilities(await Chained.fromToken(token.encode(ucan))))).toEqual([{
info: {
originator: bob.did(),
expiresAt: ucan.payload.exp,
notBefore: ucan.payload.nbf,
},
capability: {
email: "bob@email.com",
cap: "SEND"
}
}])
})
it("finds the right proof chain for the originator", async () => {
// alice -> mallory, bob -> mallory, mallory -> alice
// both alice and bob delegate their email access to mallory
// mallory then creates a UCAN with capability to send both
const leafUcanAlice = await token.build({
issuer: alice,
audience: mallory.did(),
capabilities: [{
email: "alice@email.com",
cap: "SEND",
}]
})
const leafUcanBob = await token.build({
issuer: bob,
audience: mallory.did(),
capabilities: [{
email: "bob@email.com",
cap: "SEND",
}]
})
const ucan = await token.build({
issuer: mallory,
audience: alice.did(),
capabilities: [
{
email: "alice@email.com",
cap: "SEND",
},
{
email: "bob@email.com",
cap: "SEND",
}
],
proofs: [token.encode(leafUcanAlice), token.encode(leafUcanBob)]
})
const chained = await Chained.fromToken(token.encode(ucan))
expect(Array.from(emailCapabilities(chained))).toEqual([
{
info: {
originator: alice.did(),
expiresAt: Math.min(leafUcanAlice.payload.exp, ucan.payload.exp),
notBefore: maxNbf(leafUcanAlice.payload.nbf, ucan.payload.nbf),
},
capability: {
email: "alice@email.com",
cap: "SEND",
}
},
{
info: {
originator: bob.did(),
expiresAt: Math.min(leafUcanBob.payload.exp, ucan.payload.exp),
notBefore: maxNbf(leafUcanBob.payload.nbf, ucan.payload.nbf),
},
capability: {
email: "bob@email.com",
cap: "SEND",
}
}
])
})
it("reports all chain options", async () => {
// alice -> mallory, bob -> mallory, mallory -> alice
// both alice and bob claim to have access to alice@email.com
// and both grant that capability to mallory
// a verifier needs to know both to verify valid email access
const aliceEmail = {
email: "alice@email.com",
cap: "SEND",
}
const leafUcanAlice = await token.build({
issuer: alice,
audience: mallory.did(),
capabilities: [aliceEmail]
})
const leafUcanBob = await token.build({
issuer: bob,
audience: mallory.did(),
capabilities: [aliceEmail]
})
const ucan = await token.build({
issuer: mallory,
audience: alice.did(),
capabilities: [aliceEmail],
proofs: [token.encode(leafUcanAlice), token.encode(leafUcanBob)]
})
const chained = await Chained.fromToken(token.encode(ucan))
expect(Array.from(emailCapabilities(chained))).toEqual([
{
info: {
originator: alice.did(),
expiresAt: Math.min(leafUcanAlice.payload.exp, ucan.payload.exp),
notBefore: maxNbf(leafUcanAlice.payload.nbf, ucan.payload.nbf),
},
capability: {
email: "alice@email.com",
cap: "SEND",
}
},
{
info: {
originator: bob.did(),
expiresAt: Math.min(leafUcanBob.payload.exp, ucan.payload.exp),
notBefore: maxNbf(leafUcanBob.payload.nbf, ucan.payload.nbf),
},
capability: {
email: "alice@email.com",
cap: "SEND",
}
}
])
})
})