@metamask/permission-controller 13.1.1 → 14.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 +18 -1
- package/LICENSE +1 -0
- package/dist/{Caveat.d.mts → Caveat.d.ts} +3 -3
- package/dist/Caveat.d.ts.map +1 -0
- package/dist/{Caveat.mjs → Caveat.js} +4 -4
- package/dist/Caveat.js.map +1 -0
- package/dist/{Permission.d.mts → Permission.d.ts} +7 -7
- package/dist/Permission.d.ts.map +1 -0
- package/dist/{Permission.mjs → Permission.js} +2 -2
- package/dist/Permission.js.map +1 -0
- package/dist/{PermissionController-method-action-types.d.cts → PermissionController-method-action-types.d.ts} +2 -2
- package/dist/PermissionController-method-action-types.d.ts.map +1 -0
- package/dist/{PermissionController-method-action-types.mjs → PermissionController-method-action-types.js} +1 -1
- package/dist/PermissionController-method-action-types.js.map +1 -0
- package/dist/{PermissionController.d.cts → PermissionController.d.ts} +11 -11
- package/dist/PermissionController.d.ts.map +1 -0
- package/dist/PermissionController.js +1533 -0
- package/dist/PermissionController.js.map +1 -0
- package/dist/{SubjectMetadataController-method-action-types.d.cts → SubjectMetadataController-method-action-types.d.ts} +2 -2
- package/dist/SubjectMetadataController-method-action-types.d.ts.map +1 -0
- package/dist/{SubjectMetadataController-method-action-types.mjs → SubjectMetadataController-method-action-types.js} +1 -1
- package/dist/SubjectMetadataController-method-action-types.js.map +1 -0
- package/dist/{SubjectMetadataController.d.cts → SubjectMetadataController.d.ts} +8 -8
- package/dist/SubjectMetadataController.d.ts.map +1 -0
- package/dist/SubjectMetadataController.js +157 -0
- package/dist/SubjectMetadataController.js.map +1 -0
- package/dist/{createRestrictedMethodMessenger.d.cts → createRestrictedMethodMessenger.d.ts} +3 -3
- package/dist/createRestrictedMethodMessenger.d.ts.map +1 -0
- package/dist/{createRestrictedMethodMessenger.mjs → createRestrictedMethodMessenger.js} +2 -2
- package/dist/createRestrictedMethodMessenger.js.map +1 -0
- package/dist/{errors.d.mts → errors.d.ts} +6 -6
- package/dist/errors.d.ts.map +1 -0
- package/dist/{errors.mjs → errors.js} +17 -3
- package/dist/errors.js.map +1 -0
- package/dist/{index.d.cts → index.d.ts} +12 -12
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/{permission-middleware.d.cts → permission-middleware.d.ts} +7 -7
- package/dist/permission-middleware.d.ts.map +1 -0
- package/dist/{permission-middleware.mjs → permission-middleware.js} +2 -2
- package/dist/permission-middleware.js.map +1 -0
- package/dist/{utils.d.cts → utils.d.ts} +3 -3
- package/dist/utils.d.ts.map +1 -0
- package/dist/{utils.mjs → utils.js} +1 -1
- package/dist/utils.js.map +1 -0
- package/package.json +25 -27
- package/dist/Caveat.cjs +0 -48
- package/dist/Caveat.cjs.map +0 -1
- package/dist/Caveat.d.cts +0 -211
- package/dist/Caveat.d.cts.map +0 -1
- package/dist/Caveat.d.mts.map +0 -1
- package/dist/Caveat.mjs.map +0 -1
- package/dist/Permission.cjs +0 -65
- package/dist/Permission.cjs.map +0 -1
- package/dist/Permission.d.cts +0 -415
- package/dist/Permission.d.cts.map +0 -1
- package/dist/Permission.d.mts.map +0 -1
- package/dist/Permission.mjs.map +0 -1
- package/dist/PermissionController-method-action-types.cjs +0 -7
- package/dist/PermissionController-method-action-types.cjs.map +0 -1
- package/dist/PermissionController-method-action-types.d.cts.map +0 -1
- package/dist/PermissionController-method-action-types.d.mts +0 -374
- package/dist/PermissionController-method-action-types.d.mts.map +0 -1
- package/dist/PermissionController-method-action-types.mjs.map +0 -1
- package/dist/PermissionController.cjs +0 -1257
- package/dist/PermissionController.cjs.map +0 -1
- package/dist/PermissionController.d.cts.map +0 -1
- package/dist/PermissionController.d.mts +0 -753
- package/dist/PermissionController.d.mts.map +0 -1
- package/dist/PermissionController.mjs +0 -1257
- package/dist/PermissionController.mjs.map +0 -1
- package/dist/SubjectMetadataController-method-action-types.cjs +0 -7
- package/dist/SubjectMetadataController-method-action-types.cjs.map +0 -1
- package/dist/SubjectMetadataController-method-action-types.d.cts.map +0 -1
- package/dist/SubjectMetadataController-method-action-types.d.mts +0 -51
- package/dist/SubjectMetadataController-method-action-types.d.mts.map +0 -1
- package/dist/SubjectMetadataController-method-action-types.mjs.map +0 -1
- package/dist/SubjectMetadataController.cjs +0 -158
- package/dist/SubjectMetadataController.cjs.map +0 -1
- package/dist/SubjectMetadataController.d.cts.map +0 -1
- package/dist/SubjectMetadataController.d.mts +0 -104
- package/dist/SubjectMetadataController.d.mts.map +0 -1
- package/dist/SubjectMetadataController.mjs +0 -154
- package/dist/SubjectMetadataController.mjs.map +0 -1
- package/dist/createRestrictedMethodMessenger.cjs +0 -20
- package/dist/createRestrictedMethodMessenger.cjs.map +0 -1
- package/dist/createRestrictedMethodMessenger.d.cts.map +0 -1
- package/dist/createRestrictedMethodMessenger.d.mts +0 -42
- package/dist/createRestrictedMethodMessenger.d.mts.map +0 -1
- package/dist/createRestrictedMethodMessenger.mjs.map +0 -1
- package/dist/errors.cjs +0 -226
- package/dist/errors.cjs.map +0 -1
- package/dist/errors.d.cts +0 -183
- package/dist/errors.d.cts.map +0 -1
- package/dist/errors.d.mts.map +0 -1
- package/dist/errors.mjs.map +0 -1
- package/dist/index.cjs +0 -30
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.mts +0 -12
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -9
- package/dist/index.mjs.map +0 -1
- package/dist/permission-middleware.cjs +0 -59
- package/dist/permission-middleware.cjs.map +0 -1
- package/dist/permission-middleware.d.cts.map +0 -1
- package/dist/permission-middleware.d.mts +0 -49
- package/dist/permission-middleware.d.mts.map +0 -1
- package/dist/permission-middleware.mjs.map +0 -1
- package/dist/utils.cjs +0 -44
- package/dist/utils.cjs.map +0 -1
- package/dist/utils.d.cts.map +0 -1
- package/dist/utils.d.mts +0 -34
- package/dist/utils.d.mts.map +0 -1
- package/dist/utils.mjs.map +0 -1
|
@@ -1,1257 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
8
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
10
|
-
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");
|
|
11
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
|
-
};
|
|
13
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
-
};
|
|
16
|
-
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;
|
|
17
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.PermissionController = exports.CaveatMutatorOperation = void 0;
|
|
19
|
-
const base_controller_1 = require("@metamask/base-controller");
|
|
20
|
-
const controller_utils_1 = require("@metamask/controller-utils");
|
|
21
|
-
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
22
|
-
const utils_1 = require("@metamask/utils");
|
|
23
|
-
const deep_freeze_strict_1 = __importDefault(require("deep-freeze-strict"));
|
|
24
|
-
const immer_1 = require("immer");
|
|
25
|
-
const nanoid_1 = require("nanoid");
|
|
26
|
-
const Caveat_1 = require("./Caveat.cjs");
|
|
27
|
-
const errors_1 = require("./errors.cjs");
|
|
28
|
-
const Permission_1 = require("./Permission.cjs");
|
|
29
|
-
const utils_2 = require("./utils.cjs");
|
|
30
|
-
/**
|
|
31
|
-
* The name of the {@link PermissionController}.
|
|
32
|
-
*/
|
|
33
|
-
const controllerName = 'PermissionController';
|
|
34
|
-
const MESSENGER_EXPOSED_METHODS = [
|
|
35
|
-
'clearState',
|
|
36
|
-
'executeRestrictedMethod',
|
|
37
|
-
'getEndowments',
|
|
38
|
-
'getSubjectNames',
|
|
39
|
-
'getPermissions',
|
|
40
|
-
'hasPermission',
|
|
41
|
-
'hasPermissions',
|
|
42
|
-
'hasUnrestrictedMethod',
|
|
43
|
-
'grantPermissions',
|
|
44
|
-
'grantPermissionsIncremental',
|
|
45
|
-
'requestPermissions',
|
|
46
|
-
'requestPermissionsIncremental',
|
|
47
|
-
'revokeAllPermissions',
|
|
48
|
-
'revokePermissionForAllSubjects',
|
|
49
|
-
'revokePermissions',
|
|
50
|
-
'updateCaveat',
|
|
51
|
-
'getCaveat',
|
|
52
|
-
'acceptPermissionsRequest',
|
|
53
|
-
'rejectPermissionsRequest',
|
|
54
|
-
'revokePermission',
|
|
55
|
-
'updatePermissionsByCaveat',
|
|
56
|
-
'getPermission',
|
|
57
|
-
];
|
|
58
|
-
/**
|
|
59
|
-
* Get the state metadata of the {@link PermissionController}.
|
|
60
|
-
*
|
|
61
|
-
* @template Permission - The controller's permission type union.
|
|
62
|
-
* @returns The state metadata
|
|
63
|
-
*/
|
|
64
|
-
function getStateMetadata() {
|
|
65
|
-
return {
|
|
66
|
-
subjects: {
|
|
67
|
-
includeInStateLogs: true,
|
|
68
|
-
includeInDebugSnapshot: true,
|
|
69
|
-
persist: true,
|
|
70
|
-
usedInUi: true,
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Get the default state of the {@link PermissionController}.
|
|
76
|
-
*
|
|
77
|
-
* @template Permission - The controller's permission type union.
|
|
78
|
-
* @returns The default state of the controller
|
|
79
|
-
*/
|
|
80
|
-
function getDefaultState() {
|
|
81
|
-
return { subjects: {} };
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Describes the possible results of a {@link CaveatMutator} function.
|
|
85
|
-
*/
|
|
86
|
-
var CaveatMutatorOperation;
|
|
87
|
-
(function (CaveatMutatorOperation) {
|
|
88
|
-
CaveatMutatorOperation[CaveatMutatorOperation["Noop"] = 0] = "Noop";
|
|
89
|
-
CaveatMutatorOperation[CaveatMutatorOperation["UpdateValue"] = 1] = "UpdateValue";
|
|
90
|
-
CaveatMutatorOperation[CaveatMutatorOperation["DeleteCaveat"] = 2] = "DeleteCaveat";
|
|
91
|
-
CaveatMutatorOperation[CaveatMutatorOperation["RevokePermission"] = 3] = "RevokePermission";
|
|
92
|
-
})(CaveatMutatorOperation || (exports.CaveatMutatorOperation = CaveatMutatorOperation = {}));
|
|
93
|
-
/**
|
|
94
|
-
* The permission controller. See the [Architecture](../ARCHITECTURE.md)
|
|
95
|
-
* document for details.
|
|
96
|
-
*
|
|
97
|
-
* Assumes the existence of an {@link ApprovalController} reachable via the
|
|
98
|
-
* {@link Messenger}.
|
|
99
|
-
*
|
|
100
|
-
* @template ControllerPermissionSpecification - A union of the types of all
|
|
101
|
-
* permission specifications available to the controller. Any referenced caveats
|
|
102
|
-
* must be included in the controller's caveat specifications.
|
|
103
|
-
* @template ControllerCaveatSpecification - A union of the types of all
|
|
104
|
-
* caveat specifications available to the controller.
|
|
105
|
-
*/
|
|
106
|
-
class PermissionController extends base_controller_1.BaseController {
|
|
107
|
-
/**
|
|
108
|
-
* The names of all JSON-RPC methods that will be ignored by the controller.
|
|
109
|
-
*
|
|
110
|
-
* @returns The names of all unrestricted JSON-RPC methods
|
|
111
|
-
*/
|
|
112
|
-
get unrestrictedMethods() {
|
|
113
|
-
return __classPrivateFieldGet(this, _PermissionController_unrestrictedMethods, "f");
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Checks whether the given method was declared as unrestricted at
|
|
117
|
-
* construction time. Methods unknown to the controller return `false` and
|
|
118
|
-
* would be treated as restricted by callers such as the permission
|
|
119
|
-
* middleware.
|
|
120
|
-
*
|
|
121
|
-
* @param method - The name of the method to check.
|
|
122
|
-
* @returns Whether the method is unrestricted.
|
|
123
|
-
*/
|
|
124
|
-
hasUnrestrictedMethod(method) {
|
|
125
|
-
return __classPrivateFieldGet(this, _PermissionController_unrestrictedMethods, "f").has(method);
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Constructs the PermissionController.
|
|
129
|
-
*
|
|
130
|
-
* @param options - Permission controller options.
|
|
131
|
-
* @param options.caveatSpecifications - The specifications of all caveats
|
|
132
|
-
* available to the controller. See {@link CaveatSpecificationMap} and the
|
|
133
|
-
* documentation for more details.
|
|
134
|
-
* @param options.permissionSpecifications - The specifications of all
|
|
135
|
-
* permissions available to the controller. See
|
|
136
|
-
* {@link PermissionSpecificationMap} and the README for more details.
|
|
137
|
-
* @param options.unrestrictedMethods - The callable names of all JSON-RPC
|
|
138
|
-
* methods ignored by the new controller.
|
|
139
|
-
* @param options.messenger - The messenger. See
|
|
140
|
-
* {@link BaseController} for more information.
|
|
141
|
-
* @param options.state - Existing state to hydrate the controller with at
|
|
142
|
-
* initialization.
|
|
143
|
-
*/
|
|
144
|
-
constructor(options) {
|
|
145
|
-
const { caveatSpecifications, permissionSpecifications, unrestrictedMethods, messenger, state = {}, } = options;
|
|
146
|
-
super({
|
|
147
|
-
name: controllerName,
|
|
148
|
-
metadata: getStateMetadata(),
|
|
149
|
-
messenger,
|
|
150
|
-
state: {
|
|
151
|
-
...getDefaultState(),
|
|
152
|
-
...state,
|
|
153
|
-
},
|
|
154
|
-
});
|
|
155
|
-
_PermissionController_instances.add(this);
|
|
156
|
-
_PermissionController_caveatSpecifications.set(this, void 0);
|
|
157
|
-
_PermissionController_permissionSpecifications.set(this, void 0);
|
|
158
|
-
_PermissionController_unrestrictedMethods.set(this, void 0);
|
|
159
|
-
__classPrivateFieldSet(this, _PermissionController_unrestrictedMethods, new Set(unrestrictedMethods), "f");
|
|
160
|
-
__classPrivateFieldSet(this, _PermissionController_caveatSpecifications, (0, deep_freeze_strict_1.default)({ ...caveatSpecifications }), "f");
|
|
161
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validatePermissionSpecifications).call(this, permissionSpecifications, __classPrivateFieldGet(this, _PermissionController_caveatSpecifications, "f"));
|
|
162
|
-
__classPrivateFieldSet(this, _PermissionController_permissionSpecifications, (0, deep_freeze_strict_1.default)({
|
|
163
|
-
...permissionSpecifications,
|
|
164
|
-
}), "f");
|
|
165
|
-
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Clears the state of the controller.
|
|
169
|
-
*/
|
|
170
|
-
clearState() {
|
|
171
|
-
this.update((_draftState) => {
|
|
172
|
-
return {
|
|
173
|
-
...getDefaultState(),
|
|
174
|
-
};
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Gets a list of all origins of subjects.
|
|
179
|
-
*
|
|
180
|
-
* @returns The origins (i.e. IDs) of all subjects.
|
|
181
|
-
*/
|
|
182
|
-
getSubjectNames() {
|
|
183
|
-
return Object.keys(this.state.subjects);
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Gets the permission for the specified target of the subject corresponding
|
|
187
|
-
* to the specified origin.
|
|
188
|
-
*
|
|
189
|
-
* @param origin - The origin of the subject.
|
|
190
|
-
* @param targetName - The method name as invoked by a third party (i.e., not
|
|
191
|
-
* a method key).
|
|
192
|
-
* @returns The permission if it exists, or undefined otherwise.
|
|
193
|
-
*/
|
|
194
|
-
getPermission(origin, targetName) {
|
|
195
|
-
return this.state.subjects[origin]?.permissions[targetName];
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Gets all permissions for the specified subject, if any.
|
|
199
|
-
*
|
|
200
|
-
* @param origin - The origin of the subject.
|
|
201
|
-
* @returns The permissions of the subject, if any.
|
|
202
|
-
*/
|
|
203
|
-
getPermissions(origin) {
|
|
204
|
-
return this.state.subjects[origin]?.permissions;
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* Checks whether the subject with the specified origin has the specified
|
|
208
|
-
* permission.
|
|
209
|
-
*
|
|
210
|
-
* @param origin - The origin of the subject.
|
|
211
|
-
* @param target - The target name of the permission.
|
|
212
|
-
* @returns Whether the subject has the permission.
|
|
213
|
-
*/
|
|
214
|
-
hasPermission(origin, target) {
|
|
215
|
-
return Boolean(this.getPermission(origin, target));
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Checks whether the subject with the specified origin has any permissions.
|
|
219
|
-
* Use this if you want to know if a subject "exists".
|
|
220
|
-
*
|
|
221
|
-
* @param origin - The origin of the subject to check.
|
|
222
|
-
* @returns Whether the subject has any permissions.
|
|
223
|
-
*/
|
|
224
|
-
hasPermissions(origin) {
|
|
225
|
-
return Boolean(this.state.subjects[origin]);
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* Revokes all permissions from the specified origin.
|
|
229
|
-
*
|
|
230
|
-
* Throws an error if the origin has no permissions.
|
|
231
|
-
*
|
|
232
|
-
* @param origin - The origin whose permissions to revoke.
|
|
233
|
-
*/
|
|
234
|
-
revokeAllPermissions(origin) {
|
|
235
|
-
this.update((draftState) => {
|
|
236
|
-
if (!draftState.subjects[origin]) {
|
|
237
|
-
throw new errors_1.UnrecognizedSubjectError(origin);
|
|
238
|
-
}
|
|
239
|
-
delete draftState.subjects[origin];
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Revokes the specified permission from the subject with the specified
|
|
244
|
-
* origin.
|
|
245
|
-
*
|
|
246
|
-
* Throws an error if the subject or the permission does not exist.
|
|
247
|
-
*
|
|
248
|
-
* @param origin - The origin of the subject whose permission to revoke.
|
|
249
|
-
* @param target - The target name of the permission to revoke.
|
|
250
|
-
*/
|
|
251
|
-
revokePermission(origin, target) {
|
|
252
|
-
this.revokePermissions({ [origin]: [target] });
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Revokes the specified permissions from the specified subjects.
|
|
256
|
-
*
|
|
257
|
-
* Throws an error if any of the subjects or permissions do not exist.
|
|
258
|
-
*
|
|
259
|
-
* @param subjectsAndPermissions - An object mapping subject origins
|
|
260
|
-
* to arrays of permission target names to revoke.
|
|
261
|
-
*/
|
|
262
|
-
revokePermissions(subjectsAndPermissions) {
|
|
263
|
-
this.update((draftState) => {
|
|
264
|
-
Object.keys(subjectsAndPermissions).forEach((origin) => {
|
|
265
|
-
if (!(0, utils_1.hasProperty)(draftState.subjects, origin)) {
|
|
266
|
-
throw new errors_1.UnrecognizedSubjectError(origin);
|
|
267
|
-
}
|
|
268
|
-
subjectsAndPermissions[origin].forEach((target) => {
|
|
269
|
-
const { permissions } = draftState.subjects[origin];
|
|
270
|
-
if (!(0, utils_1.hasProperty)(permissions, target)) {
|
|
271
|
-
throw new errors_1.PermissionDoesNotExistError(origin, target);
|
|
272
|
-
}
|
|
273
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_deletePermission).call(this, draftState.subjects, origin, target);
|
|
274
|
-
});
|
|
275
|
-
});
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
/**
|
|
279
|
-
* Revokes all permissions corresponding to the specified target for all subjects.
|
|
280
|
-
* Does nothing if no subjects or no such permission exists.
|
|
281
|
-
*
|
|
282
|
-
* @param target - The name of the target to revoke all permissions for.
|
|
283
|
-
*/
|
|
284
|
-
revokePermissionForAllSubjects(target) {
|
|
285
|
-
if (this.getSubjectNames().length === 0) {
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
this.update((draftState) => {
|
|
289
|
-
Object.entries(draftState.subjects).forEach(([origin, subject]) => {
|
|
290
|
-
const { permissions } = subject;
|
|
291
|
-
if ((0, utils_1.hasProperty)(permissions, target)) {
|
|
292
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_deletePermission).call(this, draftState.subjects, origin, target);
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Checks whether the permission of the subject corresponding to the given
|
|
299
|
-
* origin has a caveat of the specified type.
|
|
300
|
-
*
|
|
301
|
-
* Throws an error if the subject does not have a permission with the
|
|
302
|
-
* specified target name.
|
|
303
|
-
*
|
|
304
|
-
* @template TargetName - The permission target name. Should be inferred.
|
|
305
|
-
* @template CaveatType - The valid caveat types for the permission. Should
|
|
306
|
-
* be inferred.
|
|
307
|
-
* @param origin - The origin of the subject.
|
|
308
|
-
* @param target - The target name of the permission.
|
|
309
|
-
* @param caveatType - The type of the caveat to check for.
|
|
310
|
-
* @returns Whether the permission has the specified caveat.
|
|
311
|
-
*/
|
|
312
|
-
hasCaveat(origin, target, caveatType) {
|
|
313
|
-
return Boolean(this.getCaveat(origin, target, caveatType));
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* Gets the caveat of the specified type, if any, for the permission of
|
|
317
|
-
* the subject corresponding to the given origin.
|
|
318
|
-
*
|
|
319
|
-
* Throws an error if the subject does not have a permission with the
|
|
320
|
-
* specified target name.
|
|
321
|
-
*
|
|
322
|
-
* @template TargetName - The permission target name. Should be inferred.
|
|
323
|
-
* @template CaveatType - The valid caveat types for the permission. Should
|
|
324
|
-
* be inferred.
|
|
325
|
-
* @param origin - The origin of the subject.
|
|
326
|
-
* @param target - The target name of the permission.
|
|
327
|
-
* @param caveatType - The type of the caveat to get.
|
|
328
|
-
* @returns The caveat, or `undefined` if no such caveat exists.
|
|
329
|
-
*/
|
|
330
|
-
getCaveat(origin, target, caveatType) {
|
|
331
|
-
const permission = this.getPermission(origin, target);
|
|
332
|
-
if (!permission) {
|
|
333
|
-
throw new errors_1.PermissionDoesNotExistError(origin, target);
|
|
334
|
-
}
|
|
335
|
-
return (0, Permission_1.findCaveat)(permission, caveatType);
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Adds a caveat of the specified type, with the specified caveat value, to
|
|
339
|
-
* the permission corresponding to the given subject origin and permission
|
|
340
|
-
* target.
|
|
341
|
-
*
|
|
342
|
-
* For modifying existing caveats, use
|
|
343
|
-
* {@link PermissionController.updateCaveat}.
|
|
344
|
-
*
|
|
345
|
-
* Throws an error if no such permission exists, or if the caveat already
|
|
346
|
-
* exists.
|
|
347
|
-
*
|
|
348
|
-
* @template TargetName - The permission target name. Should be inferred.
|
|
349
|
-
* @template CaveatType - The valid caveat types for the permission. Should
|
|
350
|
-
* be inferred.
|
|
351
|
-
* @param origin - The origin of the subject.
|
|
352
|
-
* @param target - The target name of the permission.
|
|
353
|
-
* @param caveatType - The type of the caveat to add.
|
|
354
|
-
* @param caveatValue - The value of the caveat to add.
|
|
355
|
-
*/
|
|
356
|
-
addCaveat(origin, target, caveatType, caveatValue) {
|
|
357
|
-
if (this.hasCaveat(origin, target, caveatType)) {
|
|
358
|
-
throw new errors_1.CaveatAlreadyExistsError(origin, target, caveatType);
|
|
359
|
-
}
|
|
360
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_setCaveat).call(this, origin, target, caveatType, caveatValue);
|
|
361
|
-
}
|
|
362
|
-
/**
|
|
363
|
-
* Updates the value of the caveat of the specified type belonging to the
|
|
364
|
-
* permission corresponding to the given subject origin and permission
|
|
365
|
-
* target.
|
|
366
|
-
*
|
|
367
|
-
* For adding new caveats, use
|
|
368
|
-
* {@link PermissionController.addCaveat}.
|
|
369
|
-
*
|
|
370
|
-
* Throws an error if no such permission or caveat exists.
|
|
371
|
-
*
|
|
372
|
-
* @template TargetName - The permission target name. Should be inferred.
|
|
373
|
-
* @template CaveatType - The valid caveat types for the permission. Should
|
|
374
|
-
* be inferred.
|
|
375
|
-
* @param origin - The origin of the subject.
|
|
376
|
-
* @param target - The target name of the permission.
|
|
377
|
-
* @param caveatType - The type of the caveat to update.
|
|
378
|
-
* @param caveatValue - The new value of the caveat.
|
|
379
|
-
*/
|
|
380
|
-
updateCaveat(origin, target, caveatType, caveatValue) {
|
|
381
|
-
if (!this.hasCaveat(origin, target, caveatType)) {
|
|
382
|
-
throw new errors_1.CaveatDoesNotExistError(origin, target, caveatType);
|
|
383
|
-
}
|
|
384
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_setCaveat).call(this, origin, target, caveatType, caveatValue);
|
|
385
|
-
}
|
|
386
|
-
/**
|
|
387
|
-
* Updates all caveats with the specified type for all subjects and
|
|
388
|
-
* permissions by applying the specified mutator function to them.
|
|
389
|
-
*
|
|
390
|
-
* ATTN: Permissions can be revoked entirely by the action of this method,
|
|
391
|
-
* read on for details.
|
|
392
|
-
*
|
|
393
|
-
* Caveat mutators are functions that receive a caveat value and return a
|
|
394
|
-
* tuple consisting of a {@link CaveatMutatorOperation} and, optionally, a new
|
|
395
|
-
* value to update the existing caveat with.
|
|
396
|
-
*
|
|
397
|
-
* For each caveat, depending on the mutator result, this method will:
|
|
398
|
-
* - Do nothing ({@link CaveatMutatorOperation.Noop})
|
|
399
|
-
* - Update the value of the caveat ({@link CaveatMutatorOperation.UpdateValue}). The caveat specification validator, if any, will be called after updating the value.
|
|
400
|
-
* - Delete the caveat ({@link CaveatMutatorOperation.DeleteCaveat}). The permission specification validator, if any, will be called after deleting the caveat.
|
|
401
|
-
* - Revoke the parent permission ({@link CaveatMutatorOperation.RevokePermission})
|
|
402
|
-
*
|
|
403
|
-
* This method throws if the validation of any caveat or permission fails.
|
|
404
|
-
*
|
|
405
|
-
* @param targetCaveatType - The type of the caveats to update.
|
|
406
|
-
* @param mutator - The mutator function which will be applied to all caveat
|
|
407
|
-
* values.
|
|
408
|
-
*/
|
|
409
|
-
updatePermissionsByCaveat(targetCaveatType, mutator) {
|
|
410
|
-
if (Object.keys(this.state.subjects).length === 0) {
|
|
411
|
-
return;
|
|
412
|
-
}
|
|
413
|
-
this.update((draftState) => {
|
|
414
|
-
Object.values(draftState.subjects).forEach((subject) => {
|
|
415
|
-
Object.values(subject.permissions).forEach((permission) => {
|
|
416
|
-
const { caveats } = permission;
|
|
417
|
-
const targetCaveat = caveats?.find(({ type }) => type === targetCaveatType);
|
|
418
|
-
if (!targetCaveat) {
|
|
419
|
-
return;
|
|
420
|
-
}
|
|
421
|
-
// The mutator may modify the caveat value in place, and must always
|
|
422
|
-
// return a valid mutation result.
|
|
423
|
-
const mutatorResult = mutator(targetCaveat.value);
|
|
424
|
-
const { operation } = mutatorResult;
|
|
425
|
-
switch (operation) {
|
|
426
|
-
case CaveatMutatorOperation.Noop:
|
|
427
|
-
break;
|
|
428
|
-
case CaveatMutatorOperation.UpdateValue:
|
|
429
|
-
// Typecast: `Mutable` is used here to assign to a readonly
|
|
430
|
-
// property. `targetConstraint` should already be mutable because
|
|
431
|
-
// it's part of a draft, but for some reason it's not. We can't
|
|
432
|
-
// use the more-correct `Draft` type here either because it
|
|
433
|
-
// results in an error.
|
|
434
|
-
targetCaveat.value =
|
|
435
|
-
mutatorResult.value;
|
|
436
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validateCaveat).call(this, targetCaveat, subject.origin, permission.parentCapability);
|
|
437
|
-
break;
|
|
438
|
-
case CaveatMutatorOperation.DeleteCaveat:
|
|
439
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_deleteCaveat).call(this, permission, targetCaveatType, subject.origin);
|
|
440
|
-
break;
|
|
441
|
-
case CaveatMutatorOperation.RevokePermission:
|
|
442
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_deletePermission).call(this, draftState.subjects, subject.origin, permission.parentCapability);
|
|
443
|
-
break;
|
|
444
|
-
default: {
|
|
445
|
-
// Overriding as `never` is the expected result of exhaustiveness checking,
|
|
446
|
-
// and is intended to represent unchecked exception cases.
|
|
447
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
448
|
-
throw new Error(`Unrecognized mutation result: "${operation}"`);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
});
|
|
452
|
-
});
|
|
453
|
-
});
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* Removes the caveat of the specified type from the permission corresponding
|
|
457
|
-
* to the given subject origin and target name.
|
|
458
|
-
*
|
|
459
|
-
* Throws an error if no such permission or caveat exists.
|
|
460
|
-
*
|
|
461
|
-
* @template TargetName - The permission target name. Should be inferred.
|
|
462
|
-
* @template CaveatType - The valid caveat types for the permission. Should
|
|
463
|
-
* be inferred.
|
|
464
|
-
* @param origin - The origin of the subject.
|
|
465
|
-
* @param target - The target name of the permission.
|
|
466
|
-
* @param caveatType - The type of the caveat to remove.
|
|
467
|
-
*/
|
|
468
|
-
removeCaveat(origin, target, caveatType) {
|
|
469
|
-
this.update((draftState) => {
|
|
470
|
-
const permission = draftState.subjects[origin]?.permissions[target];
|
|
471
|
-
if (!permission) {
|
|
472
|
-
throw new errors_1.PermissionDoesNotExistError(origin, target);
|
|
473
|
-
}
|
|
474
|
-
if (!permission.caveats) {
|
|
475
|
-
throw new errors_1.CaveatDoesNotExistError(origin, target, caveatType);
|
|
476
|
-
}
|
|
477
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_deleteCaveat).call(this, permission, caveatType, origin);
|
|
478
|
-
});
|
|
479
|
-
}
|
|
480
|
-
/**
|
|
481
|
-
* Grants _approved_ permissions to the specified subject. Every permission and
|
|
482
|
-
* caveat is stringently validated—including by calling their specification
|
|
483
|
-
* validators—and an error is thrown if validation fails.
|
|
484
|
-
*
|
|
485
|
-
* ATTN: This method does **not** prompt the user for approval. User consent must
|
|
486
|
-
* first be obtained through some other means.
|
|
487
|
-
*
|
|
488
|
-
* @see {@link PermissionController.requestPermissions} For initiating a
|
|
489
|
-
* permissions request requiring user approval.
|
|
490
|
-
* @param options - Options bag.
|
|
491
|
-
* @param options.approvedPermissions - The requested permissions approved by
|
|
492
|
-
* the user.
|
|
493
|
-
* @param options.requestData - Permission request data. Passed to permission
|
|
494
|
-
* factory functions.
|
|
495
|
-
* @param options.preserveExistingPermissions - Whether to preserve the
|
|
496
|
-
* subject's existing permissions.
|
|
497
|
-
* @param options.subject - The subject to grant permissions to.
|
|
498
|
-
* @returns The subject's new permission state. It may or may not have changed.
|
|
499
|
-
*/
|
|
500
|
-
grantPermissions({ approvedPermissions, requestData, preserveExistingPermissions = true, subject, }) {
|
|
501
|
-
return __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_applyGrantedPermissions).call(this, {
|
|
502
|
-
approvedPermissions,
|
|
503
|
-
subject,
|
|
504
|
-
mergePermissions: false,
|
|
505
|
-
preserveExistingPermissions,
|
|
506
|
-
requestData,
|
|
507
|
-
});
|
|
508
|
-
}
|
|
509
|
-
/**
|
|
510
|
-
* Incrementally grants _approved_ permissions to the specified subject. Every
|
|
511
|
-
* permission and caveat is stringently validated—including by calling their
|
|
512
|
-
* specification validators—and an error is thrown if validation fails.
|
|
513
|
-
*
|
|
514
|
-
* ATTN: This method does **not** prompt the user for approval. User consent must
|
|
515
|
-
* first be obtained through some other means.
|
|
516
|
-
*
|
|
517
|
-
* @see {@link PermissionController.requestPermissionsIncremental} For initiating
|
|
518
|
-
* an incremental permissions request requiring user approval.
|
|
519
|
-
* @param options - Options bag.
|
|
520
|
-
* @param options.approvedPermissions - The requested permissions approved by
|
|
521
|
-
* the user.
|
|
522
|
-
* @param options.requestData - Permission request data. Passed to permission
|
|
523
|
-
* factory functions.
|
|
524
|
-
* @param options.subject - The subject to grant permissions to.
|
|
525
|
-
* @returns The subject's new permission state. It may or may not have changed.
|
|
526
|
-
*/
|
|
527
|
-
grantPermissionsIncremental({ approvedPermissions, requestData, subject, }) {
|
|
528
|
-
return __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_applyGrantedPermissions).call(this, {
|
|
529
|
-
approvedPermissions,
|
|
530
|
-
subject,
|
|
531
|
-
mergePermissions: true,
|
|
532
|
-
preserveExistingPermissions: true,
|
|
533
|
-
requestData,
|
|
534
|
-
});
|
|
535
|
-
}
|
|
536
|
-
/**
|
|
537
|
-
* Initiates a permission request that requires user approval.
|
|
538
|
-
*
|
|
539
|
-
* Either this or {@link PermissionController.requestPermissionsIncremental}
|
|
540
|
-
* should always be used to grant additional permissions to a subject,
|
|
541
|
-
* unless user approval has been obtained through some other means.
|
|
542
|
-
*
|
|
543
|
-
* Permissions are validated at every step of the approval process, and this
|
|
544
|
-
* method will reject if validation fails.
|
|
545
|
-
*
|
|
546
|
-
* @see {@link ApprovalController} For the user approval logic.
|
|
547
|
-
* @see {@link PermissionController.acceptPermissionsRequest} For the method
|
|
548
|
-
* that _accepts_ the request and resolves the user approval promise.
|
|
549
|
-
* @see {@link PermissionController.rejectPermissionsRequest} For the method
|
|
550
|
-
* that _rejects_ the request and the user approval promise.
|
|
551
|
-
* @param subject - The grantee subject.
|
|
552
|
-
* @param requestedPermissions - The requested permissions.
|
|
553
|
-
* @param options - Additional options.
|
|
554
|
-
* @param options.id - The id of the permissions request. Defaults to a unique
|
|
555
|
-
* id.
|
|
556
|
-
* @param options.preserveExistingPermissions - Whether to preserve the
|
|
557
|
-
* subject's existing permissions. Defaults to `true`.
|
|
558
|
-
* @param options.metadata - Additional metadata about the permission request.
|
|
559
|
-
* @returns The granted permissions and request metadata.
|
|
560
|
-
*/
|
|
561
|
-
async requestPermissions(subject, requestedPermissions, options = {}) {
|
|
562
|
-
const { origin } = subject;
|
|
563
|
-
const { id = (0, nanoid_1.nanoid)(), preserveExistingPermissions = true } = options;
|
|
564
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validateRequestedPermissions).call(this, origin, requestedPermissions);
|
|
565
|
-
const metadata = {
|
|
566
|
-
...options.metadata,
|
|
567
|
-
id,
|
|
568
|
-
origin,
|
|
569
|
-
};
|
|
570
|
-
const permissionsRequest = {
|
|
571
|
-
metadata,
|
|
572
|
-
permissions: requestedPermissions,
|
|
573
|
-
};
|
|
574
|
-
const approvedRequest = await __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_requestUserApproval).call(this, permissionsRequest);
|
|
575
|
-
return await __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_handleApprovedPermissions).call(this, {
|
|
576
|
-
subject,
|
|
577
|
-
metadata,
|
|
578
|
-
preserveExistingPermissions,
|
|
579
|
-
approvedRequest,
|
|
580
|
-
});
|
|
581
|
-
}
|
|
582
|
-
/**
|
|
583
|
-
* Initiates an incremental permission request that prompts for user approval.
|
|
584
|
-
* Incremental permission requests allow the caller to replace existing and/or
|
|
585
|
-
* add brand new permissions and caveats for the specified subject.
|
|
586
|
-
*
|
|
587
|
-
* Incremental permission request are merged with the subject's existing permissions
|
|
588
|
-
* through a right-biased union, where the incremental permission are the right-hand
|
|
589
|
-
* side of the merger. If both sides of the merger specify the same caveats for a
|
|
590
|
-
* given permission, the caveats are merged using their specification's caveat value
|
|
591
|
-
* merger property.
|
|
592
|
-
*
|
|
593
|
-
* Either this or {@link PermissionController.requestPermissions} should
|
|
594
|
-
* always be used to grant additional permissions to a subject, unless user
|
|
595
|
-
* approval has been obtained through some other means.
|
|
596
|
-
*
|
|
597
|
-
* Permissions are validated at every step of the approval process, and this
|
|
598
|
-
* method will reject if validation fails.
|
|
599
|
-
*
|
|
600
|
-
* @see {@link ApprovalController} For the user approval logic.
|
|
601
|
-
* @see {@link PermissionController.acceptPermissionsRequest} For the method
|
|
602
|
-
* that _accepts_ the request and resolves the user approval promise.
|
|
603
|
-
* @see {@link PermissionController.rejectPermissionsRequest} For the method
|
|
604
|
-
* that _rejects_ the request and the user approval promise.
|
|
605
|
-
* @param subject - The grantee subject.
|
|
606
|
-
* @param requestedPermissions - The requested permissions.
|
|
607
|
-
* @param options - Additional options.
|
|
608
|
-
* @param options.id - The id of the permissions request. Defaults to a unique
|
|
609
|
-
* id.
|
|
610
|
-
* @param options.metadata - Additional metadata about the permission request.
|
|
611
|
-
* @returns The granted permissions and request metadata.
|
|
612
|
-
*/
|
|
613
|
-
async requestPermissionsIncremental(subject, requestedPermissions, options = {}) {
|
|
614
|
-
const { origin } = subject;
|
|
615
|
-
const { id = (0, nanoid_1.nanoid)() } = options;
|
|
616
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validateRequestedPermissions).call(this, origin, requestedPermissions);
|
|
617
|
-
const currentPermissions = this.getPermissions(origin) ?? {};
|
|
618
|
-
const [newPermissions, permissionDiffMap] = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_mergeIncrementalPermissions).call(this, currentPermissions, requestedPermissions);
|
|
619
|
-
// The second undefined check is just for type narrowing purposes. These values
|
|
620
|
-
// will always be jointly defined or undefined.
|
|
621
|
-
if (newPermissions === undefined || permissionDiffMap === undefined) {
|
|
622
|
-
return [];
|
|
623
|
-
}
|
|
624
|
-
try {
|
|
625
|
-
// It does not spark joy to run this validation again after the merger operation.
|
|
626
|
-
// But, optimizing this procedure is probably not worth it, especially considering
|
|
627
|
-
// that the worst-case scenario for validation degrades to the below function call.
|
|
628
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validateRequestedPermissions).call(this, origin, newPermissions);
|
|
629
|
-
}
|
|
630
|
-
catch (error) {
|
|
631
|
-
if (error instanceof Error) {
|
|
632
|
-
throw new errors_1.InvalidMergedPermissionsError(origin, error, permissionDiffMap);
|
|
633
|
-
}
|
|
634
|
-
/* istanbul ignore next: This should be impossible */
|
|
635
|
-
throw (0, errors_1.internalError)('Unrecognized error type', { error });
|
|
636
|
-
}
|
|
637
|
-
const metadata = {
|
|
638
|
-
...options.metadata,
|
|
639
|
-
id,
|
|
640
|
-
origin,
|
|
641
|
-
};
|
|
642
|
-
const permissionsRequest = {
|
|
643
|
-
metadata,
|
|
644
|
-
permissions: newPermissions,
|
|
645
|
-
diff: {
|
|
646
|
-
currentPermissions,
|
|
647
|
-
permissionDiffMap,
|
|
648
|
-
},
|
|
649
|
-
};
|
|
650
|
-
const approvedRequest = await __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_requestUserApproval).call(this, permissionsRequest);
|
|
651
|
-
return await __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_handleApprovedPermissions).call(this, {
|
|
652
|
-
subject,
|
|
653
|
-
metadata,
|
|
654
|
-
preserveExistingPermissions: false,
|
|
655
|
-
approvedRequest,
|
|
656
|
-
});
|
|
657
|
-
}
|
|
658
|
-
/**
|
|
659
|
-
* Accepts a permissions request created by
|
|
660
|
-
* {@link PermissionController.requestPermissions}.
|
|
661
|
-
*
|
|
662
|
-
* @param request - The permissions request.
|
|
663
|
-
*/
|
|
664
|
-
async acceptPermissionsRequest(request) {
|
|
665
|
-
const { id } = request.metadata;
|
|
666
|
-
if (!__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_hasApprovalRequest).call(this, { id })) {
|
|
667
|
-
throw new errors_1.PermissionsRequestNotFoundError(id);
|
|
668
|
-
}
|
|
669
|
-
if (Object.keys(request.permissions).length === 0) {
|
|
670
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_rejectPermissionsRequest).call(this, id, (0, errors_1.invalidParams)({
|
|
671
|
-
message: 'Must request at least one permission.',
|
|
672
|
-
}));
|
|
673
|
-
return;
|
|
674
|
-
}
|
|
675
|
-
try {
|
|
676
|
-
await this.messenger.call('ApprovalController:acceptRequest', id, request);
|
|
677
|
-
}
|
|
678
|
-
catch (error) {
|
|
679
|
-
// If accepting unexpectedly fails, reject the request and re-throw the
|
|
680
|
-
// error
|
|
681
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_rejectPermissionsRequest).call(this, id, error);
|
|
682
|
-
throw error;
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
/**
|
|
686
|
-
* Rejects a permissions request created by
|
|
687
|
-
* {@link PermissionController.requestPermissions}.
|
|
688
|
-
*
|
|
689
|
-
* @param id - The id of the request to be rejected.
|
|
690
|
-
*/
|
|
691
|
-
async rejectPermissionsRequest(id) {
|
|
692
|
-
if (!__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_hasApprovalRequest).call(this, { id })) {
|
|
693
|
-
throw new errors_1.PermissionsRequestNotFoundError(id);
|
|
694
|
-
}
|
|
695
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_rejectPermissionsRequest).call(this, id, (0, errors_1.userRejectedRequest)());
|
|
696
|
-
}
|
|
697
|
-
/**
|
|
698
|
-
* Gets the subject's endowments per the specified endowment permission.
|
|
699
|
-
* Throws if the subject does not have the required permission or if the
|
|
700
|
-
* permission is not an endowment permission.
|
|
701
|
-
*
|
|
702
|
-
* @param origin - The origin of the subject whose endowments to retrieve.
|
|
703
|
-
* @param targetName - The name of the endowment permission. This must be a
|
|
704
|
-
* valid permission target name.
|
|
705
|
-
* @param requestData - Additional data associated with the request, if any.
|
|
706
|
-
* Forwarded to the endowment getter function for the permission.
|
|
707
|
-
* @returns The endowments, if any.
|
|
708
|
-
*/
|
|
709
|
-
async getEndowments(origin, targetName, requestData) {
|
|
710
|
-
if (!this.hasPermission(origin, targetName)) {
|
|
711
|
-
throw (0, errors_1.unauthorized)({ data: { origin, targetName } });
|
|
712
|
-
}
|
|
713
|
-
return __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_getTypedPermissionSpecification).call(this, Permission_1.PermissionType.Endowment, targetName, origin).endowmentGetter({ origin, requestData });
|
|
714
|
-
}
|
|
715
|
-
/**
|
|
716
|
-
* Executes a restricted method as the subject with the given origin.
|
|
717
|
-
* The specified params, if any, will be passed to the method implementation.
|
|
718
|
-
*
|
|
719
|
-
* ATTN: Great caution should be exercised in the use of this method.
|
|
720
|
-
* Methods that cause side effects or affect application state should
|
|
721
|
-
* be avoided.
|
|
722
|
-
*
|
|
723
|
-
* This method will first attempt to retrieve the requested restricted method
|
|
724
|
-
* implementation, throwing if it does not exist. The method will then be
|
|
725
|
-
* invoked as though the subject with the specified origin had invoked it with
|
|
726
|
-
* the specified parameters. This means that any existing caveats will be
|
|
727
|
-
* applied to the restricted method, and this method will throw if the
|
|
728
|
-
* restricted method or its caveat decorators throw.
|
|
729
|
-
*
|
|
730
|
-
* In addition, this method will throw if the subject does not have a
|
|
731
|
-
* permission for the specified restricted method.
|
|
732
|
-
*
|
|
733
|
-
* @param origin - The origin of the subject to execute the method on behalf
|
|
734
|
-
* of.
|
|
735
|
-
* @param targetName - The name of the method to execute. This must be a valid
|
|
736
|
-
* permission target name.
|
|
737
|
-
* @param params - The parameters to pass to the method implementation.
|
|
738
|
-
* @returns The result of the executed method.
|
|
739
|
-
*/
|
|
740
|
-
async executeRestrictedMethod(origin, targetName, params) {
|
|
741
|
-
// Throws if the method does not exist
|
|
742
|
-
const methodImplementation = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_getRestrictedMethod).call(this, targetName, origin);
|
|
743
|
-
const result = await __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_executeRestrictedMethod).call(this, methodImplementation, { origin }, targetName, params);
|
|
744
|
-
// This is impossible if the restricted method implementation is typed correctly,
|
|
745
|
-
// but we maintain it for backwards compatibility.
|
|
746
|
-
if (result === undefined) {
|
|
747
|
-
throw new Error(`Request for method "${targetName}" as origin "${origin}" returned no result.`);
|
|
748
|
-
}
|
|
749
|
-
return result;
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
exports.PermissionController = PermissionController;
|
|
753
|
-
_PermissionController_caveatSpecifications = new WeakMap(), _PermissionController_permissionSpecifications = new WeakMap(), _PermissionController_unrestrictedMethods = new WeakMap(), _PermissionController_instances = new WeakSet(), _PermissionController_getPermissionSpecification = function _PermissionController_getPermissionSpecification(targetName) {
|
|
754
|
-
return __classPrivateFieldGet(this, _PermissionController_permissionSpecifications, "f")[targetName];
|
|
755
|
-
}, _PermissionController_getCaveatSpecification = function _PermissionController_getCaveatSpecification(caveatType) {
|
|
756
|
-
return __classPrivateFieldGet(this, _PermissionController_caveatSpecifications, "f")[caveatType];
|
|
757
|
-
}, _PermissionController_expectGetCaveatMerger = function _PermissionController_expectGetCaveatMerger(caveatType) {
|
|
758
|
-
const { merger } = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_getCaveatSpecification).call(this, caveatType);
|
|
759
|
-
if (merger === undefined) {
|
|
760
|
-
throw new errors_1.CaveatMergerDoesNotExistError(caveatType);
|
|
761
|
-
}
|
|
762
|
-
return merger;
|
|
763
|
-
}, _PermissionController_validatePermissionSpecifications = function _PermissionController_validatePermissionSpecifications(permissionSpecifications, caveatSpecifications) {
|
|
764
|
-
Object.entries(permissionSpecifications).forEach(([targetName, { permissionType, targetName: innerTargetName, allowedCaveats },]) => {
|
|
765
|
-
if (!permissionType || !(0, utils_1.hasProperty)(Permission_1.PermissionType, permissionType)) {
|
|
766
|
-
throw new Error(`Invalid permission type: "${permissionType}"`);
|
|
767
|
-
}
|
|
768
|
-
if (!targetName) {
|
|
769
|
-
throw new Error(`Invalid permission target name: "${targetName}"`);
|
|
770
|
-
}
|
|
771
|
-
if (targetName !== innerTargetName) {
|
|
772
|
-
throw new Error(`Invalid permission specification: target name "${targetName}" must match specification.targetName value "${innerTargetName}".`);
|
|
773
|
-
}
|
|
774
|
-
if (allowedCaveats) {
|
|
775
|
-
allowedCaveats.forEach((caveatType) => {
|
|
776
|
-
if (!(0, utils_1.hasProperty)(caveatSpecifications, caveatType)) {
|
|
777
|
-
throw new errors_1.UnrecognizedCaveatTypeError(caveatType);
|
|
778
|
-
}
|
|
779
|
-
const specification = caveatSpecifications[caveatType];
|
|
780
|
-
const isRestrictedMethodCaveat = (0, Caveat_1.isRestrictedMethodCaveatSpecification)(specification);
|
|
781
|
-
if ((permissionType === Permission_1.PermissionType.RestrictedMethod &&
|
|
782
|
-
!isRestrictedMethodCaveat) ||
|
|
783
|
-
(permissionType === Permission_1.PermissionType.Endowment &&
|
|
784
|
-
isRestrictedMethodCaveat)) {
|
|
785
|
-
throw new errors_1.CaveatSpecificationMismatchError(specification, permissionType);
|
|
786
|
-
}
|
|
787
|
-
});
|
|
788
|
-
}
|
|
789
|
-
});
|
|
790
|
-
}, _PermissionController_getTypedPermissionSpecification = function _PermissionController_getTypedPermissionSpecification(permissionType, targetName, requestingOrigin) {
|
|
791
|
-
const failureError = permissionType === Permission_1.PermissionType.RestrictedMethod
|
|
792
|
-
? (0, errors_1.methodNotFound)(targetName, { origin: requestingOrigin })
|
|
793
|
-
: new errors_1.EndowmentPermissionDoesNotExistError(targetName, requestingOrigin);
|
|
794
|
-
if (!__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_targetExists).call(this, targetName)) {
|
|
795
|
-
throw failureError;
|
|
796
|
-
}
|
|
797
|
-
const specification = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_getPermissionSpecification).call(this, targetName);
|
|
798
|
-
if (!(0, Permission_1.hasSpecificationType)(specification, permissionType)) {
|
|
799
|
-
throw failureError;
|
|
800
|
-
}
|
|
801
|
-
return specification;
|
|
802
|
-
}, _PermissionController_getRestrictedMethod = function _PermissionController_getRestrictedMethod(method, origin) {
|
|
803
|
-
return __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_getTypedPermissionSpecification).call(this, Permission_1.PermissionType.RestrictedMethod, method, origin).methodImplementation;
|
|
804
|
-
}, _PermissionController_deletePermission = function _PermissionController_deletePermission(subjects, origin, target) {
|
|
805
|
-
const { permissions } = subjects[origin];
|
|
806
|
-
if (Object.keys(permissions).length > 1) {
|
|
807
|
-
delete permissions[target];
|
|
808
|
-
}
|
|
809
|
-
else {
|
|
810
|
-
delete subjects[origin];
|
|
811
|
-
}
|
|
812
|
-
}, _PermissionController_setCaveat = function _PermissionController_setCaveat(origin, target, caveatType, caveatValue) {
|
|
813
|
-
this.update((draftState) => {
|
|
814
|
-
const subject = draftState.subjects[origin];
|
|
815
|
-
// Unreachable because `hasCaveat` is always called before this, and it
|
|
816
|
-
// throws if permissions are missing. TypeScript needs this, however.
|
|
817
|
-
/* istanbul ignore if */
|
|
818
|
-
if (!subject) {
|
|
819
|
-
throw new errors_1.UnrecognizedSubjectError(origin);
|
|
820
|
-
}
|
|
821
|
-
const permission = subject.permissions[target];
|
|
822
|
-
/* istanbul ignore if: practically impossible, but TypeScript wants it */
|
|
823
|
-
if (!permission) {
|
|
824
|
-
throw new errors_1.PermissionDoesNotExistError(origin, target);
|
|
825
|
-
}
|
|
826
|
-
const caveat = {
|
|
827
|
-
type: caveatType,
|
|
828
|
-
value: caveatValue,
|
|
829
|
-
};
|
|
830
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validateCaveat).call(this, caveat, origin, target);
|
|
831
|
-
let addedCaveat = false;
|
|
832
|
-
if (permission.caveats) {
|
|
833
|
-
const caveatIndex = permission.caveats.findIndex((existingCaveat) => existingCaveat.type === caveat.type);
|
|
834
|
-
if (caveatIndex === -1) {
|
|
835
|
-
permission.caveats.push(caveat);
|
|
836
|
-
addedCaveat = true;
|
|
837
|
-
}
|
|
838
|
-
else {
|
|
839
|
-
permission.caveats.splice(caveatIndex, 1, caveat);
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
else {
|
|
843
|
-
// At this point, we don't know if the specific permission is allowed
|
|
844
|
-
// to have caveats, but it should be impossible to call this method
|
|
845
|
-
// for a permission that may not have any caveats. If all else fails,
|
|
846
|
-
// the permission validator is also called.
|
|
847
|
-
// @ts-expect-error See above comment
|
|
848
|
-
permission.caveats = [caveat];
|
|
849
|
-
addedCaveat = true;
|
|
850
|
-
}
|
|
851
|
-
// Mutating a caveat does not warrant permission validation, but mutating
|
|
852
|
-
// the caveat array does.
|
|
853
|
-
if (addedCaveat) {
|
|
854
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validateModifiedPermission).call(this, permission, origin, {
|
|
855
|
-
invokePermissionValidator: true,
|
|
856
|
-
performCaveatValidation: false, // We just validated the caveat
|
|
857
|
-
});
|
|
858
|
-
}
|
|
859
|
-
});
|
|
860
|
-
}, _PermissionController_deleteCaveat = function _PermissionController_deleteCaveat(permission, caveatType, origin) {
|
|
861
|
-
/* istanbul ignore if: not possible in our usage */
|
|
862
|
-
if (!permission.caveats) {
|
|
863
|
-
throw new errors_1.CaveatDoesNotExistError(origin, permission.parentCapability, caveatType);
|
|
864
|
-
}
|
|
865
|
-
const caveatIndex = permission.caveats.findIndex((existingCaveat) => existingCaveat.type === caveatType);
|
|
866
|
-
if (caveatIndex === -1) {
|
|
867
|
-
throw new errors_1.CaveatDoesNotExistError(origin, permission.parentCapability, caveatType);
|
|
868
|
-
}
|
|
869
|
-
if (permission.caveats.length === 1) {
|
|
870
|
-
permission.caveats = null;
|
|
871
|
-
}
|
|
872
|
-
else {
|
|
873
|
-
permission.caveats.splice(caveatIndex, 1);
|
|
874
|
-
}
|
|
875
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validateModifiedPermission).call(this, permission, origin, {
|
|
876
|
-
invokePermissionValidator: true,
|
|
877
|
-
performCaveatValidation: false, // No caveat object was mutated
|
|
878
|
-
});
|
|
879
|
-
}, _PermissionController_validateModifiedPermission = function _PermissionController_validateModifiedPermission(permission, origin, validationFlags) {
|
|
880
|
-
/* istanbul ignore if: this should be impossible */
|
|
881
|
-
if (!__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_targetExists).call(this, permission.parentCapability)) {
|
|
882
|
-
throw new Error(`Fatal: Existing permission target "${permission.parentCapability}" has no specification.`);
|
|
883
|
-
}
|
|
884
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validatePermission).call(this, __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_getPermissionSpecification).call(this, permission.parentCapability), permission, origin, validationFlags);
|
|
885
|
-
}, _PermissionController_targetExists = function _PermissionController_targetExists(target) {
|
|
886
|
-
return (0, utils_1.hasProperty)(__classPrivateFieldGet(this, _PermissionController_permissionSpecifications, "f"), target);
|
|
887
|
-
}, _PermissionController_applyGrantedPermissions = function _PermissionController_applyGrantedPermissions({ approvedPermissions, subject, mergePermissions, preserveExistingPermissions, requestData, }) {
|
|
888
|
-
const { origin } = subject;
|
|
889
|
-
if (!origin || typeof origin !== 'string') {
|
|
890
|
-
throw new errors_1.InvalidSubjectIdentifierError(origin);
|
|
891
|
-
}
|
|
892
|
-
const permissions = (preserveExistingPermissions
|
|
893
|
-
? {
|
|
894
|
-
...this.getPermissions(origin),
|
|
895
|
-
}
|
|
896
|
-
: {});
|
|
897
|
-
for (const [requestedTarget, approvedPermission] of Object.entries(approvedPermissions)) {
|
|
898
|
-
if (!__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_targetExists).call(this, requestedTarget)) {
|
|
899
|
-
throw (0, errors_1.methodNotFound)(requestedTarget);
|
|
900
|
-
}
|
|
901
|
-
if (approvedPermission.parentCapability !== undefined &&
|
|
902
|
-
requestedTarget !== approvedPermission.parentCapability) {
|
|
903
|
-
throw new errors_1.InvalidApprovedPermissionError(origin, requestedTarget, approvedPermission);
|
|
904
|
-
}
|
|
905
|
-
// We have verified that the target exists, and reassign it to change its
|
|
906
|
-
// type.
|
|
907
|
-
const targetName = requestedTarget;
|
|
908
|
-
const specification = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_getPermissionSpecification).call(this, targetName);
|
|
909
|
-
// The requested caveats are validated here.
|
|
910
|
-
const caveats = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_constructCaveats).call(this, origin, targetName, approvedPermission.caveats);
|
|
911
|
-
const permissionOptions = {
|
|
912
|
-
caveats,
|
|
913
|
-
invoker: origin,
|
|
914
|
-
target: targetName,
|
|
915
|
-
};
|
|
916
|
-
let permission;
|
|
917
|
-
if (specification.factory) {
|
|
918
|
-
permission = specification.factory(permissionOptions, requestData);
|
|
919
|
-
}
|
|
920
|
-
else {
|
|
921
|
-
permission = (0, Permission_1.constructPermission)(permissionOptions);
|
|
922
|
-
}
|
|
923
|
-
if (mergePermissions) {
|
|
924
|
-
permission = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_mergePermission).call(this, permissions[targetName], permission)[0];
|
|
925
|
-
}
|
|
926
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validatePermission).call(this, specification, permission, origin, {
|
|
927
|
-
invokePermissionValidator: true,
|
|
928
|
-
performCaveatValidation: true,
|
|
929
|
-
});
|
|
930
|
-
permissions[targetName] = permission;
|
|
931
|
-
}
|
|
932
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_setValidatedPermissions).call(this, origin, permissions);
|
|
933
|
-
return permissions;
|
|
934
|
-
}, _PermissionController_validatePermission = function _PermissionController_validatePermission(specification, permission, origin, { invokePermissionValidator, performCaveatValidation, }) {
|
|
935
|
-
const { allowedCaveats, validator, targetName } = specification;
|
|
936
|
-
if (specification.subjectTypes?.length &&
|
|
937
|
-
specification.subjectTypes.length > 0) {
|
|
938
|
-
const metadata = this.messenger.call('SubjectMetadataController:getSubjectMetadata', origin);
|
|
939
|
-
if (!metadata ||
|
|
940
|
-
metadata.subjectType === null ||
|
|
941
|
-
!specification.subjectTypes.includes(metadata.subjectType)) {
|
|
942
|
-
throw specification.permissionType === Permission_1.PermissionType.RestrictedMethod
|
|
943
|
-
? (0, errors_1.methodNotFound)(targetName, { origin })
|
|
944
|
-
: new errors_1.EndowmentPermissionDoesNotExistError(targetName, origin);
|
|
945
|
-
}
|
|
946
|
-
}
|
|
947
|
-
if ((0, utils_1.hasProperty)(permission, 'caveats')) {
|
|
948
|
-
const { caveats } = permission;
|
|
949
|
-
if (caveats !== null && !(Array.isArray(caveats) && caveats.length > 0)) {
|
|
950
|
-
throw new errors_1.InvalidCaveatsPropertyError(origin, targetName, caveats);
|
|
951
|
-
}
|
|
952
|
-
const seenCaveatTypes = new Set();
|
|
953
|
-
caveats?.forEach((caveat) => {
|
|
954
|
-
if (performCaveatValidation) {
|
|
955
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validateCaveat).call(this, caveat, origin, targetName);
|
|
956
|
-
}
|
|
957
|
-
if (!allowedCaveats?.includes(caveat.type)) {
|
|
958
|
-
throw new errors_1.ForbiddenCaveatError(caveat.type, origin, targetName);
|
|
959
|
-
}
|
|
960
|
-
if (seenCaveatTypes.has(caveat.type)) {
|
|
961
|
-
throw new errors_1.DuplicateCaveatError(caveat.type, origin, targetName);
|
|
962
|
-
}
|
|
963
|
-
seenCaveatTypes.add(caveat.type);
|
|
964
|
-
});
|
|
965
|
-
}
|
|
966
|
-
if (invokePermissionValidator && validator) {
|
|
967
|
-
validator(permission, origin, targetName);
|
|
968
|
-
}
|
|
969
|
-
}, _PermissionController_setValidatedPermissions = function _PermissionController_setValidatedPermissions(origin, permissions) {
|
|
970
|
-
this.update((draftState) => {
|
|
971
|
-
if (!draftState.subjects[origin]) {
|
|
972
|
-
draftState.subjects[origin] = { origin, permissions: {} };
|
|
973
|
-
}
|
|
974
|
-
draftState.subjects[origin].permissions = (0, immer_1.castDraft)(permissions);
|
|
975
|
-
});
|
|
976
|
-
}, _PermissionController_constructCaveats = function _PermissionController_constructCaveats(origin, target, requestedCaveats) {
|
|
977
|
-
const caveatArray = requestedCaveats?.map((requestedCaveat) => {
|
|
978
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validateCaveat).call(this, requestedCaveat, origin, target);
|
|
979
|
-
// Reassign so that we have a fresh object.
|
|
980
|
-
const { type, value } = requestedCaveat;
|
|
981
|
-
return { type, value };
|
|
982
|
-
});
|
|
983
|
-
return caveatArray && (0, controller_utils_1.isNonEmptyArray)(caveatArray)
|
|
984
|
-
? caveatArray
|
|
985
|
-
: undefined;
|
|
986
|
-
}, _PermissionController_validateCaveat = function _PermissionController_validateCaveat(caveat, origin, target) {
|
|
987
|
-
if (!(0, controller_utils_1.isPlainObject)(caveat)) {
|
|
988
|
-
throw new errors_1.InvalidCaveatError(caveat, origin, target);
|
|
989
|
-
}
|
|
990
|
-
if (Object.keys(caveat).length !== 2) {
|
|
991
|
-
throw new errors_1.InvalidCaveatFieldsError(caveat, origin, target);
|
|
992
|
-
}
|
|
993
|
-
if (typeof caveat.type !== 'string') {
|
|
994
|
-
throw new errors_1.InvalidCaveatTypeError(caveat, origin, target);
|
|
995
|
-
}
|
|
996
|
-
const specification = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_getCaveatSpecification).call(this, caveat.type);
|
|
997
|
-
if (!specification) {
|
|
998
|
-
throw new errors_1.UnrecognizedCaveatTypeError(caveat.type, origin, target);
|
|
999
|
-
}
|
|
1000
|
-
if (!(0, utils_1.hasProperty)(caveat, 'value') || caveat.value === undefined) {
|
|
1001
|
-
throw new errors_1.CaveatMissingValueError(caveat, origin, target);
|
|
1002
|
-
}
|
|
1003
|
-
if (!(0, controller_utils_1.isValidJson)(caveat.value)) {
|
|
1004
|
-
throw new errors_1.CaveatInvalidJsonError(caveat, origin, target);
|
|
1005
|
-
}
|
|
1006
|
-
// Typecast: TypeScript still believes that the caveat is a PlainObject.
|
|
1007
|
-
specification.validator?.(caveat, origin, target);
|
|
1008
|
-
}, _PermissionController_validateRequestedPermissions = function _PermissionController_validateRequestedPermissions(origin, requestedPermissions) {
|
|
1009
|
-
if (!(0, controller_utils_1.isPlainObject)(requestedPermissions)) {
|
|
1010
|
-
throw (0, errors_1.invalidParams)({
|
|
1011
|
-
message: `Requested permissions for origin "${origin}" is not a plain object.`,
|
|
1012
|
-
data: { origin, requestedPermissions },
|
|
1013
|
-
});
|
|
1014
|
-
}
|
|
1015
|
-
if (Object.keys(requestedPermissions).length === 0) {
|
|
1016
|
-
throw (0, errors_1.invalidParams)({
|
|
1017
|
-
message: `Permissions request for origin "${origin}" contains no permissions.`,
|
|
1018
|
-
data: { requestedPermissions },
|
|
1019
|
-
});
|
|
1020
|
-
}
|
|
1021
|
-
for (const targetName of Object.keys(requestedPermissions)) {
|
|
1022
|
-
const permission = requestedPermissions[targetName];
|
|
1023
|
-
if (!__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_targetExists).call(this, targetName)) {
|
|
1024
|
-
throw (0, errors_1.methodNotFound)(targetName, { origin, requestedPermissions });
|
|
1025
|
-
}
|
|
1026
|
-
if (!(0, controller_utils_1.isPlainObject)(permission) ||
|
|
1027
|
-
(permission.parentCapability !== undefined &&
|
|
1028
|
-
targetName !== permission.parentCapability)) {
|
|
1029
|
-
throw (0, errors_1.invalidParams)({
|
|
1030
|
-
message: `Permissions request for origin "${origin}" contains invalid requested permission(s).`,
|
|
1031
|
-
data: { origin, requestedPermissions },
|
|
1032
|
-
});
|
|
1033
|
-
}
|
|
1034
|
-
// Here we validate the permission without invoking its validator, if any.
|
|
1035
|
-
// The validator will be invoked after the permission has been approved.
|
|
1036
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validatePermission).call(this, __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_getPermissionSpecification).call(this, targetName),
|
|
1037
|
-
// Typecast: The permission is still a "PlainObject" here.
|
|
1038
|
-
permission, origin, { invokePermissionValidator: false, performCaveatValidation: true });
|
|
1039
|
-
}
|
|
1040
|
-
}, _PermissionController_mergeIncrementalPermissions = function _PermissionController_mergeIncrementalPermissions(existingPermissions, incrementalRequestedPermissions) {
|
|
1041
|
-
const permissionDiffMap = {};
|
|
1042
|
-
// Use immer's produce as a convenience for calculating the new permissions
|
|
1043
|
-
// without mutating the existing permissions or committing the results to state.
|
|
1044
|
-
const newPermissions = (0, immer_1.produce)(existingPermissions, (draftExistingPermissions) => {
|
|
1045
|
-
const leftPermissions = draftExistingPermissions;
|
|
1046
|
-
Object.entries(incrementalRequestedPermissions).forEach(([targetName, rightPermission]) => {
|
|
1047
|
-
const leftPermission = leftPermissions[targetName];
|
|
1048
|
-
const [newPermission, caveatsDiff] = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_mergePermission).call(this, leftPermission ?? {}, rightPermission);
|
|
1049
|
-
if (leftPermission === undefined ||
|
|
1050
|
-
Object.keys(caveatsDiff).length > 0) {
|
|
1051
|
-
leftPermissions[targetName] = newPermission;
|
|
1052
|
-
permissionDiffMap[targetName] = caveatsDiff;
|
|
1053
|
-
}
|
|
1054
|
-
// Otherwise, leave the left permission as-is; its authority has
|
|
1055
|
-
// not changed.
|
|
1056
|
-
});
|
|
1057
|
-
});
|
|
1058
|
-
if (Object.keys(permissionDiffMap).length === 0) {
|
|
1059
|
-
return [];
|
|
1060
|
-
}
|
|
1061
|
-
return [newPermissions, permissionDiffMap];
|
|
1062
|
-
}, _PermissionController_mergePermission = function _PermissionController_mergePermission(leftPermission, rightPermission) {
|
|
1063
|
-
const { caveatPairs, leftUniqueCaveats, rightUniqueCaveats } = (0, utils_2.collectUniqueAndPairedCaveats)(leftPermission, rightPermission);
|
|
1064
|
-
const [mergedCaveats, caveatDiffMap] = caveatPairs.reduce(([caveats, diffMap], [leftCaveat, rightCaveat]) => {
|
|
1065
|
-
const [newCaveat, diff] = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_mergeCaveat).call(this, leftCaveat, rightCaveat);
|
|
1066
|
-
if (newCaveat !== undefined && diff !== undefined) {
|
|
1067
|
-
caveats.push(newCaveat);
|
|
1068
|
-
diffMap[newCaveat.type] = diff;
|
|
1069
|
-
}
|
|
1070
|
-
else {
|
|
1071
|
-
caveats.push(leftCaveat);
|
|
1072
|
-
}
|
|
1073
|
-
return [caveats, diffMap];
|
|
1074
|
-
}, [[], {}]);
|
|
1075
|
-
const mergedRightUniqueCaveats = rightUniqueCaveats.map((caveat) => {
|
|
1076
|
-
const [newCaveat, diff] = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_mergeCaveat).call(this, undefined, caveat);
|
|
1077
|
-
caveatDiffMap[newCaveat.type] = diff;
|
|
1078
|
-
return newCaveat;
|
|
1079
|
-
});
|
|
1080
|
-
const allCaveats = [
|
|
1081
|
-
...mergedCaveats,
|
|
1082
|
-
...leftUniqueCaveats,
|
|
1083
|
-
...mergedRightUniqueCaveats,
|
|
1084
|
-
];
|
|
1085
|
-
const newPermission = {
|
|
1086
|
-
...leftPermission,
|
|
1087
|
-
...rightPermission,
|
|
1088
|
-
...(allCaveats.length > 0
|
|
1089
|
-
? { caveats: allCaveats }
|
|
1090
|
-
: {}),
|
|
1091
|
-
};
|
|
1092
|
-
return [newPermission, caveatDiffMap];
|
|
1093
|
-
}, _PermissionController_mergeCaveat = function _PermissionController_mergeCaveat(leftCaveat, rightCaveat) {
|
|
1094
|
-
/* istanbul ignore if: This should be impossible */
|
|
1095
|
-
if (leftCaveat !== undefined && leftCaveat.type !== rightCaveat.type) {
|
|
1096
|
-
throw new errors_1.CaveatMergeTypeMismatchError(leftCaveat.type, rightCaveat.type);
|
|
1097
|
-
}
|
|
1098
|
-
const merger = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_expectGetCaveatMerger).call(this, rightCaveat.type);
|
|
1099
|
-
if (leftCaveat === undefined) {
|
|
1100
|
-
return [
|
|
1101
|
-
{
|
|
1102
|
-
...rightCaveat,
|
|
1103
|
-
},
|
|
1104
|
-
rightCaveat.value,
|
|
1105
|
-
];
|
|
1106
|
-
}
|
|
1107
|
-
const [newValue, diff] = merger(leftCaveat.value, rightCaveat.value);
|
|
1108
|
-
return newValue !== undefined && diff !== undefined
|
|
1109
|
-
? [
|
|
1110
|
-
{
|
|
1111
|
-
type: rightCaveat.type,
|
|
1112
|
-
value: newValue,
|
|
1113
|
-
},
|
|
1114
|
-
diff,
|
|
1115
|
-
]
|
|
1116
|
-
: [];
|
|
1117
|
-
}, _PermissionController_requestUserApproval =
|
|
1118
|
-
/**
|
|
1119
|
-
* Adds a request to the {@link ApprovalController} using the
|
|
1120
|
-
* {@link ApprovalControllerAddRequestAction} action. Also validates the resulting approved
|
|
1121
|
-
* permissions request, and throws an error if validation fails.
|
|
1122
|
-
*
|
|
1123
|
-
* @param permissionsRequest - The permissions request object.
|
|
1124
|
-
* @returns The approved permissions request object.
|
|
1125
|
-
*/
|
|
1126
|
-
async function _PermissionController_requestUserApproval(permissionsRequest) {
|
|
1127
|
-
const { origin, id } = permissionsRequest.metadata;
|
|
1128
|
-
const approvedRequest = await this.messenger.call('ApprovalController:addRequest', {
|
|
1129
|
-
id,
|
|
1130
|
-
origin,
|
|
1131
|
-
requestData: permissionsRequest,
|
|
1132
|
-
type: utils_2.MethodNames.RequestPermissions,
|
|
1133
|
-
}, true);
|
|
1134
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validateApprovedPermissions).call(this, approvedRequest, { id, origin });
|
|
1135
|
-
return approvedRequest;
|
|
1136
|
-
}, _PermissionController_handleApprovedPermissions =
|
|
1137
|
-
/**
|
|
1138
|
-
* Accepts a permissions request that has been approved by the user. This
|
|
1139
|
-
* method should be called after the user has approved the request and the
|
|
1140
|
-
* {@link ApprovalController} has resolved the user approval promise.
|
|
1141
|
-
*
|
|
1142
|
-
* @param options - Options bag.
|
|
1143
|
-
* @param options.subject - The subject to grant permissions to.
|
|
1144
|
-
* @param options.metadata - The metadata of the approved permissions request.
|
|
1145
|
-
* @param options.preserveExistingPermissions - Whether to preserve the
|
|
1146
|
-
* subject's existing permissions.
|
|
1147
|
-
* @param options.approvedRequest - The approved permissions request to handle.
|
|
1148
|
-
* @returns The granted permissions and request metadata.
|
|
1149
|
-
*/
|
|
1150
|
-
async function _PermissionController_handleApprovedPermissions({ subject, metadata, preserveExistingPermissions, approvedRequest, }) {
|
|
1151
|
-
const { permissions: approvedPermissions, ...requestData } = approvedRequest;
|
|
1152
|
-
const approvedMetadata = { ...metadata };
|
|
1153
|
-
const sideEffects = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_getSideEffects).call(this, approvedPermissions);
|
|
1154
|
-
if (Object.values(sideEffects.permittedHandlers).length > 0) {
|
|
1155
|
-
const sideEffectsData = await __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_executeSideEffects).call(this, sideEffects, approvedRequest);
|
|
1156
|
-
approvedMetadata.data = Object.keys(sideEffects.permittedHandlers).reduce((acc, permission, i) => ({ [permission]: sideEffectsData[i], ...acc }), {});
|
|
1157
|
-
}
|
|
1158
|
-
return [
|
|
1159
|
-
this.grantPermissions({
|
|
1160
|
-
subject,
|
|
1161
|
-
approvedPermissions,
|
|
1162
|
-
preserveExistingPermissions,
|
|
1163
|
-
requestData,
|
|
1164
|
-
}),
|
|
1165
|
-
approvedMetadata,
|
|
1166
|
-
];
|
|
1167
|
-
}, _PermissionController_getSideEffects = function _PermissionController_getSideEffects(permissions) {
|
|
1168
|
-
return Object.keys(permissions).reduce((sideEffectList, targetName) => {
|
|
1169
|
-
if (__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_targetExists).call(this, targetName)) {
|
|
1170
|
-
const specification = __classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_getPermissionSpecification).call(this, targetName);
|
|
1171
|
-
if (specification.sideEffect) {
|
|
1172
|
-
sideEffectList.permittedHandlers[targetName] =
|
|
1173
|
-
specification.sideEffect.onPermitted;
|
|
1174
|
-
if (specification.sideEffect.onFailure) {
|
|
1175
|
-
sideEffectList.failureHandlers[targetName] =
|
|
1176
|
-
specification.sideEffect.onFailure;
|
|
1177
|
-
}
|
|
1178
|
-
}
|
|
1179
|
-
}
|
|
1180
|
-
return sideEffectList;
|
|
1181
|
-
}, { permittedHandlers: {}, failureHandlers: {} });
|
|
1182
|
-
}, _PermissionController_executeSideEffects =
|
|
1183
|
-
/**
|
|
1184
|
-
* Executes the side-effects of the approved permissions while handling the errors if any.
|
|
1185
|
-
* It will pass an instance of the {@link messenger} and the request data associated with the permission request to the handlers through its params.
|
|
1186
|
-
*
|
|
1187
|
-
* @param sideEffects - the side-effect record created by {@link #getSideEffects}
|
|
1188
|
-
* @param requestData - the permissions requestData.
|
|
1189
|
-
* @returns the value returned by all the `onPermitted` handlers in an array.
|
|
1190
|
-
*/
|
|
1191
|
-
async function _PermissionController_executeSideEffects(sideEffects, requestData) {
|
|
1192
|
-
const { permittedHandlers, failureHandlers } = sideEffects;
|
|
1193
|
-
const params = {
|
|
1194
|
-
requestData,
|
|
1195
|
-
messenger: this.messenger,
|
|
1196
|
-
};
|
|
1197
|
-
const promiseResults = await Promise.allSettled(Object.values(permittedHandlers).map((permittedHandler) => permittedHandler(params)));
|
|
1198
|
-
// lib.es2020.promise.d.ts does not export its types so we're using a simple type.
|
|
1199
|
-
const rejectedHandlers = promiseResults.filter((promise) => promise.status === 'rejected');
|
|
1200
|
-
if (rejectedHandlers.length > 0) {
|
|
1201
|
-
const failureHandlersList = Object.values(failureHandlers);
|
|
1202
|
-
if (failureHandlersList.length > 0) {
|
|
1203
|
-
try {
|
|
1204
|
-
await Promise.all(failureHandlersList.map((failureHandler) => failureHandler(params)));
|
|
1205
|
-
}
|
|
1206
|
-
catch (error) {
|
|
1207
|
-
throw (0, errors_1.internalError)('Unexpected error in side-effects', { error });
|
|
1208
|
-
}
|
|
1209
|
-
}
|
|
1210
|
-
const reasons = rejectedHandlers.map((handler) => handler.reason);
|
|
1211
|
-
reasons.forEach((reason) => {
|
|
1212
|
-
console.error(reason);
|
|
1213
|
-
});
|
|
1214
|
-
throw reasons.length > 1
|
|
1215
|
-
? (0, errors_1.internalError)('Multiple errors occurred during side-effects execution', { errors: reasons })
|
|
1216
|
-
: reasons[0];
|
|
1217
|
-
}
|
|
1218
|
-
// lib.es2020.promise.d.ts does not export its types so we're using a simple type.
|
|
1219
|
-
return promiseResults.map(({ value }) => value);
|
|
1220
|
-
}, _PermissionController_validateApprovedPermissions = function _PermissionController_validateApprovedPermissions(approvedRequest, originalMetadata) {
|
|
1221
|
-
const { id, origin } = originalMetadata;
|
|
1222
|
-
if (!(0, controller_utils_1.isPlainObject)(approvedRequest) ||
|
|
1223
|
-
!(0, controller_utils_1.isPlainObject)(approvedRequest.metadata)) {
|
|
1224
|
-
throw (0, errors_1.internalError)(`Approved permissions request for subject "${origin}" is invalid.`, { data: { approvedRequest } });
|
|
1225
|
-
}
|
|
1226
|
-
const { metadata: { id: newId, origin: newOrigin }, permissions, } = approvedRequest;
|
|
1227
|
-
if (newId !== id) {
|
|
1228
|
-
throw (0, errors_1.internalError)(`Approved permissions request for subject "${origin}" mutated its id.`, { originalId: id, mutatedId: newId });
|
|
1229
|
-
}
|
|
1230
|
-
if (newOrigin !== origin) {
|
|
1231
|
-
throw (0, errors_1.internalError)(`Approved permissions request for subject "${origin}" mutated its origin.`, { originalOrigin: origin, mutatedOrigin: newOrigin });
|
|
1232
|
-
}
|
|
1233
|
-
try {
|
|
1234
|
-
__classPrivateFieldGet(this, _PermissionController_instances, "m", _PermissionController_validateRequestedPermissions).call(this, origin, permissions);
|
|
1235
|
-
}
|
|
1236
|
-
catch (error) {
|
|
1237
|
-
if (error instanceof Error) {
|
|
1238
|
-
// Re-throw as an internal error; we should never receive invalid approved
|
|
1239
|
-
// permissions.
|
|
1240
|
-
throw (0, errors_1.internalError)(`Invalid approved permissions request: ${error.message}`, error instanceof rpc_errors_1.JsonRpcError ? error.data : undefined);
|
|
1241
|
-
}
|
|
1242
|
-
/* istanbul ignore next: This should be impossible */
|
|
1243
|
-
throw (0, errors_1.internalError)('Unrecognized error type', { error });
|
|
1244
|
-
}
|
|
1245
|
-
}, _PermissionController_hasApprovalRequest = function _PermissionController_hasApprovalRequest(options) {
|
|
1246
|
-
return this.messenger.call('ApprovalController:hasRequest', options);
|
|
1247
|
-
}, _PermissionController_rejectPermissionsRequest = function _PermissionController_rejectPermissionsRequest(id, error) {
|
|
1248
|
-
return this.messenger.call('ApprovalController:rejectRequest', id, error);
|
|
1249
|
-
}, _PermissionController_executeRestrictedMethod = function _PermissionController_executeRestrictedMethod(methodImplementation, subject, method, params = []) {
|
|
1250
|
-
const { origin } = subject;
|
|
1251
|
-
const permission = this.getPermission(origin, method);
|
|
1252
|
-
if (!permission) {
|
|
1253
|
-
throw (0, errors_1.unauthorized)({ data: { origin, method } });
|
|
1254
|
-
}
|
|
1255
|
-
return (0, Caveat_1.decorateWithCaveats)(methodImplementation, permission, __classPrivateFieldGet(this, _PermissionController_caveatSpecifications, "f"))({ method, params, context: { origin } });
|
|
1256
|
-
};
|
|
1257
|
-
//# sourceMappingURL=PermissionController.cjs.map
|