@interop/zcap 10.0.2 → 11.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +21 -3
  2. package/dist/CapabilityDelegation.d.ts +173 -0
  3. package/dist/CapabilityDelegation.d.ts.map +1 -0
  4. package/dist/CapabilityDelegation.js +372 -0
  5. package/dist/CapabilityDelegation.js.map +1 -0
  6. package/dist/CapabilityInvocation.d.ts +151 -0
  7. package/dist/CapabilityInvocation.d.ts.map +1 -0
  8. package/dist/CapabilityInvocation.js +365 -0
  9. package/dist/CapabilityInvocation.js.map +1 -0
  10. package/dist/CapabilityProofPurpose.d.ts +203 -0
  11. package/dist/CapabilityProofPurpose.d.ts.map +1 -0
  12. package/dist/CapabilityProofPurpose.js +531 -0
  13. package/dist/CapabilityProofPurpose.js.map +1 -0
  14. package/dist/constants.d.ts +11 -0
  15. package/dist/constants.d.ts.map +1 -0
  16. package/dist/constants.js +23 -0
  17. package/dist/constants.js.map +1 -0
  18. package/dist/index.d.ts +25 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +40 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/types.d.ts +224 -0
  23. package/dist/types.d.ts.map +1 -0
  24. package/dist/types.js +2 -0
  25. package/dist/types.js.map +1 -0
  26. package/dist/utils.d.ts +250 -0
  27. package/dist/utils.d.ts.map +1 -0
  28. package/dist/utils.js +591 -0
  29. package/dist/utils.js.map +1 -0
  30. package/package.json +47 -34
  31. package/lib/CapabilityDelegation.js +0 -312
  32. package/lib/CapabilityInvocation.js +0 -343
  33. package/lib/CapabilityProofPurpose.js +0 -538
  34. package/lib/constants.js +0 -32
  35. package/lib/index.js +0 -55
  36. package/lib/utils.js +0 -672
  37. package/types/lib/CapabilityDelegation.d.ts +0 -101
  38. package/types/lib/CapabilityDelegation.d.ts.map +0 -1
  39. package/types/lib/CapabilityInvocation.d.ts +0 -100
  40. package/types/lib/CapabilityInvocation.d.ts.map +0 -1
  41. package/types/lib/CapabilityProofPurpose.d.ts +0 -126
  42. package/types/lib/CapabilityProofPurpose.d.ts.map +0 -1
  43. package/types/lib/constants.d.ts +0 -15
  44. package/types/lib/constants.d.ts.map +0 -1
  45. package/types/lib/index.d.ts +0 -42
  46. package/types/lib/index.d.ts.map +0 -1
  47. package/types/lib/utils.d.ts +0 -304
  48. package/types/lib/utils.d.ts.map +0 -1
package/dist/utils.js ADDED
@@ -0,0 +1,591 @@
1
+ /*!
2
+ * Copyright (c) 2018-2024 Digital Bazaar, Inc. All rights reserved.
3
+ */
4
+ import { MAX_CHAIN_LENGTH, ZCAP_CONTEXT_URL, ZCAP_ROOT_PREFIX } from './constants.js';
5
+ /**
6
+ * Creates a root capability from a root controller and a root invocation
7
+ * target.
8
+ *
9
+ * @param options - The options.
10
+ * @param options.controller - The root controller.
11
+ * @param options.invocationTarget - The root invocation target.
12
+ *
13
+ * @returns The root capability.
14
+ */
15
+ export function createRootCapability({ controller, invocationTarget }) {
16
+ return {
17
+ '@context': ZCAP_CONTEXT_URL,
18
+ id: `${ZCAP_ROOT_PREFIX}${encodeURIComponent(invocationTarget)}`,
19
+ controller,
20
+ invocationTarget
21
+ };
22
+ }
23
+ /**
24
+ * Retrieves the controller(s) from a capability.
25
+ *
26
+ * @param options - The options.
27
+ * @param options.capability - The authorization capability (zcap).
28
+ *
29
+ * @returns The controller(s) for the capability.
30
+ */
31
+ export function getControllers({ capability }) {
32
+ const { controller } = capability;
33
+ if (!controller) {
34
+ throw new Error('Capability controller not found.');
35
+ }
36
+ return Array.isArray(controller) ? controller : [controller];
37
+ }
38
+ /**
39
+ * Returns true if the given verification method is a controller (or is
40
+ * controlled by a controller) of the given capability.
41
+ *
42
+ * @param options - The options.
43
+ * @param options.capability - The authorization capability (zcap).
44
+ * @param options.verificationMethod - The verification method to check.
45
+ *
46
+ * @returns `true` if the controller matches, `false` if not.
47
+ */
48
+ export function isController({ capability, verificationMethod }) {
49
+ const controllers = getControllers({ capability });
50
+ return (controllers.includes(verificationMethod.controller) ||
51
+ controllers.includes(verificationMethod.id));
52
+ }
53
+ /**
54
+ * Retrieves the allowed actions from a capability.
55
+ *
56
+ * @param options - The options.
57
+ * @param options.capability - The authorization capability (zcap).
58
+ *
59
+ * @returns Allowed actions.
60
+ */
61
+ export function getAllowedActions({ capability }) {
62
+ if (!('allowedAction' in capability) || !capability.allowedAction) {
63
+ return [];
64
+ }
65
+ return [capability.allowedAction].flat();
66
+ }
67
+ /**
68
+ * Retrieves the target from a capability.
69
+ *
70
+ * @param options - The options.
71
+ * @param options.capability - The authorization capability (zcap).
72
+ *
73
+ * @returns Capability target.
74
+ */
75
+ export function getTarget({ capability }) {
76
+ // zcaps MUST have an `invocationTarget` that is a string
77
+ return capability.invocationTarget;
78
+ }
79
+ /**
80
+ * Retrieves the delegation proof(s) for a capability that is associated with
81
+ * its parent capability. A capability that has no parent or no associated
82
+ * delegation proofs will cause this function to return an empty array.
83
+ *
84
+ * @param options - The options.
85
+ * @param options.capability - The authorization capability.
86
+ *
87
+ * @returns Any `capabilityDelegation` proof objects attached to the given
88
+ * capability.
89
+ */
90
+ export function getDelegationProofs({ capability }) {
91
+ // capability is root or capability has no `proof`, then it has no relevant
92
+ // delegation proofs
93
+ if (!('parentCapability' in capability) || !capability.proof) {
94
+ return [];
95
+ }
96
+ const proof = capability.proof;
97
+ const proofs = Array.isArray(proof) ? proof : [proof];
98
+ return proofs.filter(p => p && p.proofPurpose === 'capabilityDelegation');
99
+ }
100
+ /**
101
+ * Gets the `capabilityChain` associated with the given capability.
102
+ *
103
+ * @param options - The options.
104
+ * @param options.capability - The authorization capability.
105
+ *
106
+ * @returns The capability chain entries (root to parent), as stored in the
107
+ * delegation proof.
108
+ */
109
+ export function getCapabilityChain({ capability }) {
110
+ if (!('parentCapability' in capability)) {
111
+ // root capability has no chain
112
+ return [];
113
+ }
114
+ const proofs = getDelegationProofs({ capability });
115
+ const [proof] = proofs;
116
+ if (proofs.length !== 1 || !proof) {
117
+ throw new Error('Cannot get capability chain; capability is invalid; it is not the ' +
118
+ 'root capability yet it does not have exactly one delegation proof.');
119
+ }
120
+ const { capabilityChain } = proof;
121
+ if (!(capabilityChain && Array.isArray(capabilityChain))) {
122
+ throw new Error('Cannot get capability chain; capability is invalid; it does not have ' +
123
+ 'a "capabilityChain" array in its delegation proof.');
124
+ }
125
+ return capabilityChain.slice();
126
+ }
127
+ /**
128
+ * Determines if the given `invocationTarget` is valid given a
129
+ * `baseInvocationTarget`.
130
+ *
131
+ * To check for a proper delegation, `invocationTarget` must be the child
132
+ * capability's `invocationTarget` and `baseInvocationTarget` must be the
133
+ * parent capability's `invocationTarget`.
134
+ *
135
+ * To check for a proper invocation, `invocationTarget` must be the value from
136
+ * the invocation proof and `baseInvocationTarget` must be the invoked
137
+ * capability's `invocationTarget`.
138
+ *
139
+ * @param options - The options.
140
+ * @param options.invocationTarget - The invocation target to check.
141
+ * @param options.baseInvocationTarget - The base invocation target.
142
+ * @param options.allowTargetAttenuation - `true` to allow target attenuation.
143
+ *
144
+ * @returns `true` if the target is valid, `false` if not.
145
+ */
146
+ export function isValidTarget({ invocationTarget, baseInvocationTarget, allowTargetAttenuation }) {
147
+ // direct match, valid
148
+ if (baseInvocationTarget === invocationTarget) {
149
+ return true;
150
+ }
151
+ if (allowTargetAttenuation) {
152
+ /* Note: When `allowTargetAttenuation=true`, a zcap can be invoked with
153
+ a more narrow target and delegated zcap can have a different invocation
154
+ target from its parent. Here we must ensure that the invocation target
155
+ has a proper prefix relative to the base one we're comparing against.
156
+
157
+ If the `baseInvocationTarget` already has a query (has `?`) then the
158
+ suffix that follows it must start with `&`. Otherwise, it may start
159
+ with either `/` or `?`. */
160
+ const prefixes = [];
161
+ if (baseInvocationTarget.includes('?')) {
162
+ // query already present in base invocation target, so only accept new
163
+ // variables in the query
164
+ prefixes.push(`${baseInvocationTarget}&`);
165
+ }
166
+ else {
167
+ // accept path-based attenuation or new query-based attenuation
168
+ prefixes.push(`${baseInvocationTarget}/`);
169
+ prefixes.push(`${baseInvocationTarget}?`);
170
+ }
171
+ if (prefixes.some(prefix => invocationTarget.startsWith(prefix))) {
172
+ return true;
173
+ }
174
+ }
175
+ // not a match
176
+ return false;
177
+ }
178
+ /**
179
+ * Creates a capability chain for delegating a capability from the
180
+ * given `parentCapability`.
181
+ *
182
+ * @param options - The options.
183
+ * @param options.parentCapability - The parent capability from which to compute
184
+ * the capability chain (a root zcap ID string, or a full root or delegated
185
+ * zcap object).
186
+ * @param options._skipLocalValidationForTesting - Private.
187
+ *
188
+ * @returns The computed capability chain to be included in a capability
189
+ * delegation proof.
190
+ */
191
+ export function computeCapabilityChain({ parentCapability, _skipLocalValidationForTesting }) {
192
+ // if parent capability is root (string or no parent of its own)
193
+ if (typeof parentCapability === 'string') {
194
+ return [parentCapability];
195
+ }
196
+ if (!('parentCapability' in parentCapability)) {
197
+ // capability must be a root zcap
198
+ checkCapability({ capability: parentCapability, expectRoot: true });
199
+ return [parentCapability.id];
200
+ }
201
+ // capability must be a delegated zcap, check it and get its chain
202
+ checkCapability({ capability: parentCapability, expectRoot: false });
203
+ const proofs = getDelegationProofs({ capability: parentCapability });
204
+ const [proof] = proofs;
205
+ if (proofs.length !== 1 || !proof) {
206
+ throw new Error('Cannot compute capability chain; parent capability is invalid; it is ' +
207
+ 'not the root capability yet it does not have exactly one delegation ' +
208
+ 'proof.');
209
+ }
210
+ const { capabilityChain } = proof;
211
+ if (!(capabilityChain && Array.isArray(capabilityChain))) {
212
+ throw new Error('Cannot compute capability chain; parent capability is invalid; it ' +
213
+ 'does not have a "capabilityChain" array in its delegation proof.');
214
+ }
215
+ // validate parent capability chain to help prevent bad delegations
216
+ if (!_skipLocalValidationForTesting) {
217
+ // ensure that all `capabilityChain` entries except the last are strings
218
+ const lastRequiredType = capabilityChain.length > 1 ? 'object' : 'string';
219
+ const lastIndex = capabilityChain.length - 1;
220
+ for (const [i, entry] of capabilityChain.entries()) {
221
+ const entryType = typeof entry;
222
+ if (!((i === lastIndex && entryType === lastRequiredType) ||
223
+ (i !== lastIndex && entryType === 'string'))) {
224
+ throw new TypeError('Cannot compute capability chain; parent capability chain is ' +
225
+ 'invalid; it must consist of strings of capability IDs except ' +
226
+ 'the last capability if it is delegated, in which case it must ' +
227
+ 'be an object with an "id" property that is a string.');
228
+ }
229
+ }
230
+ }
231
+ // if last zcap is embedded, change it to a reference
232
+ const newChain = capabilityChain.slice(0, capabilityChain.length - 1);
233
+ const last = capabilityChain[capabilityChain.length - 1];
234
+ if (typeof last === 'string') {
235
+ newChain.push(last);
236
+ }
237
+ else if (last) {
238
+ newChain.push(last.id);
239
+ }
240
+ newChain.push(parentCapability);
241
+ // ensure new chain uses absolute URLs
242
+ for (const entry of newChain) {
243
+ if ((typeof entry === 'string' && !entry.includes(':')) ||
244
+ (typeof entry === 'object' && !entry.id.includes(':'))) {
245
+ throw new Error('Cannot compute capability chain; parent capability chain is ' +
246
+ 'invalid because uses relative URL(s) in its capability chain.');
247
+ }
248
+ }
249
+ return newChain;
250
+ }
251
+ /**
252
+ * Dereferences the capability chain associated with the given capability,
253
+ * ensuring it passes a number of validation checks.
254
+ *
255
+ * A delegated zcap's chain has a reference to a root zcap. A verifier must
256
+ * provide a hook (`getRootCapability`) to dereference this root zcap since
257
+ * the root zcap has no delegation proof and must therefore be trusted by
258
+ * the verifier. If the root zcap can't be dereferenced by the trusted hook,
259
+ * then an authorization error must be thrown by that hook.
260
+ *
261
+ * This function will dereference the root zcap and then dereference all of
262
+ * the embedded delegated zcaps from the chain, combining them into a single
263
+ * array containing full zcaps ordered from root => tail.
264
+ *
265
+ * The dereferenced chain (result of this function) should then compare the
266
+ * root zcap's ID against a list of expected root capabilities, throwing
267
+ * an error if none of them match. Otherwise, the dereferenced chain should
268
+ * then be processed to ensure that all delegation rules have been followed.
269
+ * If checking an invocation, it should also be ensured that a combination of
270
+ * an expected target and a root zcap is permitted (note it is conceivable that
271
+ * a verifier may accept more than one combination, e.g., a target of `x` could
272
+ * work with both root zcap `a` and `b`).
273
+ *
274
+ * @param options - The options.
275
+ * @param options.capability - The authorization capability to dereference the
276
+ * chain for. Pass a string (the root zcap ID) to dereference a root zcap
277
+ * directly, or a delegated zcap object.
278
+ * @param options.getRootCapability - A function for dereferencing the root
279
+ * capability (the root zcap must be deref'd in a trusted way by the verifier,
280
+ * it must not be untrusted input).
281
+ * @param options.maxChainLength - The maximum length of the capability
282
+ * delegation chain (this is inclusive of `capability` itself).
283
+ *
284
+ * @returns Resolves to an object containing the full dereferenced chain ordered
285
+ * root to tail.
286
+ */
287
+ export async function dereferenceCapabilityChain({ capability, getRootCapability, maxChainLength = MAX_CHAIN_LENGTH }) {
288
+ // capability MUST be a string if it is root; root zcaps MUST always be
289
+ // dereferenced via a trusted mechanism provided by the verifier as they
290
+ // do not have delegation proofs
291
+ let tailCapability;
292
+ if (typeof capability === 'string') {
293
+ const id = capability;
294
+ const { rootCapability } = await getRootCapability({ id });
295
+ checkCapability({ capability: rootCapability, expectRoot: true });
296
+ if (rootCapability.id !== id) {
297
+ throw new Error(`Dereferenced root capability ID "${rootCapability.id}" does not ` +
298
+ `match reference ID "${id}".`);
299
+ }
300
+ tailCapability = rootCapability;
301
+ }
302
+ else {
303
+ // ensure capability itself is valid
304
+ checkCapability({ capability, expectRoot: false });
305
+ tailCapability = capability;
306
+ }
307
+ // get a mapping of IDs to full zcaps as the chain is validated
308
+ const dereferencedChainMap = new Map();
309
+ // get the underef'd capability chain for the capability
310
+ const capabilityChain = getCapabilityChain({ capability: tailCapability });
311
+ // ensure capability chain length (add 1 to be inclusive of `capability`)
312
+ // does not exceed max chain length; only check this once at the start
313
+ // as it produces the most sensible error -- it is true that an embedded
314
+ // zcap could go over the limit but this will be caught via a congruency
315
+ // check on the length instead
316
+ if (capabilityChain.length + 1 > maxChainLength) {
317
+ throw new Error('The capability chain exceeds the maximum allowed length ' +
318
+ `of ${maxChainLength}.`);
319
+ }
320
+ // subtract one from the max chain length to start to account for
321
+ // `capability` which is not present in `capabilityChain`
322
+ let firstPass = true;
323
+ let requiredLength = capabilityChain.length;
324
+ let currentCapability = tailCapability;
325
+ let currentCapabilityChain = capabilityChain;
326
+ while (currentCapabilityChain.length > 0) {
327
+ if (currentCapabilityChain.length !== requiredLength) {
328
+ throw new Error('The capability chain length is incongruent.');
329
+ }
330
+ // if `next.length > 1`, then its last entry is a delegated
331
+ // capability and it MUST be fully embedded as an object; all other
332
+ // entries MUST be strings
333
+ const lastRequiredType = currentCapabilityChain.length > 1 ? 'object' : 'string';
334
+ // validate entries and dereference delegated zcaps
335
+ const lastIndex = currentCapabilityChain.length - 1;
336
+ for (const [i, entry] of currentCapabilityChain.entries()) {
337
+ const entryType = typeof entry;
338
+ const entryIsString = entryType === 'string';
339
+ const requiredType = i === lastIndex ? lastRequiredType : 'string';
340
+ // ensure entry is the required type and, if it is an object, its `id`
341
+ // is a string
342
+ if (!(entryType === requiredType &&
343
+ (entryIsString ||
344
+ (typeof entry === 'object' && typeof entry.id === 'string')))) {
345
+ throw new TypeError('Capability chain is invalid; it must consist of strings ' +
346
+ 'of capability IDs except the last capability if it is ' +
347
+ 'delegated, in which case it must be an object with an "id" ' +
348
+ 'property that is a string.');
349
+ }
350
+ // ensure capability ID expresses an absolute URI (i.e., it has `:`)
351
+ const id = typeof entry === 'string' ? entry : entry.id;
352
+ if (!id.includes(':')) {
353
+ throw new Error('Capability chain is invalid; it contains a capability ID ' +
354
+ 'that is not an absolute URI.');
355
+ }
356
+ // ensure last entry in chain matches parent capability
357
+ if (i === lastIndex &&
358
+ 'parentCapability' in currentCapability &&
359
+ currentCapability.parentCapability !== id) {
360
+ throw new Error('Capability chain is invalid; the last entry does not ' +
361
+ 'match the parent capability.');
362
+ }
363
+ if (typeof entry !== 'string') {
364
+ // check zcap data model
365
+ checkCapability({ capability: entry, expectRoot: i === 0 });
366
+ }
367
+ // ensure no cycles in the capability chain
368
+ if (firstPass) {
369
+ // on the first pass, the zcap must not have been seen yet
370
+ if (id === tailCapability.id || dereferencedChainMap.has(id)) {
371
+ throw new Error('The capability chain contains a cycle.');
372
+ }
373
+ // add zcap to the map whether it is only a reference (an ID) or
374
+ // a fully embedded zcap; this will be used to ensure no additional
375
+ // zcaps are added to the chain
376
+ dereferencedChainMap.set(id, entry);
377
+ }
378
+ else {
379
+ // on non-first pass, every ID should already be in the zcap map
380
+ // and they should all be strings, not objects
381
+ const existing = dereferencedChainMap.get(id);
382
+ if (!existing) {
383
+ // the chain is inconsistent across delegated zcaps
384
+ throw new Error('The capability chain is inconsistent.');
385
+ }
386
+ if (id === tailCapability.id || typeof existing === 'object') {
387
+ // the zcap has been deferenced before, there's a cycle
388
+ throw new Error('The capability chain contains a cycle.');
389
+ }
390
+ // only update the zcaps map using a fully embedded zcap
391
+ if (typeof entry !== 'string') {
392
+ dereferencedChainMap.set(id, entry);
393
+ }
394
+ }
395
+ }
396
+ // if the chain has more than the root zcap, loop to process the
397
+ // next chain from the last delegated zcap
398
+ if (currentCapabilityChain.length > 1) {
399
+ // next chain must be 1 shorter than the current one
400
+ requiredLength--;
401
+ const nextCapability = currentCapabilityChain[currentCapabilityChain.length - 1];
402
+ // the last entry of a chain longer than one is always an embedded zcap
403
+ if (typeof nextCapability !== 'object') {
404
+ throw new TypeError('The capability chain is invalid.');
405
+ }
406
+ currentCapability = nextCapability;
407
+ currentCapabilityChain = getCapabilityChain({
408
+ capability: currentCapability
409
+ });
410
+ }
411
+ else {
412
+ // no more chains to check
413
+ break;
414
+ }
415
+ firstPass = false;
416
+ }
417
+ // dereference root zcap via provided trusted `getRootCapability` function
418
+ if (capabilityChain.length > 0) {
419
+ const [id] = capabilityChain;
420
+ if (typeof id !== 'string') {
421
+ throw new TypeError('The capability chain is invalid.');
422
+ }
423
+ const { rootCapability } = await getRootCapability({ id });
424
+ checkCapability({ capability: rootCapability, expectRoot: true });
425
+ if (rootCapability.id !== id) {
426
+ throw new Error(`Dereferenced root capability ID "${rootCapability.id}" does not ` +
427
+ `match reference ID "${id}" from capability chain.`);
428
+ }
429
+ dereferencedChainMap.set(id, rootCapability);
430
+ }
431
+ // include `capability` in dereferenced map
432
+ dereferencedChainMap.set(tailCapability.id, tailCapability);
433
+ const dereferencedChain = [...dereferencedChainMap.values()];
434
+ return { dereferencedChain };
435
+ }
436
+ /**
437
+ * Asserts that a proof carries the required zcap JSON-LD context. The context
438
+ * may appear anywhere in the proof's `@context` array (it is protected
439
+ * regardless of position).
440
+ *
441
+ * @param options - The options.
442
+ * @param options.proof - The proof to check; its `@context` must be, or
443
+ * include, the zcap context URL.
444
+ *
445
+ * @throws {Error} If the zcap context is missing from the proof.
446
+ */
447
+ export function checkProofContext({ proof }) {
448
+ // zcap context can appear anywhere in the array as it *is* protected
449
+ const { '@context': ctx } = proof;
450
+ if (!((Array.isArray(ctx) && ctx.includes(ZCAP_CONTEXT_URL)) ||
451
+ ctx === ZCAP_CONTEXT_URL)) {
452
+ throw new Error(`Missing required capability proof context ("${ZCAP_CONTEXT_URL}").`);
453
+ }
454
+ }
455
+ /**
456
+ * Determines whether a child capability's `allowedAction` is valid, i.e., no
457
+ * less restrictive than its parent's. If the parent does not restrict actions
458
+ * (its `allowedAction` is absent), any child action is allowed.
459
+ *
460
+ * @param options - The options.
461
+ * @param options.allowedAction - The child capability's allowed action(s).
462
+ * @param options.parentAllowedAction - The parent capability's allowed
463
+ * action(s).
464
+ *
465
+ * @returns `true` if the child's allowed action(s) are valid.
466
+ */
467
+ export function hasValidAllowedAction({ allowedAction, parentAllowedAction }) {
468
+ // if the parent's `allowedAction` is `undefined`, then any more restrictive
469
+ // action is allowed in the child
470
+ if (!parentAllowedAction) {
471
+ return true;
472
+ }
473
+ if (Array.isArray(parentAllowedAction)) {
474
+ // parent's `allowedAction` must include every one from child's
475
+ if (Array.isArray(allowedAction)) {
476
+ return allowedAction.every(a => parentAllowedAction.includes(a));
477
+ }
478
+ return (allowedAction !== undefined && parentAllowedAction.includes(allowedAction));
479
+ }
480
+ // require exact match
481
+ return parentAllowedAction === allowedAction;
482
+ }
483
+ /**
484
+ * Validates the data model of a capability (root or delegated), throwing if it
485
+ * is malformed or if its root/delegated kind does not match `expectRoot`.
486
+ *
487
+ * Checks include: required `@context`, absolute-URI `id` and
488
+ * `invocationTarget`, `allowedAction` shape, and (for delegated zcaps) a valid
489
+ * `parentCapability`, a `capabilityDelegation` proof with a valid `created`
490
+ * date, and a valid `expires` date. Root zcaps must not carry `expires`.
491
+ *
492
+ * @param options - The options.
493
+ * @param options.capability - The capability to check.
494
+ * @param options.expectRoot - `true` if the capability is expected to be a root
495
+ * zcap, `false` if it is expected to be delegated.
496
+ *
497
+ * @throws {Error} If the capability is invalid or of an unexpected kind.
498
+ */
499
+ export function checkCapability({ capability, expectRoot }) {
500
+ const context = capability['@context'];
501
+ const { id, invocationTarget } = capability;
502
+ const parentCapability = 'parentCapability' in capability ? capability.parentCapability : undefined;
503
+ // read `allowedAction`/`expires` from the actual capability regardless of
504
+ // kind: a root zcap must be rejected if it carries `expires`, and its
505
+ // `allowedAction` shape must still be validated.
506
+ const { allowedAction, expires } = capability;
507
+ const isRoot = parentCapability === undefined;
508
+ if (isRoot) {
509
+ if (context !== ZCAP_CONTEXT_URL) {
510
+ throw new Error('Root capability must have an "@context" value of ' +
511
+ `"${ZCAP_CONTEXT_URL}".`);
512
+ }
513
+ if (expires !== undefined) {
514
+ throw new Error('Root capability must not have an "expires" field.');
515
+ }
516
+ }
517
+ else {
518
+ if (!(Array.isArray(context) && context[0] === ZCAP_CONTEXT_URL)) {
519
+ throw new Error('Delegated capability must have an "@context" array ' +
520
+ `with "${ZCAP_CONTEXT_URL}" in its first position.`);
521
+ }
522
+ if (!(typeof parentCapability === 'string' && parentCapability.includes(':'))) {
523
+ throw new Error('Delegated capability must have a "parentCapability" with a string ' +
524
+ 'value that expresses an absolute URI.');
525
+ }
526
+ const [proof] = getDelegationProofs({ capability });
527
+ if (!proof) {
528
+ throw new Error('Delegated capability must have a "proof".');
529
+ }
530
+ if (isNaN(Date.parse(proof.created))) {
531
+ throw new Error('Delegated capability must have a valid proof "created" date.');
532
+ }
533
+ if (expires === undefined || isNaN(Date.parse(expires))) {
534
+ throw new Error('Delegated capability must have a valid expires date.');
535
+ }
536
+ }
537
+ if (!(typeof id === 'string' && id.includes(':'))) {
538
+ throw new Error('Capability must have an "id" with a string value that expresses an ' +
539
+ 'absolute URI.');
540
+ }
541
+ if (!(typeof invocationTarget === 'string' && invocationTarget.includes(':'))) {
542
+ throw new Error('Capability must have an "invocationTarget" with a string value that ' +
543
+ 'expresses an absolute URI.');
544
+ }
545
+ if (allowedAction !== undefined &&
546
+ !(typeof allowedAction === 'string' ||
547
+ (Array.isArray(allowedAction) && allowedAction.length > 0))) {
548
+ throw new Error('If present on a capability, "allowedAction" must be a string or a ' +
549
+ 'non-empty array.');
550
+ }
551
+ if (isRoot !== expectRoot) {
552
+ if (expectRoot) {
553
+ throw new Error(`Expected capability "${capability.id}" to be root ` +
554
+ 'but it is delegated.');
555
+ }
556
+ throw new Error(`Expected capability "${capability.id}" to be delegated but it is root.`);
557
+ }
558
+ }
559
+ /**
560
+ * Compares two timestamps, allowing for a maximum clock skew. Times within
561
+ * `maxClockSkew` of each other are treated as equal.
562
+ *
563
+ * @param options - The options.
564
+ * @param options.t1 - The first time, in milliseconds since the epoch.
565
+ * @param options.t2 - The second time, in milliseconds since the epoch.
566
+ * @param options.maxClockSkew - The maximum allowed clock skew, in seconds.
567
+ *
568
+ * @returns `0` if equal within the skew, `-1` if `t1 < t2`, otherwise `1`.
569
+ */
570
+ export function compareTime({ t1, t2, maxClockSkew }) {
571
+ // `maxClockSkew` is in seconds, so transform to milliseconds
572
+ if (Math.abs(t1 - t2) < maxClockSkew * 1000) {
573
+ // times are equal within the max clock skew
574
+ return 0;
575
+ }
576
+ return t1 < t2 ? -1 : 1;
577
+ }
578
+ /**
579
+ * Creates an `Error` carrying a structured `details` object.
580
+ *
581
+ * @param message - The error message.
582
+ * @param details - The structured details to attach.
583
+ *
584
+ * @returns The error with `details` set.
585
+ */
586
+ export function createDetailedError(message, details) {
587
+ const error = new Error(message);
588
+ error.details = details;
589
+ return error;
590
+ }
591
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,gBAAgB,CAAA;AAavB;;;;;;;;;GASG;AACH,MAAM,UAAU,oBAAoB,CAAC,EACnC,UAAU,EACV,gBAAgB,EAIjB;IACC,OAAO;QACL,UAAU,EAAE,gBAAgB;QAC5B,EAAE,EAAE,GAAG,gBAAgB,GAAG,kBAAkB,CAAC,gBAAgB,CAAC,EAAE;QAChE,UAAU;QACV,gBAAgB;KACjB,CAAA;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,EAC7B,UAAU,EAGX;IACC,MAAM,EAAE,UAAU,EAAE,GAAG,UAAU,CAAA;IACjC,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAA;IACrD,CAAC;IACD,OAAO,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAA;AAC9D,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,YAAY,CAAC,EAC3B,UAAU,EACV,kBAAkB,EAInB;IACC,MAAM,WAAW,GAAG,cAAc,CAAC,EAAE,UAAU,EAAE,CAAC,CAAA;IAClD,OAAO,CACL,WAAW,CAAC,QAAQ,CAAC,kBAAkB,CAAC,UAAoB,CAAC;QAC7D,WAAW,CAAC,QAAQ,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAC5C,CAAA;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAAC,EAChC,UAAU,EAGX;IACC,IAAI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