-
Notifications
You must be signed in to change notification settings - Fork 6
/
error.js
395 lines (372 loc) · 8.91 KB
/
error.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import * as API from '@ucanto/interface'
import { the } from './util.js'
import { isLink } from '@ucanto/core/link'
import { fail, Failure } from '@ucanto/core/result'
export { Failure, fail }
export class EscalatedCapability extends Failure {
/**
* @param {API.ParsedCapability} claimed
* @param {object} delegated
* @param {API.Failure} cause
*/
constructor(claimed, delegated, cause) {
super()
this.claimed = claimed
this.delegated = delegated
this.cause = cause
this.name = the('EscalatedCapability')
}
describe() {
return `Constraint violation: ${this.cause.message}`
}
}
/**
* @implements {API.DelegationError}
*/
export class DelegationError extends Failure {
/**
* @param {(API.InvalidCapability | API.EscalatedDelegation | API.DelegationError)[]} causes
* @param {object} context
*/
constructor(causes, context) {
super()
this.name = the('InvalidClaim')
this.causes = causes
this.context = context
}
describe() {
return [
`Can not derive ${this.context} from delegated capabilities:`,
...this.causes.map(cause => li(cause.message)),
].join('\n')
}
/**
* @type {API.InvalidCapability | API.EscalatedDelegation | API.DelegationError}
*/
get cause() {
/* c8 ignore next 9 */
if (this.causes.length !== 1) {
return this
} else {
const [cause] = this.causes
const value = cause.name === 'InvalidClaim' ? cause.cause : cause
Object.defineProperties(this, { cause: { value } })
return value
}
}
}
/**
* @implements {API.SessionEscalation}
*/
export class SessionEscalation extends Failure {
/**
* @param {object} source
* @param {API.Delegation} source.delegation
* @param {API.Failure} source.cause
*/
constructor({ delegation, cause }) {
super()
this.name = the('SessionEscalation')
this.delegation = delegation
this.cause = cause
}
describe() {
const issuer = this.delegation.issuer.did()
return [
`Delegation ${this.delegation.cid} issued by ${issuer} has an invalid session`,
li(this.cause.message),
].join('\n')
}
}
/**
* @implements {API.InvalidSignature}
*/
export class InvalidSignature extends Failure {
/**
* @param {API.Delegation} delegation
* @param {API.Verifier} verifier
*/
constructor(delegation, verifier) {
super()
this.name = the('InvalidSignature')
this.delegation = delegation
this.verifier = verifier
}
get issuer() {
return this.delegation.issuer
}
get audience() {
return this.delegation.audience
}
get key() {
return this.verifier.toDIDKey()
}
describe() {
const issuer = this.issuer.did()
const key = this.key
return (
issuer.startsWith('did:key')
? [
`Proof ${this.delegation.cid} does not has a valid signature from ${key}`,
]
: [
`Proof ${this.delegation.cid} issued by ${issuer} does not has a valid signature from ${key}`,
` ℹ️ Probably issuer signed with a different key, which got rotated, invalidating delegations that were issued with prior keys`,
]
).join('\n')
}
}
/**
* @implements {API.UnavailableProof}
*/
export class UnavailableProof extends Failure {
/**
* @param {API.UCAN.Link} link
* @param {Error} [cause]
*/
constructor(link, cause) {
super()
this.name = the('UnavailableProof')
this.link = link
this.cause = cause
}
describe() {
return [
`Linked proof '${this.link}' is not included and could not be resolved`,
...(this.cause
? [li(`Proof resolution failed with: ${this.cause.message}`)]
: []),
].join('\n')
}
}
export class DIDKeyResolutionError extends Failure {
/**
* @param {API.UCAN.DID} did
* @param {API.Failure} [cause]
*/
constructor(did, cause) {
super()
this.name = the('DIDKeyResolutionError')
this.did = did
this.cause = cause
}
describe() {
return `Unable to resolve '${this.did}' key`
}
}
/**
* @implements {API.InvalidAudience}
*/
export class PrincipalAlignmentError extends Failure {
/**
* @param {API.UCAN.Principal} audience
* @param {API.Delegation} delegation
*/
constructor(audience, delegation) {
super()
this.name = the('InvalidAudience')
this.audience = audience
this.delegation = delegation
}
describe() {
return `Delegation audience is '${this.delegation.audience.did()}' instead of '${this.audience.did()}'`
}
toJSON() {
const { name, audience, message, stack } = this
return {
name,
audience: audience.did(),
delegation: { audience: this.delegation.audience.did() },
message,
stack,
}
}
}
/**
* @implements {API.MalformedCapability}
*/
export class MalformedCapability extends Failure {
/**
* @param {API.Capability} capability
* @param {API.Failure} cause
*/
constructor(capability, cause) {
super()
this.name = the('MalformedCapability')
this.capability = capability
this.cause = cause
}
describe() {
return [
`Encountered malformed '${this.capability.can}' capability: ${format(
this.capability
)}`,
li(this.cause.message),
].join('\n')
}
}
export class UnknownCapability extends Failure {
/**
* @param {API.Capability} capability
*/
constructor(capability) {
super()
this.name = the('UnknownCapability')
this.capability = capability
}
/* c8 ignore next 3 */
describe() {
return `Encountered unknown capability: ${format(this.capability)}`
}
}
export class Expired extends Failure {
/**
* @param {API.Delegation & { expiration: number }} delegation
*/
constructor(delegation) {
super()
this.name = the('Expired')
this.delegation = delegation
}
describe() {
return `Proof ${this.delegation.cid} has expired on ${new Date(
this.delegation.expiration * 1000
)}`
}
get expiredAt() {
return this.delegation.expiration
}
toJSON() {
const { name, expiredAt, message, stack } = this
return {
name,
message,
expiredAt,
stack,
}
}
}
/**
* @implements {API.Revoked}
*/
export class Revoked extends Failure {
/**
* @param {API.Delegation} delegation
*/
constructor(delegation) {
super()
this.name = the('Revoked')
this.delegation = delegation
}
describe() {
return `Proof ${this.delegation.cid} has been revoked`
}
toJSON() {
const { name, message, stack } = this
return {
name,
message,
stack,
}
}
}
export class NotValidBefore extends Failure {
/**
* @param {API.Delegation & { notBefore: number }} delegation
*/
constructor(delegation) {
super()
this.name = the('NotValidBefore')
this.delegation = delegation
}
describe() {
return `Proof ${this.delegation.cid} is not valid before ${new Date(
this.delegation.notBefore * 1000
)}`
}
get validAt() {
return this.delegation.notBefore
}
toJSON() {
const { name, validAt, message, stack } = this
return {
name,
message,
validAt,
stack,
}
}
}
/**
* @implements {API.Unauthorized}
*/
export class Unauthorized extends Failure {
/**
* @param {{
* capability: API.CapabilityParser
* delegationErrors: API.DelegationError[]
* unknownCapabilities: API.Capability[]
* invalidProofs: API.InvalidProof[]
* failedProofs: API.InvalidClaim[]
* }} cause
*/
constructor({
capability,
delegationErrors,
unknownCapabilities,
invalidProofs,
failedProofs,
}) {
super()
/** @type {"Unauthorized"} */
this.name = 'Unauthorized'
this.capability = capability
this.delegationErrors = delegationErrors
this.unknownCapabilities = unknownCapabilities
this.invalidProofs = invalidProofs
this.failedProofs = failedProofs
}
describe() {
const errors = [
...this.failedProofs.map(error => li(error.message)),
...this.delegationErrors.map(error => li(error.message)),
...this.invalidProofs.map(error => li(error.message)),
]
const unknown = this.unknownCapabilities.map(c => li(JSON.stringify(c)))
return [
`Claim ${this.capability} is not authorized`,
...(errors.length > 0
? errors
: [li(`No matching delegated capability found`)]),
...(unknown.length > 0
? [li(`Encountered unknown capabilities\n${unknown.join('\n')}`)]
: []),
].join('\n')
}
}
/**
* @param {unknown} capability
* @param {string|number} [space]
*/
const format = (capability, space) =>
JSON.stringify(
capability,
(_key, value) => {
/* c8 ignore next 2 */
if (isLink(value)) {
return value.toString()
} else {
return value
}
},
space
)
/**
* @param {string} message
*/
export const indent = (message, indent = ' ') =>
`${indent}${message.split('\n').join(`\n${indent}`)}`
/**
* @param {string} message
*/
export const li = message => indent(`- ${message}`)