@interop/zcap 10.1.0 → 11.0.1

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 +376 -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 -673
  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 -308
  48. package/types/lib/utils.d.ts.map +0 -1
@@ -0,0 +1,151 @@
1
+ import { CapabilityProofPurpose, type CapabilityDelegationConstructor } from './CapabilityProofPurpose.js';
2
+ import type { IProofDescription } from '@interop/jsonld-signatures';
3
+ import type { IDelegatedZcap, IZcap } from '@interop/data-integrity-core/zcap';
4
+ import type { CapabilityInvocationOptions, CapabilityMeta, CapabilityValidateResult, ValidateOptions } from './types.js';
5
+ /**
6
+ * The proof purpose for *invoking* an authorization capability (zcap).
7
+ *
8
+ * Instantiated in one of two mutually exclusive modes:
9
+ * - **Create-proof mode** — pass `{capability, capabilityAction,
10
+ * invocationTarget}`.
11
+ * - **Verify-proof mode** — pass `{expectedAction, expectedTarget,
12
+ * expectedRootCapability, suite, ...}`.
13
+ *
14
+ * Passing parameters from both modes together throws.
15
+ */
16
+ export declare class CapabilityInvocation extends CapabilityProofPurpose {
17
+ capability?: string | IDelegatedZcap;
18
+ capabilityAction?: string;
19
+ invocationTarget?: string;
20
+ expectedAction?: string;
21
+ expectedTarget?: string | string[];
22
+ /**
23
+ * @param options - The options.
24
+ * @param options.capability - The capability to add/reference in a created
25
+ * proof. A root zcap MUST be passed as its ID string; a delegated zcap must
26
+ * be passed as the full object.
27
+ * @param options.capabilityAction - The capability action that is to be added
28
+ * to a proof.
29
+ * @param options.invocationTarget - The invocation target to use; this is
30
+ * required and can be used to attenuate the capability's invocation target
31
+ * if the verifier supports target attenuation.
32
+ * @param options.allowTargetAttenuation - Allow the invocationTarget of a
33
+ * delegation chain to be increasingly restrictive based on a hierarchical
34
+ * RESTful URL structure.
35
+ * @param options.controller - The description of the controller, if it is not
36
+ * to be dereferenced via a `documentLoader`.
37
+ * @param options.date - Used during proof verification as the expected date
38
+ * for the creation of the proof (within a maximum timestamp delta) and for
39
+ * checking to see if a capability has expired; if not passed the current
40
+ * date will be used.
41
+ * @param options.expectedAction - The capability action that is expected when
42
+ * validating a proof.
43
+ * @param options.expectedRootCapability - The expected root capability for
44
+ * the delegation chain (a single root capability ID string, or an array of
45
+ * acceptable root capability ID strings).
46
+ * @param options.expectedTarget - The target(s) we expect a capability to
47
+ * apply to (absolute URI, or array of URIs).
48
+ * @param options.inspectCapabilityChain - An async function that can be used
49
+ * to check for revocations related to any of verified capabilities.
50
+ * @param options.maxChainLength - The maximum length of the capability
51
+ * delegation chain.
52
+ * @param options.maxClockSkew - A maximum number of seconds that clocks may
53
+ * be skewed when checking capability expiration date-times against `date`
54
+ * and when comparing invocation proof creation time against delegation
55
+ * proof creation time.
56
+ * @param options.maxDelegationTtl - The maximum milliseconds to live for a
57
+ * delegated zcap as measured by the time difference between `expires` and
58
+ * `created` on the delegation proof.
59
+ * @param options.maxTimestampDelta - A maximum number of seconds that
60
+ * "created" date on the capability invocation proof can deviate from
61
+ * `date`, defaults to `Infinity`.
62
+ * @param options.suite - The jsonld-signature suite(s) to use to verify the
63
+ * capability chain. Required only in verify-proof mode; unused (and
64
+ * omitted) when creating an invocation proof.
65
+ */
66
+ constructor({ capability, capabilityAction, invocationTarget, allowTargetAttenuation, controller, date, expectedAction, expectedRootCapability, expectedTarget, inspectCapabilityChain, maxChainLength, maxClockSkew, maxDelegationTtl, maxTimestampDelta, suite }?: CapabilityInvocationOptions);
67
+ /**
68
+ * Adds the capability invocation terms (`capability`, `invocationTarget`,
69
+ * `capabilityAction`, `proofPurpose`) to a proof being created. Used in
70
+ * create-proof mode.
71
+ *
72
+ * @param proof - The proof under construction.
73
+ *
74
+ * @returns Resolves to the updated proof.
75
+ */
76
+ update(proof: IProofDescription): Promise<IProofDescription>;
77
+ /**
78
+ * Determines whether the given proof matches this proof purpose, i.e., it has
79
+ * the zcap context, references a capability, and its `capabilityAction` and
80
+ * `invocationTarget` match `expectedAction` and `expectedTarget`. Used in
81
+ * verify-proof mode.
82
+ *
83
+ * @param proof - The proof to test.
84
+ * @param options - The options.
85
+ * @param options.document - The document the proof is attached to.
86
+ * @param options.documentLoader - A configured document loader.
87
+ *
88
+ * @returns Resolves to `true` if the proof matches.
89
+ */
90
+ match(proof: IProofDescription, { document, documentLoader }: ValidateOptions): Promise<boolean>;
91
+ /** @returns The `CapabilityDelegation` class. */
92
+ _getCapabilityDelegationClass(): CapabilityDelegationConstructor;
93
+ /**
94
+ * Resolves the invoked (tail) capability from the invocation proof.
95
+ *
96
+ * @param options - The options.
97
+ * @param options.proof - The capability invocation proof; its `capability` is
98
+ * the invoked capability (a root zcap ID string or a delegated zcap
99
+ * object).
100
+ *
101
+ * @returns The invoked capability.
102
+ */
103
+ _getTailCapability({ proof }: {
104
+ document?: object;
105
+ proof: IProofDescription;
106
+ }): {
107
+ capability: string | IZcap;
108
+ };
109
+ /**
110
+ * Runs invocation-specific checks before chain verification: that
111
+ * `capabilityAction` is allowed and matches `expectedAction`, that the
112
+ * proof's `invocationTarget` matches the capability target (honoring target
113
+ * attenuation) and an `expectedTarget`, and that a delegated capability is
114
+ * not invoked before its delegation proof's `created` date.
115
+ *
116
+ * @param options - The options.
117
+ * @param options.dereferencedChain - The dereferenced chain (root to tail).
118
+ * @param options.proof - The capability invocation proof.
119
+ *
120
+ * @returns Resolves with an empty `capabilityChainMeta` (the tail's
121
+ * delegation proof is verified later by `_verifyCapabilityChain`).
122
+ */
123
+ _runChecksBeforeChainVerification({ dereferencedChain, proof }: {
124
+ dereferencedChain: IZcap[];
125
+ proof: IProofDescription;
126
+ validateOptions: ValidateOptions;
127
+ }): Promise<{
128
+ capabilityChainMeta: CapabilityMeta[];
129
+ }>;
130
+ /**
131
+ * Runs invocation-specific checks after chain verification: that the invoking
132
+ * verification method (or its controller) is the capability controller, that
133
+ * a delegated capability has not expired, and the base proof validation. Sets
134
+ * `result.invoker` to the proof controller.
135
+ *
136
+ * @param options - The options.
137
+ * @param options.dereferencedChain - The dereferenced chain (root to tail).
138
+ * @param options.proof - The capability invocation proof.
139
+ * @param options.validateOptions - The validation options passed through from
140
+ * `jsigs` (including `verificationMethod`).
141
+ *
142
+ * @returns Resolves to the proof validation result with an added `invoker`.
143
+ */
144
+ _runChecksAfterChainVerification({ dereferencedChain, proof, validateOptions }: {
145
+ capabilityChainMeta: CapabilityMeta[];
146
+ dereferencedChain: IZcap[];
147
+ proof: IProofDescription;
148
+ validateOptions: ValidateOptions;
149
+ }): Promise<CapabilityValidateResult>;
150
+ }
151
+ //# sourceMappingURL=CapabilityInvocation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CapabilityInvocation.d.ts","sourceRoot":"","sources":["../src/CapabilityInvocation.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,sBAAsB,EACtB,KAAK,+BAA+B,EACrC,MAAM,6BAA6B,CAAA;AACpC,OAAO,KAAK,EACV,iBAAiB,EAElB,MAAM,4BAA4B,CAAA;AACnC,OAAO,KAAK,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAA;AAC9E,OAAO,KAAK,EACV,2BAA2B,EAC3B,cAAc,EACd,wBAAwB,EACxB,eAAe,EAChB,MAAM,YAAY,CAAA;AAEnB;;;;;;;;;;GAUG;AACH,qBAAa,oBAAqB,SAAQ,sBAAsB;IAC9D,UAAU,CAAC,EAAE,MAAM,GAAG,cAAc,CAAA;IACpC,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,cAAc,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;IAElC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2CG;gBACS,EAEV,UAAU,EACV,gBAAgB,EAChB,gBAAgB,E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) 2018-2024 Digital Bazaar, Inc. All rights reserved.
3
+ */
4
+ import * as utils from './utils.js';
5
+ import { CapabilityDelegation } from './CapabilityDelegation.js';
6
+ import { CapabilityProofPurpose } from './CapabilityProofPurpose.js';
7
+ /**
8
+ * The proof purpose for *invoking* an authorization capability (zcap).
9
+ *
10
+ * Instantiated in one of two mutually exclusive modes:
11
+ * - **Create-proof mode** — pass `{capability, capabilityAction,
12
+ * invocationTarget}`.
13
+ * - **Verify-proof mode** — pass `{expectedAction, expectedTarget,
14
+ * expectedRootCapability, suite, ...}`.
15
+ *
16
+ * Passing parameters from both modes together throws.
17
+ */
18
+ export class CapabilityInvocation extends CapabilityProofPurpose {
19
+ capability;
20
+ capabilityAction;
21
+ invocationTarget;
22
+ expectedAction;
23
+ expectedTarget;
24
+ /**
25
+ * @param options - The options.
26
+ * @param options.capability - The capability to add/reference in a created
27
+ * proof. A root zcap MUST be passed as its ID string; a delegated zcap must
28
+ * be passed as the full object.
29
+ * @param options.capabilityAction - The capability action that is to be added
30
+ * to a proof.
31
+ * @param options.invocationTarget - The invocation target to use; this is
32
+ * required and can be used to attenuate the capability's invocation target
33
+ * if the verifier supports target attenuation.
34
+ * @param options.allowTargetAttenuation - Allow the invocationTarget of a
35
+ * delegation chain to be increasingly restrictive based on a hierarchical
36
+ * RESTful URL structure.
37
+ * @param options.controller - The description of the controller, if it is not
38
+ * to be dereferenced via a `documentLoader`.
39
+ * @param options.date - Used during proof verification as the expected date
40
+ * for the creation of the proof (within a maximum timestamp delta) and for
41
+ * checking to see if a capability has expired; if not passed the current
42
+ * date will be used.
43
+ * @param options.expectedAction - The capability action that is expected when
44
+ * validating a proof.
45
+ * @param options.expectedRootCapability - The expected root capability for
46
+ * the delegation chain (a single root capability ID string, or an array of
47
+ * acceptable root capability ID strings).
48
+ * @param options.expectedTarget - The target(s) we expect a capability to
49
+ * apply to (absolute URI, or array of URIs).
50
+ * @param options.inspectCapabilityChain - An async function that can be used
51
+ * to check for revocations related to any of verified capabilities.
52
+ * @param options.maxChainLength - The maximum length of the capability
53
+ * delegation chain.
54
+ * @param options.maxClockSkew - A maximum number of seconds that clocks may
55
+ * be skewed when checking capability expiration date-times against `date`
56
+ * and when comparing invocation proof creation time against delegation
57
+ * proof creation time.
58
+ * @param options.maxDelegationTtl - The maximum milliseconds to live for a
59
+ * delegated zcap as measured by the time difference between `expires` and
60
+ * `created` on the delegation proof.
61
+ * @param options.maxTimestampDelta - A maximum number of seconds that
62
+ * "created" date on the capability invocation proof can deviate from
63
+ * `date`, defaults to `Infinity`.
64
+ * @param options.suite - The jsonld-signature suite(s) to use to verify the
65
+ * capability chain. Required only in verify-proof mode; unused (and
66
+ * omitted) when creating an invocation proof.
67
+ */
68
+ constructor({
69
+ // proof creation params
70
+ capability, capabilityAction, invocationTarget,
71
+ // proof verification params
72
+ allowTargetAttenuation, controller, date, expectedAction, expectedRootCapability, expectedTarget, inspectCapabilityChain, maxChainLength, maxClockSkew, maxDelegationTtl, maxTimestampDelta, suite } = {}) {
73
+ // parameters used to create a proof
74
+ const hasCreateProofParams = capability || capabilityAction || invocationTarget;
75
+ // params used to verify a proof
76
+ const hasVerifyProofParams = controller ||
77
+ date ||
78
+ expectedAction ||
79
+ expectedRootCapability ||
80
+ expectedTarget ||
81
+ inspectCapabilityChain ||
82
+ suite;
83
+ if (hasCreateProofParams && hasVerifyProofParams) {
84
+ // cannot provide both create and verify params
85
+ throw new Error('Parameters for both creating and verifying a proof must not be ' +
86
+ 'provided together.');
87
+ }
88
+ super({
89
+ allowTargetAttenuation,
90
+ controller,
91
+ date,
92
+ expectedRootCapability,
93
+ inspectCapabilityChain,
94
+ maxChainLength,
95
+ maxClockSkew,
96
+ maxDelegationTtl,
97
+ maxTimestampDelta,
98
+ suite,
99
+ term: 'capabilityInvocation'
100
+ });
101
+ // validate `CapabilityInvocation` specific params, the base class will
102
+ // have already handled validating common ones...
103
+ // use negative conditional to cover case where neither create nor
104
+ // verify params were provided and default to proof creation case to
105
+ // avoid creating bad proofs
106
+ if (!hasVerifyProofParams) {
107
+ if (typeof capability === 'object') {
108
+ // root capabilities MUST be passed as strings
109
+ if (!(capability && capability.parentCapability)) {
110
+ throw new Error('"capability" must be a string if it is a root capability.');
111
+ }
112
+ }
113
+ else if (typeof capability !== 'string') {
114
+ throw new TypeError('"capability" must be a string or object.');
115
+ }
116
+ if (typeof capabilityAction !== 'string') {
117
+ throw new TypeError('"capabilityAction" must be a string.');
118
+ }
119
+ if (!(typeof invocationTarget === 'string' && invocationTarget.includes(':'))) {
120
+ throw new TypeError('"invocationTarget" must be a string that expresses an absolute URI.');
121
+ }
122
+ this.capability = capability;
123
+ this.capabilityAction = capabilityAction;
124
+ this.invocationTarget = invocationTarget;
125
+ }
126
+ else {
127
+ if (typeof expectedAction !== 'string') {
128
+ throw new TypeError('"expectedAction" must be a string.');
129
+ }
130
+ if (!(typeof expectedTarget === 'string' || Array.isArray(expectedTarget))) {
131
+ throw new TypeError('"expectedTarget" must be a string or array.');
132
+ }
133
+ // expected target values must be absolute URIs
134
+ const expectedTargets = Array.isArray(expectedTarget)
135
+ ? expectedTarget
136
+ : [expectedTarget];
137
+ for (const et of expectedTargets) {
138
+ if (!(typeof et === 'string' && et.includes(':'))) {
139
+ throw new Error('"expectedTargets" values must be absolute URI strings.');
140
+ }
141
+ }
142
+ this.expectedTarget = expectedTarget;
143
+ this.expectedAction = expectedAction;
144
+ }
145
+ }
146
+ /**
147
+ * Adds the capability invocation terms (`capability`, `invocationTarget`,
148
+ * `capabilityAction`, `proofPurpose`) to a proof being created. Used in
149
+ * create-proof mode.
150
+ *
151
+ * @param proof - The proof under construction.
152
+ *
153
+ * @returns Resolves to the updated proof.
154
+ */
155
+ async update(proof) {
156
+ const { capability, capabilityAction, invocationTarget } = this;
157
+ proof.proofPurpose = this.term;
158
+ proof.capability = capability;
159
+ proof.invocationTarget = invocationTarget;
160
+ proof.capabilityAction = capabilityAction;
161
+ return proof;
162
+ }
163
+ /**
164
+ * Determines whether the given proof matches this proof purpose, i.e., it has
165
+ * the zcap context, references a capability, and its `capabilityAction` and
166
+ * `invocationTarget` match `expectedAction` and `expectedTarget`. Used in
167
+ * verify-proof mode.
168
+ *
169
+ * @param proof - The proof to test.
170
+ * @param options - The options.
171
+ * @param options.document - The document the proof is attached to.
172
+ * @param options.documentLoader - A configured document loader.
173
+ *
174
+ * @returns Resolves to `true` if the proof matches.
175
+ */
176
+ async match(proof, { document, documentLoader }) {
177
+ const { expectedAction, expectedTarget } = this;
178
+ try {
179
+ // check the `proof` context before using its terms
180
+ utils.checkProofContext({ proof });
181
+ }
182
+ catch {
183
+ // context does not match, so proof does not match
184
+ return false;
185
+ }
186
+ if (!proof.capability) {
187
+ // capability not in the proof, not a match
188
+ return false;
189
+ }
190
+ // ensure basic purpose and expected action match the proof
191
+ if (!((await super.match(proof, { document, documentLoader })) &&
192
+ expectedAction === proof.capabilityAction)) {
193
+ return false;
194
+ }
195
+ // ensure the proof's declared invocation target matches an expected one
196
+ if (Array.isArray(expectedTarget)) {
197
+ return expectedTarget.includes(proof.invocationTarget);
198
+ }
199
+ return expectedTarget === proof.invocationTarget;
200
+ }
201
+ /** @returns The `CapabilityDelegation` class. */
202
+ _getCapabilityDelegationClass() {
203
+ return CapabilityDelegation;
204
+ }
205
+ /**
206
+ * Resolves the invoked (tail) capability from the invocation proof.
207
+ *
208
+ * @param options - The options.
209
+ * @param options.proof - The capability invocation proof; its `capability` is
210
+ * the invoked capability (a root zcap ID string or a delegated zcap
211
+ * object).
212
+ *
213
+ * @returns The invoked capability.
214
+ */
215
+ _getTailCapability({ proof }) {
216
+ return { capability: proof.capability };
217
+ }
218
+ /**
219
+ * Runs invocation-specific checks before chain verification: that
220
+ * `capabilityAction` is allowed and matches `expectedAction`, that the
221
+ * proof's `invocationTarget` matches the capability target (honoring target
222
+ * attenuation) and an `expectedTarget`, and that a delegated capability is
223
+ * not invoked before its delegation proof's `created` date.
224
+ *
225
+ * @param options - The options.
226
+ * @param options.dereferencedChain - The dereferenced chain (root to tail).
227
+ * @param options.proof - The capability invocation proof.
228
+ *
229
+ * @returns Resolves with an empty `capabilityChainMeta` (the tail's
230
+ * delegation proof is verified later by `_verifyCapabilityChain`).
231
+ */
232
+ async _runChecksBeforeChainVerification({ dereferencedChain, proof }) {
233
+ const { allowTargetAttenuation, expectedAction, expectedTarget } = this;
234
+ /* 1. Ensure that `capabilityAction` is an allowed action and that
235
+ it matches `expectedAction`. Note that if it doesn't match and `match`
236
+ was called to gate calling `validate`, then this code will not execute.
237
+ However, if `validate` is called directly, this check MUST run here.
238
+
239
+ If the capability restricts the actions via `allowedAction` then
240
+ `capabilityAction` must be in its set. */
241
+ const capability = dereferencedChain[dereferencedChain.length - 1];
242
+ const capabilityAction = proof.capabilityAction;
243
+ const allowedActions = utils.getAllowedActions({ capability });
244
+ if (allowedActions.length > 0 &&
245
+ !allowedActions.includes(capabilityAction)) {
246
+ throw new Error(`Capability action "${capabilityAction}" is not allowed by the ` +
247
+ 'capability; allowed actions are: ' +
248
+ allowedActions.map(x => `"${x}"`).join(', '));
249
+ }
250
+ if (capabilityAction !== expectedAction) {
251
+ throw new Error(`Capability action "${capabilityAction}" does not match the ` +
252
+ `expected action of "${expectedAction}".`);
253
+ }
254
+ /* 2. Ensure `expectedTarget` is as expected. The invocation target
255
+ will also be checked to ensure it hasn't changed from previous zcaps
256
+ in the chain (unless attenuation is permitted) later. */
257
+ /* 3. Verify the invocation target in the proof is as expected. The
258
+ `invocationTarget` specified in the capability invocation proof must
259
+ match exactly (or follow acceptable target attenuation rules) the
260
+ `invocationTarget` specified in the invoked capability. */
261
+ const capabilityTarget = utils.getTarget({ capability });
262
+ const { invocationTarget } = proof;
263
+ if (!(typeof invocationTarget === 'string' && invocationTarget.includes(':'))) {
264
+ throw new TypeError(`Invocation target (${invocationTarget}) must be a string that ` +
265
+ 'expresses an absolute URI.');
266
+ }
267
+ if (!utils.isValidTarget({
268
+ invocationTarget,
269
+ baseInvocationTarget: capabilityTarget,
270
+ allowTargetAttenuation
271
+ })) {
272
+ throw new Error(`Invocation target (${invocationTarget}) does not match ` +
273
+ `capability target (${capabilityTarget}).`);
274
+ }
275
+ /* 4. Verify the invocation target is an expected target. Prior to this
276
+ step we ensured that the invocation target used matched th capability
277
+ that was invoked, but this check ensures that the invocation target used
278
+ matches the endpoint (the `expectedTarget`) where the capability was
279
+ actually invoked. */
280
+ if (!((Array.isArray(expectedTarget) &&
281
+ expectedTarget.includes(invocationTarget)) ||
282
+ (typeof expectedTarget === 'string' &&
283
+ invocationTarget === expectedTarget))) {
284
+ throw new Error(`Expected target (${String(expectedTarget)}) does not match ` +
285
+ `invocation target (${invocationTarget}).`);
286
+ }
287
+ /* 5. If capability is delegated (not root), then ensure the capability
288
+ invocation proof `created` date is not before the capability delegation
289
+ proof creation date. */
290
+ if ('parentCapability' in capability) {
291
+ const invoked = Date.parse(proof.created);
292
+ const [delegationProof] = utils.getDelegationProofs({ capability });
293
+ const delegated = Date.parse(delegationProof.created);
294
+ const { maxClockSkew = 300 } = this;
295
+ // use `utils.compareTime` to allow for clock drift from the machine
296
+ // that created the delegation proof and the machine that created
297
+ // the invocation proof
298
+ if (utils.compareTime({ t1: invoked, t2: delegated, maxClockSkew }) < 0) {
299
+ throw new Error('A delegated capability must not be invoked before the "created" ' +
300
+ 'date in its delegation proof.');
301
+ }
302
+ }
303
+ // return no capability delegation verify results yet; the tail's
304
+ // capability delegation proof must be verified via
305
+ // `_verifyCapabilityChain`
306
+ return { capabilityChainMeta: [] };
307
+ }
308
+ /**
309
+ * Runs invocation-specific checks after chain verification: that the invoking
310
+ * verification method (or its controller) is the capability controller, that
311
+ * a delegated capability has not expired, and the base proof validation. Sets
312
+ * `result.invoker` to the proof controller.
313
+ *
314
+ * @param options - The options.
315
+ * @param options.dereferencedChain - The dereferenced chain (root to tail).
316
+ * @param options.proof - The capability invocation proof.
317
+ * @param options.validateOptions - The validation options passed through from
318
+ * `jsigs` (including `verificationMethod`).
319
+ *
320
+ * @returns Resolves to the proof validation result with an added `invoker`.
321
+ */
322
+ async _runChecksAfterChainVerification({ dereferencedChain, proof, validateOptions }) {
323
+ /* Verify the controller of the capability. The zcap controller must
324
+ match the invoking verification method (or its controller). */
325
+ const capability = dereferencedChain[dereferencedChain.length - 1];
326
+ const { verificationMethod } = validateOptions;
327
+ if (!utils.isController({
328
+ capability,
329
+ verificationMethod: verificationMethod
330
+ })) {
331
+ throw utils.createDetailedError('The capability controller does not match the verification method ' +
332
+ '(or its controller) used to invoke.', { capability, verificationMethod });
333
+ }
334
+ // if capability is delegated, verify that it has not expired
335
+ if ('parentCapability' in capability) {
336
+ // verify expiration dates
337
+ // expires date has been previously validated, so just parse it
338
+ const currentCapabilityExpirationTime = Date.parse(capability.expires);
339
+ // use `utils.compareTime` to allow for allow for clock drift because
340
+ // we are comparing against `currentDate`
341
+ const { date, maxClockSkew = 300 } = this;
342
+ const currentDate = (date && new Date(date)) || new Date();
343
+ if (utils.compareTime({
344
+ t1: currentDate.getTime(),
345
+ t2: currentCapabilityExpirationTime,
346
+ maxClockSkew
347
+ }) > 0) {
348
+ throw new Error('The invoked capability has expired.');
349
+ }
350
+ }
351
+ // run base level validation checks
352
+ const result = await this._runBaseProofValidation({
353
+ proof,
354
+ validateOptions
355
+ });
356
+ if (!result.valid) {
357
+ throw result.error;
358
+ }
359
+ // the controller of the verification method from the proof is the
360
+ // invoker of the capability
361
+ result.invoker = result.controller;
362
+ return result;
363
+ }
364
+ }
365
+ //# sourceMappingURL=CapabilityInvocation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CapabilityInvocation.js","sourceRoot":"","sources":["../src/CapabilityInvocation.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EACL,sBAAsB,EAEvB,MAAM,6BAA6B,CAAA;AAapC;;;;;;;;;;GAUG;AACH,MAAM,OAAO,oBAAqB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