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