@metamask/permission-controller 12.2.1 → 13.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +85 -6
- package/dist/Permission.cjs.map +1 -1
- package/dist/Permission.d.cts +9 -15
- package/dist/Permission.d.cts.map +1 -1
- package/dist/Permission.d.mts +9 -15
- package/dist/Permission.d.mts.map +1 -1
- package/dist/Permission.mjs.map +1 -1
- package/dist/PermissionController-method-action-types.cjs +7 -0
- package/dist/PermissionController-method-action-types.cjs.map +1 -0
- package/dist/PermissionController-method-action-types.d.cts +301 -0
- package/dist/PermissionController-method-action-types.d.cts.map +1 -0
- package/dist/PermissionController-method-action-types.d.mts +301 -0
- package/dist/PermissionController-method-action-types.d.mts.map +1 -0
- package/dist/PermissionController-method-action-types.mjs +6 -0
- package/dist/PermissionController-method-action-types.mjs.map +1 -0
- package/dist/PermissionController.cjs +41 -45
- package/dist/PermissionController.cjs.map +1 -1
- package/dist/PermissionController.d.cts +59 -33
- package/dist/PermissionController.d.cts.map +1 -1
- package/dist/PermissionController.d.mts +59 -33
- package/dist/PermissionController.d.mts.map +1 -1
- package/dist/PermissionController.mjs +41 -45
- package/dist/PermissionController.mjs.map +1 -1
- package/dist/SubjectMetadataController-method-action-types.cjs +7 -0
- package/dist/SubjectMetadataController-method-action-types.cjs.map +1 -0
- package/dist/SubjectMetadataController-method-action-types.d.cts +51 -0
- package/dist/SubjectMetadataController-method-action-types.d.cts.map +1 -0
- package/dist/SubjectMetadataController-method-action-types.d.mts +51 -0
- package/dist/SubjectMetadataController-method-action-types.d.mts.map +1 -0
- package/dist/SubjectMetadataController-method-action-types.mjs +6 -0
- package/dist/SubjectMetadataController-method-action-types.mjs.map +1 -0
- package/dist/SubjectMetadataController.cjs +7 -2
- package/dist/SubjectMetadataController.cjs.map +1 -1
- package/dist/SubjectMetadataController.d.cts +16 -4
- package/dist/SubjectMetadataController.d.cts.map +1 -1
- package/dist/SubjectMetadataController.d.mts +16 -4
- package/dist/SubjectMetadataController.d.mts.map +1 -1
- package/dist/SubjectMetadataController.mjs +7 -2
- package/dist/SubjectMetadataController.mjs.map +1 -1
- package/dist/createRestrictedMethodMessenger.cjs +20 -0
- package/dist/createRestrictedMethodMessenger.cjs.map +1 -0
- package/dist/createRestrictedMethodMessenger.d.cts +42 -0
- package/dist/createRestrictedMethodMessenger.d.cts.map +1 -0
- package/dist/createRestrictedMethodMessenger.d.mts +42 -0
- package/dist/createRestrictedMethodMessenger.d.mts.map +1 -0
- package/dist/createRestrictedMethodMessenger.mjs +16 -0
- package/dist/createRestrictedMethodMessenger.mjs.map +1 -0
- package/dist/index.cjs +6 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +5 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -1
- package/dist/permission-middleware.cjs +47 -43
- package/dist/permission-middleware.cjs.map +1 -1
- package/dist/permission-middleware.d.cts +40 -25
- package/dist/permission-middleware.d.cts.map +1 -1
- package/dist/permission-middleware.d.mts +40 -25
- package/dist/permission-middleware.d.mts.map +1 -1
- package/dist/permission-middleware.mjs +44 -41
- package/dist/permission-middleware.mjs.map +1 -1
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +0 -23
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +0 -23
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs.map +1 -1
- package/package.json +22 -19
- package/dist/rpc-methods/getPermissions.cjs +0 -27
- package/dist/rpc-methods/getPermissions.cjs.map +0 -1
- package/dist/rpc-methods/getPermissions.d.cts +0 -9
- package/dist/rpc-methods/getPermissions.d.cts.map +0 -1
- package/dist/rpc-methods/getPermissions.d.mts +0 -9
- package/dist/rpc-methods/getPermissions.d.mts.map +0 -1
- package/dist/rpc-methods/getPermissions.mjs +0 -24
- package/dist/rpc-methods/getPermissions.mjs.map +0 -1
- package/dist/rpc-methods/index.cjs +0 -12
- package/dist/rpc-methods/index.cjs.map +0 -1
- package/dist/rpc-methods/index.d.cts +0 -6
- package/dist/rpc-methods/index.d.cts.map +0 -1
- package/dist/rpc-methods/index.d.mts +0 -6
- package/dist/rpc-methods/index.d.mts.map +0 -1
- package/dist/rpc-methods/index.mjs +0 -9
- package/dist/rpc-methods/index.mjs.map +0 -1
- package/dist/rpc-methods/requestPermissions.cjs +0 -36
- package/dist/rpc-methods/requestPermissions.cjs.map +0 -1
- package/dist/rpc-methods/requestPermissions.d.cts +0 -17
- package/dist/rpc-methods/requestPermissions.d.cts.map +0 -1
- package/dist/rpc-methods/requestPermissions.d.mts +0 -17
- package/dist/rpc-methods/requestPermissions.d.mts.map +0 -1
- package/dist/rpc-methods/requestPermissions.mjs +0 -33
- package/dist/rpc-methods/requestPermissions.mjs.map +0 -1
- package/dist/rpc-methods/revokePermissions.cjs +0 -41
- package/dist/rpc-methods/revokePermissions.cjs.map +0 -1
- package/dist/rpc-methods/revokePermissions.d.cts +0 -11
- package/dist/rpc-methods/revokePermissions.d.cts.map +0 -1
- package/dist/rpc-methods/revokePermissions.d.mts +0 -11
- package/dist/rpc-methods/revokePermissions.d.mts.map +0 -1
- package/dist/rpc-methods/revokePermissions.mjs +0 -38
- package/dist/rpc-methods/revokePermissions.mjs.map +0 -1
|
@@ -9,7 +9,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
10
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
11
|
};
|
|
12
|
-
var _PermissionController_instances, _PermissionController_caveatSpecifications, _PermissionController_permissionSpecifications, _PermissionController_unrestrictedMethods, _PermissionController_getPermissionSpecification, _PermissionController_getCaveatSpecification, _PermissionController_expectGetCaveatMerger, _PermissionController_validatePermissionSpecifications,
|
|
12
|
+
var _PermissionController_instances, _PermissionController_caveatSpecifications, _PermissionController_permissionSpecifications, _PermissionController_unrestrictedMethods, _PermissionController_getPermissionSpecification, _PermissionController_getCaveatSpecification, _PermissionController_expectGetCaveatMerger, _PermissionController_validatePermissionSpecifications, _PermissionController_getTypedPermissionSpecification, _PermissionController_getRestrictedMethod, _PermissionController_deletePermission, _PermissionController_setCaveat, _PermissionController_deleteCaveat, _PermissionController_validateModifiedPermission, _PermissionController_targetExists, _PermissionController_applyGrantedPermissions, _PermissionController_validatePermission, _PermissionController_setValidatedPermissions, _PermissionController_constructCaveats, _PermissionController_validateCaveat, _PermissionController_validateRequestedPermissions, _PermissionController_mergeIncrementalPermissions, _PermissionController_mergePermission, _PermissionController_mergeCaveat, _PermissionController_requestUserApproval, _PermissionController_handleApprovedPermissions, _PermissionController_getSideEffects, _PermissionController_executeSideEffects, _PermissionController_validateApprovedPermissions, _PermissionController_hasApprovalRequest, _PermissionController_rejectPermissionsRequest, _PermissionController_executeRestrictedMethod;
|
|
13
13
|
function $importDefault(module) {
|
|
14
14
|
if (module?.__esModule) {
|
|
15
15
|
return module.default;
|
|
@@ -27,12 +27,30 @@ import { nanoid } from "nanoid";
|
|
|
27
27
|
import { decorateWithCaveats, isRestrictedMethodCaveatSpecification } from "./Caveat.mjs";
|
|
28
28
|
import { CaveatAlreadyExistsError, CaveatDoesNotExistError, CaveatInvalidJsonError, CaveatMergerDoesNotExistError, CaveatMergeTypeMismatchError, CaveatMissingValueError, CaveatSpecificationMismatchError, DuplicateCaveatError, EndowmentPermissionDoesNotExistError, ForbiddenCaveatError, internalError, InvalidApprovedPermissionError, InvalidCaveatError, InvalidCaveatFieldsError, InvalidCaveatsPropertyError, InvalidCaveatTypeError, InvalidMergedPermissionsError, invalidParams, InvalidSubjectIdentifierError, methodNotFound, PermissionDoesNotExistError, PermissionsRequestNotFoundError, unauthorized, UnrecognizedCaveatTypeError, UnrecognizedSubjectError, userRejectedRequest } from "./errors.mjs";
|
|
29
29
|
import { constructPermission, findCaveat, hasSpecificationType, PermissionType } from "./Permission.mjs";
|
|
30
|
-
import { getPermissionMiddlewareFactory } from "./permission-middleware.mjs";
|
|
31
30
|
import { collectUniqueAndPairedCaveats, MethodNames } from "./utils.mjs";
|
|
32
31
|
/**
|
|
33
32
|
* The name of the {@link PermissionController}.
|
|
34
33
|
*/
|
|
35
34
|
const controllerName = 'PermissionController';
|
|
35
|
+
const MESSENGER_EXPOSED_METHODS = [
|
|
36
|
+
'clearState',
|
|
37
|
+
'executeRestrictedMethod',
|
|
38
|
+
'getEndowments',
|
|
39
|
+
'getSubjectNames',
|
|
40
|
+
'getPermissions',
|
|
41
|
+
'hasPermission',
|
|
42
|
+
'hasPermissions',
|
|
43
|
+
'hasUnrestrictedMethod',
|
|
44
|
+
'grantPermissions',
|
|
45
|
+
'grantPermissionsIncremental',
|
|
46
|
+
'requestPermissions',
|
|
47
|
+
'requestPermissionsIncremental',
|
|
48
|
+
'revokeAllPermissions',
|
|
49
|
+
'revokePermissionForAllSubjects',
|
|
50
|
+
'revokePermissions',
|
|
51
|
+
'updateCaveat',
|
|
52
|
+
'getCaveat',
|
|
53
|
+
];
|
|
36
54
|
/**
|
|
37
55
|
* Get the state metadata of the {@link PermissionController}.
|
|
38
56
|
*
|
|
@@ -90,6 +108,18 @@ export class PermissionController extends BaseController {
|
|
|
90
108
|
get unrestrictedMethods() {
|
|
91
109
|
return __classPrivateFieldGet(this, _PermissionController_unrestrictedMethods, "f");
|
|
92
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Checks whether the given method was declared as unrestricted at
|
|
113
|
+
* construction time. Methods unknown to the controller return `false` and
|
|
114
|
+
* would be treated as restricted by callers such as the permission
|
|
115
|
+
* middleware.
|
|
116
|
+
*
|
|
117
|
+
* @param method - The name of the method to check.
|
|
118
|
+
* @returns Whether the method is unrestricted.
|
|
119
|
+
*/
|
|
120
|
+
hasUnrestrictedMethod(method) {
|
|
121
|
+
return __classPrivateFieldGet(this, _PermissionController_unrestrictedMethods, "f").has(method);
|
|
122
|
+
}
|
|
93
123
|
/**
|
|
94
124
|
* Constructs the PermissionController.
|
|
95
125
|
*
|
|
@@ -128,12 +158,7 @@ export class PermissionController extends BaseController {
|
|
|
128
158
|
__classPrivateFieldSet(this, _PermissionController_permissionSpecifications, deepFreeze({
|
|
129
159
|
...permissionSpecifications,
|
|
130
160
|
}), "f");
|
|
131
|
-
|
|
132
|
-
this.createPermissionMiddleware = getPermissionMiddlewareFactory({
|
|
133
|
-
executeRestrictedMethod: __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_executeRestrictedMethod).bind(this),
|
|
134
|
-
getRestrictedMethod: this.getRestrictedMethod.bind(this),
|
|
135
|
-
isUnrestrictedMethod: this.unrestrictedMethods.has.bind(this.unrestrictedMethods),
|
|
136
|
-
});
|
|
161
|
+
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
137
162
|
}
|
|
138
163
|
/**
|
|
139
164
|
* Clears the state of the controller.
|
|
@@ -145,21 +170,6 @@ export class PermissionController extends BaseController {
|
|
|
145
170
|
};
|
|
146
171
|
});
|
|
147
172
|
}
|
|
148
|
-
/**
|
|
149
|
-
* Gets the implementation of the specified restricted method.
|
|
150
|
-
*
|
|
151
|
-
* A JSON-RPC error is thrown if the method does not exist.
|
|
152
|
-
*
|
|
153
|
-
* @see {@link PermissionController.executeRestrictedMethod} and
|
|
154
|
-
* {@link PermissionController.createPermissionMiddleware} for internal usage.
|
|
155
|
-
* @param method - The name of the restricted method.
|
|
156
|
-
* @param origin - The origin associated with the request for the restricted
|
|
157
|
-
* method, if any.
|
|
158
|
-
* @returns The restricted method implementation.
|
|
159
|
-
*/
|
|
160
|
-
getRestrictedMethod(method, origin) {
|
|
161
|
-
return __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_getTypedPermissionSpecification).call(this, PermissionType.RestrictedMethod, method, origin).methodImplementation;
|
|
162
|
-
}
|
|
163
173
|
/**
|
|
164
174
|
* Gets a list of all origins of subjects.
|
|
165
175
|
*
|
|
@@ -213,7 +223,7 @@ export class PermissionController extends BaseController {
|
|
|
213
223
|
/**
|
|
214
224
|
* Revokes all permissions from the specified origin.
|
|
215
225
|
*
|
|
216
|
-
* Throws an error
|
|
226
|
+
* Throws an error if the origin has no permissions.
|
|
217
227
|
*
|
|
218
228
|
* @param origin - The origin whose permissions to revoke.
|
|
219
229
|
*/
|
|
@@ -725,10 +735,12 @@ export class PermissionController extends BaseController {
|
|
|
725
735
|
*/
|
|
726
736
|
async executeRestrictedMethod(origin, targetName, params) {
|
|
727
737
|
// Throws if the method does not exist
|
|
728
|
-
const methodImplementation = this.
|
|
738
|
+
const methodImplementation = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_getRestrictedMethod).call(this, targetName, origin);
|
|
729
739
|
const result = await __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_executeRestrictedMethod).call(this, methodImplementation, { origin }, targetName, params);
|
|
740
|
+
// This is impossible if the restricted method implementation is typed correctly,
|
|
741
|
+
// but we maintain it for backwards compatibility.
|
|
730
742
|
if (result === undefined) {
|
|
731
|
-
throw new Error(`
|
|
743
|
+
throw new Error(`Request for method "${targetName}" as origin "${origin}" returned no result.`);
|
|
732
744
|
}
|
|
733
745
|
return result;
|
|
734
746
|
}
|
|
@@ -770,27 +782,9 @@ _PermissionController_caveatSpecifications = new WeakMap(), _PermissionControlle
|
|
|
770
782
|
});
|
|
771
783
|
}
|
|
772
784
|
});
|
|
773
|
-
}, _PermissionController_registerMessageHandlers = function _PermissionController_registerMessageHandlers() {
|
|
774
|
-
this.messenger.registerActionHandler(`${controllerName}:clearPermissions`, () => this.clearState());
|
|
775
|
-
this.messenger.registerActionHandler(`${controllerName}:getEndowments`, (origin, targetName, requestData) => this.getEndowments(origin, targetName, requestData));
|
|
776
|
-
this.messenger.registerActionHandler(`${controllerName}:getSubjectNames`, () => this.getSubjectNames());
|
|
777
|
-
this.messenger.registerActionHandler(`${controllerName}:getPermissions`, (origin) => this.getPermissions(origin));
|
|
778
|
-
this.messenger.registerActionHandler(`${controllerName}:hasPermission`, (origin, targetName) => this.hasPermission(origin, targetName));
|
|
779
|
-
this.messenger.registerActionHandler(`${controllerName}:hasPermissions`, (origin) => this.hasPermissions(origin));
|
|
780
|
-
this.messenger.registerActionHandler(`${controllerName}:grantPermissions`, this.grantPermissions.bind(this));
|
|
781
|
-
this.messenger.registerActionHandler(`${controllerName}:grantPermissionsIncremental`, this.grantPermissionsIncremental.bind(this));
|
|
782
|
-
this.messenger.registerActionHandler(`${controllerName}:requestPermissions`, (subject, permissions) => this.requestPermissions(subject, permissions));
|
|
783
|
-
this.messenger.registerActionHandler(`${controllerName}:requestPermissionsIncremental`, (subject, permissions) => this.requestPermissionsIncremental(subject, permissions));
|
|
784
|
-
this.messenger.registerActionHandler(`${controllerName}:revokeAllPermissions`, (origin) => this.revokeAllPermissions(origin));
|
|
785
|
-
this.messenger.registerActionHandler(`${controllerName}:revokePermissionForAllSubjects`, (target) => this.revokePermissionForAllSubjects(target));
|
|
786
|
-
this.messenger.registerActionHandler(`${controllerName}:revokePermissions`, this.revokePermissions.bind(this));
|
|
787
|
-
this.messenger.registerActionHandler(`${controllerName}:updateCaveat`, (origin, target, caveatType, caveatValue) => {
|
|
788
|
-
this.updateCaveat(origin, target, caveatType, caveatValue);
|
|
789
|
-
});
|
|
790
|
-
this.messenger.registerActionHandler(`${controllerName}:getCaveat`, (origin, target, caveatType) => this.getCaveat(origin, target, caveatType));
|
|
791
785
|
}, _PermissionController_getTypedPermissionSpecification = function _PermissionController_getTypedPermissionSpecification(permissionType, targetName, requestingOrigin) {
|
|
792
786
|
const failureError = permissionType === PermissionType.RestrictedMethod
|
|
793
|
-
? methodNotFound(targetName,
|
|
787
|
+
? methodNotFound(targetName, { origin: requestingOrigin })
|
|
794
788
|
: new EndowmentPermissionDoesNotExistError(targetName, requestingOrigin);
|
|
795
789
|
if (!__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_targetExists).call(this, targetName)) {
|
|
796
790
|
throw failureError;
|
|
@@ -800,6 +794,8 @@ _PermissionController_caveatSpecifications = new WeakMap(), _PermissionControlle
|
|
|
800
794
|
throw failureError;
|
|
801
795
|
}
|
|
802
796
|
return specification;
|
|
797
|
+
}, _PermissionController_getRestrictedMethod = function _PermissionController_getRestrictedMethod(method, origin) {
|
|
798
|
+
return __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_getTypedPermissionSpecification).call(this, PermissionType.RestrictedMethod, method, origin).methodImplementation;
|
|
803
799
|
}, _PermissionController_deletePermission = function _PermissionController_deletePermission(subjects, origin, target) {
|
|
804
800
|
const { permissions } = subjects[origin];
|
|
805
801
|
if (Object.keys(permissions).length > 1) {
|