@metamask/permission-controller 1.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 -0
- package/LICENSE +20 -0
- package/README.md +19 -0
- package/dist/Caveat.d.ts +182 -0
- package/dist/Caveat.js +57 -0
- package/dist/Caveat.js.map +1 -0
- package/dist/Permission.d.ts +422 -0
- package/dist/Permission.js +66 -0
- package/dist/Permission.js.map +1 -0
- package/dist/PermissionController.d.ts +870 -0
- package/dist/PermissionController.js +1229 -0
- package/dist/PermissionController.js.map +1 -0
- package/dist/errors.d.ts +158 -0
- package/dist/errors.js +196 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/permission-middleware.d.ts +32 -0
- package/dist/permission-middleware.js +64 -0
- package/dist/permission-middleware.js.map +1 -0
- package/dist/rpc-methods/getPermissions.d.ts +7 -0
- package/dist/rpc-methods/getPermissions.js +38 -0
- package/dist/rpc-methods/getPermissions.js.map +1 -0
- package/dist/rpc-methods/index.d.ts +4 -0
- package/dist/rpc-methods/index.js +7 -0
- package/dist/rpc-methods/index.js.map +1 -0
- package/dist/rpc-methods/requestPermissions.d.ts +16 -0
- package/dist/rpc-methods/requestPermissions.js +55 -0
- package/dist/rpc-methods/requestPermissions.js.map +1 -0
- package/dist/utils.d.ts +15 -0
- package/dist/utils.js +9 -0
- package/dist/utils.js.map +1 -0
- package/package.json +60 -0
|
@@ -0,0 +1,1229 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
22
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.PermissionController = exports.CaveatMutatorOperation = void 0;
|
|
27
|
+
const deep_freeze_strict_1 = __importDefault(require("deep-freeze-strict"));
|
|
28
|
+
const immer_1 = require("immer");
|
|
29
|
+
const nanoid_1 = require("nanoid");
|
|
30
|
+
const eth_rpc_errors_1 = require("eth-rpc-errors");
|
|
31
|
+
const base_controller_1 = require("@metamask/base-controller");
|
|
32
|
+
const controller_utils_1 = require("@metamask/controller-utils");
|
|
33
|
+
const Caveat_1 = require("./Caveat");
|
|
34
|
+
const errors_1 = require("./errors");
|
|
35
|
+
const Permission_1 = require("./Permission");
|
|
36
|
+
const permission_middleware_1 = require("./permission-middleware");
|
|
37
|
+
const utils_1 = require("./utils");
|
|
38
|
+
/**
|
|
39
|
+
* The name of the {@link PermissionController}.
|
|
40
|
+
*/
|
|
41
|
+
const controllerName = 'PermissionController';
|
|
42
|
+
/**
|
|
43
|
+
* Get the state metadata of the {@link PermissionController}.
|
|
44
|
+
*
|
|
45
|
+
* @template Permission - The controller's permission type union.
|
|
46
|
+
* @returns The state metadata
|
|
47
|
+
*/
|
|
48
|
+
function getStateMetadata() {
|
|
49
|
+
return { subjects: { anonymous: true, persist: true } };
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Get the default state of the {@link PermissionController}.
|
|
53
|
+
*
|
|
54
|
+
* @template Permission - The controller's permission type union.
|
|
55
|
+
* @returns The default state of the controller
|
|
56
|
+
*/
|
|
57
|
+
function getDefaultState() {
|
|
58
|
+
return { subjects: {} };
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Describes the possible results of a {@link CaveatMutator} function.
|
|
62
|
+
*/
|
|
63
|
+
var CaveatMutatorOperation;
|
|
64
|
+
(function (CaveatMutatorOperation) {
|
|
65
|
+
CaveatMutatorOperation[CaveatMutatorOperation["noop"] = 0] = "noop";
|
|
66
|
+
CaveatMutatorOperation[CaveatMutatorOperation["updateValue"] = 1] = "updateValue";
|
|
67
|
+
CaveatMutatorOperation[CaveatMutatorOperation["deleteCaveat"] = 2] = "deleteCaveat";
|
|
68
|
+
CaveatMutatorOperation[CaveatMutatorOperation["revokePermission"] = 3] = "revokePermission";
|
|
69
|
+
})(CaveatMutatorOperation = exports.CaveatMutatorOperation || (exports.CaveatMutatorOperation = {}));
|
|
70
|
+
/**
|
|
71
|
+
* The permission controller. See the [Architecture](../ARCHITECTURE.md)
|
|
72
|
+
* document for details.
|
|
73
|
+
*
|
|
74
|
+
* Assumes the existence of an {@link ApprovalController} reachable via the
|
|
75
|
+
* {@link ControllerMessenger}.
|
|
76
|
+
*
|
|
77
|
+
* @template ControllerPermissionSpecification - A union of the types of all
|
|
78
|
+
* permission specifications available to the controller. Any referenced caveats
|
|
79
|
+
* must be included in the controller's caveat specifications.
|
|
80
|
+
* @template ControllerCaveatSpecification - A union of the types of all
|
|
81
|
+
* caveat specifications available to the controller.
|
|
82
|
+
*/
|
|
83
|
+
class PermissionController extends base_controller_1.BaseControllerV2 {
|
|
84
|
+
/**
|
|
85
|
+
* Constructs the PermissionController.
|
|
86
|
+
*
|
|
87
|
+
* @param options - Permission controller options.
|
|
88
|
+
* @param options.caveatSpecifications - The specifications of all caveats
|
|
89
|
+
* available to the controller. See {@link CaveatSpecificationMap} and the
|
|
90
|
+
* documentation for more details.
|
|
91
|
+
* @param options.permissionSpecifications - The specifications of all
|
|
92
|
+
* permissions available to the controller. See
|
|
93
|
+
* {@link PermissionSpecificationMap} and the README for more details.
|
|
94
|
+
* @param options.unrestrictedMethods - The callable names of all JSON-RPC
|
|
95
|
+
* methods ignored by the new controller.
|
|
96
|
+
* @param options.messenger - The controller messenger. See
|
|
97
|
+
* {@link BaseControllerV2} for more information.
|
|
98
|
+
* @param options.state - Existing state to hydrate the controller with at
|
|
99
|
+
* initialization.
|
|
100
|
+
*/
|
|
101
|
+
constructor(options) {
|
|
102
|
+
const { caveatSpecifications, permissionSpecifications, unrestrictedMethods, messenger, state = {}, } = options;
|
|
103
|
+
super({
|
|
104
|
+
name: controllerName,
|
|
105
|
+
metadata: getStateMetadata(),
|
|
106
|
+
messenger,
|
|
107
|
+
state: Object.assign(Object.assign({}, getDefaultState()), state),
|
|
108
|
+
});
|
|
109
|
+
this._unrestrictedMethods = new Set(unrestrictedMethods);
|
|
110
|
+
this._caveatSpecifications = (0, deep_freeze_strict_1.default)(Object.assign({}, caveatSpecifications));
|
|
111
|
+
this.validatePermissionSpecifications(permissionSpecifications, this._caveatSpecifications);
|
|
112
|
+
this._permissionSpecifications = (0, deep_freeze_strict_1.default)(Object.assign({}, permissionSpecifications));
|
|
113
|
+
this.registerMessageHandlers();
|
|
114
|
+
this.createPermissionMiddleware = (0, permission_middleware_1.getPermissionMiddlewareFactory)({
|
|
115
|
+
executeRestrictedMethod: this._executeRestrictedMethod.bind(this),
|
|
116
|
+
getRestrictedMethod: this.getRestrictedMethod.bind(this),
|
|
117
|
+
isUnrestrictedMethod: this.unrestrictedMethods.has.bind(this.unrestrictedMethods),
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The names of all JSON-RPC methods that will be ignored by the controller.
|
|
122
|
+
*
|
|
123
|
+
* @returns The names of all unrestricted JSON-RPC methods
|
|
124
|
+
*/
|
|
125
|
+
get unrestrictedMethods() {
|
|
126
|
+
return this._unrestrictedMethods;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Gets a permission specification.
|
|
130
|
+
*
|
|
131
|
+
* @param targetKey - The target key of the permission specification to get.
|
|
132
|
+
* @returns The permission specification with the specified target key.
|
|
133
|
+
*/
|
|
134
|
+
getPermissionSpecification(targetKey) {
|
|
135
|
+
return this._permissionSpecifications[targetKey];
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Gets a caveat specification.
|
|
139
|
+
*
|
|
140
|
+
* @param caveatType - The type of the caveat specification to get.
|
|
141
|
+
* @returns The caveat specification with the specified type.
|
|
142
|
+
*/
|
|
143
|
+
getCaveatSpecification(caveatType) {
|
|
144
|
+
return this._caveatSpecifications[caveatType];
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Constructor helper for validating permission specifications. This is
|
|
148
|
+
* intended to prevent the use of invalid target keys which, while impossible
|
|
149
|
+
* to add in TypeScript, could rather easily occur in plain JavaScript.
|
|
150
|
+
*
|
|
151
|
+
* Throws an error if validation fails.
|
|
152
|
+
*
|
|
153
|
+
* @param permissionSpecifications - The permission specifications passed to
|
|
154
|
+
* this controller's constructor.
|
|
155
|
+
* @param caveatSpecifications - The caveat specifications passed to this
|
|
156
|
+
* controller.
|
|
157
|
+
*/
|
|
158
|
+
validatePermissionSpecifications(permissionSpecifications, caveatSpecifications) {
|
|
159
|
+
Object.entries(permissionSpecifications).forEach(([targetKey, { permissionType, targetKey: innerTargetKey, allowedCaveats },]) => {
|
|
160
|
+
if (!permissionType || !(0, controller_utils_1.hasProperty)(Permission_1.PermissionType, permissionType)) {
|
|
161
|
+
throw new Error(`Invalid permission type: "${permissionType}"`);
|
|
162
|
+
}
|
|
163
|
+
// Check if the target key is the empty string, ends with "_", or ends
|
|
164
|
+
// with "*" but not "_*"
|
|
165
|
+
if (!targetKey || /_$/u.test(targetKey) || /[^_]\*$/u.test(targetKey)) {
|
|
166
|
+
throw new Error(`Invalid permission target key: "${targetKey}"`);
|
|
167
|
+
}
|
|
168
|
+
if (targetKey !== innerTargetKey) {
|
|
169
|
+
throw new Error(`Invalid permission specification: key "${targetKey}" must match specification.target value "${innerTargetKey}".`);
|
|
170
|
+
}
|
|
171
|
+
if (allowedCaveats) {
|
|
172
|
+
allowedCaveats.forEach((caveatType) => {
|
|
173
|
+
if (!(0, controller_utils_1.hasProperty)(caveatSpecifications, caveatType)) {
|
|
174
|
+
throw new errors_1.UnrecognizedCaveatTypeError(caveatType);
|
|
175
|
+
}
|
|
176
|
+
const specification = caveatSpecifications[caveatType];
|
|
177
|
+
const isRestrictedMethodCaveat = (0, Caveat_1.isRestrictedMethodCaveatSpecification)(specification);
|
|
178
|
+
if ((permissionType === Permission_1.PermissionType.RestrictedMethod &&
|
|
179
|
+
!isRestrictedMethodCaveat) ||
|
|
180
|
+
(permissionType === Permission_1.PermissionType.Endowment &&
|
|
181
|
+
isRestrictedMethodCaveat)) {
|
|
182
|
+
throw new errors_1.CaveatSpecificationMismatchError(specification, permissionType);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Constructor helper for registering the controller's messaging system
|
|
190
|
+
* actions.
|
|
191
|
+
*/
|
|
192
|
+
registerMessageHandlers() {
|
|
193
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:clearPermissions`, () => this.clearState());
|
|
194
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:getEndowments`, (origin, targetName, requestData) => this.getEndowments(origin, targetName, requestData));
|
|
195
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:getSubjectNames`, () => this.getSubjectNames());
|
|
196
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:getPermissions`, (origin) => this.getPermissions(origin));
|
|
197
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:hasPermission`, (origin, targetName) => this.hasPermission(origin, targetName));
|
|
198
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:hasPermissions`, (origin) => this.hasPermissions(origin));
|
|
199
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:grantPermissions`, this.grantPermissions.bind(this));
|
|
200
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:requestPermissions`, (subject, permissions) => this.requestPermissions(subject, permissions));
|
|
201
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:revokeAllPermissions`, (origin) => this.revokeAllPermissions(origin));
|
|
202
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:revokePermissionForAllSubjects`, (target) => this.revokePermissionForAllSubjects(target));
|
|
203
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:revokePermissions`, this.revokePermissions.bind(this));
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Clears the state of the controller.
|
|
207
|
+
*/
|
|
208
|
+
clearState() {
|
|
209
|
+
this.update((_draftState) => {
|
|
210
|
+
return Object.assign({}, getDefaultState());
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Gets the permission specification corresponding to the given permission
|
|
215
|
+
* type and target name. Throws an error if the target name does not
|
|
216
|
+
* correspond to a permission, or if the specification is not of the
|
|
217
|
+
* given permission type.
|
|
218
|
+
*
|
|
219
|
+
* @template Type - The type of the permission specification to get.
|
|
220
|
+
* @param permissionType - The type of the permission specification to get.
|
|
221
|
+
* @param targetName - The name of the permission whose specification to get.
|
|
222
|
+
* @param requestingOrigin - The origin of the requesting subject, if any.
|
|
223
|
+
* Will be added to any thrown errors.
|
|
224
|
+
* @returns The specification object corresponding to the given type and
|
|
225
|
+
* target name.
|
|
226
|
+
*/
|
|
227
|
+
getTypedPermissionSpecification(permissionType, targetName, requestingOrigin) {
|
|
228
|
+
const failureError = permissionType === Permission_1.PermissionType.RestrictedMethod
|
|
229
|
+
? (0, errors_1.methodNotFound)(targetName, requestingOrigin ? { origin: requestingOrigin } : undefined)
|
|
230
|
+
: new errors_1.EndowmentPermissionDoesNotExistError(targetName, requestingOrigin);
|
|
231
|
+
const targetKey = this.getTargetKey(targetName);
|
|
232
|
+
if (!targetKey) {
|
|
233
|
+
throw failureError;
|
|
234
|
+
}
|
|
235
|
+
const specification = this.getPermissionSpecification(targetKey);
|
|
236
|
+
if (!(0, Permission_1.hasSpecificationType)(specification, permissionType)) {
|
|
237
|
+
throw failureError;
|
|
238
|
+
}
|
|
239
|
+
return specification;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Gets the implementation of the specified restricted method.
|
|
243
|
+
*
|
|
244
|
+
* A JSON-RPC error is thrown if the method does not exist.
|
|
245
|
+
*
|
|
246
|
+
* @see {@link PermissionController.executeRestrictedMethod} and
|
|
247
|
+
* {@link PermissionController.createPermissionMiddleware} for internal usage.
|
|
248
|
+
* @param method - The name of the restricted method.
|
|
249
|
+
* @param origin - The origin associated with the request for the restricted
|
|
250
|
+
* method, if any.
|
|
251
|
+
* @returns The restricted method implementation.
|
|
252
|
+
*/
|
|
253
|
+
getRestrictedMethod(method, origin) {
|
|
254
|
+
return this.getTypedPermissionSpecification(Permission_1.PermissionType.RestrictedMethod, method, origin).methodImplementation;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Gets a list of all origins of subjects.
|
|
258
|
+
*
|
|
259
|
+
* @returns The origins (i.e. IDs) of all subjects.
|
|
260
|
+
*/
|
|
261
|
+
getSubjectNames() {
|
|
262
|
+
return Object.keys(this.state.subjects);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Gets the permission for the specified target of the subject corresponding
|
|
266
|
+
* to the specified origin.
|
|
267
|
+
*
|
|
268
|
+
* @param origin - The origin of the subject.
|
|
269
|
+
* @param targetName - The method name as invoked by a third party (i.e., not
|
|
270
|
+
* a method key).
|
|
271
|
+
* @returns The permission if it exists, or undefined otherwise.
|
|
272
|
+
*/
|
|
273
|
+
getPermission(origin, targetName) {
|
|
274
|
+
var _a;
|
|
275
|
+
return (_a = this.state.subjects[origin]) === null || _a === void 0 ? void 0 : _a.permissions[targetName];
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Gets all permissions for the specified subject, if any.
|
|
279
|
+
*
|
|
280
|
+
* @param origin - The origin of the subject.
|
|
281
|
+
* @returns The permissions of the subject, if any.
|
|
282
|
+
*/
|
|
283
|
+
getPermissions(origin) {
|
|
284
|
+
var _a;
|
|
285
|
+
return (_a = this.state.subjects[origin]) === null || _a === void 0 ? void 0 : _a.permissions;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Checks whether the subject with the specified origin has the specified
|
|
289
|
+
* permission.
|
|
290
|
+
*
|
|
291
|
+
* @param origin - The origin of the subject.
|
|
292
|
+
* @param target - The target name of the permission.
|
|
293
|
+
* @returns Whether the subject has the permission.
|
|
294
|
+
*/
|
|
295
|
+
hasPermission(origin, target) {
|
|
296
|
+
return Boolean(this.getPermission(origin, target));
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Checks whether the subject with the specified origin has any permissions.
|
|
300
|
+
* Use this if you want to know if a subject "exists".
|
|
301
|
+
*
|
|
302
|
+
* @param origin - The origin of the subject to check.
|
|
303
|
+
* @returns Whether the subject has any permissions.
|
|
304
|
+
*/
|
|
305
|
+
hasPermissions(origin) {
|
|
306
|
+
return Boolean(this.state.subjects[origin]);
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Revokes all permissions from the specified origin.
|
|
310
|
+
*
|
|
311
|
+
* Throws an error of the origin has no permissions.
|
|
312
|
+
*
|
|
313
|
+
* @param origin - The origin whose permissions to revoke.
|
|
314
|
+
*/
|
|
315
|
+
revokeAllPermissions(origin) {
|
|
316
|
+
this.update((draftState) => {
|
|
317
|
+
if (!draftState.subjects[origin]) {
|
|
318
|
+
throw new errors_1.UnrecognizedSubjectError(origin);
|
|
319
|
+
}
|
|
320
|
+
delete draftState.subjects[origin];
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Revokes the specified permission from the subject with the specified
|
|
325
|
+
* origin.
|
|
326
|
+
*
|
|
327
|
+
* Throws an error if the subject or the permission does not exist.
|
|
328
|
+
*
|
|
329
|
+
* @param origin - The origin of the subject whose permission to revoke.
|
|
330
|
+
* @param target - The target name of the permission to revoke.
|
|
331
|
+
*/
|
|
332
|
+
revokePermission(origin, target) {
|
|
333
|
+
this.revokePermissions({ [origin]: [target] });
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Revokes the specified permissions from the specified subjects.
|
|
337
|
+
*
|
|
338
|
+
* Throws an error if any of the subjects or permissions do not exist.
|
|
339
|
+
*
|
|
340
|
+
* @param subjectsAndPermissions - An object mapping subject origins
|
|
341
|
+
* to arrays of permission target names to revoke.
|
|
342
|
+
*/
|
|
343
|
+
revokePermissions(subjectsAndPermissions) {
|
|
344
|
+
this.update((draftState) => {
|
|
345
|
+
Object.keys(subjectsAndPermissions).forEach((origin) => {
|
|
346
|
+
if (!(0, controller_utils_1.hasProperty)(draftState.subjects, origin)) {
|
|
347
|
+
throw new errors_1.UnrecognizedSubjectError(origin);
|
|
348
|
+
}
|
|
349
|
+
subjectsAndPermissions[origin].forEach((target) => {
|
|
350
|
+
const { permissions } = draftState.subjects[origin];
|
|
351
|
+
if (!(0, controller_utils_1.hasProperty)(permissions, target)) {
|
|
352
|
+
throw new errors_1.PermissionDoesNotExistError(origin, target);
|
|
353
|
+
}
|
|
354
|
+
this.deletePermission(draftState.subjects, origin, target);
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Revokes all permissions corresponding to the specified target for all subjects.
|
|
361
|
+
* Does nothing if no subjects or no such permission exists.
|
|
362
|
+
*
|
|
363
|
+
* @param target - The name of the target to revoke all permissions for.
|
|
364
|
+
*/
|
|
365
|
+
revokePermissionForAllSubjects(target) {
|
|
366
|
+
if (this.getSubjectNames().length === 0) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
this.update((draftState) => {
|
|
370
|
+
Object.entries(draftState.subjects).forEach(([origin, subject]) => {
|
|
371
|
+
const { permissions } = subject;
|
|
372
|
+
if ((0, controller_utils_1.hasProperty)(permissions, target)) {
|
|
373
|
+
this.deletePermission(draftState.subjects, origin, target);
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Deletes the permission identified by the given origin and target. If the
|
|
380
|
+
* permission is the single remaining permission of its subject, the subject
|
|
381
|
+
* is also deleted.
|
|
382
|
+
*
|
|
383
|
+
* @param subjects - The draft permission controller subjects.
|
|
384
|
+
* @param origin - The origin of the subject associated with the permission
|
|
385
|
+
* to delete.
|
|
386
|
+
* @param target - The target name of the permission to delete.
|
|
387
|
+
*/
|
|
388
|
+
deletePermission(subjects, origin, target) {
|
|
389
|
+
const { permissions } = subjects[origin];
|
|
390
|
+
if (Object.keys(permissions).length > 1) {
|
|
391
|
+
delete permissions[target];
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
delete subjects[origin];
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Checks whether the permission of the subject corresponding to the given
|
|
399
|
+
* origin has a caveat of the specified type.
|
|
400
|
+
*
|
|
401
|
+
* Throws an error if the subject does not have a permission with the
|
|
402
|
+
* specified target name.
|
|
403
|
+
*
|
|
404
|
+
* @template TargetName - The permission target name. Should be inferred.
|
|
405
|
+
* @template CaveatType - The valid caveat types for the permission. Should
|
|
406
|
+
* be inferred.
|
|
407
|
+
* @param origin - The origin of the subject.
|
|
408
|
+
* @param target - The target name of the permission.
|
|
409
|
+
* @param caveatType - The type of the caveat to check for.
|
|
410
|
+
* @returns Whether the permission has the specified caveat.
|
|
411
|
+
*/
|
|
412
|
+
hasCaveat(origin, target, caveatType) {
|
|
413
|
+
return Boolean(this.getCaveat(origin, target, caveatType));
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Gets the caveat of the specified type, if any, for the permission of
|
|
417
|
+
* the subject corresponding to the given origin.
|
|
418
|
+
*
|
|
419
|
+
* Throws an error if the subject does not have a permission with the
|
|
420
|
+
* specified target name.
|
|
421
|
+
*
|
|
422
|
+
* @template TargetName - The permission target name. Should be inferred.
|
|
423
|
+
* @template CaveatType - The valid caveat types for the permission. Should
|
|
424
|
+
* be inferred.
|
|
425
|
+
* @param origin - The origin of the subject.
|
|
426
|
+
* @param target - The target name of the permission.
|
|
427
|
+
* @param caveatType - The type of the caveat to get.
|
|
428
|
+
* @returns The caveat, or `undefined` if no such caveat exists.
|
|
429
|
+
*/
|
|
430
|
+
getCaveat(origin, target, caveatType) {
|
|
431
|
+
const permission = this.getPermission(origin, target);
|
|
432
|
+
if (!permission) {
|
|
433
|
+
throw new errors_1.PermissionDoesNotExistError(origin, target);
|
|
434
|
+
}
|
|
435
|
+
return (0, Permission_1.findCaveat)(permission, caveatType);
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Adds a caveat of the specified type, with the specified caveat value, to
|
|
439
|
+
* the permission corresponding to the given subject origin and permission
|
|
440
|
+
* target.
|
|
441
|
+
*
|
|
442
|
+
* For modifying existing caveats, use
|
|
443
|
+
* {@link PermissionController.updateCaveat}.
|
|
444
|
+
*
|
|
445
|
+
* Throws an error if no such permission exists, or if the caveat already
|
|
446
|
+
* exists.
|
|
447
|
+
*
|
|
448
|
+
* @template TargetName - The permission target name. Should be inferred.
|
|
449
|
+
* @template CaveatType - The valid caveat types for the permission. Should
|
|
450
|
+
* be inferred.
|
|
451
|
+
* @param origin - The origin of the subject.
|
|
452
|
+
* @param target - The target name of the permission.
|
|
453
|
+
* @param caveatType - The type of the caveat to add.
|
|
454
|
+
* @param caveatValue - The value of the caveat to add.
|
|
455
|
+
*/
|
|
456
|
+
addCaveat(origin, target, caveatType, caveatValue) {
|
|
457
|
+
if (this.hasCaveat(origin, target, caveatType)) {
|
|
458
|
+
throw new errors_1.CaveatAlreadyExistsError(origin, target, caveatType);
|
|
459
|
+
}
|
|
460
|
+
this.setCaveat(origin, target, caveatType, caveatValue);
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Updates the value of the caveat of the specified type belonging to the
|
|
464
|
+
* permission corresponding to the given subject origin and permission
|
|
465
|
+
* target.
|
|
466
|
+
*
|
|
467
|
+
* For adding new caveats, use
|
|
468
|
+
* {@link PermissionController.addCaveat}.
|
|
469
|
+
*
|
|
470
|
+
* Throws an error if no such permission or caveat exists.
|
|
471
|
+
*
|
|
472
|
+
* @template TargetName - The permission target name. Should be inferred.
|
|
473
|
+
* @template CaveatType - The valid caveat types for the permission. Should
|
|
474
|
+
* be inferred.
|
|
475
|
+
* @param origin - The origin of the subject.
|
|
476
|
+
* @param target - The target name of the permission.
|
|
477
|
+
* @param caveatType - The type of the caveat to update.
|
|
478
|
+
* @param caveatValue - The new value of the caveat.
|
|
479
|
+
*/
|
|
480
|
+
updateCaveat(origin, target, caveatType, caveatValue) {
|
|
481
|
+
if (!this.hasCaveat(origin, target, caveatType)) {
|
|
482
|
+
throw new errors_1.CaveatDoesNotExistError(origin, target, caveatType);
|
|
483
|
+
}
|
|
484
|
+
this.setCaveat(origin, target, caveatType, caveatValue);
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Sets the specified caveat on the specified permission. Overwrites existing
|
|
488
|
+
* caveats of the same type in-place (preserving array order), and adds the
|
|
489
|
+
* caveat to the end of the array otherwise.
|
|
490
|
+
*
|
|
491
|
+
* Throws an error if the permission does not exist or fails to validate after
|
|
492
|
+
* its caveats have been modified.
|
|
493
|
+
*
|
|
494
|
+
* @see {@link PermissionController.addCaveat}
|
|
495
|
+
* @see {@link PermissionController.updateCaveat}
|
|
496
|
+
* @template TargetName - The permission target name. Should be inferred.
|
|
497
|
+
* @template CaveatType - The valid caveat types for the permission. Should
|
|
498
|
+
* be inferred.
|
|
499
|
+
* @param origin - The origin of the subject.
|
|
500
|
+
* @param target - The target name of the permission.
|
|
501
|
+
* @param caveatType - The type of the caveat to set.
|
|
502
|
+
* @param caveatValue - The value of the caveat to set.
|
|
503
|
+
*/
|
|
504
|
+
setCaveat(origin, target, caveatType, caveatValue) {
|
|
505
|
+
this.update((draftState) => {
|
|
506
|
+
const subject = draftState.subjects[origin];
|
|
507
|
+
// Unreachable because `hasCaveat` is always called before this, and it
|
|
508
|
+
// throws if permissions are missing. TypeScript needs this, however.
|
|
509
|
+
/* istanbul ignore if */
|
|
510
|
+
if (!subject) {
|
|
511
|
+
throw new errors_1.UnrecognizedSubjectError(origin);
|
|
512
|
+
}
|
|
513
|
+
const permission = subject.permissions[target];
|
|
514
|
+
/* istanbul ignore if: practically impossible, but TypeScript wants it */
|
|
515
|
+
if (!permission) {
|
|
516
|
+
throw new errors_1.PermissionDoesNotExistError(origin, target);
|
|
517
|
+
}
|
|
518
|
+
const caveat = {
|
|
519
|
+
type: caveatType,
|
|
520
|
+
value: caveatValue,
|
|
521
|
+
};
|
|
522
|
+
this.validateCaveat(caveat, origin, target);
|
|
523
|
+
if (permission.caveats) {
|
|
524
|
+
const caveatIndex = permission.caveats.findIndex((existingCaveat) => existingCaveat.type === caveat.type);
|
|
525
|
+
if (caveatIndex === -1) {
|
|
526
|
+
permission.caveats.push(caveat);
|
|
527
|
+
}
|
|
528
|
+
else {
|
|
529
|
+
permission.caveats.splice(caveatIndex, 1, caveat);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
// Typecast: At this point, we don't know if the specific permission
|
|
534
|
+
// is allowed to have caveats, but it should be impossible to call
|
|
535
|
+
// this method for a permission that may not have any caveats.
|
|
536
|
+
// If all else fails, the permission validator is also called.
|
|
537
|
+
permission.caveats = [caveat];
|
|
538
|
+
}
|
|
539
|
+
this.validateModifiedPermission(permission, origin, target);
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Updates all caveats with the specified type for all subjects and
|
|
544
|
+
* permissions by applying the specified mutator function to them.
|
|
545
|
+
*
|
|
546
|
+
* ATTN: Permissions can be revoked entirely by the action of this method,
|
|
547
|
+
* read on for details.
|
|
548
|
+
*
|
|
549
|
+
* Caveat mutators are functions that receive a caveat value and return a
|
|
550
|
+
* tuple consisting of a {@link CaveatMutatorOperation} and, optionally, a new
|
|
551
|
+
* value to update the existing caveat with.
|
|
552
|
+
*
|
|
553
|
+
* For each caveat, depending on the mutator result, this method will:
|
|
554
|
+
* - Do nothing ({@link CaveatMutatorOperation.noop})
|
|
555
|
+
* - Update the value of the caveat ({@link CaveatMutatorOperation.updateValue}). The caveat specification validator, if any, will be called after updating the value.
|
|
556
|
+
* - Delete the caveat ({@link CaveatMutatorOperation.deleteCaveat}). The permission specification validator, if any, will be called after deleting the caveat.
|
|
557
|
+
* - Revoke the parent permission ({@link CaveatMutatorOperation.revokePermission})
|
|
558
|
+
*
|
|
559
|
+
* This method throws if the validation of any caveat or permission fails.
|
|
560
|
+
*
|
|
561
|
+
* @param targetCaveatType - The type of the caveats to update.
|
|
562
|
+
* @param mutator - The mutator function which will be applied to all caveat
|
|
563
|
+
* values.
|
|
564
|
+
*/
|
|
565
|
+
updatePermissionsByCaveat(targetCaveatType, mutator) {
|
|
566
|
+
if (Object.keys(this.state.subjects).length === 0) {
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
this.update((draftState) => {
|
|
570
|
+
Object.values(draftState.subjects).forEach((subject) => {
|
|
571
|
+
Object.values(subject.permissions).forEach((permission) => {
|
|
572
|
+
const { caveats } = permission;
|
|
573
|
+
const targetCaveat = caveats === null || caveats === void 0 ? void 0 : caveats.find(({ type }) => type === targetCaveatType);
|
|
574
|
+
if (!targetCaveat) {
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
// The mutator may modify the caveat value in place, and must always
|
|
578
|
+
// return a valid mutation result.
|
|
579
|
+
const mutatorResult = mutator(targetCaveat.value);
|
|
580
|
+
switch (mutatorResult.operation) {
|
|
581
|
+
case CaveatMutatorOperation.noop:
|
|
582
|
+
break;
|
|
583
|
+
case CaveatMutatorOperation.updateValue:
|
|
584
|
+
// Typecast: `Mutable` is used here to assign to a readonly
|
|
585
|
+
// property. `targetConstraint` should already be mutable because
|
|
586
|
+
// it's part of a draft, but for some reason it's not. We can't
|
|
587
|
+
// use the more-correct `Draft` type here either because it
|
|
588
|
+
// results in an error.
|
|
589
|
+
targetCaveat.value =
|
|
590
|
+
mutatorResult.value;
|
|
591
|
+
this.validateCaveat(targetCaveat, subject.origin, permission.parentCapability);
|
|
592
|
+
break;
|
|
593
|
+
case CaveatMutatorOperation.deleteCaveat:
|
|
594
|
+
this.deleteCaveat(permission, targetCaveatType, subject.origin, permission.parentCapability);
|
|
595
|
+
break;
|
|
596
|
+
case CaveatMutatorOperation.revokePermission:
|
|
597
|
+
this.deletePermission(draftState.subjects, subject.origin, permission.parentCapability);
|
|
598
|
+
break;
|
|
599
|
+
default: {
|
|
600
|
+
// This type check ensures that the switch statement is
|
|
601
|
+
// exhaustive.
|
|
602
|
+
const _exhaustiveCheck = mutatorResult;
|
|
603
|
+
throw new Error(`Unrecognized mutation result: "${_exhaustiveCheck.operation}"`);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
});
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Removes the caveat of the specified type from the permission corresponding
|
|
612
|
+
* to the given subject origin and target name.
|
|
613
|
+
*
|
|
614
|
+
* Throws an error if no such permission or caveat exists.
|
|
615
|
+
*
|
|
616
|
+
* @template TargetName - The permission target name. Should be inferred.
|
|
617
|
+
* @template CaveatType - The valid caveat types for the permission. Should
|
|
618
|
+
* be inferred.
|
|
619
|
+
* @param origin - The origin of the subject.
|
|
620
|
+
* @param target - The target name of the permission.
|
|
621
|
+
* @param caveatType - The type of the caveat to remove.
|
|
622
|
+
*/
|
|
623
|
+
removeCaveat(origin, target, caveatType) {
|
|
624
|
+
this.update((draftState) => {
|
|
625
|
+
var _a;
|
|
626
|
+
const permission = (_a = draftState.subjects[origin]) === null || _a === void 0 ? void 0 : _a.permissions[target];
|
|
627
|
+
if (!permission) {
|
|
628
|
+
throw new errors_1.PermissionDoesNotExistError(origin, target);
|
|
629
|
+
}
|
|
630
|
+
if (!permission.caveats) {
|
|
631
|
+
throw new errors_1.CaveatDoesNotExistError(origin, target, caveatType);
|
|
632
|
+
}
|
|
633
|
+
this.deleteCaveat(permission, caveatType, origin, target);
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Deletes the specified caveat from the specified permission. If no caveats
|
|
638
|
+
* remain after deletion, the permission's caveat property is set to `null`.
|
|
639
|
+
* The permission is validated after being modified.
|
|
640
|
+
*
|
|
641
|
+
* Throws an error if the permission does not have a caveat with the specified
|
|
642
|
+
* type.
|
|
643
|
+
*
|
|
644
|
+
* @param permission - The permission whose caveat to delete.
|
|
645
|
+
* @param caveatType - The type of the caveat to delete.
|
|
646
|
+
* @param origin - The origin the permission subject.
|
|
647
|
+
* @param target - The name of the permission target.
|
|
648
|
+
*/
|
|
649
|
+
deleteCaveat(permission, caveatType, origin, target) {
|
|
650
|
+
/* istanbul ignore if: not possible in our usage */
|
|
651
|
+
if (!permission.caveats) {
|
|
652
|
+
throw new errors_1.CaveatDoesNotExistError(origin, target, caveatType);
|
|
653
|
+
}
|
|
654
|
+
const caveatIndex = permission.caveats.findIndex((existingCaveat) => existingCaveat.type === caveatType);
|
|
655
|
+
if (caveatIndex === -1) {
|
|
656
|
+
throw new errors_1.CaveatDoesNotExistError(origin, target, caveatType);
|
|
657
|
+
}
|
|
658
|
+
if (permission.caveats.length === 1) {
|
|
659
|
+
permission.caveats = null;
|
|
660
|
+
}
|
|
661
|
+
else {
|
|
662
|
+
permission.caveats.splice(caveatIndex, 1);
|
|
663
|
+
}
|
|
664
|
+
this.validateModifiedPermission(permission, origin, target);
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Validates the specified modified permission. Should **always** be invoked
|
|
668
|
+
* on a permission after its caveats have been modified.
|
|
669
|
+
*
|
|
670
|
+
* Just like {@link PermissionController.validatePermission}, except that the
|
|
671
|
+
* corresponding target key and specification are retrieved first, and an
|
|
672
|
+
* error is thrown if the target key does not exist.
|
|
673
|
+
*
|
|
674
|
+
* @param permission - The modified permission to validate.
|
|
675
|
+
* @param origin - The origin associated with the permission.
|
|
676
|
+
* @param targetName - The target name name of the permission.
|
|
677
|
+
*/
|
|
678
|
+
validateModifiedPermission(permission, origin, targetName) {
|
|
679
|
+
const targetKey = this.getTargetKey(permission.parentCapability);
|
|
680
|
+
/* istanbul ignore if: this should be impossible */
|
|
681
|
+
if (!targetKey) {
|
|
682
|
+
throw new Error(`Fatal: Existing permission target key "${targetKey}" has no specification.`);
|
|
683
|
+
}
|
|
684
|
+
this.validatePermission(this.getPermissionSpecification(targetKey), permission, origin, targetName);
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Gets the key for the specified permission target.
|
|
688
|
+
*
|
|
689
|
+
* Used to support our namespaced permission target feature, which is used
|
|
690
|
+
* to implement namespaced restricted JSON-RPC methods.
|
|
691
|
+
*
|
|
692
|
+
* @param target - The requested permission target.
|
|
693
|
+
* @returns The internal key of the permission target.
|
|
694
|
+
*/
|
|
695
|
+
getTargetKey(target) {
|
|
696
|
+
if ((0, controller_utils_1.hasProperty)(this._permissionSpecifications, target)) {
|
|
697
|
+
return target;
|
|
698
|
+
}
|
|
699
|
+
const namespacedTargetsWithoutWildcard = {};
|
|
700
|
+
for (const targetKey of Object.keys(this._permissionSpecifications)) {
|
|
701
|
+
const wildCardMatch = targetKey.match(/(.+)\*$/u);
|
|
702
|
+
if (wildCardMatch) {
|
|
703
|
+
namespacedTargetsWithoutWildcard[wildCardMatch[1]] = true;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
// Check for potentially nested namespaces:
|
|
707
|
+
// Ex: wildzone_
|
|
708
|
+
// Ex: eth_plugin_
|
|
709
|
+
const segments = target.split('_');
|
|
710
|
+
let targetKey = '';
|
|
711
|
+
while (segments.length > 0 &&
|
|
712
|
+
!(0, controller_utils_1.hasProperty)(this._permissionSpecifications, targetKey) &&
|
|
713
|
+
!namespacedTargetsWithoutWildcard[targetKey]) {
|
|
714
|
+
targetKey += `${segments.shift()}_`;
|
|
715
|
+
}
|
|
716
|
+
if (namespacedTargetsWithoutWildcard[targetKey]) {
|
|
717
|
+
return `${targetKey}*`;
|
|
718
|
+
}
|
|
719
|
+
return undefined;
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* Grants _approved_ permissions to the specified subject. Every permission and
|
|
723
|
+
* caveat is stringently validated – including by calling every specification
|
|
724
|
+
* validator – and an error is thrown if any validation fails.
|
|
725
|
+
*
|
|
726
|
+
* ATTN: This method does **not** prompt the user for approval.
|
|
727
|
+
*
|
|
728
|
+
* @see {@link PermissionController.requestPermissions} For initiating a
|
|
729
|
+
* permissions request requiring user approval.
|
|
730
|
+
* @param options - Options bag.
|
|
731
|
+
* @param options.approvedPermissions - The requested permissions approved by
|
|
732
|
+
* the user.
|
|
733
|
+
* @param options.requestData - Permission request data. Passed to permission
|
|
734
|
+
* factory functions.
|
|
735
|
+
* @param options.preserveExistingPermissions - Whether to preserve the
|
|
736
|
+
* subject's existing permissions.
|
|
737
|
+
* @param options.subject - The subject to grant permissions to.
|
|
738
|
+
* @returns The granted permissions.
|
|
739
|
+
*/
|
|
740
|
+
grantPermissions({ approvedPermissions, requestData, preserveExistingPermissions = true, subject, }) {
|
|
741
|
+
const { origin } = subject;
|
|
742
|
+
if (!origin || typeof origin !== 'string') {
|
|
743
|
+
throw new errors_1.InvalidSubjectIdentifierError(origin);
|
|
744
|
+
}
|
|
745
|
+
const permissions = (preserveExistingPermissions
|
|
746
|
+
? Object.assign({}, this.getPermissions(origin)) : {});
|
|
747
|
+
for (const [requestedTarget, approvedPermission] of Object.entries(approvedPermissions)) {
|
|
748
|
+
const targetKey = this.getTargetKey(requestedTarget);
|
|
749
|
+
if (!targetKey) {
|
|
750
|
+
throw (0, errors_1.methodNotFound)(requestedTarget);
|
|
751
|
+
}
|
|
752
|
+
if (approvedPermission.parentCapability !== undefined &&
|
|
753
|
+
requestedTarget !== approvedPermission.parentCapability) {
|
|
754
|
+
throw new errors_1.InvalidApprovedPermissionError(origin, requestedTarget, approvedPermission);
|
|
755
|
+
}
|
|
756
|
+
// The requested target must be a valid target name if we found its key.
|
|
757
|
+
// We reassign it to change its type.
|
|
758
|
+
const targetName = requestedTarget;
|
|
759
|
+
const specification = this.getPermissionSpecification(targetKey);
|
|
760
|
+
// The requested caveats are validated here.
|
|
761
|
+
const caveats = this.constructCaveats(origin, targetName, approvedPermission.caveats);
|
|
762
|
+
const permissionOptions = {
|
|
763
|
+
caveats,
|
|
764
|
+
invoker: origin,
|
|
765
|
+
target: targetName,
|
|
766
|
+
};
|
|
767
|
+
let permission;
|
|
768
|
+
if (specification.factory) {
|
|
769
|
+
permission = specification.factory(permissionOptions, requestData);
|
|
770
|
+
// Full caveat and permission validation is performed here since the
|
|
771
|
+
// factory function can arbitrarily modify the entire permission object,
|
|
772
|
+
// including its caveats.
|
|
773
|
+
this.validatePermission(specification, permission, origin, targetName);
|
|
774
|
+
}
|
|
775
|
+
else {
|
|
776
|
+
permission = (0, Permission_1.constructPermission)(permissionOptions);
|
|
777
|
+
// We do not need to validate caveats in this case, because the plain
|
|
778
|
+
// permission constructor function does not modify the caveats, which
|
|
779
|
+
// were already validated by `constructCaveats` above.
|
|
780
|
+
this.validatePermission(specification, permission, origin, targetName, {
|
|
781
|
+
invokePermissionValidator: true,
|
|
782
|
+
performCaveatValidation: false,
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
permissions[targetName] = permission;
|
|
786
|
+
}
|
|
787
|
+
this.setValidatedPermissions(origin, permissions);
|
|
788
|
+
return permissions;
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Validates the specified permission by:
|
|
792
|
+
* - Ensuring that its `caveats` property is either `null` or a non-empty array.
|
|
793
|
+
* - Ensuring that it only includes caveats allowed by its specification.
|
|
794
|
+
* - Ensuring that it includes no duplicate caveats (by caveat type).
|
|
795
|
+
* - Validating each caveat object, if `performCaveatValidation` is `true`.
|
|
796
|
+
* - Calling the validator of its specification, if one exists and `invokePermissionValidator` is `true`.
|
|
797
|
+
*
|
|
798
|
+
* An error is thrown if validation fails.
|
|
799
|
+
*
|
|
800
|
+
* @param specification - The specification of the permission.
|
|
801
|
+
* @param permission - The permission to validate.
|
|
802
|
+
* @param origin - The origin associated with the permission.
|
|
803
|
+
* @param targetName - The target name of the permission.
|
|
804
|
+
* @param validationOptions - Validation options.
|
|
805
|
+
* @param validationOptions.invokePermissionValidator - Whether to invoke the
|
|
806
|
+
* permission's consumer-specified validator function, if any.
|
|
807
|
+
* @param validationOptions.performCaveatValidation - Whether to invoke
|
|
808
|
+
* {@link PermissionController.validateCaveat} on each of the permission's
|
|
809
|
+
* caveats.
|
|
810
|
+
*/
|
|
811
|
+
validatePermission(specification, permission, origin, targetName, { invokePermissionValidator, performCaveatValidation } = {
|
|
812
|
+
invokePermissionValidator: true,
|
|
813
|
+
performCaveatValidation: true,
|
|
814
|
+
}) {
|
|
815
|
+
const { allowedCaveats, validator } = specification;
|
|
816
|
+
if ((0, controller_utils_1.hasProperty)(permission, 'caveats')) {
|
|
817
|
+
const { caveats } = permission;
|
|
818
|
+
if (caveats !== null && !(Array.isArray(caveats) && caveats.length > 0)) {
|
|
819
|
+
throw new errors_1.InvalidCaveatsPropertyError(origin, targetName, caveats);
|
|
820
|
+
}
|
|
821
|
+
const seenCaveatTypes = new Set();
|
|
822
|
+
caveats === null || caveats === void 0 ? void 0 : caveats.forEach((caveat) => {
|
|
823
|
+
if (performCaveatValidation) {
|
|
824
|
+
this.validateCaveat(caveat, origin, targetName);
|
|
825
|
+
}
|
|
826
|
+
if (!(allowedCaveats === null || allowedCaveats === void 0 ? void 0 : allowedCaveats.includes(caveat.type))) {
|
|
827
|
+
throw new errors_1.ForbiddenCaveatError(caveat.type, origin, targetName);
|
|
828
|
+
}
|
|
829
|
+
if (seenCaveatTypes.has(caveat.type)) {
|
|
830
|
+
throw new errors_1.DuplicateCaveatError(caveat.type, origin, targetName);
|
|
831
|
+
}
|
|
832
|
+
seenCaveatTypes.add(caveat.type);
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
if (invokePermissionValidator && validator) {
|
|
836
|
+
validator(permission, origin, targetName);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
/**
|
|
840
|
+
* Assigns the specified permissions to the subject with the given origin.
|
|
841
|
+
* Overwrites all existing permissions, and creates a subject entry if it
|
|
842
|
+
* doesn't already exist.
|
|
843
|
+
*
|
|
844
|
+
* ATTN: Assumes that the new permissions have been validated.
|
|
845
|
+
*
|
|
846
|
+
* @param origin - The origin of the grantee subject.
|
|
847
|
+
* @param permissions - The new permissions for the grantee subject.
|
|
848
|
+
*/
|
|
849
|
+
setValidatedPermissions(origin, permissions) {
|
|
850
|
+
this.update((draftState) => {
|
|
851
|
+
if (!draftState.subjects[origin]) {
|
|
852
|
+
draftState.subjects[origin] = { origin, permissions: {} };
|
|
853
|
+
}
|
|
854
|
+
draftState.subjects[origin].permissions = (0, immer_1.castDraft)(permissions);
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* Validates the requested caveats for the permission of the specified
|
|
859
|
+
* subject origin and target name and returns the validated caveat array.
|
|
860
|
+
*
|
|
861
|
+
* Throws an error if validation fails.
|
|
862
|
+
*
|
|
863
|
+
* @param origin - The origin of the permission subject.
|
|
864
|
+
* @param target - The permission target name.
|
|
865
|
+
* @param requestedCaveats - The requested caveats to construct.
|
|
866
|
+
* @returns The constructed caveats.
|
|
867
|
+
*/
|
|
868
|
+
constructCaveats(origin, target, requestedCaveats) {
|
|
869
|
+
const caveatArray = requestedCaveats === null || requestedCaveats === void 0 ? void 0 : requestedCaveats.map((requestedCaveat) => {
|
|
870
|
+
this.validateCaveat(requestedCaveat, origin, target);
|
|
871
|
+
// Reassign so that we have a fresh object.
|
|
872
|
+
const { type, value } = requestedCaveat;
|
|
873
|
+
return { type, value };
|
|
874
|
+
});
|
|
875
|
+
return caveatArray && (0, controller_utils_1.isNonEmptyArray)(caveatArray)
|
|
876
|
+
? caveatArray
|
|
877
|
+
: undefined;
|
|
878
|
+
}
|
|
879
|
+
/**
|
|
880
|
+
* This methods validates that the specified caveat is an object with the
|
|
881
|
+
* expected properties and types. It also ensures that a caveat specification
|
|
882
|
+
* exists for the requested caveat type, and calls the specification
|
|
883
|
+
* validator, if it exists, on the caveat object.
|
|
884
|
+
*
|
|
885
|
+
* Throws an error if validation fails.
|
|
886
|
+
*
|
|
887
|
+
* @param caveat - The caveat object to validate.
|
|
888
|
+
* @param origin - The origin associated with the subject of the parent
|
|
889
|
+
* permission.
|
|
890
|
+
* @param target - The target name associated with the parent permission.
|
|
891
|
+
*/
|
|
892
|
+
validateCaveat(caveat, origin, target) {
|
|
893
|
+
var _a;
|
|
894
|
+
if (!(0, controller_utils_1.isPlainObject)(caveat)) {
|
|
895
|
+
throw new errors_1.InvalidCaveatError(caveat, origin, target);
|
|
896
|
+
}
|
|
897
|
+
if (Object.keys(caveat).length !== 2) {
|
|
898
|
+
throw new errors_1.InvalidCaveatFieldsError(caveat, origin, target);
|
|
899
|
+
}
|
|
900
|
+
if (typeof caveat.type !== 'string') {
|
|
901
|
+
throw new errors_1.InvalidCaveatTypeError(caveat, origin, target);
|
|
902
|
+
}
|
|
903
|
+
const specification = this.getCaveatSpecification(caveat.type);
|
|
904
|
+
if (!specification) {
|
|
905
|
+
throw new errors_1.UnrecognizedCaveatTypeError(caveat.type, origin, target);
|
|
906
|
+
}
|
|
907
|
+
if (!(0, controller_utils_1.hasProperty)(caveat, 'value') || caveat.value === undefined) {
|
|
908
|
+
throw new errors_1.CaveatMissingValueError(caveat, origin, target);
|
|
909
|
+
}
|
|
910
|
+
if (!(0, controller_utils_1.isValidJson)(caveat.value)) {
|
|
911
|
+
throw new errors_1.CaveatInvalidJsonError(caveat, origin, target);
|
|
912
|
+
}
|
|
913
|
+
// Typecast: TypeScript still believes that the caveat is a PlainObject.
|
|
914
|
+
(_a = specification.validator) === null || _a === void 0 ? void 0 : _a.call(specification, caveat, origin, target);
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* Initiates a permission request that requires user approval. This should
|
|
918
|
+
* always be used to grant additional permissions to a subject, unless user
|
|
919
|
+
* approval has been obtained through some other means.
|
|
920
|
+
*
|
|
921
|
+
* Permissions are validated at every step of the approval process, and this
|
|
922
|
+
* method will reject if validation fails.
|
|
923
|
+
*
|
|
924
|
+
* @see {@link ApprovalController} For the user approval logic.
|
|
925
|
+
* @see {@link PermissionController.acceptPermissionsRequest} For the method
|
|
926
|
+
* that _accepts_ the request and resolves the user approval promise.
|
|
927
|
+
* @see {@link PermissionController.rejectPermissionsRequest} For the method
|
|
928
|
+
* that _rejects_ the request and the user approval promise.
|
|
929
|
+
* @param subject - The grantee subject.
|
|
930
|
+
* @param requestedPermissions - The requested permissions.
|
|
931
|
+
* @param options - Additional options.
|
|
932
|
+
* @param options.id - The id of the permissions request. Defaults to a unique
|
|
933
|
+
* id.
|
|
934
|
+
* @param options.preserveExistingPermissions - Whether to preserve the
|
|
935
|
+
* subject's existing permissions. Defaults to `true`.
|
|
936
|
+
* @returns The granted permissions and request metadata.
|
|
937
|
+
*/
|
|
938
|
+
requestPermissions(subject, requestedPermissions, options = {}) {
|
|
939
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
940
|
+
const { origin } = subject;
|
|
941
|
+
const { id = (0, nanoid_1.nanoid)(), preserveExistingPermissions = true } = options;
|
|
942
|
+
this.validateRequestedPermissions(origin, requestedPermissions);
|
|
943
|
+
const metadata = {
|
|
944
|
+
id,
|
|
945
|
+
origin,
|
|
946
|
+
};
|
|
947
|
+
const permissionsRequest = {
|
|
948
|
+
metadata,
|
|
949
|
+
permissions: requestedPermissions,
|
|
950
|
+
};
|
|
951
|
+
const _a = yield this.requestUserApproval(permissionsRequest), { permissions: approvedPermissions } = _a, requestData = __rest(_a, ["permissions"]);
|
|
952
|
+
return [
|
|
953
|
+
this.grantPermissions({
|
|
954
|
+
subject,
|
|
955
|
+
approvedPermissions,
|
|
956
|
+
preserveExistingPermissions,
|
|
957
|
+
requestData,
|
|
958
|
+
}),
|
|
959
|
+
metadata,
|
|
960
|
+
];
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* Validates requested permissions. Throws if validation fails.
|
|
965
|
+
*
|
|
966
|
+
* This method ensures that the requested permissions are a properly
|
|
967
|
+
* formatted {@link RequestedPermissions} object, and performs the same
|
|
968
|
+
* validation as {@link PermissionController.grantPermissions}, except that
|
|
969
|
+
* consumer-specified permission validator functions are not called, since
|
|
970
|
+
* they are only called on fully constructed, approved permissions that are
|
|
971
|
+
* otherwise completely valid.
|
|
972
|
+
*
|
|
973
|
+
* Unrecognzied properties on requested permissions are ignored.
|
|
974
|
+
*
|
|
975
|
+
* @param origin - The origin of the grantee subject.
|
|
976
|
+
* @param requestedPermissions - The requested permissions.
|
|
977
|
+
*/
|
|
978
|
+
validateRequestedPermissions(origin, requestedPermissions) {
|
|
979
|
+
if (!(0, controller_utils_1.isPlainObject)(requestedPermissions)) {
|
|
980
|
+
throw (0, errors_1.invalidParams)({
|
|
981
|
+
message: `Requested permissions for origin "${origin}" is not a plain object.`,
|
|
982
|
+
data: { origin, requestedPermissions },
|
|
983
|
+
});
|
|
984
|
+
}
|
|
985
|
+
if (Object.keys(requestedPermissions).length === 0) {
|
|
986
|
+
throw (0, errors_1.invalidParams)({
|
|
987
|
+
message: `Permissions request for origin "${origin}" contains no permissions.`,
|
|
988
|
+
data: { requestedPermissions },
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
for (const targetName of Object.keys(requestedPermissions)) {
|
|
992
|
+
const permission = requestedPermissions[targetName];
|
|
993
|
+
const targetKey = this.getTargetKey(targetName);
|
|
994
|
+
if (!targetKey) {
|
|
995
|
+
throw (0, errors_1.methodNotFound)(targetName, { origin, requestedPermissions });
|
|
996
|
+
}
|
|
997
|
+
if (!(0, controller_utils_1.isPlainObject)(permission) ||
|
|
998
|
+
(permission.parentCapability !== undefined &&
|
|
999
|
+
targetName !== permission.parentCapability)) {
|
|
1000
|
+
throw (0, errors_1.invalidParams)({
|
|
1001
|
+
message: `Permissions request for origin "${origin}" contains invalid requested permission(s).`,
|
|
1002
|
+
data: { origin, requestedPermissions },
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
// Here we validate the permission without invoking its validator, if any.
|
|
1006
|
+
// The validator will be invoked after the permission has been approved.
|
|
1007
|
+
this.validatePermission(this.getPermissionSpecification(targetKey),
|
|
1008
|
+
// Typecast: The permission is still a "PlainObject" here.
|
|
1009
|
+
permission, origin, targetName, { invokePermissionValidator: false, performCaveatValidation: true });
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Adds a request to the {@link ApprovalController} using the
|
|
1014
|
+
* {@link AddApprovalRequest} action. Also validates the resulting approved
|
|
1015
|
+
* permissions request, and throws an error if validation fails.
|
|
1016
|
+
*
|
|
1017
|
+
* @param permissionsRequest - The permissions request object.
|
|
1018
|
+
* @returns The approved permissions request object.
|
|
1019
|
+
*/
|
|
1020
|
+
requestUserApproval(permissionsRequest) {
|
|
1021
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1022
|
+
const { origin, id } = permissionsRequest.metadata;
|
|
1023
|
+
const approvedRequest = yield this.messagingSystem.call('ApprovalController:addRequest', {
|
|
1024
|
+
id,
|
|
1025
|
+
origin,
|
|
1026
|
+
requestData: permissionsRequest,
|
|
1027
|
+
type: utils_1.MethodNames.requestPermissions,
|
|
1028
|
+
}, true);
|
|
1029
|
+
this.validateApprovedPermissions(approvedRequest, { id, origin });
|
|
1030
|
+
return approvedRequest;
|
|
1031
|
+
});
|
|
1032
|
+
}
|
|
1033
|
+
/**
|
|
1034
|
+
* Validates an approved {@link PermissionsRequest} object. The approved
|
|
1035
|
+
* request must have the required `metadata` and `permissions` properties,
|
|
1036
|
+
* the `id` and `origin` of the `metadata` must match the original request
|
|
1037
|
+
* metadata, and the requested permissions must be valid per
|
|
1038
|
+
* {@link PermissionController.validateRequestedPermissions}. Any extra
|
|
1039
|
+
* metadata properties are ignored.
|
|
1040
|
+
*
|
|
1041
|
+
* An error is thrown if validation fails.
|
|
1042
|
+
*
|
|
1043
|
+
* @param approvedRequest - The approved permissions request object.
|
|
1044
|
+
* @param originalMetadata - The original request metadata.
|
|
1045
|
+
*/
|
|
1046
|
+
validateApprovedPermissions(approvedRequest, originalMetadata) {
|
|
1047
|
+
const { id, origin } = originalMetadata;
|
|
1048
|
+
if (!(0, controller_utils_1.isPlainObject)(approvedRequest) ||
|
|
1049
|
+
!(0, controller_utils_1.isPlainObject)(approvedRequest.metadata)) {
|
|
1050
|
+
throw (0, errors_1.internalError)(`Approved permissions request for subject "${origin}" is invalid.`, { data: { approvedRequest } });
|
|
1051
|
+
}
|
|
1052
|
+
const { metadata: { id: newId, origin: newOrigin }, permissions, } = approvedRequest;
|
|
1053
|
+
if (newId !== id) {
|
|
1054
|
+
throw (0, errors_1.internalError)(`Approved permissions request for subject "${origin}" mutated its id.`, { originalId: id, mutatedId: newId });
|
|
1055
|
+
}
|
|
1056
|
+
if (newOrigin !== origin) {
|
|
1057
|
+
throw (0, errors_1.internalError)(`Approved permissions request for subject "${origin}" mutated its origin.`, { originalOrigin: origin, mutatedOrigin: newOrigin });
|
|
1058
|
+
}
|
|
1059
|
+
try {
|
|
1060
|
+
this.validateRequestedPermissions(origin, permissions);
|
|
1061
|
+
}
|
|
1062
|
+
catch (error) {
|
|
1063
|
+
if (error instanceof eth_rpc_errors_1.EthereumRpcError) {
|
|
1064
|
+
// Re-throw as an internal error; we should never receive invalid approved
|
|
1065
|
+
// permissions.
|
|
1066
|
+
throw (0, errors_1.internalError)(`Invalid approved permissions request: ${error.message}`, error.data);
|
|
1067
|
+
}
|
|
1068
|
+
throw (0, errors_1.internalError)('Unrecognized error type', { error });
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
/**
|
|
1072
|
+
* Accepts a permissions request created by
|
|
1073
|
+
* {@link PermissionController.requestPermissions}.
|
|
1074
|
+
*
|
|
1075
|
+
* @param request - The permissions request.
|
|
1076
|
+
*/
|
|
1077
|
+
acceptPermissionsRequest(request) {
|
|
1078
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1079
|
+
const { id } = request.metadata;
|
|
1080
|
+
if (!this.hasApprovalRequest({ id })) {
|
|
1081
|
+
throw new errors_1.PermissionsRequestNotFoundError(id);
|
|
1082
|
+
}
|
|
1083
|
+
if (Object.keys(request.permissions).length === 0) {
|
|
1084
|
+
this._rejectPermissionsRequest(id, (0, errors_1.invalidParams)({
|
|
1085
|
+
message: 'Must request at least one permission.',
|
|
1086
|
+
}));
|
|
1087
|
+
return;
|
|
1088
|
+
}
|
|
1089
|
+
try {
|
|
1090
|
+
this.messagingSystem.call('ApprovalController:acceptRequest', id, request);
|
|
1091
|
+
}
|
|
1092
|
+
catch (error) {
|
|
1093
|
+
// If accepting unexpectedly fails, reject the request and re-throw the
|
|
1094
|
+
// error
|
|
1095
|
+
this._rejectPermissionsRequest(id, error);
|
|
1096
|
+
throw error;
|
|
1097
|
+
}
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1100
|
+
/**
|
|
1101
|
+
* Rejects a permissions request created by
|
|
1102
|
+
* {@link PermissionController.requestPermissions}.
|
|
1103
|
+
*
|
|
1104
|
+
* @param id - The id of the request to be rejected.
|
|
1105
|
+
*/
|
|
1106
|
+
rejectPermissionsRequest(id) {
|
|
1107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1108
|
+
if (!this.hasApprovalRequest({ id })) {
|
|
1109
|
+
throw new errors_1.PermissionsRequestNotFoundError(id);
|
|
1110
|
+
}
|
|
1111
|
+
this._rejectPermissionsRequest(id, (0, errors_1.userRejectedRequest)());
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
/**
|
|
1115
|
+
* Checks whether the {@link ApprovalController} has a particular permissions
|
|
1116
|
+
* request.
|
|
1117
|
+
*
|
|
1118
|
+
* @see {@link PermissionController.acceptPermissionsRequest} and
|
|
1119
|
+
* {@link PermissionController.rejectPermissionsRequest} for usage.
|
|
1120
|
+
* @param options - The {@link HasApprovalRequest} options.
|
|
1121
|
+
* @param options.id - The id of the approval request to check for.
|
|
1122
|
+
* @returns Whether the specified request exists.
|
|
1123
|
+
*/
|
|
1124
|
+
hasApprovalRequest(options) {
|
|
1125
|
+
return this.messagingSystem.call('ApprovalController:hasRequest',
|
|
1126
|
+
// Typecast: For some reason, the type here expects all of the possible
|
|
1127
|
+
// HasApprovalRequest options to be specified, when they're actually all
|
|
1128
|
+
// optional. Passing just the id is definitely valid, so we just cast it.
|
|
1129
|
+
options);
|
|
1130
|
+
}
|
|
1131
|
+
/**
|
|
1132
|
+
* Rejects the permissions request with the specified id, with the specified
|
|
1133
|
+
* error as the reason. This method is effectively a wrapper around a
|
|
1134
|
+
* messenger call for the `ApprovalController:rejectRequest` action.
|
|
1135
|
+
*
|
|
1136
|
+
* @see {@link PermissionController.acceptPermissionsRequest} and
|
|
1137
|
+
* {@link PermissionController.rejectPermissionsRequest} for usage.
|
|
1138
|
+
* @param id - The id of the request to reject.
|
|
1139
|
+
* @param error - The error associated with the rejection.
|
|
1140
|
+
* @returns Nothing
|
|
1141
|
+
*/
|
|
1142
|
+
_rejectPermissionsRequest(id, error) {
|
|
1143
|
+
return this.messagingSystem.call('ApprovalController:rejectRequest', id, error);
|
|
1144
|
+
}
|
|
1145
|
+
/**
|
|
1146
|
+
* Gets the subject's endowments per the specified endowment permission.
|
|
1147
|
+
* Throws if the subject does not have the required permission or if the
|
|
1148
|
+
* permission is not an endowment permission.
|
|
1149
|
+
*
|
|
1150
|
+
* @param origin - The origin of the subject whose endowments to retrieve.
|
|
1151
|
+
* @param targetName - The name of the endowment permission. This must be a
|
|
1152
|
+
* valid permission target name.
|
|
1153
|
+
* @param requestData - Additional data associated with the request, if any.
|
|
1154
|
+
* Forwarded to the endowment getter function for the permission.
|
|
1155
|
+
* @returns The endowments, if any.
|
|
1156
|
+
*/
|
|
1157
|
+
getEndowments(origin, targetName, requestData) {
|
|
1158
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1159
|
+
if (!this.hasPermission(origin, targetName)) {
|
|
1160
|
+
throw (0, errors_1.unauthorized)({ data: { origin, targetName } });
|
|
1161
|
+
}
|
|
1162
|
+
return this.getTypedPermissionSpecification(Permission_1.PermissionType.Endowment, targetName, origin).endowmentGetter({ origin, requestData });
|
|
1163
|
+
});
|
|
1164
|
+
}
|
|
1165
|
+
/**
|
|
1166
|
+
* Executes a restricted method as the subject with the given origin.
|
|
1167
|
+
* The specified params, if any, will be passed to the method implementation.
|
|
1168
|
+
*
|
|
1169
|
+
* ATTN: Great caution should be exercised in the use of this method.
|
|
1170
|
+
* Methods that cause side effects or affect application state should
|
|
1171
|
+
* be avoided.
|
|
1172
|
+
*
|
|
1173
|
+
* This method will first attempt to retrieve the requested restricted method
|
|
1174
|
+
* implementation, throwing if it does not exist. The method will then be
|
|
1175
|
+
* invoked as though the subject with the specified origin had invoked it with
|
|
1176
|
+
* the specified parameters. This means that any existing caveats will be
|
|
1177
|
+
* applied to the restricted method, and this method will throw if the
|
|
1178
|
+
* restricted method or its caveat decorators throw.
|
|
1179
|
+
*
|
|
1180
|
+
* In addition, this method will throw if the subject does not have a
|
|
1181
|
+
* permission for the specified restricted method.
|
|
1182
|
+
*
|
|
1183
|
+
* @param origin - The origin of the subject to execute the method on behalf
|
|
1184
|
+
* of.
|
|
1185
|
+
* @param targetName - The name of the method to execute. This must be a valid
|
|
1186
|
+
* permission target name.
|
|
1187
|
+
* @param params - The parameters to pass to the method implementation.
|
|
1188
|
+
* @returns The result of the executed method.
|
|
1189
|
+
*/
|
|
1190
|
+
executeRestrictedMethod(origin, targetName, params) {
|
|
1191
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1192
|
+
// Throws if the method does not exist
|
|
1193
|
+
const methodImplementation = this.getRestrictedMethod(targetName, origin);
|
|
1194
|
+
const result = yield this._executeRestrictedMethod(methodImplementation, { origin }, targetName, params);
|
|
1195
|
+
if (result === undefined) {
|
|
1196
|
+
throw new Error(`Internal request for method "${targetName}" as origin "${origin}" returned no result.`);
|
|
1197
|
+
}
|
|
1198
|
+
return result;
|
|
1199
|
+
});
|
|
1200
|
+
}
|
|
1201
|
+
/**
|
|
1202
|
+
* An internal method used in the controller's `json-rpc-engine` middleware
|
|
1203
|
+
* and {@link PermissionController.executeRestrictedMethod}. Calls the
|
|
1204
|
+
* specified restricted method implementation after decorating it with the
|
|
1205
|
+
* caveats of its permission. Throws if the subject does not have the
|
|
1206
|
+
* requisite permission.
|
|
1207
|
+
*
|
|
1208
|
+
* ATTN: Parameter validation is the responsibility of the caller, or
|
|
1209
|
+
* the restricted method implementation in the case of `params`.
|
|
1210
|
+
*
|
|
1211
|
+
* @see {@link PermissionController.executeRestrictedMethod} and
|
|
1212
|
+
* {@link PermissionController.createPermissionMiddleware} for usage.
|
|
1213
|
+
* @param methodImplementation - The implementation of the method to call.
|
|
1214
|
+
* @param subject - Metadata about the subject that made the request.
|
|
1215
|
+
* @param method - The method name
|
|
1216
|
+
* @param params - Params needed for executing the restricted method
|
|
1217
|
+
* @returns The result of the restricted method implementation
|
|
1218
|
+
*/
|
|
1219
|
+
_executeRestrictedMethod(methodImplementation, subject, method, params = []) {
|
|
1220
|
+
const { origin } = subject;
|
|
1221
|
+
const permission = this.getPermission(origin, method);
|
|
1222
|
+
if (!permission) {
|
|
1223
|
+
throw (0, errors_1.unauthorized)({ data: { origin, method } });
|
|
1224
|
+
}
|
|
1225
|
+
return (0, Caveat_1.decorateWithCaveats)(methodImplementation, permission, this._caveatSpecifications)({ method, params, context: { origin } });
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
exports.PermissionController = PermissionController;
|
|
1229
|
+
//# sourceMappingURL=PermissionController.js.map
|