@metamask/permission-controller 3.0.0 → 3.2.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.
- package/CHANGELOG.md +16 -2
- package/dist/Caveat.d.ts +1 -0
- package/dist/Caveat.d.ts.map +1 -0
- package/dist/Permission.d.ts +42 -0
- package/dist/Permission.d.ts.map +1 -0
- package/dist/Permission.js.map +1 -1
- package/dist/PermissionController.d.ts +28 -3
- package/dist/PermissionController.d.ts.map +1 -0
- package/dist/PermissionController.js +92 -1
- package/dist/PermissionController.js.map +1 -1
- package/dist/SubjectMetadataController.d.ts +109 -0
- package/dist/SubjectMetadataController.d.ts.map +1 -0
- package/dist/SubjectMetadataController.js +139 -0
- package/dist/SubjectMetadataController.js.map +1 -0
- package/dist/errors.d.ts +1 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/permission-middleware.d.ts +1 -0
- package/dist/permission-middleware.d.ts.map +1 -0
- package/dist/rpc-methods/getPermissions.d.ts +1 -0
- package/dist/rpc-methods/getPermissions.d.ts.map +1 -0
- package/dist/rpc-methods/index.d.ts +1 -0
- package/dist/rpc-methods/index.d.ts.map +1 -0
- package/dist/rpc-methods/requestPermissions.d.ts +1 -0
- package/dist/rpc-methods/requestPermissions.d.ts.map +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -0
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [3.2.0]
|
|
10
|
+
### Added
|
|
11
|
+
- Allow restricting permissions by subject type ([#1233](https://github.com/MetaMask/core/pull/1233))
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
- Move `SubjectMetadataController` to permission-controller package ([#1234](https://github.com/MetaMask/core/pull/1234))
|
|
15
|
+
- Update minimum `eth-rpc-errors` version from `4.0.0` to `4.0.2` ([#1215](https://github.com/MetaMask/core/pull/1215))
|
|
16
|
+
|
|
17
|
+
## [3.1.0]
|
|
18
|
+
### Added
|
|
19
|
+
- Add side-effects to permissions ([#1069](https://github.com/MetaMask/core/pull/1069))
|
|
20
|
+
|
|
9
21
|
## [3.0.0]
|
|
10
22
|
### Removed
|
|
11
23
|
- **BREAKING:** Remove `isomorphic-fetch` ([#1106](https://github.com/MetaMask/controllers/pull/1106))
|
|
@@ -18,7 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
18
30
|
### Changed
|
|
19
31
|
- **BREAKING:** Update `@metamask/network-controller` peer dependency to v3 ([#1041](https://github.com/MetaMask/controllers/pull/1041))
|
|
20
32
|
- Rename this repository to `core` ([#1031](https://github.com/MetaMask/controllers/pull/1031))
|
|
21
|
-
- Update `@metamask/controller-utils` package ([#1041](https://github.com/MetaMask/controllers/pull/1041))
|
|
33
|
+
- Update `@metamask/controller-utils` package ([#1041](https://github.com/MetaMask/controllers/pull/1041))
|
|
22
34
|
|
|
23
35
|
## [1.0.2]
|
|
24
36
|
### Fixed
|
|
@@ -36,7 +48,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
36
48
|
|
|
37
49
|
All changes listed after this point were applied to this package following the monorepo conversion.
|
|
38
50
|
|
|
39
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/permission-controller@3.
|
|
51
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/permission-controller@3.2.0...HEAD
|
|
52
|
+
[3.2.0]: https://github.com/MetaMask/core/compare/@metamask/permission-controller@3.1.0...@metamask/permission-controller@3.2.0
|
|
53
|
+
[3.1.0]: https://github.com/MetaMask/core/compare/@metamask/permission-controller@3.0.0...@metamask/permission-controller@3.1.0
|
|
40
54
|
[3.0.0]: https://github.com/MetaMask/core/compare/@metamask/permission-controller@2.0.0...@metamask/permission-controller@3.0.0
|
|
41
55
|
[2.0.0]: https://github.com/MetaMask/core/compare/@metamask/permission-controller@1.0.2...@metamask/permission-controller@2.0.0
|
|
42
56
|
[1.0.2]: https://github.com/MetaMask/core/compare/@metamask/permission-controller@1.0.1...@metamask/permission-controller@1.0.2
|
package/dist/Caveat.d.ts
CHANGED
|
@@ -180,3 +180,4 @@ export declare function isRestrictedMethodCaveatSpecification(specification: Cav
|
|
|
180
180
|
export declare function decorateWithCaveats<CaveatSpecifications extends CaveatSpecificationConstraint>(methodImplementation: RestrictedMethod<RestrictedMethodParameters, Json>, permission: Readonly<PermissionConstraint>, // bound to the requesting origin
|
|
181
181
|
caveatSpecifications: CaveatSpecificationMap<CaveatSpecifications>): RestrictedMethod<RestrictedMethodParameters, Json>;
|
|
182
182
|
export {};
|
|
183
|
+
//# sourceMappingURL=Caveat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Caveat.d.ts","sourceRoot":"","sources":["../src/Caveat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAMvC,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,oBAAoB,EACpB,0BAA0B,EAE3B,MAAM,cAAc,CAAC;AAItB,oBAAY,gBAAgB,GAAG;IAC7B;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAGtB;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC;CACtB,CAAC;AAEF;;;;;;;;;GASG;AACH,oBAAY,MAAM,CAAC,IAAI,SAAS,MAAM,EAAE,KAAK,SAAS,IAAI,IAAI;IAC5D;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IAGpB;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;CACvB,CAAC;AAOF;;;;;;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CAAA;CAAE,CAAC,CAAC;AAExE;;;;;;GAMG;AACH,oBAAY,kBAAkB,CAC5B,oBAAoB,SAAS,6BAA6B,EAC1D,UAAU,SAAS,MAAM,IACvB,aAAa,CAAC,oBAAoB,EAAE,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC;AAE7D;;;;;GAKG;AACH,wBAAgB,qCAAqC,CACnD,aAAa,EAAE,6BAA6B,GAC3C,aAAa,IAAI,6CAA6C,CAEhE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CACjC,oBAAoB,SAAS,6BAA6B,EAE1D,oBAAoB,EAAE,gBAAgB,CAAC,0BAA0B,EAAE,IAAI,CAAC,EACxE,UAAU,EAAE,QAAQ,CAAC,oBAAoB,CAAC,EAAE,iCAAiC;AAC7E,oBAAoB,EAAE,sBAAsB,CAAC,oBAAoB,CAAC,GACjE,gBAAgB,CAAC,0BAA0B,EAAE,IAAI,CAAC,CA2BpD"}
|
package/dist/Permission.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { Json } from '@metamask/types';
|
|
2
2
|
import { NonEmptyArray } from '@metamask/controller-utils';
|
|
3
|
+
import { ActionConstraint, EventConstraint } from '@metamask/base-controller';
|
|
4
|
+
import type { SubjectType } from './SubjectMetadataController';
|
|
3
5
|
import { CaveatConstraint } from './Caveat';
|
|
6
|
+
import type { PermissionsRequest, SideEffectMessenger } from './PermissionController';
|
|
4
7
|
/**
|
|
5
8
|
* The origin of a subject.
|
|
6
9
|
* Effectively the GUID of an entity that can have permissions.
|
|
@@ -236,6 +239,30 @@ export declare type EndowmentGetterParams = {
|
|
|
236
239
|
export declare type EndowmentGetter<Endowments extends Json> = (options: EndowmentGetterParams) => Endowments | Promise<Endowments>;
|
|
237
240
|
export declare type PermissionFactory<TargetPermission extends PermissionConstraint, RequestData extends Record<string, unknown>> = (options: PermissionOptions<TargetPermission>, requestData?: RequestData) => TargetPermission;
|
|
238
241
|
export declare type PermissionValidatorConstraint = (permission: PermissionConstraint, origin?: OriginString, target?: string) => void;
|
|
242
|
+
/**
|
|
243
|
+
* The parameters passed to the side-effect function.
|
|
244
|
+
*/
|
|
245
|
+
export declare type SideEffectParams<Actions extends ActionConstraint, Events extends EventConstraint> = {
|
|
246
|
+
requestData: PermissionsRequest;
|
|
247
|
+
messagingSystem: SideEffectMessenger<Actions, Events>;
|
|
248
|
+
};
|
|
249
|
+
/**
|
|
250
|
+
* A function that will execute actions as a permission side-effect.
|
|
251
|
+
*/
|
|
252
|
+
export declare type SideEffectHandler<Actions extends ActionConstraint, Events extends EventConstraint> = (params: SideEffectParams<Actions, Events>) => Promise<unknown>;
|
|
253
|
+
/**
|
|
254
|
+
* The permissions side effects.
|
|
255
|
+
*/
|
|
256
|
+
export declare type PermissionSideEffect<Actions extends ActionConstraint, Events extends EventConstraint> = {
|
|
257
|
+
/**
|
|
258
|
+
* A method triggered when the permission is accepted by the user
|
|
259
|
+
*/
|
|
260
|
+
onPermitted: SideEffectHandler<Actions, Events>;
|
|
261
|
+
/**
|
|
262
|
+
* A method triggered if a `onPermitted` method rejected.
|
|
263
|
+
*/
|
|
264
|
+
onFailure?: SideEffectHandler<Actions, Events>;
|
|
265
|
+
};
|
|
239
266
|
/**
|
|
240
267
|
* A utility type for ensuring that the given permission target key conforms to
|
|
241
268
|
* our naming conventions.
|
|
@@ -306,6 +333,20 @@ declare type PermissionSpecificationBase<Type extends PermissionType> = {
|
|
|
306
333
|
* The validator should throw an appropriate JSON-RPC error if validation fails.
|
|
307
334
|
*/
|
|
308
335
|
validator?: PermissionValidatorConstraint;
|
|
336
|
+
/**
|
|
337
|
+
* The side-effect triggered by the {@link PermissionController} once the user approved it.
|
|
338
|
+
* The side-effect can only be an action allowed to be called inside the {@link PermissionController}.
|
|
339
|
+
*
|
|
340
|
+
* If the side-effect action fails, the permission that triggered it is revoked.
|
|
341
|
+
*/
|
|
342
|
+
sideEffect?: PermissionSideEffect<any, any>;
|
|
343
|
+
/**
|
|
344
|
+
* The Permission may be available to only a subset of the subject types. If so, specify the subject types as an array.
|
|
345
|
+
* If a subject with a type not in this array tries to request the permission, the call will fail.
|
|
346
|
+
*
|
|
347
|
+
* Leaving this as undefined uses default behaviour where the permission is available to request for all subject types.
|
|
348
|
+
*/
|
|
349
|
+
subjectTypes?: readonly SubjectType[];
|
|
309
350
|
};
|
|
310
351
|
/**
|
|
311
352
|
* The constraint for restricted method permission specification objects.
|
|
@@ -420,3 +461,4 @@ export declare type ExtractPermissionSpecification<Specification extends Permiss
|
|
|
420
461
|
targetKey: TargetKey;
|
|
421
462
|
} ? Specification : never;
|
|
422
463
|
export {};
|
|
464
|
+
//# sourceMappingURL=Permission.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Permission.d.ts","sourceRoot":"","sources":["../src/Permission.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAEvC,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC9E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5C,OAAO,KAAK,EAGV,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,wBAAwB,CAAC;AAIhC;;;GAGG;AACH,oBAAY,YAAY,GAAG,MAAM,CAAC;AAElC;;GAEG;AACH,aAAK,UAAU,GAAG,MAAM,CAAC;AAEzB;;;;;;;;GAQG;AACH,oBAAY,oBAAoB,GAAG;IACjC;;;;;OAKG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAG5C;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,IAAI,GAAG,aAAa,CAAC,gBAAgB,CAAC,CAAC;IAEzD;;OAEG;IACH,QAAQ,C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AC;AAEV;;;;;GAKG;AACH,oBAAY,4BAA4B,CACtC,aAAa,SAAS,iCAAiC,IACrD,aAAa,CAAC,WAAW,CAAC,SAAS,cAAc,CACnD,aAAa,CAAC,WAAW,CAAC,CAC3B,GACG,aAAa,CAAC,gBAAgB,CAAC,SAAS,cAAc,CAAC,SAAS,GAC9D,aAAa,GACb,aAAa,CAAC,gBAAgB,CAAC,SAAS,cAAc,CAAC,gBAAgB,GACvE,kCAAkC,CAChC,OAAO,CAAC,aAAa,EAAE,uCAAuC,CAAC,CAChE,GACD,KAAK,GACP,KAAK,CAAC;AAEV;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAClC,aAAa,SAAS,iCAAiC,EACvD,IAAI,SAAS,cAAc,EAE3B,aAAa,EAAE,aAAa,EAC5B,YAAY,EAAE,IAAI,GACjB,aAAa,IAAI,aAAa,GAAG;IAClC,cAAc,EAAE,IAAI,CAAC;CACtB,CAEA;AAED;;;;;GAKG;AACH,oBAAY,0BAA0B,CACpC,aAAa,SAAS,iCAAiC,IACrD;KACD,SAAS,IAAI,aAAa,CAAC,WAAW,CAAC,GAAG,aAAa,SAAS;QAC/D,SAAS,EAAE,SAAS,CAAC;KACtB,GACG,aAAa,GACb,KAAK;CACV,CAAC;AAEF;;;;;;GAMG;AACH,oBAAY,8BAA8B,CACxC,aAAa,SAAS,iCAAiC,EACvD,SAAS,SAAS,aAAa,CAAC,WAAW,CAAC,IAC1C,aAAa,SAAS;IACxB,SAAS,EAAE,SAAS,CAAC;CACtB,GACG,aAAa,GACb,KAAK,CAAC"}
|
package/dist/Permission.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Permission.js","sourceRoot":"","sources":["../src/Permission.ts"],"names":[],"mappings":";;;AACA,mCAAgC;AAmMhC;;;;;;;;GAQG;AACH,SAAgB,mBAAmB,CAEjC,OAA4C;IAC5C,MAAM,EAAE,OAAO,GAAG,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAEpD,OAAO;QACL,EAAE,EAAE,IAAA,eAAM,GAAE;QACZ,gBAAgB,EAAE,MAAM;QACxB,OAAO;QACP,OAAO;QACP,IAAI,EAAE,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE;KACP,CAAC;AACxB,CAAC;AAZD,kDAYC;AAED;;;;;;GAMG;AACH,SAAgB,UAAU,CACxB,UAAgC,EAChC,UAAkB;;IAElB,OAAO,MAAA,UAAU,CAAC,OAAO,0CAAE,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC;AAC1E,CAAC;AALD,gCAKC;AAwID;;GAEG;AACH,IAAY,cAYX;AAZD,WAAY,cAAc;IACxB;;;OAGG;IACH,uDAAqC,CAAA;IAErC;;;OAGG;IACH,yCAAuB,CAAA;AACzB,CAAC,EAZW,cAAc,GAAd,sBAAc,KAAd,sBAAc,QAYzB;AA+KD;;;;;;;;GAQG;AACH,SAAgB,oBAAoB,CAIlC,aAA4B,EAC5B,YAAkB;IAIlB,OAAO,aAAa,CAAC,cAAc,KAAK,YAAY,CAAC;AACvD,CAAC;AAVD,oDAUC","sourcesContent":["import { Json } from '@metamask/types';\nimport { nanoid } from 'nanoid';\nimport { NonEmptyArray } from '@metamask/controller-utils';\nimport { CaveatConstraint } from './Caveat';\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { PermissionController } from './PermissionController';\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { Caveat } from './Caveat';\n\n/**\n * The origin of a subject.\n * Effectively the GUID of an entity that can have permissions.\n */\nexport type OriginString = string;\n\n/**\n * The name of a permission target.\n */\ntype TargetName = string;\n\n/**\n * A `ZCAP-LD`-like permission object. A permission is associated with a\n * particular `invoker`, which is the holder of the permission. Possessing the\n * permission grants access to a particular restricted resource, identified by\n * the `parentCapability`. The use of the restricted resource may be further\n * restricted by any `caveats` associated with the permission.\n *\n * See the README for details.\n */\nexport type PermissionConstraint = {\n /**\n * The context(s) in which this capability is meaningful.\n *\n * It is required by the standard, but we make it optional since there is only\n * one context in our usage (i.e. the user's MetaMask instance).\n */\n readonly '@context'?: NonEmptyArray<string>;\n\n // TODO:TS4.4 Make optional\n /**\n * The caveats of the permission.\n *\n * @see {@link Caveat} For more information.\n */\n readonly caveats: null | NonEmptyArray<CaveatConstraint>;\n\n /**\n * The creation date of the permission, in UNIX epoch time.\n */\n readonly date: number;\n\n /**\n * The GUID of the permission object.\n */\n readonly id: string;\n\n /**\n * The origin string of the subject that has the permission.\n */\n readonly invoker: OriginString;\n\n /**\n * A pointer to the resource that possession of the capability grants\n * access to, for example a JSON-RPC method or endowment.\n */\n readonly parentCapability: string;\n};\n\n/**\n * A `ZCAP-LD`-like permission object. A permission is associated with a\n * particular `invoker`, which is the holder of the permission. Possessing the\n * permission grants access to a particular restricted resource, identified by\n * the `parentCapability`. The use of the restricted resource may be further\n * restricted by any `caveats` associated with the permission.\n *\n * See the README for details.\n *\n * @template TargetKey - They key of the permission target that the permission\n * corresponds to.\n * @template AllowedCaveat - A union of the allowed {@link Caveat} types\n * for the permission.\n */\nexport type ValidPermission<\n TargetKey extends TargetName,\n AllowedCaveat extends CaveatConstraint,\n> = PermissionConstraint & {\n // TODO:TS4.4 Make optional\n /**\n * The caveats of the permission.\n *\n * @see {@link Caveat} For more information.\n */\n readonly caveats: AllowedCaveat extends never\n ? null\n : NonEmptyArray<AllowedCaveat> | null;\n\n /**\n * A pointer to the resource that possession of the capability grants\n * access to, for example a JSON-RPC method or endowment.\n */\n readonly parentCapability: ExtractPermissionTargetNames<TargetKey>;\n};\n\n/**\n * A utility type for ensuring that the given permission target name conforms to\n * our naming conventions.\n *\n * See the README for the distinction between target names and keys.\n */\ntype ValidTargetName<Name extends string> = Name extends `${string}*`\n ? never\n : Name extends `${string}_`\n ? never\n : Name;\n\n/**\n * A utility type for extracting permission target names from a union of target\n * keys.\n *\n * See the README for the distinction between target names and keys.\n *\n * @template Key - The target key type to extract target names from.\n */\nexport type ExtractPermissionTargetNames<Key extends string> = ValidTargetName<\n Key extends `${infer Base}_*` ? `${Base}_${string}` : Key\n>;\n\n/**\n * Extracts the permission key of a particular name from a union of keys.\n * An internal utility type used in {@link ExtractPermissionTargetKey}.\n *\n * @template Key - The target key type to extract from.\n * @template Name - The name whose key to extract.\n */\ntype KeyOfTargetName<\n Key extends string,\n Name extends string,\n> = Name extends ExtractPermissionTargetNames<Key> ? Key : never;\n\n/**\n * A utility type for finding the permission target key corresponding to a\n * target name. In a way, the inverse of {@link ExtractPermissionTargetNames}.\n *\n * See the README for the distinction between target names and keys.\n *\n * @template Key - The target key type to extract from.\n * @template Name - The name whose key to extract.\n */\nexport type ExtractPermissionTargetKey<\n Key extends string,\n Name extends string,\n> = Key extends Name ? Key : Extract<Key, KeyOfTargetName<Key, Name>>;\n\n/**\n * Internal utility for extracting the members types of an array. The type\n * evalutes to `never` if the specified type is the empty tuple or neither\n * an array nor a tuple.\n *\n * @template ArrayType - The array type whose members to extract.\n */\ntype ExtractArrayMembers<ArrayType> = ArrayType extends []\n ? never\n : ArrayType extends any[] | readonly any[]\n ? ArrayType[number]\n : never;\n\n/**\n * A utility type for extracting the allowed caveat types for a particular\n * permission from a permission specification type.\n *\n * @template PermissionSpecification - The permission specification type to\n * extract valid caveat types from.\n */\nexport type ExtractAllowedCaveatTypes<\n PermissionSpecification extends PermissionSpecificationConstraint,\n> = ExtractArrayMembers<PermissionSpecification['allowedCaveats']>;\n\n/**\n * The options object of {@link constructPermission}.\n *\n * @template TargetPermission - The {@link Permission} that will be constructed.\n */\nexport type PermissionOptions<TargetPermission extends PermissionConstraint> = {\n target: TargetPermission['parentCapability'];\n /**\n * The origin string of the subject that has the permission.\n */\n invoker: OriginString;\n\n /**\n * The caveats of the permission.\n * See {@link Caveat}.\n */\n caveats?: NonEmptyArray<CaveatConstraint>;\n};\n\n/**\n * The default permission factory function. Naively constructs a permission from\n * the inputs. Sets a default, random `id` if none is provided.\n *\n * @see {@link Permission} For more details.\n * @template TargetPermission- - The {@link Permission} that will be constructed.\n * @param options - The options for the permission.\n * @returns The new permission object.\n */\nexport function constructPermission<\n TargetPermission extends PermissionConstraint,\n>(options: PermissionOptions<TargetPermission>): TargetPermission {\n const { caveats = null, invoker, target } = options;\n\n return {\n id: nanoid(),\n parentCapability: target,\n invoker,\n caveats,\n date: new Date().getTime(),\n } as TargetPermission;\n}\n\n/**\n * Gets the caveat of the specified type belonging to the specified permission.\n *\n * @param permission - The permission whose caveat to retrieve.\n * @param caveatType - The type of the caveat to retrieve.\n * @returns The caveat, or undefined if no such caveat exists.\n */\nexport function findCaveat(\n permission: PermissionConstraint,\n caveatType: string,\n): CaveatConstraint | undefined {\n return permission.caveats?.find((caveat) => caveat.type === caveatType);\n}\n\n/**\n * A requested permission object. Just an object with any of the properties\n * of a {@link PermissionConstraint} object.\n */\ntype RequestedPermission = Partial<PermissionConstraint>;\n\n/**\n * A record of target names and their {@link RequestedPermission} objects.\n */\nexport type RequestedPermissions = Record<TargetName, RequestedPermission>;\n\n/**\n * The restricted method context object. Essentially a way to pass internal\n * arguments to restricted methods and caveat functions, most importantly the\n * requesting origin.\n */\ntype RestrictedMethodContext = Readonly<{\n origin: OriginString;\n [key: string]: any;\n}>;\n\nexport type RestrictedMethodParameters = Json[] | Record<string, Json> | void;\n\n/**\n * The arguments passed to a restricted method implementation.\n *\n * @template Params - The JSON-RPC parameters of the restricted method.\n */\nexport type RestrictedMethodOptions<Params extends RestrictedMethodParameters> =\n {\n method: TargetName;\n params?: Params;\n context: RestrictedMethodContext;\n };\n\n/**\n * A synchronous restricted method implementation.\n *\n * @template Params - The JSON-RPC parameters of the restricted method.\n * @template Result - The JSON-RPC result of the restricted method.\n */\nexport type SyncRestrictedMethod<\n Params extends RestrictedMethodParameters,\n Result extends Json,\n> = (args: RestrictedMethodOptions<Params>) => Result;\n\n/**\n * An asynchronous restricted method implementation.\n *\n * @template Params - The JSON-RPC parameters of the restricted method.\n * @template Result - The JSON-RPC result of the restricted method.\n */\nexport type AsyncRestrictedMethod<\n Params extends RestrictedMethodParameters,\n Result extends Json,\n> = (args: RestrictedMethodOptions<Params>) => Promise<Result>;\n\n/**\n * A synchronous or asynchronous restricted method implementation.\n *\n * @template Params - The JSON-RPC parameters of the restricted method.\n * @template Result - The JSON-RPC result of the restricted method.\n */\nexport type RestrictedMethod<\n Params extends RestrictedMethodParameters,\n Result extends Json,\n> =\n | SyncRestrictedMethod<Params, Result>\n | AsyncRestrictedMethod<Params, Result>;\n\nexport type ValidRestrictedMethod<\n MethodImplementation extends RestrictedMethod<any, any>,\n> = MethodImplementation extends (args: infer Options) => Json | Promise<Json>\n ? Options extends RestrictedMethodOptions<RestrictedMethodParameters>\n ? MethodImplementation\n : never\n : never;\n\n/**\n * {@link EndowmentGetter} parameter object.\n */\nexport type EndowmentGetterParams = {\n /**\n * The origin of the requesting subject.\n */\n origin: string;\n\n /**\n * Any additional data associated with the request.\n */\n requestData?: unknown;\n\n [key: string]: unknown;\n};\n\n/**\n * A synchronous or asynchronous function that gets the endowments for a\n * particular endowment permission. The getter receives the origin of the\n * requesting subject and, optionally, additional request metadata.\n */\nexport type EndowmentGetter<Endowments extends Json> = (\n options: EndowmentGetterParams,\n) => Endowments | Promise<Endowments>;\n\nexport type PermissionFactory<\n TargetPermission extends PermissionConstraint,\n RequestData extends Record<string, unknown>,\n> = (\n options: PermissionOptions<TargetPermission>,\n requestData?: RequestData,\n) => TargetPermission;\n\nexport type PermissionValidatorConstraint = (\n permission: PermissionConstraint,\n origin?: OriginString,\n target?: string,\n) => void;\n\n/**\n * A utility type for ensuring that the given permission target key conforms to\n * our naming conventions.\n *\n * See the README for the distinction between target names and keys.\n *\n * @template Key - The target key string to apply the constraint to.\n */\ntype ValidTargetKey<Key extends string> = Key extends `${string}_*`\n ? Key\n : Key extends `${string}_`\n ? never\n : Key extends `${string}*`\n ? never\n : Key;\n\n/**\n * The different possible types of permissions.\n */\nexport enum PermissionType {\n /**\n * A restricted JSON-RPC method. A subject must have the requisite permission\n * to call a restricted JSON-RPC method.\n */\n RestrictedMethod = 'RestrictedMethod',\n\n /**\n * An \"endowment\" granted to subjects that possess the requisite permission,\n * such as a global environment variable exposing a restricted API, etc.\n */\n Endowment = 'Endowment',\n}\n\n/**\n * The base constraint for permission specification objects. Every\n * {@link Permission} supported by a {@link PermissionController} must have an\n * associated specification, which is the source of truth for all permission-\n * related types. A permission specification includes the list of permitted\n * caveats, and any factory and validation functions specified by the consumer.\n * A concrete permission specification may specify further fields as necessary.\n *\n * See the README for more details.\n */\ntype PermissionSpecificationBase<Type extends PermissionType> = {\n /**\n * The type of the specified permission.\n */\n permissionType: Type;\n\n /**\n * The target resource of the permission. The shape of this string depends on\n * the permission type. For example, a restricted method target key will\n * consist of either a complete method name or the prefix of a namespaced\n * method, e.g. `wallet_snap_*`.\n */\n targetKey: string;\n\n /**\n * An array of the caveat types that may be added to instances of this\n * permission.\n */\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n\n /**\n * The factory function used to get permission objects. Permissions returned\n * by this function are presumed to valid, and they will not be passed to the\n * validator function associated with this specification (if any). In other\n * words, the factory function should validate the permissions it creates.\n *\n * If no factory is specified, the {@link Permission} constructor will be\n * used, and the validator function (if specified) will be called on newly\n * constructed permissions.\n */\n factory?: PermissionFactory<any, Record<string, unknown>>;\n\n /**\n * The validator function used to validate permissions of the associated type\n * whenever they are mutated. The only way a permission can be legally mutated\n * is when its caveats are modified by the permission controller.\n *\n * The validator should throw an appropriate JSON-RPC error if validation fails.\n */\n validator?: PermissionValidatorConstraint;\n};\n\n/**\n * The constraint for restricted method permission specification objects.\n * Permissions that correspond to JSON-RPC methods are specified using objects\n * that conform to this type.\n *\n * See the README for more details.\n */\nexport type RestrictedMethodSpecificationConstraint =\n PermissionSpecificationBase<PermissionType.RestrictedMethod> & {\n /**\n * The implementation of the restricted method that the permission\n * corresponds to.\n */\n methodImplementation: RestrictedMethod<any, any>;\n };\n\n/**\n * The constraint for endowment permission specification objects. Permissions\n * that endow callers with some restricted resource are specified using objects\n * that conform to this type.\n *\n * See the README for more details.\n */\nexport type EndowmentSpecificationConstraint =\n PermissionSpecificationBase<PermissionType.Endowment> & {\n /**\n * The {@link EndowmentGetter} function for the permission. This function\n * will be called by the {@link PermissionController} whenever the\n * permission is invoked, after which the host can apply the endowments to\n * the requesting subject in the intended manner.\n */\n endowmentGetter: EndowmentGetter<any>;\n };\n\n/**\n * The constraint for permission specification objects. Every {@link Permission}\n * supported by a {@link PermissionController} must have an associated\n * specification, which is the source of truth for all permission-related types.\n * All specifications must adhere to the {@link PermissionSpecificationBase}\n * interface, but specifications may have different fields depending on the\n * {@link PermissionType}.\n *\n * See the README for more details.\n */\nexport type PermissionSpecificationConstraint =\n | EndowmentSpecificationConstraint\n | RestrictedMethodSpecificationConstraint;\n\n/**\n * Options for {@link PermissionSpecificationBuilder} functions.\n */\ntype PermissionSpecificationBuilderOptions<\n FactoryHooks extends Record<string, unknown>,\n MethodHooks extends Record<string, unknown>,\n ValidatorHooks extends Record<string, unknown>,\n> = {\n targetKey?: string;\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n factoryHooks?: FactoryHooks;\n methodHooks?: MethodHooks;\n validatorHooks?: ValidatorHooks;\n};\n\n/**\n * A function that builds a permission specification. Modules that specify\n * permissions for external consumption should make this their primary /\n * default export so that host applications can use them to generate concrete\n * specifications tailored to their requirements.\n */\nexport type PermissionSpecificationBuilder<\n Type extends PermissionType,\n Options extends PermissionSpecificationBuilderOptions<any, any, any>,\n Specification extends PermissionSpecificationConstraint & {\n permissionType: Type;\n },\n> = (options: Options) => Specification;\n\n/**\n * A restricted method permission export object, containing the\n * {@link PermissionSpecificationBuilder} function and \"hook name\" objects.\n */\nexport type PermissionSpecificationBuilderExportConstraint = {\n targetKey: string;\n specificationBuilder: PermissionSpecificationBuilder<\n PermissionType,\n PermissionSpecificationBuilderOptions<any, any, any>,\n PermissionSpecificationConstraint\n >;\n factoryHookNames?: Record<string, true>;\n methodHookNames?: Record<string, true>;\n validatorHookNames?: Record<string, true>;\n};\n\ntype ValidRestrictedMethodSpecification<\n Specification extends RestrictedMethodSpecificationConstraint,\n> = Specification['methodImplementation'] extends ValidRestrictedMethod<\n Specification['methodImplementation']\n>\n ? Specification\n : never;\n\n/**\n * Constraint for {@link PermissionSpecificationConstraint} objects that\n * evaluates to `never` if the specification contains any invalid fields.\n *\n * @template Specification - The permission specification to validate.\n */\nexport type ValidPermissionSpecification<\n Specification extends PermissionSpecificationConstraint,\n> = Specification['targetKey'] extends ValidTargetKey<\n Specification['targetKey']\n>\n ? Specification['permissionType'] extends PermissionType.Endowment\n ? Specification\n : Specification['permissionType'] extends PermissionType.RestrictedMethod\n ? ValidRestrictedMethodSpecification<\n Extract<Specification, RestrictedMethodSpecificationConstraint>\n >\n : never\n : never;\n\n/**\n * Checks that the specification has the expected permission type.\n *\n * @param specification - The specification to check.\n * @param expectedType - The expected permission type.\n * @template Specification - The specification to check.\n * @template Type - The expected permission type.\n * @returns Whether or not the specification is of the expected type.\n */\nexport function hasSpecificationType<\n Specification extends PermissionSpecificationConstraint,\n Type extends PermissionType,\n>(\n specification: Specification,\n expectedType: Type,\n): specification is Specification & {\n permissionType: Type;\n} {\n return specification.permissionType === expectedType;\n}\n\n/**\n * The specifications for all permissions supported by a particular\n * {@link PermissionController}.\n *\n * @template Specifications - The union of all {@link PermissionSpecificationConstraint} types.\n */\nexport type PermissionSpecificationMap<\n Specification extends PermissionSpecificationConstraint,\n> = {\n [TargetKey in Specification['targetKey']]: Specification extends {\n targetKey: TargetKey;\n }\n ? Specification\n : never;\n};\n\n/**\n * Extracts a specific {@link PermissionSpecificationConstraint} from a union of\n * permission specifications.\n *\n * @template Specification - The specification union type to extract from.\n * @template TargetKey - The `targetKey` of the specification to extract.\n */\nexport type ExtractPermissionSpecification<\n Specification extends PermissionSpecificationConstraint,\n TargetKey extends Specification['targetKey'],\n> = Specification extends {\n targetKey: TargetKey;\n}\n ? Specification\n : never;\n"]}
|
|
1
|
+
{"version":3,"file":"Permission.js","sourceRoot":"","sources":["../src/Permission.ts"],"names":[],"mappings":";;;AACA,mCAAgC;AA0MhC;;;;;;;;GAQG;AACH,SAAgB,mBAAmB,CAEjC,OAA4C;IAC5C,MAAM,EAAE,OAAO,GAAG,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAEpD,OAAO;QACL,EAAE,EAAE,IAAA,eAAM,GAAE;QACZ,gBAAgB,EAAE,MAAM;QACxB,OAAO;QACP,OAAO;QACP,IAAI,EAAE,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE;KACP,CAAC;AACxB,CAAC;AAZD,kDAYC;AAED;;;;;;GAMG;AACH,SAAgB,UAAU,CACxB,UAAgC,EAChC,UAAkB;;IAElB,OAAO,MAAA,UAAU,CAAC,OAAO,0CAAE,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC;AAC1E,CAAC;AALD,gCAKC;AA4KD;;GAEG;AACH,IAAY,cAYX;AAZD,WAAY,cAAc;IACxB;;;OAGG;IACH,uDAAqC,CAAA;IAErC;;;OAGG;IACH,yCAAuB,CAAA;AACzB,CAAC,EAZW,cAAc,GAAd,sBAAc,KAAd,sBAAc,QAYzB;AA+LD;;;;;;;;GAQG;AACH,SAAgB,oBAAoB,CAIlC,aAA4B,EAC5B,YAAkB;IAIlB,OAAO,aAAa,CAAC,cAAc,KAAK,YAAY,CAAC;AACvD,CAAC;AAVD,oDAUC","sourcesContent":["import { Json } from '@metamask/types';\nimport { nanoid } from 'nanoid';\nimport { NonEmptyArray } from '@metamask/controller-utils';\nimport { ActionConstraint, EventConstraint } from '@metamask/base-controller';\nimport type { SubjectType } from './SubjectMetadataController';\nimport { CaveatConstraint } from './Caveat';\n\nimport type {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n PermissionController,\n PermissionsRequest,\n SideEffectMessenger,\n} from './PermissionController';\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { Caveat } from './Caveat';\n\n/**\n * The origin of a subject.\n * Effectively the GUID of an entity that can have permissions.\n */\nexport type OriginString = string;\n\n/**\n * The name of a permission target.\n */\ntype TargetName = string;\n\n/**\n * A `ZCAP-LD`-like permission object. A permission is associated with a\n * particular `invoker`, which is the holder of the permission. Possessing the\n * permission grants access to a particular restricted resource, identified by\n * the `parentCapability`. The use of the restricted resource may be further\n * restricted by any `caveats` associated with the permission.\n *\n * See the README for details.\n */\nexport type PermissionConstraint = {\n /**\n * The context(s) in which this capability is meaningful.\n *\n * It is required by the standard, but we make it optional since there is only\n * one context in our usage (i.e. the user's MetaMask instance).\n */\n readonly '@context'?: NonEmptyArray<string>;\n\n // TODO:TS4.4 Make optional\n /**\n * The caveats of the permission.\n *\n * @see {@link Caveat} For more information.\n */\n readonly caveats: null | NonEmptyArray<CaveatConstraint>;\n\n /**\n * The creation date of the permission, in UNIX epoch time.\n */\n readonly date: number;\n\n /**\n * The GUID of the permission object.\n */\n readonly id: string;\n\n /**\n * The origin string of the subject that has the permission.\n */\n readonly invoker: OriginString;\n\n /**\n * A pointer to the resource that possession of the capability grants\n * access to, for example a JSON-RPC method or endowment.\n */\n readonly parentCapability: string;\n};\n\n/**\n * A `ZCAP-LD`-like permission object. A permission is associated with a\n * particular `invoker`, which is the holder of the permission. Possessing the\n * permission grants access to a particular restricted resource, identified by\n * the `parentCapability`. The use of the restricted resource may be further\n * restricted by any `caveats` associated with the permission.\n *\n * See the README for details.\n *\n * @template TargetKey - They key of the permission target that the permission\n * corresponds to.\n * @template AllowedCaveat - A union of the allowed {@link Caveat} types\n * for the permission.\n */\nexport type ValidPermission<\n TargetKey extends TargetName,\n AllowedCaveat extends CaveatConstraint,\n> = PermissionConstraint & {\n // TODO:TS4.4 Make optional\n /**\n * The caveats of the permission.\n *\n * @see {@link Caveat} For more information.\n */\n readonly caveats: AllowedCaveat extends never\n ? null\n : NonEmptyArray<AllowedCaveat> | null;\n\n /**\n * A pointer to the resource that possession of the capability grants\n * access to, for example a JSON-RPC method or endowment.\n */\n readonly parentCapability: ExtractPermissionTargetNames<TargetKey>;\n};\n\n/**\n * A utility type for ensuring that the given permission target name conforms to\n * our naming conventions.\n *\n * See the README for the distinction between target names and keys.\n */\ntype ValidTargetName<Name extends string> = Name extends `${string}*`\n ? never\n : Name extends `${string}_`\n ? never\n : Name;\n\n/**\n * A utility type for extracting permission target names from a union of target\n * keys.\n *\n * See the README for the distinction between target names and keys.\n *\n * @template Key - The target key type to extract target names from.\n */\nexport type ExtractPermissionTargetNames<Key extends string> = ValidTargetName<\n Key extends `${infer Base}_*` ? `${Base}_${string}` : Key\n>;\n\n/**\n * Extracts the permission key of a particular name from a union of keys.\n * An internal utility type used in {@link ExtractPermissionTargetKey}.\n *\n * @template Key - The target key type to extract from.\n * @template Name - The name whose key to extract.\n */\ntype KeyOfTargetName<\n Key extends string,\n Name extends string,\n> = Name extends ExtractPermissionTargetNames<Key> ? Key : never;\n\n/**\n * A utility type for finding the permission target key corresponding to a\n * target name. In a way, the inverse of {@link ExtractPermissionTargetNames}.\n *\n * See the README for the distinction between target names and keys.\n *\n * @template Key - The target key type to extract from.\n * @template Name - The name whose key to extract.\n */\nexport type ExtractPermissionTargetKey<\n Key extends string,\n Name extends string,\n> = Key extends Name ? Key : Extract<Key, KeyOfTargetName<Key, Name>>;\n\n/**\n * Internal utility for extracting the members types of an array. The type\n * evalutes to `never` if the specified type is the empty tuple or neither\n * an array nor a tuple.\n *\n * @template ArrayType - The array type whose members to extract.\n */\ntype ExtractArrayMembers<ArrayType> = ArrayType extends []\n ? never\n : ArrayType extends any[] | readonly any[]\n ? ArrayType[number]\n : never;\n\n/**\n * A utility type for extracting the allowed caveat types for a particular\n * permission from a permission specification type.\n *\n * @template PermissionSpecification - The permission specification type to\n * extract valid caveat types from.\n */\nexport type ExtractAllowedCaveatTypes<\n PermissionSpecification extends PermissionSpecificationConstraint,\n> = ExtractArrayMembers<PermissionSpecification['allowedCaveats']>;\n\n/**\n * The options object of {@link constructPermission}.\n *\n * @template TargetPermission - The {@link Permission} that will be constructed.\n */\nexport type PermissionOptions<TargetPermission extends PermissionConstraint> = {\n target: TargetPermission['parentCapability'];\n /**\n * The origin string of the subject that has the permission.\n */\n invoker: OriginString;\n\n /**\n * The caveats of the permission.\n * See {@link Caveat}.\n */\n caveats?: NonEmptyArray<CaveatConstraint>;\n};\n\n/**\n * The default permission factory function. Naively constructs a permission from\n * the inputs. Sets a default, random `id` if none is provided.\n *\n * @see {@link Permission} For more details.\n * @template TargetPermission- - The {@link Permission} that will be constructed.\n * @param options - The options for the permission.\n * @returns The new permission object.\n */\nexport function constructPermission<\n TargetPermission extends PermissionConstraint,\n>(options: PermissionOptions<TargetPermission>): TargetPermission {\n const { caveats = null, invoker, target } = options;\n\n return {\n id: nanoid(),\n parentCapability: target,\n invoker,\n caveats,\n date: new Date().getTime(),\n } as TargetPermission;\n}\n\n/**\n * Gets the caveat of the specified type belonging to the specified permission.\n *\n * @param permission - The permission whose caveat to retrieve.\n * @param caveatType - The type of the caveat to retrieve.\n * @returns The caveat, or undefined if no such caveat exists.\n */\nexport function findCaveat(\n permission: PermissionConstraint,\n caveatType: string,\n): CaveatConstraint | undefined {\n return permission.caveats?.find((caveat) => caveat.type === caveatType);\n}\n\n/**\n * A requested permission object. Just an object with any of the properties\n * of a {@link PermissionConstraint} object.\n */\ntype RequestedPermission = Partial<PermissionConstraint>;\n\n/**\n * A record of target names and their {@link RequestedPermission} objects.\n */\nexport type RequestedPermissions = Record<TargetName, RequestedPermission>;\n\n/**\n * The restricted method context object. Essentially a way to pass internal\n * arguments to restricted methods and caveat functions, most importantly the\n * requesting origin.\n */\ntype RestrictedMethodContext = Readonly<{\n origin: OriginString;\n [key: string]: any;\n}>;\n\nexport type RestrictedMethodParameters = Json[] | Record<string, Json> | void;\n\n/**\n * The arguments passed to a restricted method implementation.\n *\n * @template Params - The JSON-RPC parameters of the restricted method.\n */\nexport type RestrictedMethodOptions<Params extends RestrictedMethodParameters> =\n {\n method: TargetName;\n params?: Params;\n context: RestrictedMethodContext;\n };\n\n/**\n * A synchronous restricted method implementation.\n *\n * @template Params - The JSON-RPC parameters of the restricted method.\n * @template Result - The JSON-RPC result of the restricted method.\n */\nexport type SyncRestrictedMethod<\n Params extends RestrictedMethodParameters,\n Result extends Json,\n> = (args: RestrictedMethodOptions<Params>) => Result;\n\n/**\n * An asynchronous restricted method implementation.\n *\n * @template Params - The JSON-RPC parameters of the restricted method.\n * @template Result - The JSON-RPC result of the restricted method.\n */\nexport type AsyncRestrictedMethod<\n Params extends RestrictedMethodParameters,\n Result extends Json,\n> = (args: RestrictedMethodOptions<Params>) => Promise<Result>;\n\n/**\n * A synchronous or asynchronous restricted method implementation.\n *\n * @template Params - The JSON-RPC parameters of the restricted method.\n * @template Result - The JSON-RPC result of the restricted method.\n */\nexport type RestrictedMethod<\n Params extends RestrictedMethodParameters,\n Result extends Json,\n> =\n | SyncRestrictedMethod<Params, Result>\n | AsyncRestrictedMethod<Params, Result>;\n\nexport type ValidRestrictedMethod<\n MethodImplementation extends RestrictedMethod<any, any>,\n> = MethodImplementation extends (args: infer Options) => Json | Promise<Json>\n ? Options extends RestrictedMethodOptions<RestrictedMethodParameters>\n ? MethodImplementation\n : never\n : never;\n\n/**\n * {@link EndowmentGetter} parameter object.\n */\nexport type EndowmentGetterParams = {\n /**\n * The origin of the requesting subject.\n */\n origin: string;\n\n /**\n * Any additional data associated with the request.\n */\n requestData?: unknown;\n\n [key: string]: unknown;\n};\n\n/**\n * A synchronous or asynchronous function that gets the endowments for a\n * particular endowment permission. The getter receives the origin of the\n * requesting subject and, optionally, additional request metadata.\n */\nexport type EndowmentGetter<Endowments extends Json> = (\n options: EndowmentGetterParams,\n) => Endowments | Promise<Endowments>;\n\nexport type PermissionFactory<\n TargetPermission extends PermissionConstraint,\n RequestData extends Record<string, unknown>,\n> = (\n options: PermissionOptions<TargetPermission>,\n requestData?: RequestData,\n) => TargetPermission;\n\nexport type PermissionValidatorConstraint = (\n permission: PermissionConstraint,\n origin?: OriginString,\n target?: string,\n) => void;\n\n/**\n * The parameters passed to the side-effect function.\n */\nexport type SideEffectParams<\n Actions extends ActionConstraint,\n Events extends EventConstraint,\n> = {\n requestData: PermissionsRequest;\n messagingSystem: SideEffectMessenger<Actions, Events>;\n};\n\n/**\n * A function that will execute actions as a permission side-effect.\n */\nexport type SideEffectHandler<\n Actions extends ActionConstraint,\n Events extends EventConstraint,\n> = (params: SideEffectParams<Actions, Events>) => Promise<unknown>;\n\n/**\n * The permissions side effects.\n */\nexport type PermissionSideEffect<\n Actions extends ActionConstraint,\n Events extends EventConstraint,\n> = {\n /**\n * A method triggered when the permission is accepted by the user\n */\n onPermitted: SideEffectHandler<Actions, Events>;\n /**\n * A method triggered if a `onPermitted` method rejected.\n */\n onFailure?: SideEffectHandler<Actions, Events>;\n};\n\n/**\n * A utility type for ensuring that the given permission target key conforms to\n * our naming conventions.\n *\n * See the README for the distinction between target names and keys.\n *\n * @template Key - The target key string to apply the constraint to.\n */\ntype ValidTargetKey<Key extends string> = Key extends `${string}_*`\n ? Key\n : Key extends `${string}_`\n ? never\n : Key extends `${string}*`\n ? never\n : Key;\n\n/**\n * The different possible types of permissions.\n */\nexport enum PermissionType {\n /**\n * A restricted JSON-RPC method. A subject must have the requisite permission\n * to call a restricted JSON-RPC method.\n */\n RestrictedMethod = 'RestrictedMethod',\n\n /**\n * An \"endowment\" granted to subjects that possess the requisite permission,\n * such as a global environment variable exposing a restricted API, etc.\n */\n Endowment = 'Endowment',\n}\n\n/**\n * The base constraint for permission specification objects. Every\n * {@link Permission} supported by a {@link PermissionController} must have an\n * associated specification, which is the source of truth for all permission-\n * related types. A permission specification includes the list of permitted\n * caveats, and any factory and validation functions specified by the consumer.\n * A concrete permission specification may specify further fields as necessary.\n *\n * See the README for more details.\n */\ntype PermissionSpecificationBase<Type extends PermissionType> = {\n /**\n * The type of the specified permission.\n */\n permissionType: Type;\n\n /**\n * The target resource of the permission. The shape of this string depends on\n * the permission type. For example, a restricted method target key will\n * consist of either a complete method name or the prefix of a namespaced\n * method, e.g. `wallet_snap_*`.\n */\n targetKey: string;\n\n /**\n * An array of the caveat types that may be added to instances of this\n * permission.\n */\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n\n /**\n * The factory function used to get permission objects. Permissions returned\n * by this function are presumed to valid, and they will not be passed to the\n * validator function associated with this specification (if any). In other\n * words, the factory function should validate the permissions it creates.\n *\n * If no factory is specified, the {@link Permission} constructor will be\n * used, and the validator function (if specified) will be called on newly\n * constructed permissions.\n */\n factory?: PermissionFactory<any, Record<string, unknown>>;\n\n /**\n * The validator function used to validate permissions of the associated type\n * whenever they are mutated. The only way a permission can be legally mutated\n * is when its caveats are modified by the permission controller.\n *\n * The validator should throw an appropriate JSON-RPC error if validation fails.\n */\n validator?: PermissionValidatorConstraint;\n\n /**\n * The side-effect triggered by the {@link PermissionController} once the user approved it.\n * The side-effect can only be an action allowed to be called inside the {@link PermissionController}.\n *\n * If the side-effect action fails, the permission that triggered it is revoked.\n */\n sideEffect?: PermissionSideEffect<any, any>;\n\n /**\n * The Permission may be available to only a subset of the subject types. If so, specify the subject types as an array.\n * If a subject with a type not in this array tries to request the permission, the call will fail.\n *\n * Leaving this as undefined uses default behaviour where the permission is available to request for all subject types.\n */\n subjectTypes?: readonly SubjectType[];\n};\n\n/**\n * The constraint for restricted method permission specification objects.\n * Permissions that correspond to JSON-RPC methods are specified using objects\n * that conform to this type.\n *\n * See the README for more details.\n */\nexport type RestrictedMethodSpecificationConstraint =\n PermissionSpecificationBase<PermissionType.RestrictedMethod> & {\n /**\n * The implementation of the restricted method that the permission\n * corresponds to.\n */\n methodImplementation: RestrictedMethod<any, any>;\n };\n\n/**\n * The constraint for endowment permission specification objects. Permissions\n * that endow callers with some restricted resource are specified using objects\n * that conform to this type.\n *\n * See the README for more details.\n */\nexport type EndowmentSpecificationConstraint =\n PermissionSpecificationBase<PermissionType.Endowment> & {\n /**\n * The {@link EndowmentGetter} function for the permission. This function\n * will be called by the {@link PermissionController} whenever the\n * permission is invoked, after which the host can apply the endowments to\n * the requesting subject in the intended manner.\n */\n endowmentGetter: EndowmentGetter<any>;\n };\n\n/**\n * The constraint for permission specification objects. Every {@link Permission}\n * supported by a {@link PermissionController} must have an associated\n * specification, which is the source of truth for all permission-related types.\n * All specifications must adhere to the {@link PermissionSpecificationBase}\n * interface, but specifications may have different fields depending on the\n * {@link PermissionType}.\n *\n * See the README for more details.\n */\nexport type PermissionSpecificationConstraint =\n | EndowmentSpecificationConstraint\n | RestrictedMethodSpecificationConstraint;\n\n/**\n * Options for {@link PermissionSpecificationBuilder} functions.\n */\ntype PermissionSpecificationBuilderOptions<\n FactoryHooks extends Record<string, unknown>,\n MethodHooks extends Record<string, unknown>,\n ValidatorHooks extends Record<string, unknown>,\n> = {\n targetKey?: string;\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n factoryHooks?: FactoryHooks;\n methodHooks?: MethodHooks;\n validatorHooks?: ValidatorHooks;\n};\n\n/**\n * A function that builds a permission specification. Modules that specify\n * permissions for external consumption should make this their primary /\n * default export so that host applications can use them to generate concrete\n * specifications tailored to their requirements.\n */\nexport type PermissionSpecificationBuilder<\n Type extends PermissionType,\n Options extends PermissionSpecificationBuilderOptions<any, any, any>,\n Specification extends PermissionSpecificationConstraint & {\n permissionType: Type;\n },\n> = (options: Options) => Specification;\n\n/**\n * A restricted method permission export object, containing the\n * {@link PermissionSpecificationBuilder} function and \"hook name\" objects.\n */\nexport type PermissionSpecificationBuilderExportConstraint = {\n targetKey: string;\n specificationBuilder: PermissionSpecificationBuilder<\n PermissionType,\n PermissionSpecificationBuilderOptions<any, any, any>,\n PermissionSpecificationConstraint\n >;\n factoryHookNames?: Record<string, true>;\n methodHookNames?: Record<string, true>;\n validatorHookNames?: Record<string, true>;\n};\n\ntype ValidRestrictedMethodSpecification<\n Specification extends RestrictedMethodSpecificationConstraint,\n> = Specification['methodImplementation'] extends ValidRestrictedMethod<\n Specification['methodImplementation']\n>\n ? Specification\n : never;\n\n/**\n * Constraint for {@link PermissionSpecificationConstraint} objects that\n * evaluates to `never` if the specification contains any invalid fields.\n *\n * @template Specification - The permission specification to validate.\n */\nexport type ValidPermissionSpecification<\n Specification extends PermissionSpecificationConstraint,\n> = Specification['targetKey'] extends ValidTargetKey<\n Specification['targetKey']\n>\n ? Specification['permissionType'] extends PermissionType.Endowment\n ? Specification\n : Specification['permissionType'] extends PermissionType.RestrictedMethod\n ? ValidRestrictedMethodSpecification<\n Extract<Specification, RestrictedMethodSpecificationConstraint>\n >\n : never\n : never;\n\n/**\n * Checks that the specification has the expected permission type.\n *\n * @param specification - The specification to check.\n * @param expectedType - The expected permission type.\n * @template Specification - The specification to check.\n * @template Type - The expected permission type.\n * @returns Whether or not the specification is of the expected type.\n */\nexport function hasSpecificationType<\n Specification extends PermissionSpecificationConstraint,\n Type extends PermissionType,\n>(\n specification: Specification,\n expectedType: Type,\n): specification is Specification & {\n permissionType: Type;\n} {\n return specification.permissionType === expectedType;\n}\n\n/**\n * The specifications for all permissions supported by a particular\n * {@link PermissionController}.\n *\n * @template Specifications - The union of all {@link PermissionSpecificationConstraint} types.\n */\nexport type PermissionSpecificationMap<\n Specification extends PermissionSpecificationConstraint,\n> = {\n [TargetKey in Specification['targetKey']]: Specification extends {\n targetKey: TargetKey;\n }\n ? Specification\n : never;\n};\n\n/**\n * Extracts a specific {@link PermissionSpecificationConstraint} from a union of\n * permission specifications.\n *\n * @template Specification - The specification union type to extract from.\n * @template TargetKey - The `targetKey` of the specification to extract.\n */\nexport type ExtractPermissionSpecification<\n Specification extends PermissionSpecificationConstraint,\n TargetKey extends Specification['targetKey'],\n> = Specification extends {\n targetKey: TargetKey;\n}\n ? Specification\n : never;\n"]}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Patch } from 'immer';
|
|
2
2
|
import { AcceptRequest as AcceptApprovalRequest, AddApprovalRequest, HasApprovalRequest, RejectRequest as RejectApprovalRequest } from '@metamask/approval-controller';
|
|
3
|
-
import { BaseControllerV2, RestrictedControllerMessenger } from '@metamask/base-controller';
|
|
3
|
+
import { BaseControllerV2, RestrictedControllerMessenger, ActionConstraint, EventConstraint } from '@metamask/base-controller';
|
|
4
4
|
import { Json, NonEmptyArray } from '@metamask/controller-utils';
|
|
5
|
+
import { GetSubjectMetadata } from './SubjectMetadataController';
|
|
5
6
|
import { CaveatConstraint, CaveatSpecificationConstraint, CaveatSpecificationMap, ExtractCaveat, ExtractCaveats, ExtractCaveatValue } from './Caveat';
|
|
6
|
-
import { EndowmentSpecificationConstraint, ExtractAllowedCaveatTypes, OriginString, PermissionConstraint, PermissionSpecificationConstraint, PermissionSpecificationMap, RequestedPermissions, RestrictedMethod, RestrictedMethodParameters, RestrictedMethodSpecificationConstraint, ValidPermission, ValidPermissionSpecification } from './Permission';
|
|
7
|
+
import { EndowmentSpecificationConstraint, ExtractAllowedCaveatTypes, OriginString, PermissionConstraint, PermissionSpecificationConstraint, PermissionSpecificationMap, RequestedPermissions, RestrictedMethod, RestrictedMethodParameters, RestrictedMethodSpecificationConstraint, SideEffectHandler, ValidPermission, ValidPermissionSpecification } from './Permission';
|
|
7
8
|
import { getPermissionMiddlewareFactory } from './permission-middleware';
|
|
8
9
|
/**
|
|
9
10
|
* Metadata associated with {@link PermissionController} subjects.
|
|
@@ -30,6 +31,10 @@ export declare type PermissionsRequest = {
|
|
|
30
31
|
permissions: RequestedPermissions;
|
|
31
32
|
[key: string]: Json;
|
|
32
33
|
};
|
|
34
|
+
export declare type SideEffects = {
|
|
35
|
+
permittedHandlers: Record<string, SideEffectHandler<any, any>>;
|
|
36
|
+
failureHandlers: Record<string, SideEffectHandler<any, any>>;
|
|
37
|
+
};
|
|
33
38
|
/**
|
|
34
39
|
* The name of the {@link PermissionController}.
|
|
35
40
|
*/
|
|
@@ -175,11 +180,12 @@ export declare type PermissionControllerEvents = PermissionControllerStateChange
|
|
|
175
180
|
* The external {@link ControllerMessenger} actions available to the
|
|
176
181
|
* {@link PermissionController}.
|
|
177
182
|
*/
|
|
178
|
-
declare type AllowedActions = AddApprovalRequest | HasApprovalRequest | AcceptApprovalRequest | RejectApprovalRequest;
|
|
183
|
+
declare type AllowedActions = AddApprovalRequest | HasApprovalRequest | AcceptApprovalRequest | RejectApprovalRequest | GetSubjectMetadata;
|
|
179
184
|
/**
|
|
180
185
|
* The messenger of the {@link PermissionController}.
|
|
181
186
|
*/
|
|
182
187
|
export declare type PermissionControllerMessenger = RestrictedControllerMessenger<typeof controllerName, PermissionControllerActions | AllowedActions, PermissionControllerEvents, AllowedActions['type'], never>;
|
|
188
|
+
export declare type SideEffectMessenger<Actions extends ActionConstraint, Events extends EventConstraint> = RestrictedControllerMessenger<typeof controllerName, Actions, Events, string, never>;
|
|
183
189
|
/**
|
|
184
190
|
* A generic {@link PermissionController}.
|
|
185
191
|
*/
|
|
@@ -650,6 +656,7 @@ export declare class PermissionController<ControllerPermissionSpecification exte
|
|
|
650
656
|
}): SubjectPermissions<ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>>;
|
|
651
657
|
/**
|
|
652
658
|
* Validates the specified permission by:
|
|
659
|
+
* - Ensuring that if `subjectTypes` is specified, the subject requesting the permission is of a type in the list.
|
|
653
660
|
* - Ensuring that its `caveats` property is either `null` or a non-empty array.
|
|
654
661
|
* - Ensuring that it only includes caveats allowed by its specification.
|
|
655
662
|
* - Ensuring that it includes no duplicate caveats (by caveat type).
|
|
@@ -735,6 +742,7 @@ export declare class PermissionController<ControllerPermissionSpecification exte
|
|
|
735
742
|
}): Promise<[
|
|
736
743
|
SubjectPermissions<ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>>,
|
|
737
744
|
{
|
|
745
|
+
data?: Record<string, unknown>;
|
|
738
746
|
id: string;
|
|
739
747
|
origin: OriginString;
|
|
740
748
|
}
|
|
@@ -764,6 +772,22 @@ export declare class PermissionController<ControllerPermissionSpecification exte
|
|
|
764
772
|
* @returns The approved permissions request object.
|
|
765
773
|
*/
|
|
766
774
|
private requestUserApproval;
|
|
775
|
+
/**
|
|
776
|
+
* Reunites all the side-effects (onPermitted and onFailure) of the requested permissions inside a record of arrays.
|
|
777
|
+
*
|
|
778
|
+
* @param permissions - The approved permissions.
|
|
779
|
+
* @returns The {@link SideEffects} object containing the handlers arrays.
|
|
780
|
+
*/
|
|
781
|
+
private getSideEffects;
|
|
782
|
+
/**
|
|
783
|
+
* Executes the side-effects of the approved permissions while handling the errors if any.
|
|
784
|
+
* It will pass an instance of the {@link messagingSystem} and the request data associated with the permission request to the handlers through its params.
|
|
785
|
+
*
|
|
786
|
+
* @param sideEffects - the side-effect record created by {@link getSideEffects}
|
|
787
|
+
* @param requestData - the permissions requestData.
|
|
788
|
+
* @returns the value returned by all the `onPermitted` handlers in an array.
|
|
789
|
+
*/
|
|
790
|
+
private executeSideEffects;
|
|
767
791
|
/**
|
|
768
792
|
* Validates an approved {@link PermissionsRequest} object. The approved
|
|
769
793
|
* request must have the required `metadata` and `permissions` properties,
|
|
@@ -875,3 +899,4 @@ export declare class PermissionController<ControllerPermissionSpecification exte
|
|
|
875
899
|
private _executeRestrictedMethod;
|
|
876
900
|
}
|
|
877
901
|
export {};
|
|
902
|
+
//# sourceMappingURL=PermissionController.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PermissionController.d.ts","sourceRoot":"","sources":["../src/PermissionController.ts"],"names":[],"mappings":"AAGA,OAAO,EAAoB,KAAK,EAAE,MAAM,OAAO,CAAC;AAGhD,OAAO,EACL,aAAa,IAAI,qBAAqB,EACtC,kBAAkB,EAClB,kBAAkB,EAClB,aAAa,IAAI,qBAAqB,EACvC,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACL,gBAAgB,EAEhB,6BAA6B,EAC7B,gBAAgB,EAChB,eAAe,EAChB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAKL,IAAI,EACJ,aAAa,EACd,MAAM,4BAA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C;IAoBhB;;;;;;;;;;;;;;;;;OAiBG;IACH,OAAO,CAAC,wBAAwB;CAsBjC"}
|
|
@@ -792,6 +792,7 @@ class PermissionController extends base_controller_1.BaseControllerV2 {
|
|
|
792
792
|
}
|
|
793
793
|
/**
|
|
794
794
|
* Validates the specified permission by:
|
|
795
|
+
* - Ensuring that if `subjectTypes` is specified, the subject requesting the permission is of a type in the list.
|
|
795
796
|
* - Ensuring that its `caveats` property is either `null` or a non-empty array.
|
|
796
797
|
* - Ensuring that it only includes caveats allowed by its specification.
|
|
797
798
|
* - Ensuring that it includes no duplicate caveats (by caveat type).
|
|
@@ -815,7 +816,19 @@ class PermissionController extends base_controller_1.BaseControllerV2 {
|
|
|
815
816
|
invokePermissionValidator: true,
|
|
816
817
|
performCaveatValidation: true,
|
|
817
818
|
}) {
|
|
819
|
+
var _a;
|
|
818
820
|
const { allowedCaveats, validator } = specification;
|
|
821
|
+
if (((_a = specification.subjectTypes) === null || _a === void 0 ? void 0 : _a.length) &&
|
|
822
|
+
specification.subjectTypes.length > 0) {
|
|
823
|
+
const metadata = this.messagingSystem.call('SubjectMetadataController:getSubjectMetadata', origin);
|
|
824
|
+
if (!metadata ||
|
|
825
|
+
metadata.subjectType === null ||
|
|
826
|
+
!specification.subjectTypes.includes(metadata.subjectType)) {
|
|
827
|
+
throw specification.permissionType === Permission_1.PermissionType.RestrictedMethod
|
|
828
|
+
? (0, errors_1.methodNotFound)(targetName, { origin })
|
|
829
|
+
: new errors_1.EndowmentPermissionDoesNotExistError(targetName, origin);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
819
832
|
if ((0, controller_utils_1.hasProperty)(permission, 'caveats')) {
|
|
820
833
|
const { caveats } = permission;
|
|
821
834
|
if (caveats !== null && !(Array.isArray(caveats) && caveats.length > 0)) {
|
|
@@ -951,7 +964,22 @@ class PermissionController extends base_controller_1.BaseControllerV2 {
|
|
|
951
964
|
metadata,
|
|
952
965
|
permissions: requestedPermissions,
|
|
953
966
|
};
|
|
954
|
-
const
|
|
967
|
+
const approvedRequest = yield this.requestUserApproval(permissionsRequest);
|
|
968
|
+
const { permissions: approvedPermissions } = approvedRequest, requestData = __rest(approvedRequest, ["permissions"]);
|
|
969
|
+
const sideEffects = this.getSideEffects(approvedPermissions);
|
|
970
|
+
if (Object.values(sideEffects.permittedHandlers).length > 0) {
|
|
971
|
+
const sideEffectsData = yield this.executeSideEffects(sideEffects, approvedRequest);
|
|
972
|
+
const mappedData = Object.keys(sideEffects.permittedHandlers).reduce((acc, permission, i) => (Object.assign({ [permission]: sideEffectsData[i] }, acc)), {});
|
|
973
|
+
return [
|
|
974
|
+
this.grantPermissions({
|
|
975
|
+
subject,
|
|
976
|
+
approvedPermissions,
|
|
977
|
+
preserveExistingPermissions,
|
|
978
|
+
requestData,
|
|
979
|
+
}),
|
|
980
|
+
Object.assign({ data: mappedData }, metadata),
|
|
981
|
+
];
|
|
982
|
+
}
|
|
955
983
|
return [
|
|
956
984
|
this.grantPermissions({
|
|
957
985
|
subject,
|
|
@@ -1033,6 +1061,69 @@ class PermissionController extends base_controller_1.BaseControllerV2 {
|
|
|
1033
1061
|
return approvedRequest;
|
|
1034
1062
|
});
|
|
1035
1063
|
}
|
|
1064
|
+
/**
|
|
1065
|
+
* Reunites all the side-effects (onPermitted and onFailure) of the requested permissions inside a record of arrays.
|
|
1066
|
+
*
|
|
1067
|
+
* @param permissions - The approved permissions.
|
|
1068
|
+
* @returns The {@link SideEffects} object containing the handlers arrays.
|
|
1069
|
+
*/
|
|
1070
|
+
getSideEffects(permissions) {
|
|
1071
|
+
return Object.keys(permissions).reduce((sideEffectList, targetName) => {
|
|
1072
|
+
const targetKey = this.getTargetKey(targetName);
|
|
1073
|
+
if (targetKey) {
|
|
1074
|
+
const specification = this.getPermissionSpecification(targetKey);
|
|
1075
|
+
if (specification.sideEffect) {
|
|
1076
|
+
sideEffectList.permittedHandlers[targetName] =
|
|
1077
|
+
specification.sideEffect.onPermitted;
|
|
1078
|
+
if (specification.sideEffect.onFailure) {
|
|
1079
|
+
sideEffectList.failureHandlers[targetName] =
|
|
1080
|
+
specification.sideEffect.onFailure;
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
return sideEffectList;
|
|
1085
|
+
}, { permittedHandlers: {}, failureHandlers: {} });
|
|
1086
|
+
}
|
|
1087
|
+
/**
|
|
1088
|
+
* Executes the side-effects of the approved permissions while handling the errors if any.
|
|
1089
|
+
* It will pass an instance of the {@link messagingSystem} and the request data associated with the permission request to the handlers through its params.
|
|
1090
|
+
*
|
|
1091
|
+
* @param sideEffects - the side-effect record created by {@link getSideEffects}
|
|
1092
|
+
* @param requestData - the permissions requestData.
|
|
1093
|
+
* @returns the value returned by all the `onPermitted` handlers in an array.
|
|
1094
|
+
*/
|
|
1095
|
+
executeSideEffects(sideEffects, requestData) {
|
|
1096
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1097
|
+
const { permittedHandlers, failureHandlers } = sideEffects;
|
|
1098
|
+
const params = {
|
|
1099
|
+
requestData,
|
|
1100
|
+
messagingSystem: this.messagingSystem,
|
|
1101
|
+
};
|
|
1102
|
+
const promiseResults = yield Promise.allSettled(Object.values(permittedHandlers).map((permittedHandler) => permittedHandler(params)));
|
|
1103
|
+
// lib.es2020.promise.d.ts does not export its types so we're using a simple type.
|
|
1104
|
+
const rejectedHandlers = promiseResults.filter((promise) => promise.status === 'rejected');
|
|
1105
|
+
if (rejectedHandlers.length > 0) {
|
|
1106
|
+
const failureHandlersList = Object.values(failureHandlers);
|
|
1107
|
+
if (failureHandlersList.length > 0) {
|
|
1108
|
+
try {
|
|
1109
|
+
yield Promise.all(failureHandlersList.map((failureHandler) => failureHandler(params)));
|
|
1110
|
+
}
|
|
1111
|
+
catch (error) {
|
|
1112
|
+
throw (0, errors_1.internalError)('Unexpected error in side-effects', { error });
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
const reasons = rejectedHandlers.map((handler) => handler.reason);
|
|
1116
|
+
reasons.forEach((reason) => {
|
|
1117
|
+
console.error(reason);
|
|
1118
|
+
});
|
|
1119
|
+
throw reasons.length > 1
|
|
1120
|
+
? (0, errors_1.internalError)('Multiple errors occurred during side-effects execution', { errors: reasons })
|
|
1121
|
+
: reasons[0];
|
|
1122
|
+
}
|
|
1123
|
+
// lib.es2020.promise.d.ts does not export its types so we're using a simple type.
|
|
1124
|
+
return promiseResults.map(({ value }) => value);
|
|
1125
|
+
});
|
|
1126
|
+
}
|
|
1036
1127
|
/**
|
|
1037
1128
|
* Validates an approved {@link PermissionsRequest} object. The approved
|
|
1038
1129
|
* request must have the required `metadata` and `permissions` properties,
|