@metamask-previews/permission-controller 4.1.0-preview.3dbf14f

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/CHANGELOG.md +86 -0
  2. package/LICENSE +20 -0
  3. package/README.md +19 -0
  4. package/dist/Caveat.d.ts +183 -0
  5. package/dist/Caveat.d.ts.map +1 -0
  6. package/dist/Caveat.js +57 -0
  7. package/dist/Caveat.js.map +1 -0
  8. package/dist/Permission.d.ts +417 -0
  9. package/dist/Permission.d.ts.map +1 -0
  10. package/dist/Permission.js +66 -0
  11. package/dist/Permission.js.map +1 -0
  12. package/dist/PermissionController.d.ts +897 -0
  13. package/dist/PermissionController.d.ts.map +1 -0
  14. package/dist/PermissionController.js +1287 -0
  15. package/dist/PermissionController.js.map +1 -0
  16. package/dist/SubjectMetadataController.d.ts +110 -0
  17. package/dist/SubjectMetadataController.d.ts.map +1 -0
  18. package/dist/SubjectMetadataController.js +139 -0
  19. package/dist/SubjectMetadataController.js.map +1 -0
  20. package/dist/errors.d.ts +159 -0
  21. package/dist/errors.d.ts.map +1 -0
  22. package/dist/errors.js +196 -0
  23. package/dist/errors.js.map +1 -0
  24. package/dist/index.d.ts +8 -0
  25. package/dist/index.d.ts.map +1 -0
  26. package/dist/index.js +37 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/permission-middleware.d.ts +33 -0
  29. package/dist/permission-middleware.d.ts.map +1 -0
  30. package/dist/permission-middleware.js +64 -0
  31. package/dist/permission-middleware.js.map +1 -0
  32. package/dist/rpc-methods/getPermissions.d.ts +8 -0
  33. package/dist/rpc-methods/getPermissions.d.ts.map +1 -0
  34. package/dist/rpc-methods/getPermissions.js +38 -0
  35. package/dist/rpc-methods/getPermissions.js.map +1 -0
  36. package/dist/rpc-methods/index.d.ts +5 -0
  37. package/dist/rpc-methods/index.d.ts.map +1 -0
  38. package/dist/rpc-methods/index.js +10 -0
  39. package/dist/rpc-methods/index.js.map +1 -0
  40. package/dist/rpc-methods/requestPermissions.d.ts +17 -0
  41. package/dist/rpc-methods/requestPermissions.d.ts.map +1 -0
  42. package/dist/rpc-methods/requestPermissions.js +55 -0
  43. package/dist/rpc-methods/requestPermissions.js.map +1 -0
  44. package/dist/utils.d.ts +39 -0
  45. package/dist/utils.d.ts.map +1 -0
  46. package/dist/utils.js +9 -0
  47. package/dist/utils.js.map +1 -0
  48. package/package.json +62 -0
@@ -0,0 +1,897 @@
1
+ import type { AcceptRequest as AcceptApprovalRequest, AddApprovalRequest, HasApprovalRequest, RejectRequest as RejectApprovalRequest } from '@metamask/approval-controller';
2
+ import type { RestrictedControllerMessenger, ActionConstraint, EventConstraint } from '@metamask/base-controller';
3
+ import { BaseControllerV2 } from '@metamask/base-controller';
4
+ import type { NonEmptyArray } from '@metamask/controller-utils';
5
+ import type { Json } from '@metamask/utils';
6
+ import type { Patch } from 'immer';
7
+ import type { CaveatConstraint, CaveatSpecificationConstraint, CaveatSpecificationMap, ExtractCaveat, ExtractCaveats, ExtractCaveatValue } from './Caveat';
8
+ import type { EndowmentSpecificationConstraint, ExtractAllowedCaveatTypes, OriginString, PermissionConstraint, PermissionSpecificationConstraint, PermissionSpecificationMap, RequestedPermissions, RestrictedMethod, RestrictedMethodParameters, RestrictedMethodSpecificationConstraint, SideEffectHandler, ValidPermission, ValidPermissionSpecification } from './Permission';
9
+ import { getPermissionMiddlewareFactory } from './permission-middleware';
10
+ import type { GetSubjectMetadata } from './SubjectMetadataController';
11
+ /**
12
+ * Metadata associated with {@link PermissionController} subjects.
13
+ */
14
+ export declare type PermissionSubjectMetadata = {
15
+ origin: OriginString;
16
+ };
17
+ /**
18
+ * Metadata associated with permission requests.
19
+ */
20
+ export declare type PermissionsRequestMetadata = PermissionSubjectMetadata & {
21
+ id: string;
22
+ };
23
+ /**
24
+ * Used for prompting the user about a proposed new permission.
25
+ * Includes information about the grantee subject, requested permissions, and
26
+ * any additional information added by the consumer.
27
+ *
28
+ * All properties except `permissions` are passed to any factories found for
29
+ * the requested permissions.
30
+ */
31
+ export declare type PermissionsRequest = {
32
+ metadata: PermissionsRequestMetadata;
33
+ permissions: RequestedPermissions;
34
+ [key: string]: Json;
35
+ };
36
+ export declare type SideEffects = {
37
+ permittedHandlers: Record<string, SideEffectHandler<any, any>>;
38
+ failureHandlers: Record<string, SideEffectHandler<any, any>>;
39
+ };
40
+ /**
41
+ * The name of the {@link PermissionController}.
42
+ */
43
+ declare const controllerName = "PermissionController";
44
+ /**
45
+ * Permissions associated with a {@link PermissionController} subject.
46
+ */
47
+ export declare type SubjectPermissions<Permission extends PermissionConstraint> = Record<Permission['parentCapability'], Permission>;
48
+ /**
49
+ * Permissions and metadata associated with a {@link PermissionController}
50
+ * subject.
51
+ */
52
+ export declare type PermissionSubjectEntry<SubjectPermission extends PermissionConstraint> = {
53
+ origin: SubjectPermission['invoker'];
54
+ permissions: SubjectPermissions<SubjectPermission>;
55
+ };
56
+ /**
57
+ * All subjects of a {@link PermissionController}.
58
+ *
59
+ * @template SubjectPermission - The permissions of the subject.
60
+ */
61
+ export declare type PermissionControllerSubjects<SubjectPermission extends PermissionConstraint> = Record<SubjectPermission['invoker'], PermissionSubjectEntry<SubjectPermission>>;
62
+ /**
63
+ * The state of a {@link PermissionController}.
64
+ *
65
+ * @template Permission - The controller's permission type union.
66
+ */
67
+ export declare type PermissionControllerState<Permission> = Permission extends PermissionConstraint ? {
68
+ subjects: PermissionControllerSubjects<Permission>;
69
+ } : never;
70
+ /**
71
+ * Gets the state of the {@link PermissionController}.
72
+ */
73
+ export declare type GetPermissionControllerState = {
74
+ type: `${typeof controllerName}:getState`;
75
+ handler: () => PermissionControllerState<PermissionConstraint>;
76
+ };
77
+ /**
78
+ * Gets the names of all subjects from the {@link PermissionController}.
79
+ */
80
+ export declare type GetSubjects = {
81
+ type: `${typeof controllerName}:getSubjectNames`;
82
+ handler: () => (keyof PermissionControllerSubjects<PermissionConstraint>)[];
83
+ };
84
+ /**
85
+ * Gets the permissions for specified subject
86
+ */
87
+ export declare type GetPermissions = {
88
+ type: `${typeof controllerName}:getPermissions`;
89
+ handler: GenericPermissionController['getPermissions'];
90
+ };
91
+ /**
92
+ * Checks whether the specified subject has any permissions.
93
+ */
94
+ export declare type HasPermissions = {
95
+ type: `${typeof controllerName}:hasPermissions`;
96
+ handler: GenericPermissionController['hasPermissions'];
97
+ };
98
+ /**
99
+ * Checks whether the specified subject has a specific permission.
100
+ */
101
+ export declare type HasPermission = {
102
+ type: `${typeof controllerName}:hasPermission`;
103
+ handler: GenericPermissionController['hasPermission'];
104
+ };
105
+ /**
106
+ * Directly grants given permissions for a specificed origin without requesting user approval
107
+ */
108
+ export declare type GrantPermissions = {
109
+ type: `${typeof controllerName}:grantPermissions`;
110
+ handler: GenericPermissionController['grantPermissions'];
111
+ };
112
+ /**
113
+ * Requests given permissions for a specified origin
114
+ */
115
+ export declare type RequestPermissions = {
116
+ type: `${typeof controllerName}:requestPermissions`;
117
+ handler: GenericPermissionController['requestPermissions'];
118
+ };
119
+ /**
120
+ * Removes the specified permissions for each origin.
121
+ */
122
+ export declare type RevokePermissions = {
123
+ type: `${typeof controllerName}:revokePermissions`;
124
+ handler: GenericPermissionController['revokePermissions'];
125
+ };
126
+ /**
127
+ * Removes all permissions for a given origin
128
+ */
129
+ export declare type RevokeAllPermissions = {
130
+ type: `${typeof controllerName}:revokeAllPermissions`;
131
+ handler: GenericPermissionController['revokeAllPermissions'];
132
+ };
133
+ /**
134
+ * Revokes all permissions corresponding to the specified target for all subjects.
135
+ * Does nothing if no subjects or no such permission exists.
136
+ */
137
+ export declare type RevokePermissionForAllSubjects = {
138
+ type: `${typeof controllerName}:revokePermissionForAllSubjects`;
139
+ handler: GenericPermissionController['revokePermissionForAllSubjects'];
140
+ };
141
+ /**
142
+ * Updates a caveat value for a specified caveat type belonging to a specific target and origin.
143
+ */
144
+ export declare type UpdateCaveat = {
145
+ type: `${typeof controllerName}:updateCaveat`;
146
+ handler: GenericPermissionController['updateCaveat'];
147
+ };
148
+ /**
149
+ * Clears all permissions from the {@link PermissionController}.
150
+ */
151
+ export declare type ClearPermissions = {
152
+ type: `${typeof controllerName}:clearPermissions`;
153
+ handler: () => void;
154
+ };
155
+ /**
156
+ * Gets the endowments for the given subject and permission.
157
+ */
158
+ export declare type GetEndowments = {
159
+ type: `${typeof controllerName}:getEndowments`;
160
+ handler: GenericPermissionController['getEndowments'];
161
+ };
162
+ /**
163
+ * The {@link ControllerMessenger} actions of the {@link PermissionController}.
164
+ */
165
+ export declare type PermissionControllerActions = ClearPermissions | GetEndowments | GetPermissionControllerState | GetSubjects | GetPermissions | HasPermission | HasPermissions | GrantPermissions | RequestPermissions | RevokeAllPermissions | RevokePermissionForAllSubjects | RevokePermissions | UpdateCaveat;
166
+ /**
167
+ * The generic state change event of the {@link PermissionController}.
168
+ */
169
+ export declare type PermissionControllerStateChange = {
170
+ type: `${typeof controllerName}:stateChange`;
171
+ payload: [PermissionControllerState<PermissionConstraint>, Patch[]];
172
+ };
173
+ /**
174
+ * The {@link ControllerMessenger} events of the {@link PermissionController}.
175
+ *
176
+ * The permission controller only emits its generic state change events.
177
+ * Consumers should use selector subscriptions to subscribe to relevant
178
+ * substate.
179
+ */
180
+ export declare type PermissionControllerEvents = PermissionControllerStateChange;
181
+ /**
182
+ * The external {@link ControllerMessenger} actions available to the
183
+ * {@link PermissionController}.
184
+ */
185
+ declare type AllowedActions = AddApprovalRequest | HasApprovalRequest | AcceptApprovalRequest | RejectApprovalRequest | GetSubjectMetadata;
186
+ /**
187
+ * The messenger of the {@link PermissionController}.
188
+ */
189
+ export declare type PermissionControllerMessenger = RestrictedControllerMessenger<typeof controllerName, PermissionControllerActions | AllowedActions, PermissionControllerEvents, AllowedActions['type'], never>;
190
+ export declare type SideEffectMessenger<Actions extends ActionConstraint, Events extends EventConstraint> = RestrictedControllerMessenger<typeof controllerName, Actions, Events, string, never>;
191
+ /**
192
+ * A generic {@link PermissionController}.
193
+ */
194
+ export declare type GenericPermissionController = PermissionController<PermissionSpecificationConstraint, CaveatSpecificationConstraint>;
195
+ /**
196
+ * Describes the possible results of a {@link CaveatMutator} function.
197
+ */
198
+ export declare enum CaveatMutatorOperation {
199
+ noop = 0,
200
+ updateValue = 1,
201
+ deleteCaveat = 2,
202
+ revokePermission = 3
203
+ }
204
+ /**
205
+ * Given a caveat value, returns a {@link CaveatMutatorOperation} and, optionally,
206
+ * a new caveat value.
207
+ *
208
+ * @see {@link PermissionController.updatePermissionsByCaveat} for more details.
209
+ * @template Caveat - The caveat type for which this mutator is intended.
210
+ * @param caveatValue - The existing value of the caveat being mutated.
211
+ * @returns A tuple of the mutation result and, optionally, the new caveat
212
+ * value.
213
+ */
214
+ export declare type CaveatMutator<TargetCaveat extends CaveatConstraint> = (caveatValue: TargetCaveat['value']) => CaveatMutatorResult;
215
+ declare type CaveatMutatorResult = Readonly<{
216
+ operation: CaveatMutatorOperation.updateValue;
217
+ value: CaveatConstraint['value'];
218
+ }> | Readonly<{
219
+ operation: Exclude<CaveatMutatorOperation, CaveatMutatorOperation.updateValue>;
220
+ }>;
221
+ /**
222
+ * Extracts the permission(s) specified by the given permission and caveat
223
+ * specifications.
224
+ *
225
+ * @template ControllerPermissionSpecification - The permission specification(s)
226
+ * to extract from.
227
+ * @template ControllerCaveatSpecification - The caveat specification(s) to
228
+ * extract from. Necessary because {@link Permission} has a generic parameter
229
+ * that describes the allowed caveats for the permission.
230
+ */
231
+ export declare type ExtractPermission<ControllerPermissionSpecification extends PermissionSpecificationConstraint, ControllerCaveatSpecification extends CaveatSpecificationConstraint> = ControllerPermissionSpecification extends ValidPermissionSpecification<ControllerPermissionSpecification> ? ValidPermission<ControllerPermissionSpecification['targetName'], ExtractCaveats<ControllerCaveatSpecification>> : never;
232
+ /**
233
+ * Extracts the restricted method permission(s) specified by the given
234
+ * permission and caveat specifications.
235
+ *
236
+ * @template ControllerPermissionSpecification - The permission specification(s)
237
+ * to extract from.
238
+ * @template ControllerCaveatSpecification - The caveat specification(s) to
239
+ * extract from. Necessary because {@link Permission} has a generic parameter
240
+ * that describes the allowed caveats for the permission.
241
+ */
242
+ export declare type ExtractRestrictedMethodPermission<ControllerPermissionSpecification extends PermissionSpecificationConstraint, ControllerCaveatSpecification extends CaveatSpecificationConstraint> = ExtractPermission<Extract<ControllerPermissionSpecification, RestrictedMethodSpecificationConstraint>, ControllerCaveatSpecification>;
243
+ /**
244
+ * Extracts the endowment permission(s) specified by the given permission and
245
+ * caveat specifications.
246
+ *
247
+ * @template ControllerPermissionSpecification - The permission specification(s)
248
+ * to extract from.
249
+ * @template ControllerCaveatSpecification - The caveat specification(s) to
250
+ * extract from. Necessary because {@link Permission} has a generic parameter
251
+ * that describes the allowed caveats for the permission.
252
+ */
253
+ export declare type ExtractEndowmentPermission<ControllerPermissionSpecification extends PermissionSpecificationConstraint, ControllerCaveatSpecification extends CaveatSpecificationConstraint> = ExtractPermission<Extract<ControllerPermissionSpecification, EndowmentSpecificationConstraint>, ControllerCaveatSpecification>;
254
+ /**
255
+ * Options for the {@link PermissionController} constructor.
256
+ *
257
+ * @template ControllerPermissionSpecification - A union of the types of all
258
+ * permission specifications available to the controller. Any referenced caveats
259
+ * must be included in the controller's caveat specifications.
260
+ * @template ControllerCaveatSpecification - A union of the types of all
261
+ * caveat specifications available to the controller.
262
+ */
263
+ export declare type PermissionControllerOptions<ControllerPermissionSpecification extends PermissionSpecificationConstraint, ControllerCaveatSpecification extends CaveatSpecificationConstraint> = {
264
+ messenger: PermissionControllerMessenger;
265
+ caveatSpecifications: CaveatSpecificationMap<ControllerCaveatSpecification>;
266
+ permissionSpecifications: PermissionSpecificationMap<ControllerPermissionSpecification>;
267
+ unrestrictedMethods: readonly string[];
268
+ state?: Partial<PermissionControllerState<ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>>>;
269
+ };
270
+ /**
271
+ * The permission controller. See the [Architecture](../ARCHITECTURE.md)
272
+ * document for details.
273
+ *
274
+ * Assumes the existence of an {@link ApprovalController} reachable via the
275
+ * {@link ControllerMessenger}.
276
+ *
277
+ * @template ControllerPermissionSpecification - A union of the types of all
278
+ * permission specifications available to the controller. Any referenced caveats
279
+ * must be included in the controller's caveat specifications.
280
+ * @template ControllerCaveatSpecification - A union of the types of all
281
+ * caveat specifications available to the controller.
282
+ */
283
+ export declare class PermissionController<ControllerPermissionSpecification extends PermissionSpecificationConstraint, ControllerCaveatSpecification extends CaveatSpecificationConstraint> extends BaseControllerV2<typeof controllerName, PermissionControllerState<ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>>, PermissionControllerMessenger> {
284
+ private readonly _caveatSpecifications;
285
+ private readonly _permissionSpecifications;
286
+ private readonly _unrestrictedMethods;
287
+ /**
288
+ * The names of all JSON-RPC methods that will be ignored by the controller.
289
+ *
290
+ * @returns The names of all unrestricted JSON-RPC methods
291
+ */
292
+ get unrestrictedMethods(): ReadonlySet<string>;
293
+ /**
294
+ * Returns a `json-rpc-engine` middleware function factory, so that the rules
295
+ * described by the state of this controller can be applied to incoming
296
+ * JSON-RPC requests.
297
+ *
298
+ * The middleware **must** be added in the correct place in the middleware
299
+ * stack in order for it to work. See the README for an example.
300
+ */
301
+ createPermissionMiddleware: ReturnType<typeof getPermissionMiddlewareFactory>;
302
+ /**
303
+ * Constructs the PermissionController.
304
+ *
305
+ * @param options - Permission controller options.
306
+ * @param options.caveatSpecifications - The specifications of all caveats
307
+ * available to the controller. See {@link CaveatSpecificationMap} and the
308
+ * documentation for more details.
309
+ * @param options.permissionSpecifications - The specifications of all
310
+ * permissions available to the controller. See
311
+ * {@link PermissionSpecificationMap} and the README for more details.
312
+ * @param options.unrestrictedMethods - The callable names of all JSON-RPC
313
+ * methods ignored by the new controller.
314
+ * @param options.messenger - The controller messenger. See
315
+ * {@link BaseControllerV2} for more information.
316
+ * @param options.state - Existing state to hydrate the controller with at
317
+ * initialization.
318
+ */
319
+ constructor(options: PermissionControllerOptions<ControllerPermissionSpecification, ControllerCaveatSpecification>);
320
+ /**
321
+ * Gets a permission specification.
322
+ *
323
+ * @param targetName - The name of the permission specification to get.
324
+ * @returns The permission specification with the specified target name.
325
+ */
326
+ private getPermissionSpecification;
327
+ /**
328
+ * Gets a caveat specification.
329
+ *
330
+ * @param caveatType - The type of the caveat specification to get.
331
+ * @returns The caveat specification with the specified type.
332
+ */
333
+ private getCaveatSpecification;
334
+ /**
335
+ * Constructor helper for validating permission specifications.
336
+ *
337
+ * Throws an error if validation fails.
338
+ *
339
+ * @param permissionSpecifications - The permission specifications passed to
340
+ * this controller's constructor.
341
+ * @param caveatSpecifications - The caveat specifications passed to this
342
+ * controller.
343
+ */
344
+ private validatePermissionSpecifications;
345
+ /**
346
+ * Constructor helper for registering the controller's messaging system
347
+ * actions.
348
+ */
349
+ private registerMessageHandlers;
350
+ /**
351
+ * Clears the state of the controller.
352
+ */
353
+ clearState(): void;
354
+ /**
355
+ * Gets the permission specification corresponding to the given permission
356
+ * type and target name. Throws an error if the target name does not
357
+ * correspond to a permission, or if the specification is not of the
358
+ * given permission type.
359
+ *
360
+ * @template Type - The type of the permission specification to get.
361
+ * @param permissionType - The type of the permission specification to get.
362
+ * @param targetName - The name of the permission whose specification to get.
363
+ * @param requestingOrigin - The origin of the requesting subject, if any.
364
+ * Will be added to any thrown errors.
365
+ * @returns The specification object corresponding to the given type and
366
+ * target name.
367
+ */
368
+ private getTypedPermissionSpecification;
369
+ /**
370
+ * Gets the implementation of the specified restricted method.
371
+ *
372
+ * A JSON-RPC error is thrown if the method does not exist.
373
+ *
374
+ * @see {@link PermissionController.executeRestrictedMethod} and
375
+ * {@link PermissionController.createPermissionMiddleware} for internal usage.
376
+ * @param method - The name of the restricted method.
377
+ * @param origin - The origin associated with the request for the restricted
378
+ * method, if any.
379
+ * @returns The restricted method implementation.
380
+ */
381
+ getRestrictedMethod(method: string, origin?: string): RestrictedMethod<RestrictedMethodParameters, Json>;
382
+ /**
383
+ * Gets a list of all origins of subjects.
384
+ *
385
+ * @returns The origins (i.e. IDs) of all subjects.
386
+ */
387
+ getSubjectNames(): OriginString[];
388
+ /**
389
+ * Gets the permission for the specified target of the subject corresponding
390
+ * to the specified origin.
391
+ *
392
+ * @param origin - The origin of the subject.
393
+ * @param targetName - The method name as invoked by a third party (i.e., not
394
+ * a method key).
395
+ * @returns The permission if it exists, or undefined otherwise.
396
+ */
397
+ getPermission<SubjectPermission extends ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>>(origin: OriginString, targetName: SubjectPermission['parentCapability']): SubjectPermission | undefined;
398
+ /**
399
+ * Gets all permissions for the specified subject, if any.
400
+ *
401
+ * @param origin - The origin of the subject.
402
+ * @returns The permissions of the subject, if any.
403
+ */
404
+ getPermissions(origin: OriginString): SubjectPermissions<ValidPermission<string, ExtractCaveats<ControllerCaveatSpecification>>> | undefined;
405
+ /**
406
+ * Checks whether the subject with the specified origin has the specified
407
+ * permission.
408
+ *
409
+ * @param origin - The origin of the subject.
410
+ * @param target - The target name of the permission.
411
+ * @returns Whether the subject has the permission.
412
+ */
413
+ hasPermission(origin: OriginString, target: ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability']): boolean;
414
+ /**
415
+ * Checks whether the subject with the specified origin has any permissions.
416
+ * Use this if you want to know if a subject "exists".
417
+ *
418
+ * @param origin - The origin of the subject to check.
419
+ * @returns Whether the subject has any permissions.
420
+ */
421
+ hasPermissions(origin: OriginString): boolean;
422
+ /**
423
+ * Revokes all permissions from the specified origin.
424
+ *
425
+ * Throws an error of the origin has no permissions.
426
+ *
427
+ * @param origin - The origin whose permissions to revoke.
428
+ */
429
+ revokeAllPermissions(origin: OriginString): void;
430
+ /**
431
+ * Revokes the specified permission from the subject with the specified
432
+ * origin.
433
+ *
434
+ * Throws an error if the subject or the permission does not exist.
435
+ *
436
+ * @param origin - The origin of the subject whose permission to revoke.
437
+ * @param target - The target name of the permission to revoke.
438
+ */
439
+ revokePermission(origin: OriginString, target: ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability']): void;
440
+ /**
441
+ * Revokes the specified permissions from the specified subjects.
442
+ *
443
+ * Throws an error if any of the subjects or permissions do not exist.
444
+ *
445
+ * @param subjectsAndPermissions - An object mapping subject origins
446
+ * to arrays of permission target names to revoke.
447
+ */
448
+ revokePermissions(subjectsAndPermissions: Record<OriginString, NonEmptyArray<ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability']>>): void;
449
+ /**
450
+ * Revokes all permissions corresponding to the specified target for all subjects.
451
+ * Does nothing if no subjects or no such permission exists.
452
+ *
453
+ * @param target - The name of the target to revoke all permissions for.
454
+ */
455
+ revokePermissionForAllSubjects(target: ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability']): void;
456
+ /**
457
+ * Deletes the permission identified by the given origin and target. If the
458
+ * permission is the single remaining permission of its subject, the subject
459
+ * is also deleted.
460
+ *
461
+ * @param subjects - The draft permission controller subjects.
462
+ * @param origin - The origin of the subject associated with the permission
463
+ * to delete.
464
+ * @param target - The target name of the permission to delete.
465
+ */
466
+ private deletePermission;
467
+ /**
468
+ * Checks whether the permission of the subject corresponding to the given
469
+ * origin has a caveat of the specified type.
470
+ *
471
+ * Throws an error if the subject does not have a permission with the
472
+ * specified target name.
473
+ *
474
+ * @template TargetName - The permission target name. Should be inferred.
475
+ * @template CaveatType - The valid caveat types for the permission. Should
476
+ * be inferred.
477
+ * @param origin - The origin of the subject.
478
+ * @param target - The target name of the permission.
479
+ * @param caveatType - The type of the caveat to check for.
480
+ * @returns Whether the permission has the specified caveat.
481
+ */
482
+ hasCaveat<TargetName extends ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability'], CaveatType extends ExtractAllowedCaveatTypes<ControllerPermissionSpecification>>(origin: OriginString, target: TargetName, caveatType: CaveatType): boolean;
483
+ /**
484
+ * Gets the caveat of the specified type, if any, for the permission of
485
+ * the subject corresponding to the given origin.
486
+ *
487
+ * Throws an error if the subject does not have a permission with the
488
+ * specified target name.
489
+ *
490
+ * @template TargetName - The permission target name. Should be inferred.
491
+ * @template CaveatType - The valid caveat types for the permission. Should
492
+ * be inferred.
493
+ * @param origin - The origin of the subject.
494
+ * @param target - The target name of the permission.
495
+ * @param caveatType - The type of the caveat to get.
496
+ * @returns The caveat, or `undefined` if no such caveat exists.
497
+ */
498
+ getCaveat<TargetName extends ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability'], CaveatType extends ExtractAllowedCaveatTypes<ControllerPermissionSpecification>>(origin: OriginString, target: TargetName, caveatType: CaveatType): ExtractCaveat<ControllerCaveatSpecification, CaveatType> | undefined;
499
+ /**
500
+ * Adds a caveat of the specified type, with the specified caveat value, to
501
+ * the permission corresponding to the given subject origin and permission
502
+ * target.
503
+ *
504
+ * For modifying existing caveats, use
505
+ * {@link PermissionController.updateCaveat}.
506
+ *
507
+ * Throws an error if no such permission exists, or if the caveat already
508
+ * exists.
509
+ *
510
+ * @template TargetName - The permission target name. Should be inferred.
511
+ * @template CaveatType - The valid caveat types for the permission. Should
512
+ * be inferred.
513
+ * @param origin - The origin of the subject.
514
+ * @param target - The target name of the permission.
515
+ * @param caveatType - The type of the caveat to add.
516
+ * @param caveatValue - The value of the caveat to add.
517
+ */
518
+ addCaveat<TargetName extends ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability'], CaveatType extends ExtractAllowedCaveatTypes<ControllerPermissionSpecification>>(origin: OriginString, target: TargetName, caveatType: CaveatType, caveatValue: ExtractCaveatValue<ControllerCaveatSpecification, CaveatType>): void;
519
+ /**
520
+ * Updates the value of the caveat of the specified type belonging to the
521
+ * permission corresponding to the given subject origin and permission
522
+ * target.
523
+ *
524
+ * For adding new caveats, use
525
+ * {@link PermissionController.addCaveat}.
526
+ *
527
+ * Throws an error if no such permission or caveat exists.
528
+ *
529
+ * @template TargetName - The permission target name. Should be inferred.
530
+ * @template CaveatType - The valid caveat types for the permission. Should
531
+ * be inferred.
532
+ * @param origin - The origin of the subject.
533
+ * @param target - The target name of the permission.
534
+ * @param caveatType - The type of the caveat to update.
535
+ * @param caveatValue - The new value of the caveat.
536
+ */
537
+ updateCaveat<TargetName extends ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability'], CaveatType extends ExtractAllowedCaveatTypes<ControllerPermissionSpecification>, CaveatValue extends ExtractCaveatValue<ControllerCaveatSpecification, CaveatType>>(origin: OriginString, target: TargetName, caveatType: CaveatType, caveatValue: CaveatValue): void;
538
+ /**
539
+ * Sets the specified caveat on the specified permission. Overwrites existing
540
+ * caveats of the same type in-place (preserving array order), and adds the
541
+ * caveat to the end of the array otherwise.
542
+ *
543
+ * Throws an error if the permission does not exist or fails to validate after
544
+ * its caveats have been modified.
545
+ *
546
+ * @see {@link PermissionController.addCaveat}
547
+ * @see {@link PermissionController.updateCaveat}
548
+ * @template TargetName - The permission target name. Should be inferred.
549
+ * @template CaveatType - The valid caveat types for the permission. Should
550
+ * be inferred.
551
+ * @param origin - The origin of the subject.
552
+ * @param target - The target name of the permission.
553
+ * @param caveatType - The type of the caveat to set.
554
+ * @param caveatValue - The value of the caveat to set.
555
+ */
556
+ private setCaveat;
557
+ /**
558
+ * Updates all caveats with the specified type for all subjects and
559
+ * permissions by applying the specified mutator function to them.
560
+ *
561
+ * ATTN: Permissions can be revoked entirely by the action of this method,
562
+ * read on for details.
563
+ *
564
+ * Caveat mutators are functions that receive a caveat value and return a
565
+ * tuple consisting of a {@link CaveatMutatorOperation} and, optionally, a new
566
+ * value to update the existing caveat with.
567
+ *
568
+ * For each caveat, depending on the mutator result, this method will:
569
+ * - Do nothing ({@link CaveatMutatorOperation.noop})
570
+ * - Update the value of the caveat ({@link CaveatMutatorOperation.updateValue}). The caveat specification validator, if any, will be called after updating the value.
571
+ * - Delete the caveat ({@link CaveatMutatorOperation.deleteCaveat}). The permission specification validator, if any, will be called after deleting the caveat.
572
+ * - Revoke the parent permission ({@link CaveatMutatorOperation.revokePermission})
573
+ *
574
+ * This method throws if the validation of any caveat or permission fails.
575
+ *
576
+ * @param targetCaveatType - The type of the caveats to update.
577
+ * @param mutator - The mutator function which will be applied to all caveat
578
+ * values.
579
+ */
580
+ updatePermissionsByCaveat<CaveatType extends ExtractCaveats<ControllerCaveatSpecification>['type'], TargetCaveat extends ExtractCaveat<ControllerCaveatSpecification, CaveatType>>(targetCaveatType: CaveatType, mutator: CaveatMutator<TargetCaveat>): void;
581
+ /**
582
+ * Removes the caveat of the specified type from the permission corresponding
583
+ * to the given subject origin and target name.
584
+ *
585
+ * Throws an error if no such permission or caveat exists.
586
+ *
587
+ * @template TargetName - The permission target name. Should be inferred.
588
+ * @template CaveatType - The valid caveat types for the permission. Should
589
+ * be inferred.
590
+ * @param origin - The origin of the subject.
591
+ * @param target - The target name of the permission.
592
+ * @param caveatType - The type of the caveat to remove.
593
+ */
594
+ removeCaveat<TargetName extends ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability'], CaveatType extends ExtractAllowedCaveatTypes<ControllerPermissionSpecification>>(origin: OriginString, target: TargetName, caveatType: CaveatType): void;
595
+ /**
596
+ * Deletes the specified caveat from the specified permission. If no caveats
597
+ * remain after deletion, the permission's caveat property is set to `null`.
598
+ * The permission is validated after being modified.
599
+ *
600
+ * Throws an error if the permission does not have a caveat with the specified
601
+ * type.
602
+ *
603
+ * @param permission - The permission whose caveat to delete.
604
+ * @param caveatType - The type of the caveat to delete.
605
+ * @param origin - The origin the permission subject.
606
+ */
607
+ private deleteCaveat;
608
+ /**
609
+ * Validates the specified modified permission. Should **always** be invoked
610
+ * on a permission after its caveats have been modified.
611
+ *
612
+ * Just like {@link PermissionController.validatePermission}, except that the
613
+ * corresponding target name and specification are retrieved first, and an
614
+ * error is thrown if the target name does not exist.
615
+ *
616
+ * @param permission - The modified permission to validate.
617
+ * @param origin - The origin associated with the permission.
618
+ */
619
+ private validateModifiedPermission;
620
+ /**
621
+ * Verifies the existence the specified permission target, i.e. whether it has
622
+ * a specification.
623
+ *
624
+ * @param target - The requested permission target.
625
+ * @returns Whether the permission target exists.
626
+ */
627
+ private targetExists;
628
+ /**
629
+ * Grants _approved_ permissions to the specified subject. Every permission and
630
+ * caveat is stringently validated – including by calling every specification
631
+ * validator – and an error is thrown if any validation fails.
632
+ *
633
+ * ATTN: This method does **not** prompt the user for approval.
634
+ *
635
+ * @see {@link PermissionController.requestPermissions} For initiating a
636
+ * permissions request requiring user approval.
637
+ * @param options - Options bag.
638
+ * @param options.approvedPermissions - The requested permissions approved by
639
+ * the user.
640
+ * @param options.requestData - Permission request data. Passed to permission
641
+ * factory functions.
642
+ * @param options.preserveExistingPermissions - Whether to preserve the
643
+ * subject's existing permissions.
644
+ * @param options.subject - The subject to grant permissions to.
645
+ * @returns The granted permissions.
646
+ */
647
+ grantPermissions({ approvedPermissions, requestData, preserveExistingPermissions, subject, }: {
648
+ approvedPermissions: RequestedPermissions;
649
+ subject: PermissionSubjectMetadata;
650
+ preserveExistingPermissions?: boolean;
651
+ requestData?: Record<string, unknown>;
652
+ }): SubjectPermissions<ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>>;
653
+ /**
654
+ * Validates the specified permission by:
655
+ * - Ensuring that if `subjectTypes` is specified, the subject requesting the permission is of a type in the list.
656
+ * - Ensuring that its `caveats` property is either `null` or a non-empty array.
657
+ * - Ensuring that it only includes caveats allowed by its specification.
658
+ * - Ensuring that it includes no duplicate caveats (by caveat type).
659
+ * - Validating each caveat object, if `performCaveatValidation` is `true`.
660
+ * - Calling the validator of its specification, if one exists and `invokePermissionValidator` is `true`.
661
+ *
662
+ * An error is thrown if validation fails.
663
+ *
664
+ * @param specification - The specification of the permission.
665
+ * @param permission - The permission to validate.
666
+ * @param origin - The origin associated with the permission.
667
+ * @param validationOptions - Validation options.
668
+ * @param validationOptions.invokePermissionValidator - Whether to invoke the
669
+ * permission's consumer-specified validator function, if any.
670
+ * @param validationOptions.performCaveatValidation - Whether to invoke
671
+ * {@link PermissionController.validateCaveat} on each of the permission's
672
+ * caveats.
673
+ */
674
+ private validatePermission;
675
+ /**
676
+ * Assigns the specified permissions to the subject with the given origin.
677
+ * Overwrites all existing permissions, and creates a subject entry if it
678
+ * doesn't already exist.
679
+ *
680
+ * ATTN: Assumes that the new permissions have been validated.
681
+ *
682
+ * @param origin - The origin of the grantee subject.
683
+ * @param permissions - The new permissions for the grantee subject.
684
+ */
685
+ private setValidatedPermissions;
686
+ /**
687
+ * Validates the requested caveats for the permission of the specified
688
+ * subject origin and target name and returns the validated caveat array.
689
+ *
690
+ * Throws an error if validation fails.
691
+ *
692
+ * @param origin - The origin of the permission subject.
693
+ * @param target - The permission target name.
694
+ * @param requestedCaveats - The requested caveats to construct.
695
+ * @returns The constructed caveats.
696
+ */
697
+ private constructCaveats;
698
+ /**
699
+ * This methods validates that the specified caveat is an object with the
700
+ * expected properties and types. It also ensures that a caveat specification
701
+ * exists for the requested caveat type, and calls the specification
702
+ * validator, if it exists, on the caveat object.
703
+ *
704
+ * Throws an error if validation fails.
705
+ *
706
+ * @param caveat - The caveat object to validate.
707
+ * @param origin - The origin associated with the subject of the parent
708
+ * permission.
709
+ * @param target - The target name associated with the parent permission.
710
+ */
711
+ private validateCaveat;
712
+ /**
713
+ * Initiates a permission request that requires user approval. This should
714
+ * always be used to grant additional permissions to a subject, unless user
715
+ * approval has been obtained through some other means.
716
+ *
717
+ * Permissions are validated at every step of the approval process, and this
718
+ * method will reject if validation fails.
719
+ *
720
+ * @see {@link ApprovalController} For the user approval logic.
721
+ * @see {@link PermissionController.acceptPermissionsRequest} For the method
722
+ * that _accepts_ the request and resolves the user approval promise.
723
+ * @see {@link PermissionController.rejectPermissionsRequest} For the method
724
+ * that _rejects_ the request and the user approval promise.
725
+ * @param subject - The grantee subject.
726
+ * @param requestedPermissions - The requested permissions.
727
+ * @param options - Additional options.
728
+ * @param options.id - The id of the permissions request. Defaults to a unique
729
+ * id.
730
+ * @param options.preserveExistingPermissions - Whether to preserve the
731
+ * subject's existing permissions. Defaults to `true`.
732
+ * @returns The granted permissions and request metadata.
733
+ */
734
+ requestPermissions(subject: PermissionSubjectMetadata, requestedPermissions: RequestedPermissions, options?: {
735
+ id?: string;
736
+ preserveExistingPermissions?: boolean;
737
+ }): Promise<[
738
+ SubjectPermissions<ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>>,
739
+ {
740
+ data?: Record<string, unknown>;
741
+ id: string;
742
+ origin: OriginString;
743
+ }
744
+ ]>;
745
+ /**
746
+ * Validates requested permissions. Throws if validation fails.
747
+ *
748
+ * This method ensures that the requested permissions are a properly
749
+ * formatted {@link RequestedPermissions} object, and performs the same
750
+ * validation as {@link PermissionController.grantPermissions}, except that
751
+ * consumer-specified permission validator functions are not called, since
752
+ * they are only called on fully constructed, approved permissions that are
753
+ * otherwise completely valid.
754
+ *
755
+ * Unrecognzied properties on requested permissions are ignored.
756
+ *
757
+ * @param origin - The origin of the grantee subject.
758
+ * @param requestedPermissions - The requested permissions.
759
+ */
760
+ private validateRequestedPermissions;
761
+ /**
762
+ * Adds a request to the {@link ApprovalController} using the
763
+ * {@link AddApprovalRequest} action. Also validates the resulting approved
764
+ * permissions request, and throws an error if validation fails.
765
+ *
766
+ * @param permissionsRequest - The permissions request object.
767
+ * @returns The approved permissions request object.
768
+ */
769
+ private requestUserApproval;
770
+ /**
771
+ * Reunites all the side-effects (onPermitted and onFailure) of the requested permissions inside a record of arrays.
772
+ *
773
+ * @param permissions - The approved permissions.
774
+ * @returns The {@link SideEffects} object containing the handlers arrays.
775
+ */
776
+ private getSideEffects;
777
+ /**
778
+ * Executes the side-effects of the approved permissions while handling the errors if any.
779
+ * It will pass an instance of the {@link messagingSystem} and the request data associated with the permission request to the handlers through its params.
780
+ *
781
+ * @param sideEffects - the side-effect record created by {@link getSideEffects}
782
+ * @param requestData - the permissions requestData.
783
+ * @returns the value returned by all the `onPermitted` handlers in an array.
784
+ */
785
+ private executeSideEffects;
786
+ /**
787
+ * Validates an approved {@link PermissionsRequest} object. The approved
788
+ * request must have the required `metadata` and `permissions` properties,
789
+ * the `id` and `origin` of the `metadata` must match the original request
790
+ * metadata, and the requested permissions must be valid per
791
+ * {@link PermissionController.validateRequestedPermissions}. Any extra
792
+ * metadata properties are ignored.
793
+ *
794
+ * An error is thrown if validation fails.
795
+ *
796
+ * @param approvedRequest - The approved permissions request object.
797
+ * @param originalMetadata - The original request metadata.
798
+ */
799
+ private validateApprovedPermissions;
800
+ /**
801
+ * Accepts a permissions request created by
802
+ * {@link PermissionController.requestPermissions}.
803
+ *
804
+ * @param request - The permissions request.
805
+ */
806
+ acceptPermissionsRequest(request: PermissionsRequest): Promise<void>;
807
+ /**
808
+ * Rejects a permissions request created by
809
+ * {@link PermissionController.requestPermissions}.
810
+ *
811
+ * @param id - The id of the request to be rejected.
812
+ */
813
+ rejectPermissionsRequest(id: string): Promise<void>;
814
+ /**
815
+ * Checks whether the {@link ApprovalController} has a particular permissions
816
+ * request.
817
+ *
818
+ * @see {@link PermissionController.acceptPermissionsRequest} and
819
+ * {@link PermissionController.rejectPermissionsRequest} for usage.
820
+ * @param options - The {@link HasApprovalRequest} options.
821
+ * @param options.id - The id of the approval request to check for.
822
+ * @returns Whether the specified request exists.
823
+ */
824
+ private hasApprovalRequest;
825
+ /**
826
+ * Rejects the permissions request with the specified id, with the specified
827
+ * error as the reason. This method is effectively a wrapper around a
828
+ * messenger call for the `ApprovalController:rejectRequest` action.
829
+ *
830
+ * @see {@link PermissionController.acceptPermissionsRequest} and
831
+ * {@link PermissionController.rejectPermissionsRequest} for usage.
832
+ * @param id - The id of the request to reject.
833
+ * @param error - The error associated with the rejection.
834
+ * @returns Nothing
835
+ */
836
+ private _rejectPermissionsRequest;
837
+ /**
838
+ * Gets the subject's endowments per the specified endowment permission.
839
+ * Throws if the subject does not have the required permission or if the
840
+ * permission is not an endowment permission.
841
+ *
842
+ * @param origin - The origin of the subject whose endowments to retrieve.
843
+ * @param targetName - The name of the endowment permission. This must be a
844
+ * valid permission target name.
845
+ * @param requestData - Additional data associated with the request, if any.
846
+ * Forwarded to the endowment getter function for the permission.
847
+ * @returns The endowments, if any.
848
+ */
849
+ getEndowments(origin: string, targetName: ExtractEndowmentPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability'], requestData?: unknown): Promise<Json>;
850
+ /**
851
+ * Executes a restricted method as the subject with the given origin.
852
+ * The specified params, if any, will be passed to the method implementation.
853
+ *
854
+ * ATTN: Great caution should be exercised in the use of this method.
855
+ * Methods that cause side effects or affect application state should
856
+ * be avoided.
857
+ *
858
+ * This method will first attempt to retrieve the requested restricted method
859
+ * implementation, throwing if it does not exist. The method will then be
860
+ * invoked as though the subject with the specified origin had invoked it with
861
+ * the specified parameters. This means that any existing caveats will be
862
+ * applied to the restricted method, and this method will throw if the
863
+ * restricted method or its caveat decorators throw.
864
+ *
865
+ * In addition, this method will throw if the subject does not have a
866
+ * permission for the specified restricted method.
867
+ *
868
+ * @param origin - The origin of the subject to execute the method on behalf
869
+ * of.
870
+ * @param targetName - The name of the method to execute. This must be a valid
871
+ * permission target name.
872
+ * @param params - The parameters to pass to the method implementation.
873
+ * @returns The result of the executed method.
874
+ */
875
+ executeRestrictedMethod(origin: OriginString, targetName: ExtractRestrictedMethodPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability'], params?: RestrictedMethodParameters): Promise<Json>;
876
+ /**
877
+ * An internal method used in the controller's `json-rpc-engine` middleware
878
+ * and {@link PermissionController.executeRestrictedMethod}. Calls the
879
+ * specified restricted method implementation after decorating it with the
880
+ * caveats of its permission. Throws if the subject does not have the
881
+ * requisite permission.
882
+ *
883
+ * ATTN: Parameter validation is the responsibility of the caller, or
884
+ * the restricted method implementation in the case of `params`.
885
+ *
886
+ * @see {@link PermissionController.executeRestrictedMethod} and
887
+ * {@link PermissionController.createPermissionMiddleware} for usage.
888
+ * @param methodImplementation - The implementation of the method to call.
889
+ * @param subject - Metadata about the subject that made the request.
890
+ * @param method - The method name
891
+ * @param params - Params needed for executing the restricted method
892
+ * @returns The result of the restricted method implementation
893
+ */
894
+ private _executeRestrictedMethod;
895
+ }
896
+ export {};
897
+ //# sourceMappingURL=PermissionController.d.ts.map