@metamask/permission-controller 11.0.0 → 11.0.2

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.
Files changed (185) hide show
  1. package/CHANGELOG.md +29 -1
  2. package/dist/Caveat.cjs +48 -0
  3. package/dist/Caveat.cjs.map +1 -0
  4. package/dist/{types/Caveat.d.ts → Caveat.d.cts} +3 -3
  5. package/dist/Caveat.d.cts.map +1 -0
  6. package/dist/Caveat.d.mts +210 -0
  7. package/dist/Caveat.d.mts.map +1 -0
  8. package/dist/Caveat.mjs +42 -11
  9. package/dist/Caveat.mjs.map +1 -1
  10. package/dist/Permission.cjs +65 -0
  11. package/dist/Permission.cjs.map +1 -0
  12. package/dist/{types/Permission.d.ts → Permission.d.cts} +7 -7
  13. package/dist/Permission.d.cts.map +1 -0
  14. package/dist/Permission.d.mts +417 -0
  15. package/dist/Permission.d.mts.map +1 -0
  16. package/dist/Permission.mjs +58 -13
  17. package/dist/Permission.mjs.map +1 -1
  18. package/dist/PermissionController.cjs +1481 -0
  19. package/dist/{chunk-COGJLF22.mjs.map → PermissionController.cjs.map} +1 -1
  20. package/dist/{types/PermissionController.d.ts → PermissionController.d.cts} +10 -10
  21. package/dist/PermissionController.d.cts.map +1 -0
  22. package/dist/PermissionController.d.mts +986 -0
  23. package/dist/PermissionController.d.mts.map +1 -0
  24. package/dist/PermissionController.mjs +1479 -13
  25. package/dist/PermissionController.mjs.map +1 -1
  26. package/dist/SubjectMetadataController.cjs +152 -0
  27. package/dist/SubjectMetadataController.cjs.map +1 -0
  28. package/dist/{types/SubjectMetadataController.d.ts → SubjectMetadataController.d.cts} +5 -5
  29. package/dist/SubjectMetadataController.d.cts.map +1 -0
  30. package/dist/SubjectMetadataController.d.mts +107 -0
  31. package/dist/SubjectMetadataController.d.mts.map +1 -0
  32. package/dist/SubjectMetadataController.mjs +146 -8
  33. package/dist/SubjectMetadataController.mjs.map +1 -1
  34. package/dist/errors.cjs +217 -0
  35. package/dist/errors.cjs.map +1 -0
  36. package/dist/{types/errors.d.ts → errors.d.cts} +6 -6
  37. package/dist/errors.d.cts.map +1 -0
  38. package/dist/errors.d.mts +185 -0
  39. package/dist/errors.d.mts.map +1 -0
  40. package/dist/errors.mjs +187 -57
  41. package/dist/errors.mjs.map +1 -1
  42. package/dist/index.cjs +38 -0
  43. package/dist/index.cjs.map +1 -0
  44. package/dist/index.d.cts +9 -0
  45. package/dist/index.d.cts.map +1 -0
  46. package/dist/index.d.mts +9 -0
  47. package/dist/index.d.mts.map +1 -0
  48. package/dist/index.mjs +7 -97
  49. package/dist/index.mjs.map +1 -1
  50. package/dist/permission-middleware.cjs +55 -0
  51. package/dist/permission-middleware.cjs.map +1 -0
  52. package/dist/{types/permission-middleware.d.ts → permission-middleware.d.cts} +4 -4
  53. package/dist/permission-middleware.d.cts.map +1 -0
  54. package/dist/permission-middleware.d.mts +33 -0
  55. package/dist/permission-middleware.d.mts.map +1 -0
  56. package/dist/permission-middleware.mjs +50 -8
  57. package/dist/permission-middleware.mjs.map +1 -1
  58. package/dist/rpc-methods/getPermissions.cjs +27 -0
  59. package/dist/rpc-methods/getPermissions.cjs.map +1 -0
  60. package/dist/rpc-methods/getPermissions.d.cts +9 -0
  61. package/dist/rpc-methods/getPermissions.d.cts.map +1 -0
  62. package/dist/rpc-methods/getPermissions.d.mts +9 -0
  63. package/dist/rpc-methods/getPermissions.d.mts.map +1 -0
  64. package/dist/rpc-methods/getPermissions.mjs +22 -7
  65. package/dist/rpc-methods/getPermissions.mjs.map +1 -1
  66. package/dist/rpc-methods/index.cjs +12 -0
  67. package/dist/rpc-methods/index.cjs.map +1 -0
  68. package/dist/rpc-methods/index.d.cts +6 -0
  69. package/dist/rpc-methods/index.d.cts.map +1 -0
  70. package/dist/rpc-methods/index.d.mts +6 -0
  71. package/dist/rpc-methods/index.d.mts.map +1 -0
  72. package/dist/rpc-methods/index.mjs +8 -12
  73. package/dist/rpc-methods/index.mjs.map +1 -1
  74. package/dist/rpc-methods/requestPermissions.cjs +36 -0
  75. package/dist/rpc-methods/requestPermissions.cjs.map +1 -0
  76. package/dist/{types/rpc-methods/requestPermissions.d.ts → rpc-methods/requestPermissions.d.cts} +3 -3
  77. package/dist/rpc-methods/requestPermissions.d.cts.map +1 -0
  78. package/dist/rpc-methods/requestPermissions.d.mts +17 -0
  79. package/dist/rpc-methods/requestPermissions.d.mts.map +1 -0
  80. package/dist/rpc-methods/requestPermissions.mjs +31 -8
  81. package/dist/rpc-methods/requestPermissions.mjs.map +1 -1
  82. package/dist/rpc-methods/revokePermissions.cjs +41 -0
  83. package/dist/rpc-methods/revokePermissions.cjs.map +1 -0
  84. package/dist/{types/rpc-methods/revokePermissions.d.ts → rpc-methods/revokePermissions.d.cts} +4 -4
  85. package/dist/rpc-methods/revokePermissions.d.cts.map +1 -0
  86. package/dist/rpc-methods/revokePermissions.d.mts +11 -0
  87. package/dist/rpc-methods/revokePermissions.d.mts.map +1 -0
  88. package/dist/rpc-methods/revokePermissions.mjs +36 -8
  89. package/dist/rpc-methods/revokePermissions.mjs.map +1 -1
  90. package/dist/utils.cjs +44 -0
  91. package/dist/utils.cjs.map +1 -0
  92. package/dist/{types/utils.d.ts → utils.d.cts} +5 -5
  93. package/dist/utils.d.cts.map +1 -0
  94. package/dist/utils.d.mts +57 -0
  95. package/dist/utils.d.mts.map +1 -0
  96. package/dist/utils.mjs +39 -9
  97. package/dist/utils.mjs.map +1 -1
  98. package/package.json +18 -12
  99. package/dist/Caveat.js +0 -12
  100. package/dist/Caveat.js.map +0 -1
  101. package/dist/Permission.js +0 -14
  102. package/dist/Permission.js.map +0 -1
  103. package/dist/PermissionController.js +0 -15
  104. package/dist/PermissionController.js.map +0 -1
  105. package/dist/SubjectMetadataController.js +0 -10
  106. package/dist/SubjectMetadataController.js.map +0 -1
  107. package/dist/chunk-2L4QPE5A.mjs +0 -25
  108. package/dist/chunk-2L4QPE5A.mjs.map +0 -1
  109. package/dist/chunk-3WWJKO7P.mjs +0 -37
  110. package/dist/chunk-3WWJKO7P.mjs.map +0 -1
  111. package/dist/chunk-3YOPLPVY.js +0 -37
  112. package/dist/chunk-3YOPLPVY.js.map +0 -1
  113. package/dist/chunk-42QSJHWO.mjs +0 -155
  114. package/dist/chunk-42QSJHWO.mjs.map +0 -1
  115. package/dist/chunk-4FMYQC3Y.mjs +0 -29
  116. package/dist/chunk-4FMYQC3Y.mjs.map +0 -1
  117. package/dist/chunk-4NAVRO44.mjs +0 -35
  118. package/dist/chunk-4NAVRO44.mjs.map +0 -1
  119. package/dist/chunk-5RFW5THA.mjs +0 -17
  120. package/dist/chunk-5RFW5THA.mjs.map +0 -1
  121. package/dist/chunk-6PXDVUYM.js +0 -29
  122. package/dist/chunk-6PXDVUYM.js.map +0 -1
  123. package/dist/chunk-74H4CVH7.mjs +0 -34
  124. package/dist/chunk-74H4CVH7.mjs.map +0 -1
  125. package/dist/chunk-7A3VYLCK.js +0 -30
  126. package/dist/chunk-7A3VYLCK.js.map +0 -1
  127. package/dist/chunk-B6PDRQ7N.js +0 -1679
  128. package/dist/chunk-B6PDRQ7N.js.map +0 -1
  129. package/dist/chunk-COGJLF22.mjs +0 -1679
  130. package/dist/chunk-F5TBMVWC.js +0 -46
  131. package/dist/chunk-F5TBMVWC.js.map +0 -1
  132. package/dist/chunk-FYADAA2G.js +0 -220
  133. package/dist/chunk-FYADAA2G.js.map +0 -1
  134. package/dist/chunk-G4BWJ7EA.mjs +0 -220
  135. package/dist/chunk-G4BWJ7EA.mjs.map +0 -1
  136. package/dist/chunk-HYMS7IGB.mjs +0 -31
  137. package/dist/chunk-HYMS7IGB.mjs.map +0 -1
  138. package/dist/chunk-I62TTXZ6.mjs +0 -46
  139. package/dist/chunk-I62TTXZ6.mjs.map +0 -1
  140. package/dist/chunk-OCLNDUYO.mjs +0 -30
  141. package/dist/chunk-OCLNDUYO.mjs.map +0 -1
  142. package/dist/chunk-VBIZGGQL.js +0 -17
  143. package/dist/chunk-VBIZGGQL.js.map +0 -1
  144. package/dist/chunk-VQPP5PWS.js +0 -35
  145. package/dist/chunk-VQPP5PWS.js.map +0 -1
  146. package/dist/chunk-VSDHL2GQ.js +0 -155
  147. package/dist/chunk-VSDHL2GQ.js.map +0 -1
  148. package/dist/chunk-X4ZAW4QR.js +0 -34
  149. package/dist/chunk-X4ZAW4QR.js.map +0 -1
  150. package/dist/chunk-Z2XKIXLS.js +0 -25
  151. package/dist/chunk-Z2XKIXLS.js.map +0 -1
  152. package/dist/chunk-ZH4MLSXX.js +0 -31
  153. package/dist/chunk-ZH4MLSXX.js.map +0 -1
  154. package/dist/errors.js +0 -58
  155. package/dist/errors.js.map +0 -1
  156. package/dist/index.js +0 -98
  157. package/dist/index.js.map +0 -1
  158. package/dist/permission-middleware.js +0 -9
  159. package/dist/permission-middleware.js.map +0 -1
  160. package/dist/rpc-methods/getPermissions.js +0 -9
  161. package/dist/rpc-methods/getPermissions.js.map +0 -1
  162. package/dist/rpc-methods/index.js +0 -13
  163. package/dist/rpc-methods/index.js.map +0 -1
  164. package/dist/rpc-methods/requestPermissions.js +0 -10
  165. package/dist/rpc-methods/requestPermissions.js.map +0 -1
  166. package/dist/rpc-methods/revokePermissions.js +0 -10
  167. package/dist/rpc-methods/revokePermissions.js.map +0 -1
  168. package/dist/tsconfig.build.tsbuildinfo +0 -1
  169. package/dist/types/Caveat.d.ts.map +0 -1
  170. package/dist/types/Permission.d.ts.map +0 -1
  171. package/dist/types/PermissionController.d.ts.map +0 -1
  172. package/dist/types/SubjectMetadataController.d.ts.map +0 -1
  173. package/dist/types/errors.d.ts.map +0 -1
  174. package/dist/types/index.d.ts +0 -9
  175. package/dist/types/index.d.ts.map +0 -1
  176. package/dist/types/permission-middleware.d.ts.map +0 -1
  177. package/dist/types/rpc-methods/getPermissions.d.ts +0 -9
  178. package/dist/types/rpc-methods/getPermissions.d.ts.map +0 -1
  179. package/dist/types/rpc-methods/index.d.ts +0 -6
  180. package/dist/types/rpc-methods/index.d.ts.map +0 -1
  181. package/dist/types/rpc-methods/requestPermissions.d.ts.map +0 -1
  182. package/dist/types/rpc-methods/revokePermissions.d.ts.map +0 -1
  183. package/dist/types/utils.d.ts.map +0 -1
  184. package/dist/utils.js +0 -10
  185. package/dist/utils.js.map +0 -1
@@ -0,0 +1,986 @@
1
+ import type { AcceptRequest as AcceptApprovalRequest, AddApprovalRequest, HasApprovalRequest, RejectRequest as RejectApprovalRequest } from "@metamask/approval-controller";
2
+ import type { RestrictedControllerMessenger, ActionConstraint, EventConstraint, ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
3
+ import { BaseController } from "@metamask/base-controller";
4
+ import type { NonEmptyArray } from "@metamask/controller-utils";
5
+ import type { Json } from "@metamask/utils";
6
+ import type { CaveatConstraint, CaveatDiffMap, CaveatSpecificationConstraint, CaveatSpecificationMap, ExtractCaveat, ExtractCaveats, ExtractCaveatValue } from "./Caveat.mjs";
7
+ import type { EndowmentSpecificationConstraint, ExtractAllowedCaveatTypes, OriginString, PermissionConstraint, PermissionSpecificationConstraint, PermissionSpecificationMap, RequestedPermissions, RestrictedMethod, RestrictedMethodParameters, RestrictedMethodSpecificationConstraint, SideEffectHandler, ValidPermission, ValidPermissionSpecification } from "./Permission.mjs";
8
+ import { getPermissionMiddlewareFactory } from "./permission-middleware.mjs";
9
+ import type { GetSubjectMetadata } from "./SubjectMetadataController.mjs";
10
+ /**
11
+ * Metadata associated with {@link PermissionController} subjects.
12
+ */
13
+ export type PermissionSubjectMetadata = {
14
+ origin: OriginString;
15
+ };
16
+ /**
17
+ * Metadata associated with permission requests.
18
+ */
19
+ export type PermissionsRequestMetadata = PermissionSubjectMetadata & {
20
+ id: string;
21
+ [key: string]: Json;
22
+ };
23
+ /**
24
+ * A diff produced by an incremental permissions request.
25
+ */
26
+ export type PermissionDiffMap<TargetName extends string, AllowedCaveats extends CaveatConstraint> = Record<TargetName, CaveatDiffMap<AllowedCaveats>>;
27
+ /**
28
+ * Used for prompting the user about a proposed new permission.
29
+ * Includes information about the grantee subject, requested permissions, the
30
+ * diff relative to the previously granted permissions (if relevant), and any
31
+ * additional information added by the consumer.
32
+ *
33
+ * All properties except `diff` and `permissions` are passed to any factories
34
+ * for the requested permissions.
35
+ */
36
+ export type PermissionsRequest = {
37
+ metadata: PermissionsRequestMetadata;
38
+ permissions: RequestedPermissions;
39
+ [key: string]: Json;
40
+ } & {
41
+ diff?: {
42
+ currentPermissions: SubjectPermissions<PermissionConstraint>;
43
+ permissionDiffMap: PermissionDiffMap<string, CaveatConstraint>;
44
+ };
45
+ };
46
+ /**
47
+ * Metadata associated with an approved permission request.
48
+ */
49
+ type ApprovedPermissionsMetadata = {
50
+ data?: Record<string, unknown>;
51
+ id: string;
52
+ origin: OriginString;
53
+ };
54
+ export type SideEffects = {
55
+ permittedHandlers: Record<string, SideEffectHandler<ActionConstraint, EventConstraint>>;
56
+ failureHandlers: Record<string, SideEffectHandler<ActionConstraint, EventConstraint>>;
57
+ };
58
+ /**
59
+ * The name of the {@link PermissionController}.
60
+ */
61
+ declare const controllerName = "PermissionController";
62
+ /**
63
+ * Permissions associated with a {@link PermissionController} subject.
64
+ */
65
+ export type SubjectPermissions<Permission extends PermissionConstraint> = Record<Permission['parentCapability'], Permission>;
66
+ /**
67
+ * Permissions and metadata associated with a {@link PermissionController}
68
+ * subject.
69
+ */
70
+ export type PermissionSubjectEntry<SubjectPermission extends PermissionConstraint> = {
71
+ origin: SubjectPermission['invoker'];
72
+ permissions: SubjectPermissions<SubjectPermission>;
73
+ };
74
+ /**
75
+ * All subjects of a {@link PermissionController}.
76
+ *
77
+ * @template SubjectPermission - The permissions of the subject.
78
+ */
79
+ export type PermissionControllerSubjects<SubjectPermission extends PermissionConstraint> = Record<SubjectPermission['invoker'], PermissionSubjectEntry<SubjectPermission>>;
80
+ /**
81
+ * The state of a {@link PermissionController}.
82
+ *
83
+ * @template Permission - The controller's permission type union.
84
+ */
85
+ export type PermissionControllerState<Permission> = Permission extends PermissionConstraint ? {
86
+ subjects: PermissionControllerSubjects<Permission>;
87
+ } : never;
88
+ /**
89
+ * Gets the state of the {@link PermissionController}.
90
+ */
91
+ export type GetPermissionControllerState = ControllerGetStateAction<typeof controllerName, PermissionControllerState<PermissionConstraint>>;
92
+ /**
93
+ * Gets the names of all subjects from the {@link PermissionController}.
94
+ */
95
+ export type GetSubjects = {
96
+ type: `${typeof controllerName}:getSubjectNames`;
97
+ handler: () => (keyof PermissionControllerSubjects<PermissionConstraint>)[];
98
+ };
99
+ /**
100
+ * Gets the permissions for specified subject
101
+ */
102
+ export type GetPermissions = {
103
+ type: `${typeof controllerName}:getPermissions`;
104
+ handler: GenericPermissionController['getPermissions'];
105
+ };
106
+ /**
107
+ * Checks whether the specified subject has any permissions.
108
+ */
109
+ export type HasPermissions = {
110
+ type: `${typeof controllerName}:hasPermissions`;
111
+ handler: GenericPermissionController['hasPermissions'];
112
+ };
113
+ /**
114
+ * Checks whether the specified subject has a specific permission.
115
+ */
116
+ export type HasPermission = {
117
+ type: `${typeof controllerName}:hasPermission`;
118
+ handler: GenericPermissionController['hasPermission'];
119
+ };
120
+ /**
121
+ * Directly grants given permissions for a specificed origin without requesting user approval
122
+ */
123
+ export type GrantPermissions = {
124
+ type: `${typeof controllerName}:grantPermissions`;
125
+ handler: GenericPermissionController['grantPermissions'];
126
+ };
127
+ /**
128
+ * Directly grants given permissions for a specificed origin without requesting user approval
129
+ */
130
+ export type GrantPermissionsIncremental = {
131
+ type: `${typeof controllerName}:grantPermissionsIncremental`;
132
+ handler: GenericPermissionController['grantPermissionsIncremental'];
133
+ };
134
+ /**
135
+ * Requests given permissions for a specified origin
136
+ */
137
+ export type RequestPermissions = {
138
+ type: `${typeof controllerName}:requestPermissions`;
139
+ handler: GenericPermissionController['requestPermissions'];
140
+ };
141
+ /**
142
+ * Requests given permissions for a specified origin
143
+ */
144
+ export type RequestPermissionsIncremental = {
145
+ type: `${typeof controllerName}:requestPermissionsIncremental`;
146
+ handler: GenericPermissionController['requestPermissionsIncremental'];
147
+ };
148
+ /**
149
+ * Removes the specified permissions for each origin.
150
+ */
151
+ export type RevokePermissions = {
152
+ type: `${typeof controllerName}:revokePermissions`;
153
+ handler: GenericPermissionController['revokePermissions'];
154
+ };
155
+ /**
156
+ * Removes all permissions for a given origin
157
+ */
158
+ export type RevokeAllPermissions = {
159
+ type: `${typeof controllerName}:revokeAllPermissions`;
160
+ handler: GenericPermissionController['revokeAllPermissions'];
161
+ };
162
+ /**
163
+ * Revokes all permissions corresponding to the specified target for all subjects.
164
+ * Does nothing if no subjects or no such permission exists.
165
+ */
166
+ export type RevokePermissionForAllSubjects = {
167
+ type: `${typeof controllerName}:revokePermissionForAllSubjects`;
168
+ handler: GenericPermissionController['revokePermissionForAllSubjects'];
169
+ };
170
+ /**
171
+ * Updates a caveat value for a specified caveat type belonging to a specific target and origin.
172
+ */
173
+ export type UpdateCaveat = {
174
+ type: `${typeof controllerName}:updateCaveat`;
175
+ handler: GenericPermissionController['updateCaveat'];
176
+ };
177
+ /**
178
+ * Clears all permissions from the {@link PermissionController}.
179
+ */
180
+ export type ClearPermissions = {
181
+ type: `${typeof controllerName}:clearPermissions`;
182
+ handler: () => void;
183
+ };
184
+ /**
185
+ * Gets the endowments for the given subject and permission.
186
+ */
187
+ export type GetEndowments = {
188
+ type: `${typeof controllerName}:getEndowments`;
189
+ handler: GenericPermissionController['getEndowments'];
190
+ };
191
+ /**
192
+ * The {@link ControllerMessenger} actions of the {@link PermissionController}.
193
+ */
194
+ export type PermissionControllerActions = ClearPermissions | GetEndowments | GetPermissionControllerState | GetSubjects | GetPermissions | HasPermission | HasPermissions | GrantPermissions | GrantPermissionsIncremental | RequestPermissions | RequestPermissionsIncremental | RevokeAllPermissions | RevokePermissionForAllSubjects | RevokePermissions | UpdateCaveat;
195
+ /**
196
+ * The generic state change event of the {@link PermissionController}.
197
+ */
198
+ export type PermissionControllerStateChange = ControllerStateChangeEvent<typeof controllerName, PermissionControllerState<PermissionConstraint>>;
199
+ /**
200
+ * The {@link ControllerMessenger} events of the {@link PermissionController}.
201
+ *
202
+ * The permission controller only emits its generic state change events.
203
+ * Consumers should use selector subscriptions to subscribe to relevant
204
+ * substate.
205
+ */
206
+ export type PermissionControllerEvents = PermissionControllerStateChange;
207
+ /**
208
+ * The external {@link ControllerMessenger} actions available to the
209
+ * {@link PermissionController}.
210
+ */
211
+ type AllowedActions = AddApprovalRequest | HasApprovalRequest | AcceptApprovalRequest | RejectApprovalRequest | GetSubjectMetadata;
212
+ /**
213
+ * The messenger of the {@link PermissionController}.
214
+ */
215
+ export type PermissionControllerMessenger = RestrictedControllerMessenger<typeof controllerName, PermissionControllerActions | AllowedActions, PermissionControllerEvents, AllowedActions['type'], never>;
216
+ export type SideEffectMessenger<Actions extends ActionConstraint, Events extends EventConstraint> = RestrictedControllerMessenger<typeof controllerName, Actions | AllowedActions, Events, AllowedActions['type'] | Actions['type'], Events['type']>;
217
+ /**
218
+ * A generic {@link PermissionController}.
219
+ */
220
+ export type GenericPermissionController = PermissionController<PermissionSpecificationConstraint, CaveatSpecificationConstraint>;
221
+ /**
222
+ * Describes the possible results of a {@link CaveatMutator} function.
223
+ */
224
+ export declare enum CaveatMutatorOperation {
225
+ Noop = 0,
226
+ UpdateValue = 1,
227
+ DeleteCaveat = 2,
228
+ RevokePermission = 3
229
+ }
230
+ /**
231
+ * Given a caveat value, returns a {@link CaveatMutatorOperation} and, optionally,
232
+ * a new caveat value.
233
+ *
234
+ * @see {@link PermissionController.updatePermissionsByCaveat} for more details.
235
+ * @template Caveat - The caveat type for which this mutator is intended.
236
+ * @param caveatValue - The existing value of the caveat being mutated.
237
+ * @returns A tuple of the mutation result and, optionally, the new caveat
238
+ * value.
239
+ */
240
+ export type CaveatMutator<TargetCaveat extends CaveatConstraint> = (caveatValue: TargetCaveat['value']) => CaveatMutatorResult;
241
+ type CaveatMutatorResult = Readonly<{
242
+ operation: CaveatMutatorOperation.UpdateValue;
243
+ value: CaveatConstraint['value'];
244
+ }> | Readonly<{
245
+ operation: Exclude<CaveatMutatorOperation, CaveatMutatorOperation.UpdateValue>;
246
+ }>;
247
+ /**
248
+ * Extracts the permission(s) specified by the given permission and caveat
249
+ * specifications.
250
+ *
251
+ * @template ControllerPermissionSpecification - The permission specification(s)
252
+ * to extract from.
253
+ * @template ControllerCaveatSpecification - The caveat specification(s) to
254
+ * extract from. Necessary because {@link Permission} has a generic parameter
255
+ * that describes the allowed caveats for the permission.
256
+ */
257
+ export type ExtractPermission<ControllerPermissionSpecification extends PermissionSpecificationConstraint, ControllerCaveatSpecification extends CaveatSpecificationConstraint> = ControllerPermissionSpecification extends ValidPermissionSpecification<ControllerPermissionSpecification> ? ValidPermission<ControllerPermissionSpecification['targetName'], ExtractCaveats<ControllerCaveatSpecification>> : never;
258
+ /**
259
+ * Extracts the restricted method permission(s) specified by the given
260
+ * permission and caveat specifications.
261
+ *
262
+ * @template ControllerPermissionSpecification - The permission specification(s)
263
+ * to extract from.
264
+ * @template ControllerCaveatSpecification - The caveat specification(s) to
265
+ * extract from. Necessary because {@link Permission} has a generic parameter
266
+ * that describes the allowed caveats for the permission.
267
+ */
268
+ export type ExtractRestrictedMethodPermission<ControllerPermissionSpecification extends PermissionSpecificationConstraint, ControllerCaveatSpecification extends CaveatSpecificationConstraint> = ExtractPermission<Extract<ControllerPermissionSpecification, RestrictedMethodSpecificationConstraint>, ControllerCaveatSpecification>;
269
+ /**
270
+ * Extracts the endowment permission(s) specified by the given permission and
271
+ * caveat specifications.
272
+ *
273
+ * @template ControllerPermissionSpecification - The permission specification(s)
274
+ * to extract from.
275
+ * @template ControllerCaveatSpecification - The caveat specification(s) to
276
+ * extract from. Necessary because {@link Permission} has a generic parameter
277
+ * that describes the allowed caveats for the permission.
278
+ */
279
+ export type ExtractEndowmentPermission<ControllerPermissionSpecification extends PermissionSpecificationConstraint, ControllerCaveatSpecification extends CaveatSpecificationConstraint> = ExtractPermission<Extract<ControllerPermissionSpecification, EndowmentSpecificationConstraint>, ControllerCaveatSpecification>;
280
+ /**
281
+ * Options for the {@link PermissionController} constructor.
282
+ *
283
+ * @template ControllerPermissionSpecification - A union of the types of all
284
+ * permission specifications available to the controller. Any referenced caveats
285
+ * must be included in the controller's caveat specifications.
286
+ * @template ControllerCaveatSpecification - A union of the types of all
287
+ * caveat specifications available to the controller.
288
+ */
289
+ export type PermissionControllerOptions<ControllerPermissionSpecification extends PermissionSpecificationConstraint, ControllerCaveatSpecification extends CaveatSpecificationConstraint> = {
290
+ messenger: PermissionControllerMessenger;
291
+ caveatSpecifications: CaveatSpecificationMap<ControllerCaveatSpecification>;
292
+ permissionSpecifications: PermissionSpecificationMap<ControllerPermissionSpecification>;
293
+ unrestrictedMethods: readonly string[];
294
+ state?: Partial<PermissionControllerState<ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>>>;
295
+ };
296
+ /**
297
+ * The permission controller. See the [Architecture](../ARCHITECTURE.md)
298
+ * document for details.
299
+ *
300
+ * Assumes the existence of an {@link ApprovalController} reachable via the
301
+ * {@link ControllerMessenger}.
302
+ *
303
+ * @template ControllerPermissionSpecification - A union of the types of all
304
+ * permission specifications available to the controller. Any referenced caveats
305
+ * must be included in the controller's caveat specifications.
306
+ * @template ControllerCaveatSpecification - A union of the types of all
307
+ * caveat specifications available to the controller.
308
+ */
309
+ export declare class PermissionController<ControllerPermissionSpecification extends PermissionSpecificationConstraint, ControllerCaveatSpecification extends CaveatSpecificationConstraint> extends BaseController<typeof controllerName, PermissionControllerState<ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>>, PermissionControllerMessenger> {
310
+ #private;
311
+ private readonly _caveatSpecifications;
312
+ private readonly _permissionSpecifications;
313
+ private readonly _unrestrictedMethods;
314
+ /**
315
+ * The names of all JSON-RPC methods that will be ignored by the controller.
316
+ *
317
+ * @returns The names of all unrestricted JSON-RPC methods
318
+ */
319
+ get unrestrictedMethods(): ReadonlySet<string>;
320
+ /**
321
+ * Returns a `json-rpc-engine` middleware function factory, so that the rules
322
+ * described by the state of this controller can be applied to incoming
323
+ * JSON-RPC requests.
324
+ *
325
+ * The middleware **must** be added in the correct place in the middleware
326
+ * stack in order for it to work. See the README for an example.
327
+ */
328
+ createPermissionMiddleware: ReturnType<typeof getPermissionMiddlewareFactory>;
329
+ /**
330
+ * Constructs the PermissionController.
331
+ *
332
+ * @param options - Permission controller options.
333
+ * @param options.caveatSpecifications - The specifications of all caveats
334
+ * available to the controller. See {@link CaveatSpecificationMap} and the
335
+ * documentation for more details.
336
+ * @param options.permissionSpecifications - The specifications of all
337
+ * permissions available to the controller. See
338
+ * {@link PermissionSpecificationMap} and the README for more details.
339
+ * @param options.unrestrictedMethods - The callable names of all JSON-RPC
340
+ * methods ignored by the new controller.
341
+ * @param options.messenger - The controller messenger. See
342
+ * {@link BaseController} for more information.
343
+ * @param options.state - Existing state to hydrate the controller with at
344
+ * initialization.
345
+ */
346
+ constructor(options: PermissionControllerOptions<ControllerPermissionSpecification, ControllerCaveatSpecification>);
347
+ /**
348
+ * Gets a permission specification.
349
+ *
350
+ * @param targetName - The name of the permission specification to get.
351
+ * @returns The permission specification with the specified target name.
352
+ */
353
+ private getPermissionSpecification;
354
+ /**
355
+ * Gets a caveat specification.
356
+ *
357
+ * @param caveatType - The type of the caveat specification to get.
358
+ * @returns The caveat specification with the specified type.
359
+ */
360
+ private getCaveatSpecification;
361
+ /**
362
+ * Constructor helper for validating permission specifications.
363
+ *
364
+ * Throws an error if validation fails.
365
+ *
366
+ * @param permissionSpecifications - The permission specifications passed to
367
+ * this controller's constructor.
368
+ * @param caveatSpecifications - The caveat specifications passed to this
369
+ * controller.
370
+ */
371
+ private validatePermissionSpecifications;
372
+ /**
373
+ * Constructor helper for registering the controller's messaging system
374
+ * actions.
375
+ */
376
+ private registerMessageHandlers;
377
+ /**
378
+ * Clears the state of the controller.
379
+ */
380
+ clearState(): void;
381
+ /**
382
+ * Gets the permission specification corresponding to the given permission
383
+ * type and target name. Throws an error if the target name does not
384
+ * correspond to a permission, or if the specification is not of the
385
+ * given permission type.
386
+ *
387
+ * @template Type - The type of the permission specification to get.
388
+ * @param permissionType - The type of the permission specification to get.
389
+ * @param targetName - The name of the permission whose specification to get.
390
+ * @param requestingOrigin - The origin of the requesting subject, if any.
391
+ * Will be added to any thrown errors.
392
+ * @returns The specification object corresponding to the given type and
393
+ * target name.
394
+ */
395
+ private getTypedPermissionSpecification;
396
+ /**
397
+ * Gets the implementation of the specified restricted method.
398
+ *
399
+ * A JSON-RPC error is thrown if the method does not exist.
400
+ *
401
+ * @see {@link PermissionController.executeRestrictedMethod} and
402
+ * {@link PermissionController.createPermissionMiddleware} for internal usage.
403
+ * @param method - The name of the restricted method.
404
+ * @param origin - The origin associated with the request for the restricted
405
+ * method, if any.
406
+ * @returns The restricted method implementation.
407
+ */
408
+ getRestrictedMethod(method: string, origin?: string): RestrictedMethod<RestrictedMethodParameters, Json>;
409
+ /**
410
+ * Gets a list of all origins of subjects.
411
+ *
412
+ * @returns The origins (i.e. IDs) of all subjects.
413
+ */
414
+ getSubjectNames(): OriginString[];
415
+ /**
416
+ * Gets the permission for the specified target of the subject corresponding
417
+ * to the specified origin.
418
+ *
419
+ * @param origin - The origin of the subject.
420
+ * @param targetName - The method name as invoked by a third party (i.e., not
421
+ * a method key).
422
+ * @returns The permission if it exists, or undefined otherwise.
423
+ */
424
+ getPermission<SubjectPermission extends ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>>(origin: OriginString, targetName: SubjectPermission['parentCapability']): SubjectPermission | undefined;
425
+ /**
426
+ * Gets all permissions for the specified subject, if any.
427
+ *
428
+ * @param origin - The origin of the subject.
429
+ * @returns The permissions of the subject, if any.
430
+ */
431
+ getPermissions(origin: OriginString): SubjectPermissions<ValidPermission<string, ExtractCaveats<ControllerCaveatSpecification>>> | undefined;
432
+ /**
433
+ * Checks whether the subject with the specified origin has the specified
434
+ * permission.
435
+ *
436
+ * @param origin - The origin of the subject.
437
+ * @param target - The target name of the permission.
438
+ * @returns Whether the subject has the permission.
439
+ */
440
+ hasPermission(origin: OriginString, target: ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability']): boolean;
441
+ /**
442
+ * Checks whether the subject with the specified origin has any permissions.
443
+ * Use this if you want to know if a subject "exists".
444
+ *
445
+ * @param origin - The origin of the subject to check.
446
+ * @returns Whether the subject has any permissions.
447
+ */
448
+ hasPermissions(origin: OriginString): boolean;
449
+ /**
450
+ * Revokes all permissions from the specified origin.
451
+ *
452
+ * Throws an error of the origin has no permissions.
453
+ *
454
+ * @param origin - The origin whose permissions to revoke.
455
+ */
456
+ revokeAllPermissions(origin: OriginString): void;
457
+ /**
458
+ * Revokes the specified permission from the subject with the specified
459
+ * origin.
460
+ *
461
+ * Throws an error if the subject or the permission does not exist.
462
+ *
463
+ * @param origin - The origin of the subject whose permission to revoke.
464
+ * @param target - The target name of the permission to revoke.
465
+ */
466
+ revokePermission(origin: OriginString, target: ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability']): void;
467
+ /**
468
+ * Revokes the specified permissions from the specified subjects.
469
+ *
470
+ * Throws an error if any of the subjects or permissions do not exist.
471
+ *
472
+ * @param subjectsAndPermissions - An object mapping subject origins
473
+ * to arrays of permission target names to revoke.
474
+ */
475
+ revokePermissions(subjectsAndPermissions: Record<OriginString, NonEmptyArray<ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability']>>): void;
476
+ /**
477
+ * Revokes all permissions corresponding to the specified target for all subjects.
478
+ * Does nothing if no subjects or no such permission exists.
479
+ *
480
+ * @param target - The name of the target to revoke all permissions for.
481
+ */
482
+ revokePermissionForAllSubjects(target: ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability']): void;
483
+ /**
484
+ * Deletes the permission identified by the given origin and target. If the
485
+ * permission is the single remaining permission of its subject, the subject
486
+ * is also deleted.
487
+ *
488
+ * @param subjects - The draft permission controller subjects.
489
+ * @param origin - The origin of the subject associated with the permission
490
+ * to delete.
491
+ * @param target - The target name of the permission to delete.
492
+ */
493
+ private deletePermission;
494
+ /**
495
+ * Checks whether the permission of the subject corresponding to the given
496
+ * origin has a caveat of the specified type.
497
+ *
498
+ * Throws an error if the subject does not have a permission with the
499
+ * specified target name.
500
+ *
501
+ * @template TargetName - The permission target name. Should be inferred.
502
+ * @template CaveatType - The valid caveat types for the permission. Should
503
+ * be inferred.
504
+ * @param origin - The origin of the subject.
505
+ * @param target - The target name of the permission.
506
+ * @param caveatType - The type of the caveat to check for.
507
+ * @returns Whether the permission has the specified caveat.
508
+ */
509
+ hasCaveat<TargetName extends ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability'], CaveatType extends ExtractAllowedCaveatTypes<ControllerPermissionSpecification>>(origin: OriginString, target: TargetName, caveatType: CaveatType): boolean;
510
+ /**
511
+ * Gets the caveat of the specified type, if any, for the permission of
512
+ * the subject corresponding to the given origin.
513
+ *
514
+ * Throws an error if the subject does not have a permission with the
515
+ * specified target name.
516
+ *
517
+ * @template TargetName - The permission target name. Should be inferred.
518
+ * @template CaveatType - The valid caveat types for the permission. Should
519
+ * be inferred.
520
+ * @param origin - The origin of the subject.
521
+ * @param target - The target name of the permission.
522
+ * @param caveatType - The type of the caveat to get.
523
+ * @returns The caveat, or `undefined` if no such caveat exists.
524
+ */
525
+ getCaveat<TargetName extends ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability'], CaveatType extends ExtractAllowedCaveatTypes<ControllerPermissionSpecification>>(origin: OriginString, target: TargetName, caveatType: CaveatType): ExtractCaveat<ControllerCaveatSpecification, CaveatType> | undefined;
526
+ /**
527
+ * Adds a caveat of the specified type, with the specified caveat value, to
528
+ * the permission corresponding to the given subject origin and permission
529
+ * target.
530
+ *
531
+ * For modifying existing caveats, use
532
+ * {@link PermissionController.updateCaveat}.
533
+ *
534
+ * Throws an error if no such permission exists, or if the caveat already
535
+ * exists.
536
+ *
537
+ * @template TargetName - The permission target name. Should be inferred.
538
+ * @template CaveatType - The valid caveat types for the permission. Should
539
+ * be inferred.
540
+ * @param origin - The origin of the subject.
541
+ * @param target - The target name of the permission.
542
+ * @param caveatType - The type of the caveat to add.
543
+ * @param caveatValue - The value of the caveat to add.
544
+ */
545
+ addCaveat<TargetName extends ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability'], CaveatType extends ExtractAllowedCaveatTypes<ControllerPermissionSpecification>>(origin: OriginString, target: TargetName, caveatType: CaveatType, caveatValue: ExtractCaveatValue<ControllerCaveatSpecification, CaveatType>): void;
546
+ /**
547
+ * Updates the value of the caveat of the specified type belonging to the
548
+ * permission corresponding to the given subject origin and permission
549
+ * target.
550
+ *
551
+ * For adding new caveats, use
552
+ * {@link PermissionController.addCaveat}.
553
+ *
554
+ * Throws an error if no such permission or caveat exists.
555
+ *
556
+ * @template TargetName - The permission target name. Should be inferred.
557
+ * @template CaveatType - The valid caveat types for the permission. Should
558
+ * be inferred.
559
+ * @param origin - The origin of the subject.
560
+ * @param target - The target name of the permission.
561
+ * @param caveatType - The type of the caveat to update.
562
+ * @param caveatValue - The new value of the caveat.
563
+ */
564
+ updateCaveat<TargetName extends ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability'], CaveatType extends ExtractAllowedCaveatTypes<ControllerPermissionSpecification>, CaveatValue extends ExtractCaveatValue<ControllerCaveatSpecification, CaveatType>>(origin: OriginString, target: TargetName, caveatType: CaveatType, caveatValue: CaveatValue): void;
565
+ /**
566
+ * Sets the specified caveat on the specified permission. Overwrites existing
567
+ * caveats of the same type in-place (preserving array order), and adds the
568
+ * caveat to the end of the array otherwise.
569
+ *
570
+ * Throws an error if the permission does not exist or fails to validate after
571
+ * its caveats have been modified.
572
+ *
573
+ * @see {@link PermissionController.addCaveat}
574
+ * @see {@link PermissionController.updateCaveat}
575
+ * @template TargetName - The permission target name. Should be inferred.
576
+ * @template CaveatType - The valid caveat types for the permission. Should
577
+ * be inferred.
578
+ * @param origin - The origin of the subject.
579
+ * @param target - The target name of the permission.
580
+ * @param caveatType - The type of the caveat to set.
581
+ * @param caveatValue - The value of the caveat to set.
582
+ */
583
+ private setCaveat;
584
+ /**
585
+ * Updates all caveats with the specified type for all subjects and
586
+ * permissions by applying the specified mutator function to them.
587
+ *
588
+ * ATTN: Permissions can be revoked entirely by the action of this method,
589
+ * read on for details.
590
+ *
591
+ * Caveat mutators are functions that receive a caveat value and return a
592
+ * tuple consisting of a {@link CaveatMutatorOperation} and, optionally, a new
593
+ * value to update the existing caveat with.
594
+ *
595
+ * For each caveat, depending on the mutator result, this method will:
596
+ * - Do nothing ({@link CaveatMutatorOperation.Noop})
597
+ * - Update the value of the caveat ({@link CaveatMutatorOperation.UpdateValue}). The caveat specification validator, if any, will be called after updating the value.
598
+ * - Delete the caveat ({@link CaveatMutatorOperation.DeleteCaveat}). The permission specification validator, if any, will be called after deleting the caveat.
599
+ * - Revoke the parent permission ({@link CaveatMutatorOperation.RevokePermission})
600
+ *
601
+ * This method throws if the validation of any caveat or permission fails.
602
+ *
603
+ * @param targetCaveatType - The type of the caveats to update.
604
+ * @param mutator - The mutator function which will be applied to all caveat
605
+ * values.
606
+ */
607
+ updatePermissionsByCaveat<CaveatType extends ExtractCaveats<ControllerCaveatSpecification>['type'], TargetCaveat extends ExtractCaveat<ControllerCaveatSpecification, CaveatType>>(targetCaveatType: CaveatType, mutator: CaveatMutator<TargetCaveat>): void;
608
+ /**
609
+ * Removes the caveat of the specified type from the permission corresponding
610
+ * to the given subject origin and target name.
611
+ *
612
+ * Throws an error if no such permission or caveat exists.
613
+ *
614
+ * @template TargetName - The permission target name. Should be inferred.
615
+ * @template CaveatType - The valid caveat types for the permission. Should
616
+ * be inferred.
617
+ * @param origin - The origin of the subject.
618
+ * @param target - The target name of the permission.
619
+ * @param caveatType - The type of the caveat to remove.
620
+ */
621
+ removeCaveat<TargetName extends ControllerPermissionSpecification['targetName'], CaveatType extends ExtractAllowedCaveatTypes<ControllerPermissionSpecification>>(origin: OriginString, target: TargetName, caveatType: CaveatType): void;
622
+ /**
623
+ * Deletes the specified caveat from the specified permission. If no caveats
624
+ * remain after deletion, the permission's caveat property is set to `null`.
625
+ * The permission is validated after being modified.
626
+ *
627
+ * Throws an error if the permission does not have a caveat with the specified
628
+ * type.
629
+ *
630
+ * @param permission - The permission whose caveat to delete.
631
+ * @param caveatType - The type of the caveat to delete.
632
+ * @param origin - The origin the permission subject.
633
+ */
634
+ private deleteCaveat;
635
+ /**
636
+ * Validates the specified modified permission. Should **always** be invoked
637
+ * on a permission after its caveats have been modified.
638
+ *
639
+ * Just like {@link PermissionController.validatePermission}, except that the
640
+ * corresponding target name and specification are retrieved first, and an
641
+ * error is thrown if the target name does not exist.
642
+ *
643
+ * @param permission - The modified permission to validate.
644
+ * @param origin - The origin associated with the permission.
645
+ */
646
+ private validateModifiedPermission;
647
+ /**
648
+ * Verifies the existence the specified permission target, i.e. whether it has
649
+ * a specification.
650
+ *
651
+ * @param target - The requested permission target.
652
+ * @returns Whether the permission target exists.
653
+ */
654
+ private targetExists;
655
+ /**
656
+ * Grants _approved_ permissions to the specified subject. Every permission and
657
+ * caveat is stringently validated—including by calling their specification
658
+ * validators—and an error is thrown if validation fails.
659
+ *
660
+ * ATTN: This method does **not** prompt the user for approval. User consent must
661
+ * first be obtained through some other means.
662
+ *
663
+ * @see {@link PermissionController.requestPermissions} For initiating a
664
+ * permissions request requiring user approval.
665
+ * @param options - Options bag.
666
+ * @param options.approvedPermissions - The requested permissions approved by
667
+ * the user.
668
+ * @param options.requestData - Permission request data. Passed to permission
669
+ * factory functions.
670
+ * @param options.preserveExistingPermissions - Whether to preserve the
671
+ * subject's existing permissions.
672
+ * @param options.subject - The subject to grant permissions to.
673
+ * @returns The subject's new permission state. It may or may not have changed.
674
+ */
675
+ grantPermissions({ approvedPermissions, requestData, preserveExistingPermissions, subject, }: {
676
+ approvedPermissions: RequestedPermissions;
677
+ subject: PermissionSubjectMetadata;
678
+ preserveExistingPermissions?: boolean;
679
+ requestData?: Record<string, unknown>;
680
+ }): Partial<SubjectPermissions<ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>>>;
681
+ /**
682
+ * Incrementally grants _approved_ permissions to the specified subject. Every
683
+ * permission and caveat is stringently validated—including by calling their
684
+ * specification validators—and an error is thrown if validation fails.
685
+ *
686
+ * ATTN: This method does **not** prompt the user for approval. User consent must
687
+ * first be obtained through some other means.
688
+ *
689
+ * @see {@link PermissionController.requestPermissionsIncremental} For initiating
690
+ * an incremental permissions request requiring user approval.
691
+ * @param options - Options bag.
692
+ * @param options.approvedPermissions - The requested permissions approved by
693
+ * the user.
694
+ * @param options.requestData - Permission request data. Passed to permission
695
+ * factory functions.
696
+ * @param options.subject - The subject to grant permissions to.
697
+ * @returns The subject's new permission state. It may or may not have changed.
698
+ */
699
+ grantPermissionsIncremental({ approvedPermissions, requestData, subject, }: {
700
+ approvedPermissions: RequestedPermissions;
701
+ subject: PermissionSubjectMetadata;
702
+ requestData?: Record<string, unknown>;
703
+ }): Partial<SubjectPermissions<ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>>>;
704
+ /**
705
+ * Validates the specified permission by:
706
+ * - Ensuring that if `subjectTypes` is specified, the subject requesting the permission is of a type in the list.
707
+ * - Ensuring that its `caveats` property is either `null` or a non-empty array.
708
+ * - Ensuring that it only includes caveats allowed by its specification.
709
+ * - Ensuring that it includes no duplicate caveats (by caveat type).
710
+ * - Validating each caveat object, if `performCaveatValidation` is `true`.
711
+ * - Calling the validator of its specification, if one exists and `invokePermissionValidator` is `true`.
712
+ *
713
+ * An error is thrown if validation fails.
714
+ *
715
+ * @param specification - The specification of the permission.
716
+ * @param permission - The permission to validate.
717
+ * @param origin - The origin associated with the permission.
718
+ * @param validationOptions - Validation options.
719
+ * @param validationOptions.invokePermissionValidator - Whether to invoke the
720
+ * permission's consumer-specified validator function, if any.
721
+ * @param validationOptions.performCaveatValidation - Whether to invoke
722
+ * {@link PermissionController.validateCaveat} on each of the permission's
723
+ * caveats.
724
+ */
725
+ private validatePermission;
726
+ /**
727
+ * Assigns the specified permissions to the subject with the given origin.
728
+ * Overwrites all existing permissions, and creates a subject entry if it
729
+ * doesn't already exist.
730
+ *
731
+ * ATTN: Assumes that the new permissions have been validated.
732
+ *
733
+ * @param origin - The origin of the grantee subject.
734
+ * @param permissions - The new permissions for the grantee subject.
735
+ */
736
+ private setValidatedPermissions;
737
+ /**
738
+ * Validates the requested caveats for the permission of the specified
739
+ * subject origin and target name and returns the validated caveat array.
740
+ *
741
+ * Throws an error if validation fails.
742
+ *
743
+ * @param origin - The origin of the permission subject.
744
+ * @param target - The permission target name.
745
+ * @param requestedCaveats - The requested caveats to construct.
746
+ * @returns The constructed caveats.
747
+ */
748
+ private constructCaveats;
749
+ /**
750
+ * This methods validates that the specified caveat is an object with the
751
+ * expected properties and types. It also ensures that a caveat specification
752
+ * exists for the requested caveat type, and calls the specification
753
+ * validator, if it exists, on the caveat object.
754
+ *
755
+ * Throws an error if validation fails.
756
+ *
757
+ * @param caveat - The caveat object to validate.
758
+ * @param origin - The origin associated with the subject of the parent
759
+ * permission.
760
+ * @param target - The target name associated with the parent permission.
761
+ */
762
+ private validateCaveat;
763
+ /**
764
+ * Initiates a permission request that requires user approval.
765
+ *
766
+ * Either this or {@link PermissionController.requestPermissionsIncremental}
767
+ * should always be used to grant additional permissions to a subject,
768
+ * unless user approval has been obtained through some other means.
769
+ *
770
+ * Permissions are validated at every step of the approval process, and this
771
+ * method will reject if validation fails.
772
+ *
773
+ * @see {@link ApprovalController} For the user approval logic.
774
+ * @see {@link PermissionController.acceptPermissionsRequest} For the method
775
+ * that _accepts_ the request and resolves the user approval promise.
776
+ * @see {@link PermissionController.rejectPermissionsRequest} For the method
777
+ * that _rejects_ the request and the user approval promise.
778
+ * @param subject - The grantee subject.
779
+ * @param requestedPermissions - The requested permissions.
780
+ * @param options - Additional options.
781
+ * @param options.id - The id of the permissions request. Defaults to a unique
782
+ * id.
783
+ * @param options.preserveExistingPermissions - Whether to preserve the
784
+ * subject's existing permissions. Defaults to `true`.
785
+ * @param options.metadata - Additional metadata about the permission request.
786
+ * @returns The granted permissions and request metadata.
787
+ */
788
+ requestPermissions(subject: PermissionSubjectMetadata, requestedPermissions: RequestedPermissions, options?: {
789
+ id?: string;
790
+ preserveExistingPermissions?: boolean;
791
+ metadata?: Record<string, Json>;
792
+ }): Promise<[
793
+ Partial<SubjectPermissions<ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>>>,
794
+ ApprovedPermissionsMetadata
795
+ ]>;
796
+ /**
797
+ * Initiates an incremental permission request that prompts for user approval.
798
+ * Incremental permission requests allow the caller to replace existing and/or
799
+ * add brand new permissions and caveats for the specified subject.
800
+ *
801
+ * Incremental permission request are merged with the subject's existing permissions
802
+ * through a right-biased union, where the incremental permission are the right-hand
803
+ * side of the merger. If both sides of the merger specify the same caveats for a
804
+ * given permission, the caveats are merged using their specification's caveat value
805
+ * merger property.
806
+ *
807
+ * Either this or {@link PermissionController.requestPermissions} should
808
+ * always be used to grant additional permissions to a subject, unless user
809
+ * approval has been obtained through some other means.
810
+ *
811
+ * Permissions are validated at every step of the approval process, and this
812
+ * method will reject if validation fails.
813
+ *
814
+ * @see {@link ApprovalController} For the user approval logic.
815
+ * @see {@link PermissionController.acceptPermissionsRequest} For the method
816
+ * that _accepts_ the request and resolves the user approval promise.
817
+ * @see {@link PermissionController.rejectPermissionsRequest} For the method
818
+ * that _rejects_ the request and the user approval promise.
819
+ * @param subject - The grantee subject.
820
+ * @param requestedPermissions - The requested permissions.
821
+ * @param options - Additional options.
822
+ * @param options.id - The id of the permissions request. Defaults to a unique
823
+ * id.
824
+ * @param options.metadata - Additional metadata about the permission request.
825
+ * @returns The granted permissions and request metadata.
826
+ */
827
+ requestPermissionsIncremental(subject: PermissionSubjectMetadata, requestedPermissions: RequestedPermissions, options?: {
828
+ id?: string;
829
+ metadata?: Record<string, Json>;
830
+ }): Promise<[
831
+ Partial<SubjectPermissions<ExtractPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>>>,
832
+ ApprovedPermissionsMetadata
833
+ ] | []>;
834
+ /**
835
+ * Validates requested permissions. Throws if validation fails.
836
+ *
837
+ * This method ensures that the requested permissions are a properly
838
+ * formatted {@link RequestedPermissions} object, and performs the same
839
+ * validation as {@link PermissionController.grantPermissions}, except that
840
+ * consumer-specified permission validator functions are not called, since
841
+ * they are only called on fully constructed, approved permissions that are
842
+ * otherwise completely valid.
843
+ *
844
+ * Unrecognzied properties on requested permissions are ignored.
845
+ *
846
+ * @param origin - The origin of the grantee subject.
847
+ * @param requestedPermissions - The requested permissions.
848
+ */
849
+ private validateRequestedPermissions;
850
+ /**
851
+ * Adds a request to the {@link ApprovalController} using the
852
+ * {@link AddApprovalRequest} action. Also validates the resulting approved
853
+ * permissions request, and throws an error if validation fails.
854
+ *
855
+ * @param permissionsRequest - The permissions request object.
856
+ * @returns The approved permissions request object.
857
+ */
858
+ private requestUserApproval;
859
+ /**
860
+ * Reunites all the side-effects (onPermitted and onFailure) of the requested permissions inside a record of arrays.
861
+ *
862
+ * @param permissions - The approved permissions.
863
+ * @returns The {@link SideEffects} object containing the handlers arrays.
864
+ */
865
+ private getSideEffects;
866
+ /**
867
+ * Executes the side-effects of the approved permissions while handling the errors if any.
868
+ * It will pass an instance of the {@link messagingSystem} and the request data associated with the permission request to the handlers through its params.
869
+ *
870
+ * @param sideEffects - the side-effect record created by {@link getSideEffects}
871
+ * @param requestData - the permissions requestData.
872
+ * @returns the value returned by all the `onPermitted` handlers in an array.
873
+ */
874
+ private executeSideEffects;
875
+ /**
876
+ * Validates an approved {@link PermissionsRequest} object. The approved
877
+ * request must have the required `metadata` and `permissions` properties,
878
+ * the `id` and `origin` of the `metadata` must match the original request
879
+ * metadata, and the requested permissions must be valid per
880
+ * {@link PermissionController.validateRequestedPermissions}. Any extra
881
+ * metadata properties are ignored.
882
+ *
883
+ * An error is thrown if validation fails.
884
+ *
885
+ * @param approvedRequest - The approved permissions request object.
886
+ * @param originalMetadata - The original request metadata.
887
+ */
888
+ private validateApprovedPermissions;
889
+ /**
890
+ * Accepts a permissions request created by
891
+ * {@link PermissionController.requestPermissions}.
892
+ *
893
+ * @param request - The permissions request.
894
+ */
895
+ acceptPermissionsRequest(request: PermissionsRequest): Promise<void>;
896
+ /**
897
+ * Rejects a permissions request created by
898
+ * {@link PermissionController.requestPermissions}.
899
+ *
900
+ * @param id - The id of the request to be rejected.
901
+ */
902
+ rejectPermissionsRequest(id: string): Promise<void>;
903
+ /**
904
+ * Checks whether the {@link ApprovalController} has a particular permissions
905
+ * request.
906
+ *
907
+ * @see {@link PermissionController.acceptPermissionsRequest} and
908
+ * {@link PermissionController.rejectPermissionsRequest} for usage.
909
+ * @param options - The {@link HasApprovalRequest} options.
910
+ * @param options.id - The id of the approval request to check for.
911
+ * @returns Whether the specified request exists.
912
+ */
913
+ private hasApprovalRequest;
914
+ /**
915
+ * Rejects the permissions request with the specified id, with the specified
916
+ * error as the reason. This method is effectively a wrapper around a
917
+ * messenger call for the `ApprovalController:rejectRequest` action.
918
+ *
919
+ * @see {@link PermissionController.acceptPermissionsRequest} and
920
+ * {@link PermissionController.rejectPermissionsRequest} for usage.
921
+ * @param id - The id of the request to reject.
922
+ * @param error - The error associated with the rejection.
923
+ * @returns Nothing
924
+ */
925
+ private _rejectPermissionsRequest;
926
+ /**
927
+ * Gets the subject's endowments per the specified endowment permission.
928
+ * Throws if the subject does not have the required permission or if the
929
+ * permission is not an endowment permission.
930
+ *
931
+ * @param origin - The origin of the subject whose endowments to retrieve.
932
+ * @param targetName - The name of the endowment permission. This must be a
933
+ * valid permission target name.
934
+ * @param requestData - Additional data associated with the request, if any.
935
+ * Forwarded to the endowment getter function for the permission.
936
+ * @returns The endowments, if any.
937
+ */
938
+ getEndowments(origin: string, targetName: ExtractEndowmentPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability'], requestData?: unknown): Promise<Json>;
939
+ /**
940
+ * Executes a restricted method as the subject with the given origin.
941
+ * The specified params, if any, will be passed to the method implementation.
942
+ *
943
+ * ATTN: Great caution should be exercised in the use of this method.
944
+ * Methods that cause side effects or affect application state should
945
+ * be avoided.
946
+ *
947
+ * This method will first attempt to retrieve the requested restricted method
948
+ * implementation, throwing if it does not exist. The method will then be
949
+ * invoked as though the subject with the specified origin had invoked it with
950
+ * the specified parameters. This means that any existing caveats will be
951
+ * applied to the restricted method, and this method will throw if the
952
+ * restricted method or its caveat decorators throw.
953
+ *
954
+ * In addition, this method will throw if the subject does not have a
955
+ * permission for the specified restricted method.
956
+ *
957
+ * @param origin - The origin of the subject to execute the method on behalf
958
+ * of.
959
+ * @param targetName - The name of the method to execute. This must be a valid
960
+ * permission target name.
961
+ * @param params - The parameters to pass to the method implementation.
962
+ * @returns The result of the executed method.
963
+ */
964
+ executeRestrictedMethod(origin: OriginString, targetName: ExtractRestrictedMethodPermission<ControllerPermissionSpecification, ControllerCaveatSpecification>['parentCapability'], params?: RestrictedMethodParameters): Promise<Json>;
965
+ /**
966
+ * An internal method used in the controller's `json-rpc-engine` middleware
967
+ * and {@link PermissionController.executeRestrictedMethod}. Calls the
968
+ * specified restricted method implementation after decorating it with the
969
+ * caveats of its permission. Throws if the subject does not have the
970
+ * requisite permission.
971
+ *
972
+ * ATTN: Parameter validation is the responsibility of the caller, or
973
+ * the restricted method implementation in the case of `params`.
974
+ *
975
+ * @see {@link PermissionController.executeRestrictedMethod} and
976
+ * {@link PermissionController.createPermissionMiddleware} for usage.
977
+ * @param methodImplementation - The implementation of the method to call.
978
+ * @param subject - Metadata about the subject that made the request.
979
+ * @param method - The method name
980
+ * @param params - Params needed for executing the restricted method
981
+ * @returns The result of the restricted method implementation
982
+ */
983
+ private _executeRestrictedMethod;
984
+ }
985
+ export {};
986
+ //# sourceMappingURL=PermissionController.d.mts.map