@metamask/permission-controller 12.2.1 → 13.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/CHANGELOG.md +85 -6
  2. package/dist/Permission.cjs.map +1 -1
  3. package/dist/Permission.d.cts +9 -15
  4. package/dist/Permission.d.cts.map +1 -1
  5. package/dist/Permission.d.mts +9 -15
  6. package/dist/Permission.d.mts.map +1 -1
  7. package/dist/Permission.mjs.map +1 -1
  8. package/dist/PermissionController-method-action-types.cjs +7 -0
  9. package/dist/PermissionController-method-action-types.cjs.map +1 -0
  10. package/dist/PermissionController-method-action-types.d.cts +301 -0
  11. package/dist/PermissionController-method-action-types.d.cts.map +1 -0
  12. package/dist/PermissionController-method-action-types.d.mts +301 -0
  13. package/dist/PermissionController-method-action-types.d.mts.map +1 -0
  14. package/dist/PermissionController-method-action-types.mjs +6 -0
  15. package/dist/PermissionController-method-action-types.mjs.map +1 -0
  16. package/dist/PermissionController.cjs +41 -45
  17. package/dist/PermissionController.cjs.map +1 -1
  18. package/dist/PermissionController.d.cts +59 -33
  19. package/dist/PermissionController.d.cts.map +1 -1
  20. package/dist/PermissionController.d.mts +59 -33
  21. package/dist/PermissionController.d.mts.map +1 -1
  22. package/dist/PermissionController.mjs +41 -45
  23. package/dist/PermissionController.mjs.map +1 -1
  24. package/dist/SubjectMetadataController-method-action-types.cjs +7 -0
  25. package/dist/SubjectMetadataController-method-action-types.cjs.map +1 -0
  26. package/dist/SubjectMetadataController-method-action-types.d.cts +51 -0
  27. package/dist/SubjectMetadataController-method-action-types.d.cts.map +1 -0
  28. package/dist/SubjectMetadataController-method-action-types.d.mts +51 -0
  29. package/dist/SubjectMetadataController-method-action-types.d.mts.map +1 -0
  30. package/dist/SubjectMetadataController-method-action-types.mjs +6 -0
  31. package/dist/SubjectMetadataController-method-action-types.mjs.map +1 -0
  32. package/dist/SubjectMetadataController.cjs +7 -2
  33. package/dist/SubjectMetadataController.cjs.map +1 -1
  34. package/dist/SubjectMetadataController.d.cts +16 -4
  35. package/dist/SubjectMetadataController.d.cts.map +1 -1
  36. package/dist/SubjectMetadataController.d.mts +16 -4
  37. package/dist/SubjectMetadataController.d.mts.map +1 -1
  38. package/dist/SubjectMetadataController.mjs +7 -2
  39. package/dist/SubjectMetadataController.mjs.map +1 -1
  40. package/dist/createRestrictedMethodMessenger.cjs +20 -0
  41. package/dist/createRestrictedMethodMessenger.cjs.map +1 -0
  42. package/dist/createRestrictedMethodMessenger.d.cts +42 -0
  43. package/dist/createRestrictedMethodMessenger.d.cts.map +1 -0
  44. package/dist/createRestrictedMethodMessenger.d.mts +42 -0
  45. package/dist/createRestrictedMethodMessenger.d.mts.map +1 -0
  46. package/dist/createRestrictedMethodMessenger.mjs +16 -0
  47. package/dist/createRestrictedMethodMessenger.mjs.map +1 -0
  48. package/dist/index.cjs +6 -14
  49. package/dist/index.cjs.map +1 -1
  50. package/dist/index.d.cts +5 -2
  51. package/dist/index.d.cts.map +1 -1
  52. package/dist/index.d.mts +5 -2
  53. package/dist/index.d.mts.map +1 -1
  54. package/dist/index.mjs +2 -1
  55. package/dist/index.mjs.map +1 -1
  56. package/dist/permission-middleware.cjs +47 -43
  57. package/dist/permission-middleware.cjs.map +1 -1
  58. package/dist/permission-middleware.d.cts +40 -25
  59. package/dist/permission-middleware.d.cts.map +1 -1
  60. package/dist/permission-middleware.d.mts +40 -25
  61. package/dist/permission-middleware.d.mts.map +1 -1
  62. package/dist/permission-middleware.mjs +44 -41
  63. package/dist/permission-middleware.mjs.map +1 -1
  64. package/dist/utils.cjs.map +1 -1
  65. package/dist/utils.d.cts +0 -23
  66. package/dist/utils.d.cts.map +1 -1
  67. package/dist/utils.d.mts +0 -23
  68. package/dist/utils.d.mts.map +1 -1
  69. package/dist/utils.mjs.map +1 -1
  70. package/package.json +22 -19
  71. package/dist/rpc-methods/getPermissions.cjs +0 -27
  72. package/dist/rpc-methods/getPermissions.cjs.map +0 -1
  73. package/dist/rpc-methods/getPermissions.d.cts +0 -9
  74. package/dist/rpc-methods/getPermissions.d.cts.map +0 -1
  75. package/dist/rpc-methods/getPermissions.d.mts +0 -9
  76. package/dist/rpc-methods/getPermissions.d.mts.map +0 -1
  77. package/dist/rpc-methods/getPermissions.mjs +0 -24
  78. package/dist/rpc-methods/getPermissions.mjs.map +0 -1
  79. package/dist/rpc-methods/index.cjs +0 -12
  80. package/dist/rpc-methods/index.cjs.map +0 -1
  81. package/dist/rpc-methods/index.d.cts +0 -6
  82. package/dist/rpc-methods/index.d.cts.map +0 -1
  83. package/dist/rpc-methods/index.d.mts +0 -6
  84. package/dist/rpc-methods/index.d.mts.map +0 -1
  85. package/dist/rpc-methods/index.mjs +0 -9
  86. package/dist/rpc-methods/index.mjs.map +0 -1
  87. package/dist/rpc-methods/requestPermissions.cjs +0 -36
  88. package/dist/rpc-methods/requestPermissions.cjs.map +0 -1
  89. package/dist/rpc-methods/requestPermissions.d.cts +0 -17
  90. package/dist/rpc-methods/requestPermissions.d.cts.map +0 -1
  91. package/dist/rpc-methods/requestPermissions.d.mts +0 -17
  92. package/dist/rpc-methods/requestPermissions.d.mts.map +0 -1
  93. package/dist/rpc-methods/requestPermissions.mjs +0 -33
  94. package/dist/rpc-methods/requestPermissions.mjs.map +0 -1
  95. package/dist/rpc-methods/revokePermissions.cjs +0 -41
  96. package/dist/rpc-methods/revokePermissions.cjs.map +0 -1
  97. package/dist/rpc-methods/revokePermissions.d.cts +0 -11
  98. package/dist/rpc-methods/revokePermissions.d.cts.map +0 -1
  99. package/dist/rpc-methods/revokePermissions.d.mts +0 -11
  100. package/dist/rpc-methods/revokePermissions.d.mts.map +0 -1
  101. package/dist/rpc-methods/revokePermissions.mjs +0 -38
  102. package/dist/rpc-methods/revokePermissions.mjs.map +0 -1
package/dist/index.cjs CHANGED
@@ -10,29 +10,21 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
10
10
  if (k2 === undefined) k2 = k;
11
11
  o[k2] = m[k];
12
12
  }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
13
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
15
  };
21
- var __importStar = (this && this.__importStar) || function (mod) {
22
- if (mod && mod.__esModule) return mod;
23
- var result = {};
24
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25
- __setModuleDefault(result, mod);
26
- return result;
27
- };
28
16
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.permissionRpcMethods = exports.MethodNames = void 0;
17
+ exports.MethodNames = exports.createPermissionMiddlewareV2 = exports.createPermissionMiddleware = exports.createRestrictedMethodMessenger = void 0;
30
18
  __exportStar(require("./Caveat.cjs"), exports);
19
+ var createRestrictedMethodMessenger_1 = require("./createRestrictedMethodMessenger.cjs");
20
+ Object.defineProperty(exports, "createRestrictedMethodMessenger", { enumerable: true, get: function () { return createRestrictedMethodMessenger_1.createRestrictedMethodMessenger; } });
31
21
  __exportStar(require("./errors.cjs"), exports);
32
22
  __exportStar(require("./Permission.cjs"), exports);
33
23
  __exportStar(require("./PermissionController.cjs"), exports);
24
+ var permission_middleware_1 = require("./permission-middleware.cjs");
25
+ Object.defineProperty(exports, "createPermissionMiddleware", { enumerable: true, get: function () { return permission_middleware_1.createPermissionMiddleware; } });
26
+ Object.defineProperty(exports, "createPermissionMiddlewareV2", { enumerable: true, get: function () { return permission_middleware_1.createPermissionMiddlewareV2; } });
34
27
  var utils_1 = require("./utils.cjs");
35
28
  Object.defineProperty(exports, "MethodNames", { enumerable: true, get: function () { return utils_1.MethodNames; } });
36
- exports.permissionRpcMethods = __importStar(require("./rpc-methods/index.cjs"));
37
29
  __exportStar(require("./SubjectMetadataController.cjs"), exports);
38
30
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAyB;AACzB,+CAAyB;AACzB,mDAA6B;AAC7B,6DAAuC;AAOvC,qCAAsC;AAA7B,oGAAA,WAAW,OAAA;AACpB,gFAAsD;AACtD,kEAA4C","sourcesContent":["export * from './Caveat';\nexport * from './errors';\nexport * from './Permission';\nexport * from './PermissionController';\nexport type {\n ExtractSpecifications,\n HandlerMiddlewareFunction,\n HookNames,\n PermittedHandlerExport,\n} from './utils';\nexport { MethodNames } from './utils';\nexport * as permissionRpcMethods from './rpc-methods';\nexport * from './SubjectMetadataController';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,+CAAyB;AACzB,yFAAoF;AAA3E,kJAAA,+BAA+B,OAAA;AACxC,+CAAyB;AACzB,mDAA6B;AAC7B,6DAAuC;AAoBvC,qEAIiC;AAH/B,mIAAA,0BAA0B,OAAA;AAC1B,qIAAA,4BAA4B,OAAA;AAI9B,qCAAsC;AAA7B,oGAAA,WAAW,OAAA;AACpB,kEAA4C","sourcesContent":["export * from './Caveat';\nexport { createRestrictedMethodMessenger } from './createRestrictedMethodMessenger';\nexport * from './errors';\nexport * from './Permission';\nexport * from './PermissionController';\nexport type {\n PermissionControllerClearStateAction,\n PermissionControllerExecuteRestrictedMethodAction,\n PermissionControllerGetCaveatAction,\n PermissionControllerGetEndowmentsAction,\n PermissionControllerGetPermissionsAction,\n PermissionControllerGetSubjectNamesAction,\n PermissionControllerGrantPermissionsAction,\n PermissionControllerGrantPermissionsIncrementalAction,\n PermissionControllerHasPermissionAction,\n PermissionControllerHasPermissionsAction,\n PermissionControllerHasUnrestrictedMethodAction,\n PermissionControllerRequestPermissionsAction,\n PermissionControllerRequestPermissionsIncrementalAction,\n PermissionControllerRevokeAllPermissionsAction,\n PermissionControllerRevokePermissionForAllSubjectsAction,\n PermissionControllerRevokePermissionsAction,\n PermissionControllerUpdateCaveatAction,\n} from './PermissionController-method-action-types';\nexport {\n createPermissionMiddleware,\n createPermissionMiddlewareV2,\n type PermissionMiddlewareActions,\n} from './permission-middleware';\nexport type { ExtractSpecifications } from './utils';\nexport { MethodNames } from './utils';\nexport * from './SubjectMetadataController';\nexport type {\n SubjectMetadataControllerClearStateAction,\n SubjectMetadataControllerAddSubjectMetadataAction,\n SubjectMetadataControllerGetSubjectMetadataAction,\n SubjectMetadataControllerTrimMetadataStateAction,\n} from './SubjectMetadataController-method-action-types';\n"]}
package/dist/index.d.cts CHANGED
@@ -1,9 +1,12 @@
1
1
  export * from "./Caveat.cjs";
2
+ export { createRestrictedMethodMessenger } from "./createRestrictedMethodMessenger.cjs";
2
3
  export * from "./errors.cjs";
3
4
  export * from "./Permission.cjs";
4
5
  export * from "./PermissionController.cjs";
5
- export type { ExtractSpecifications, HandlerMiddlewareFunction, HookNames, PermittedHandlerExport, } from "./utils.cjs";
6
+ export type { PermissionControllerClearStateAction, PermissionControllerExecuteRestrictedMethodAction, PermissionControllerGetCaveatAction, PermissionControllerGetEndowmentsAction, PermissionControllerGetPermissionsAction, PermissionControllerGetSubjectNamesAction, PermissionControllerGrantPermissionsAction, PermissionControllerGrantPermissionsIncrementalAction, PermissionControllerHasPermissionAction, PermissionControllerHasPermissionsAction, PermissionControllerHasUnrestrictedMethodAction, PermissionControllerRequestPermissionsAction, PermissionControllerRequestPermissionsIncrementalAction, PermissionControllerRevokeAllPermissionsAction, PermissionControllerRevokePermissionForAllSubjectsAction, PermissionControllerRevokePermissionsAction, PermissionControllerUpdateCaveatAction, } from "./PermissionController-method-action-types.cjs";
7
+ export { createPermissionMiddleware, createPermissionMiddlewareV2, type PermissionMiddlewareActions, } from "./permission-middleware.cjs";
8
+ export type { ExtractSpecifications } from "./utils.cjs";
6
9
  export { MethodNames } from "./utils.cjs";
7
- export * as permissionRpcMethods from "./rpc-methods/index.cjs";
8
10
  export * from "./SubjectMetadataController.cjs";
11
+ export type { SubjectMetadataControllerClearStateAction, SubjectMetadataControllerAddSubjectMetadataAction, SubjectMetadataControllerGetSubjectMetadataAction, SubjectMetadataControllerTrimMetadataStateAction, } from "./SubjectMetadataController-method-action-types.cjs";
9
12
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,iCAA6B;AAC7B,2CAAuC;AACvC,YAAY,EACV,qBAAqB,EACrB,yBAAyB,EACzB,SAAS,EACT,sBAAsB,GACvB,oBAAgB;AACjB,OAAO,EAAE,WAAW,EAAE,oBAAgB;AACtC,OAAO,KAAK,oBAAoB,gCAAsB;AACtD,gDAA4C"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,OAAO,EAAE,+BAA+B,EAAE,8CAA0C;AACpF,6BAAyB;AACzB,iCAA6B;AAC7B,2CAAuC;AACvC,YAAY,EACV,oCAAoC,EACpC,iDAAiD,EACjD,mCAAmC,EACnC,uCAAuC,EACvC,wCAAwC,EACxC,yCAAyC,EACzC,0CAA0C,EAC1C,qDAAqD,EACrD,uCAAuC,EACvC,wCAAwC,EACxC,+CAA+C,EAC/C,4CAA4C,EAC5C,uDAAuD,EACvD,8CAA8C,EAC9C,wDAAwD,EACxD,2CAA2C,EAC3C,sCAAsC,GACvC,uDAAmD;AACpD,OAAO,EACL,0BAA0B,EAC1B,4BAA4B,EAC5B,KAAK,2BAA2B,GACjC,oCAAgC;AACjC,YAAY,EAAE,qBAAqB,EAAE,oBAAgB;AACrD,OAAO,EAAE,WAAW,EAAE,oBAAgB;AACtC,gDAA4C;AAC5C,YAAY,EACV,yCAAyC,EACzC,iDAAiD,EACjD,iDAAiD,EACjD,gDAAgD,GACjD,4DAAwD"}
package/dist/index.d.mts CHANGED
@@ -1,9 +1,12 @@
1
1
  export * from "./Caveat.mjs";
2
+ export { createRestrictedMethodMessenger } from "./createRestrictedMethodMessenger.mjs";
2
3
  export * from "./errors.mjs";
3
4
  export * from "./Permission.mjs";
4
5
  export * from "./PermissionController.mjs";
5
- export type { ExtractSpecifications, HandlerMiddlewareFunction, HookNames, PermittedHandlerExport, } from "./utils.mjs";
6
+ export type { PermissionControllerClearStateAction, PermissionControllerExecuteRestrictedMethodAction, PermissionControllerGetCaveatAction, PermissionControllerGetEndowmentsAction, PermissionControllerGetPermissionsAction, PermissionControllerGetSubjectNamesAction, PermissionControllerGrantPermissionsAction, PermissionControllerGrantPermissionsIncrementalAction, PermissionControllerHasPermissionAction, PermissionControllerHasPermissionsAction, PermissionControllerHasUnrestrictedMethodAction, PermissionControllerRequestPermissionsAction, PermissionControllerRequestPermissionsIncrementalAction, PermissionControllerRevokeAllPermissionsAction, PermissionControllerRevokePermissionForAllSubjectsAction, PermissionControllerRevokePermissionsAction, PermissionControllerUpdateCaveatAction, } from "./PermissionController-method-action-types.mjs";
7
+ export { createPermissionMiddleware, createPermissionMiddlewareV2, type PermissionMiddlewareActions, } from "./permission-middleware.mjs";
8
+ export type { ExtractSpecifications } from "./utils.mjs";
6
9
  export { MethodNames } from "./utils.mjs";
7
- export * as permissionRpcMethods from "./rpc-methods/index.mjs";
8
10
  export * from "./SubjectMetadataController.mjs";
11
+ export type { SubjectMetadataControllerClearStateAction, SubjectMetadataControllerAddSubjectMetadataAction, SubjectMetadataControllerGetSubjectMetadataAction, SubjectMetadataControllerTrimMetadataStateAction, } from "./SubjectMetadataController-method-action-types.mjs";
9
12
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,iCAA6B;AAC7B,2CAAuC;AACvC,YAAY,EACV,qBAAqB,EACrB,yBAAyB,EACzB,SAAS,EACT,sBAAsB,GACvB,oBAAgB;AACjB,OAAO,EAAE,WAAW,EAAE,oBAAgB;AACtC,OAAO,KAAK,oBAAoB,gCAAsB;AACtD,gDAA4C"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,OAAO,EAAE,+BAA+B,EAAE,8CAA0C;AACpF,6BAAyB;AACzB,iCAA6B;AAC7B,2CAAuC;AACvC,YAAY,EACV,oCAAoC,EACpC,iDAAiD,EACjD,mCAAmC,EACnC,uCAAuC,EACvC,wCAAwC,EACxC,yCAAyC,EACzC,0CAA0C,EAC1C,qDAAqD,EACrD,uCAAuC,EACvC,wCAAwC,EACxC,+CAA+C,EAC/C,4CAA4C,EAC5C,uDAAuD,EACvD,8CAA8C,EAC9C,wDAAwD,EACxD,2CAA2C,EAC3C,sCAAsC,GACvC,uDAAmD;AACpD,OAAO,EACL,0BAA0B,EAC1B,4BAA4B,EAC5B,KAAK,2BAA2B,GACjC,oCAAgC;AACjC,YAAY,EAAE,qBAAqB,EAAE,oBAAgB;AACrD,OAAO,EAAE,WAAW,EAAE,oBAAgB;AACtC,gDAA4C;AAC5C,YAAY,EACV,yCAAyC,EACzC,iDAAiD,EACjD,iDAAiD,EACjD,gDAAgD,GACjD,4DAAwD"}
package/dist/index.mjs CHANGED
@@ -1,8 +1,9 @@
1
1
  export * from "./Caveat.mjs";
2
+ export { createRestrictedMethodMessenger } from "./createRestrictedMethodMessenger.mjs";
2
3
  export * from "./errors.mjs";
3
4
  export * from "./Permission.mjs";
4
5
  export * from "./PermissionController.mjs";
6
+ export { createPermissionMiddleware, createPermissionMiddlewareV2 } from "./permission-middleware.mjs";
5
7
  export { MethodNames } from "./utils.mjs";
6
- export * as permissionRpcMethods from "./rpc-methods/index.mjs";
7
8
  export * from "./SubjectMetadataController.mjs";
8
9
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,6BAAyB;AACzB,iCAA6B;AAC7B,2CAAuC;AAOvC,OAAO,EAAE,WAAW,EAAE,oBAAgB;AACtC,OAAO,KAAK,oBAAoB,gCAAsB;AACtD,gDAA4C","sourcesContent":["export * from './Caveat';\nexport * from './errors';\nexport * from './Permission';\nexport * from './PermissionController';\nexport type {\n ExtractSpecifications,\n HandlerMiddlewareFunction,\n HookNames,\n PermittedHandlerExport,\n} from './utils';\nexport { MethodNames } from './utils';\nexport * as permissionRpcMethods from './rpc-methods';\nexport * from './SubjectMetadataController';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6BAAyB;AACzB,OAAO,EAAE,+BAA+B,EAAE,8CAA0C;AACpF,6BAAyB;AACzB,iCAA6B;AAC7B,2CAAuC;AAoBvC,OAAO,EACL,0BAA0B,EAC1B,4BAA4B,EAE7B,oCAAgC;AAEjC,OAAO,EAAE,WAAW,EAAE,oBAAgB;AACtC,gDAA4C","sourcesContent":["export * from './Caveat';\nexport { createRestrictedMethodMessenger } from './createRestrictedMethodMessenger';\nexport * from './errors';\nexport * from './Permission';\nexport * from './PermissionController';\nexport type {\n PermissionControllerClearStateAction,\n PermissionControllerExecuteRestrictedMethodAction,\n PermissionControllerGetCaveatAction,\n PermissionControllerGetEndowmentsAction,\n PermissionControllerGetPermissionsAction,\n PermissionControllerGetSubjectNamesAction,\n PermissionControllerGrantPermissionsAction,\n PermissionControllerGrantPermissionsIncrementalAction,\n PermissionControllerHasPermissionAction,\n PermissionControllerHasPermissionsAction,\n PermissionControllerHasUnrestrictedMethodAction,\n PermissionControllerRequestPermissionsAction,\n PermissionControllerRequestPermissionsIncrementalAction,\n PermissionControllerRevokeAllPermissionsAction,\n PermissionControllerRevokePermissionForAllSubjectsAction,\n PermissionControllerRevokePermissionsAction,\n PermissionControllerUpdateCaveatAction,\n} from './PermissionController-method-action-types';\nexport {\n createPermissionMiddleware,\n createPermissionMiddlewareV2,\n type PermissionMiddlewareActions,\n} from './permission-middleware';\nexport type { ExtractSpecifications } from './utils';\nexport { MethodNames } from './utils';\nexport * from './SubjectMetadataController';\nexport type {\n SubjectMetadataControllerClearStateAction,\n SubjectMetadataControllerAddSubjectMetadataAction,\n SubjectMetadataControllerGetSubjectMetadataAction,\n SubjectMetadataControllerTrimMetadataStateAction,\n} from './SubjectMetadataController-method-action-types';\n"]}
@@ -1,55 +1,59 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPermissionMiddlewareFactory = void 0;
3
+ exports.createPermissionMiddlewareV2 = exports.createPermissionMiddleware = void 0;
4
4
  const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
5
- const errors_1 = require("./errors.cjs");
6
5
  /**
7
- * Creates a permission middleware function factory. Intended for internal use
8
- * in the {@link PermissionController}. Like any {@link JsonRpcEngine}
9
- * middleware, each middleware will only receive requests from a particular
10
- * subject / origin. However, each middleware also requires access to some
11
- * `PermissionController` internals, which is why this "factory factory" exists.
6
+ * Creates a JSON-RPC middleware that enforces permissions for a single subject.
12
7
  *
13
- * The middlewares returned by the factory will pass through requests for
14
- * unrestricted methods, and attempt to execute restricted methods. If a method
15
- * is neither restricted nor unrestricted, a "method not found" error will be
16
- * returned.
17
- * If a method is restricted, the middleware will first attempt to retrieve the
18
- * subject's permission for that method. If the permission is found, the method
19
- * will be executed. Otherwise, an "unauthorized" error will be returned.
8
+ * The middleware passes through unrestricted methods, and otherwise dispatches
9
+ * restricted methods to the `PermissionController` via messenger actions. If
10
+ * the subject lacks the required permission, or if the method does not exist,
11
+ * the corresponding error is propagated to the JSON-RPC response.
20
12
  *
13
+ * @deprecated Use {@link createPermissionMiddlewareV2} with `JsonRpcEngineV2`.
21
14
  * @param options - Options bag.
22
- * @param options.executeRestrictedMethod - `PermissionController.executeRestrictedMethod`.
23
- * @param options.getRestrictedMethod - {@link PermissionController.getRestrictedMethod}.
24
- * @param options.isUnrestrictedMethod - A function that checks whether a
25
- * particular method is unrestricted.
26
- * @returns A permission middleware factory function.
15
+ * @param options.messenger - A messenger with the
16
+ * `PermissionController:executeRestrictedMethod` and
17
+ * `PermissionController:hasUnrestrictedMethod` actions.
18
+ * @param options.origin - The origin of the subject for which to create the middleware.
19
+ * @returns A `json-rpc-engine` middleware.
27
20
  */
28
- function getPermissionMiddlewareFactory({ executeRestrictedMethod, getRestrictedMethod, isUnrestrictedMethod, }) {
29
- return function createPermissionMiddleware(subject) {
30
- const { origin } = subject;
31
- if (typeof origin !== 'string' || !origin) {
32
- throw new Error('The subject "origin" must be a non-empty string.');
21
+ function createPermissionMiddleware({ messenger, origin, }) {
22
+ const permissionsMiddleware = async (request, response, next) => {
23
+ const { method, params } = request;
24
+ if (messenger.call('PermissionController:hasUnrestrictedMethod', method)) {
25
+ return next();
33
26
  }
34
- const permissionsMiddleware = async (req, res, next) => {
35
- const { method, params } = req;
36
- // Skip registered unrestricted methods.
37
- if (isUnrestrictedMethod(method)) {
38
- return next();
39
- }
40
- // This will throw if no restricted method implementation is found.
41
- const methodImplementation = getRestrictedMethod(method, origin);
42
- // This will throw if the permission does not exist.
43
- const result = await executeRestrictedMethod(methodImplementation, subject, method, params);
44
- if (result === undefined) {
45
- res.error = (0, errors_1.internalError)(`Request for method "${req.method}" returned undefined result.`, { request: req });
46
- return undefined;
47
- }
48
- res.result = result;
49
- return undefined;
50
- };
51
- return (0, json_rpc_engine_1.createAsyncMiddleware)(permissionsMiddleware);
27
+ response.result = await messenger.call('PermissionController:executeRestrictedMethod', origin, method, params);
28
+ return undefined;
52
29
  };
30
+ return (0, json_rpc_engine_1.createAsyncMiddleware)(permissionsMiddleware);
53
31
  }
54
- exports.getPermissionMiddlewareFactory = getPermissionMiddlewareFactory;
32
+ exports.createPermissionMiddleware = createPermissionMiddleware;
33
+ /**
34
+ * Creates a `JsonRpcEngineV2` middleware that enforces permissions for a
35
+ * single subject.
36
+ *
37
+ * The middleware passes through unrestricted methods, and otherwise dispatches
38
+ * restricted methods to the `PermissionController` via messenger actions. If
39
+ * the subject lacks the required permission, or if the method does not exist,
40
+ * the corresponding error is thrown.
41
+ *
42
+ * @param options - Options bag.
43
+ * @param options.messenger - A messenger with the
44
+ * `PermissionController:executeRestrictedMethod` and
45
+ * `PermissionController:hasUnrestrictedMethod` actions.
46
+ * @param options.origin - The origin of the subject for which to create the middleware.
47
+ * @returns A `JsonRpcEngineV2` middleware.
48
+ */
49
+ function createPermissionMiddlewareV2({ messenger, origin, }) {
50
+ return async ({ request, next }) => {
51
+ const { method, params } = request;
52
+ if (messenger.call('PermissionController:hasUnrestrictedMethod', method)) {
53
+ return next();
54
+ }
55
+ return messenger.call('PermissionController:executeRestrictedMethod', origin, method, params);
56
+ };
57
+ }
58
+ exports.createPermissionMiddlewareV2 = createPermissionMiddlewareV2;
55
59
  //# sourceMappingURL=permission-middleware.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"permission-middleware.cjs","sourceRoot":"","sources":["../src/permission-middleware.ts"],"names":[],"mappings":";;;AAAA,+DAAkE;AAmBlE,yCAAyC;AAiBzC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,8BAA8B,CAAC,EAC7C,uBAAuB,EACvB,mBAAmB,EACnB,oBAAoB,GACe;IACnC,OAAO,SAAS,0BAA0B,CACxC,OAAkC;QAElC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAC3B,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,MAAM,EAAE,CAAC;YAC1C,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;QACtE,CAAC;QAED,MAAM,qBAAqB,GAAG,KAAK,EACjC,GAA+C,EAC/C,GAA2B,EAC3B,IAAoC,EACrB,EAAE;YACjB,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;YAE/B,wCAAwC;YACxC,IAAI,oBAAoB,CAAC,MAAM,CAAC,EAAE,CAAC;gBACjC,OAAO,IAAI,EAAE,CAAC;YAChB,CAAC;YAED,mEAAmE;YACnE,MAAM,oBAAoB,GAAG,mBAAmB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;YAEjE,oDAAoD;YACpD,MAAM,MAAM,GAAG,MAAM,uBAAuB,CAC1C,oBAAoB,EACpB,OAAO,EACP,MAAM,EACN,MAAM,CACP,CAAC;YAEF,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBACzB,GAAG,CAAC,KAAK,GAAG,IAAA,sBAAa,EACvB,uBAAuB,GAAG,CAAC,MAAM,8BAA8B,EAC/D,EAAE,OAAO,EAAE,GAAG,EAAE,CACjB,CAAC;gBACF,OAAO,SAAS,CAAC;YACnB,CAAC;YAED,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC;YACpB,OAAO,SAAS,CAAC;QACnB,CAAC,CAAC;QAEF,OAAO,IAAA,uCAAqB,EAAC,qBAAqB,CAAC,CAAC;IACtD,CAAC,CAAC;AACJ,CAAC;AAlDD,wEAkDC","sourcesContent":["import { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport type {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n JsonRpcEngine,\n JsonRpcMiddleware,\n AsyncJsonRpcEngineNextCallback,\n} from '@metamask/json-rpc-engine';\nimport type {\n Json,\n PendingJsonRpcResponse,\n JsonRpcRequest,\n} from '@metamask/utils';\n\nimport type {\n GenericPermissionController,\n PermissionSubjectMetadata,\n RestrictedMethod,\n RestrictedMethodParameters,\n} from '.';\nimport { internalError } from './errors';\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { PermissionController } from './PermissionController';\n\ntype ExecuteRestrictedMethod = (\n methodImplementation: RestrictedMethod<RestrictedMethodParameters, Json>,\n subject: PermissionSubjectMetadata,\n method: string,\n params?: RestrictedMethodParameters,\n) => ReturnType<RestrictedMethod<RestrictedMethodParameters, Json>>;\n\ntype PermissionMiddlewareFactoryOptions = {\n executeRestrictedMethod: ExecuteRestrictedMethod;\n getRestrictedMethod: GenericPermissionController['getRestrictedMethod'];\n isUnrestrictedMethod: (method: string) => boolean;\n};\n\n/**\n * Creates a permission middleware function factory. Intended for internal use\n * in the {@link PermissionController}. Like any {@link JsonRpcEngine}\n * middleware, each middleware will only receive requests from a particular\n * subject / origin. However, each middleware also requires access to some\n * `PermissionController` internals, which is why this \"factory factory\" exists.\n *\n * The middlewares returned by the factory will pass through requests for\n * unrestricted methods, and attempt to execute restricted methods. If a method\n * is neither restricted nor unrestricted, a \"method not found\" error will be\n * returned.\n * If a method is restricted, the middleware will first attempt to retrieve the\n * subject's permission for that method. If the permission is found, the method\n * will be executed. Otherwise, an \"unauthorized\" error will be returned.\n *\n * @param options - Options bag.\n * @param options.executeRestrictedMethod - `PermissionController.executeRestrictedMethod`.\n * @param options.getRestrictedMethod - {@link PermissionController.getRestrictedMethod}.\n * @param options.isUnrestrictedMethod - A function that checks whether a\n * particular method is unrestricted.\n * @returns A permission middleware factory function.\n */\nexport function getPermissionMiddlewareFactory({\n executeRestrictedMethod,\n getRestrictedMethod,\n isUnrestrictedMethod,\n}: PermissionMiddlewareFactoryOptions) {\n return function createPermissionMiddleware(\n subject: PermissionSubjectMetadata,\n ): JsonRpcMiddleware<RestrictedMethodParameters, Json> {\n const { origin } = subject;\n if (typeof origin !== 'string' || !origin) {\n throw new Error('The subject \"origin\" must be a non-empty string.');\n }\n\n const permissionsMiddleware = async (\n req: JsonRpcRequest<RestrictedMethodParameters>,\n res: PendingJsonRpcResponse,\n next: AsyncJsonRpcEngineNextCallback,\n ): Promise<void> => {\n const { method, params } = req;\n\n // Skip registered unrestricted methods.\n if (isUnrestrictedMethod(method)) {\n return next();\n }\n\n // This will throw if no restricted method implementation is found.\n const methodImplementation = getRestrictedMethod(method, origin);\n\n // This will throw if the permission does not exist.\n const result = await executeRestrictedMethod(\n methodImplementation,\n subject,\n method,\n params,\n );\n\n if (result === undefined) {\n res.error = internalError(\n `Request for method \"${req.method}\" returned undefined result.`,\n { request: req },\n );\n return undefined;\n }\n\n res.result = result;\n return undefined;\n };\n\n return createAsyncMiddleware(permissionsMiddleware);\n };\n}\n"]}
1
+ {"version":3,"file":"permission-middleware.cjs","sourceRoot":"","sources":["../src/permission-middleware.ts"],"names":[],"mappings":";;;AAAA,+DAAkE;AA+BlE;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,0BAA0B,CAAC,EACzC,SAAS,EACT,MAAM,GAC4B;IAIlC,MAAM,qBAAqB,GAAG,KAAK,EACjC,OAAmD,EACnD,QAAgC,EAChC,IAAoC,EACrB,EAAE;QACjB,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAEnC,IAAI,SAAS,CAAC,IAAI,CAAC,4CAA4C,EAAE,MAAM,CAAC,EAAE,CAAC;YACzE,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,QAAQ,CAAC,MAAM,GAAG,MAAM,SAAS,CAAC,IAAI,CACpC,8CAA8C,EAC9C,MAAM,EACN,MAAM,EACN,MAAM,CACP,CAAC;QACF,OAAO,SAAS,CAAC;IACnB,CAAC,CAAC;IAEF,OAAO,IAAA,uCAAqB,EAAC,qBAAqB,CAAC,CAAC;AACtD,CAAC;AA5BD,gEA4BC;AAED;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,4BAA4B,CAAC,EAC3C,SAAS,EACT,MAAM,GAC4B;IAClC,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACjC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAEnC,IAAI,SAAS,CAAC,IAAI,CAAC,4CAA4C,EAAE,MAAM,CAAC,EAAE,CAAC;YACzE,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,OAAO,SAAS,CAAC,IAAI,CACnB,8CAA8C,EAC9C,MAAM,EACN,MAAM,EACN,MAAM,CACP,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAlBD,oEAkBC","sourcesContent":["import { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport type {\n AsyncJsonRpcEngineNextCallback,\n JsonRpcMiddleware,\n} from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware as JsonRpcMiddlewareV2 } from '@metamask/json-rpc-engine/v2';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nimport type { RestrictedMethodParameters } from './Permission';\nimport type {\n PermissionControllerExecuteRestrictedMethodAction,\n PermissionControllerHasUnrestrictedMethodAction,\n} from './PermissionController-method-action-types';\n\n/**\n * The set of messenger actions required by the permission middleware.\n */\nexport type PermissionMiddlewareActions =\n | PermissionControllerExecuteRestrictedMethodAction\n | PermissionControllerHasUnrestrictedMethodAction;\n\nexport type CreatePermissionMiddlewareOptions = {\n messenger: Messenger<string, PermissionMiddlewareActions>;\n origin: string;\n};\n\n/**\n * Creates a JSON-RPC middleware that enforces permissions for a single subject.\n *\n * The middleware passes through unrestricted methods, and otherwise dispatches\n * restricted methods to the `PermissionController` via messenger actions. If\n * the subject lacks the required permission, or if the method does not exist,\n * the corresponding error is propagated to the JSON-RPC response.\n *\n * @deprecated Use {@link createPermissionMiddlewareV2} with `JsonRpcEngineV2`.\n * @param options - Options bag.\n * @param options.messenger - A messenger with the\n * `PermissionController:executeRestrictedMethod` and\n * `PermissionController:hasUnrestrictedMethod` actions.\n * @param options.origin - The origin of the subject for which to create the middleware.\n * @returns A `json-rpc-engine` middleware.\n */\nexport function createPermissionMiddleware({\n messenger,\n origin,\n}: CreatePermissionMiddlewareOptions): JsonRpcMiddleware<\n RestrictedMethodParameters,\n Json\n> {\n const permissionsMiddleware = async (\n request: JsonRpcRequest<RestrictedMethodParameters>,\n response: PendingJsonRpcResponse,\n next: AsyncJsonRpcEngineNextCallback,\n ): Promise<void> => {\n const { method, params } = request;\n\n if (messenger.call('PermissionController:hasUnrestrictedMethod', method)) {\n return next();\n }\n\n response.result = await messenger.call(\n 'PermissionController:executeRestrictedMethod',\n origin,\n method,\n params,\n );\n return undefined;\n };\n\n return createAsyncMiddleware(permissionsMiddleware);\n}\n\n/**\n * Creates a `JsonRpcEngineV2` middleware that enforces permissions for a\n * single subject.\n *\n * The middleware passes through unrestricted methods, and otherwise dispatches\n * restricted methods to the `PermissionController` via messenger actions. If\n * the subject lacks the required permission, or if the method does not exist,\n * the corresponding error is thrown.\n *\n * @param options - Options bag.\n * @param options.messenger - A messenger with the\n * `PermissionController:executeRestrictedMethod` and\n * `PermissionController:hasUnrestrictedMethod` actions.\n * @param options.origin - The origin of the subject for which to create the middleware.\n * @returns A `JsonRpcEngineV2` middleware.\n */\nexport function createPermissionMiddlewareV2({\n messenger,\n origin,\n}: CreatePermissionMiddlewareOptions): JsonRpcMiddlewareV2 {\n return async ({ request, next }) => {\n const { method, params } = request;\n\n if (messenger.call('PermissionController:hasUnrestrictedMethod', method)) {\n return next();\n }\n\n return messenger.call(\n 'PermissionController:executeRestrictedMethod',\n origin,\n method,\n params,\n );\n };\n}\n"]}
@@ -1,34 +1,49 @@
1
1
  import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
2
+ import type { JsonRpcMiddleware as JsonRpcMiddlewareV2 } from "@metamask/json-rpc-engine/v2";
3
+ import type { Messenger } from "@metamask/messenger";
2
4
  import type { Json } from "@metamask/utils";
3
- import type { GenericPermissionController, PermissionSubjectMetadata, RestrictedMethod, RestrictedMethodParameters } from "./index.cjs";
4
- type ExecuteRestrictedMethod = (methodImplementation: RestrictedMethod<RestrictedMethodParameters, Json>, subject: PermissionSubjectMetadata, method: string, params?: RestrictedMethodParameters) => ReturnType<RestrictedMethod<RestrictedMethodParameters, Json>>;
5
- type PermissionMiddlewareFactoryOptions = {
6
- executeRestrictedMethod: ExecuteRestrictedMethod;
7
- getRestrictedMethod: GenericPermissionController['getRestrictedMethod'];
8
- isUnrestrictedMethod: (method: string) => boolean;
5
+ import type { RestrictedMethodParameters } from "./Permission.cjs";
6
+ import type { PermissionControllerExecuteRestrictedMethodAction, PermissionControllerHasUnrestrictedMethodAction } from "./PermissionController-method-action-types.cjs";
7
+ /**
8
+ * The set of messenger actions required by the permission middleware.
9
+ */
10
+ export type PermissionMiddlewareActions = PermissionControllerExecuteRestrictedMethodAction | PermissionControllerHasUnrestrictedMethodAction;
11
+ export type CreatePermissionMiddlewareOptions = {
12
+ messenger: Messenger<string, PermissionMiddlewareActions>;
13
+ origin: string;
9
14
  };
10
15
  /**
11
- * Creates a permission middleware function factory. Intended for internal use
12
- * in the {@link PermissionController}. Like any {@link JsonRpcEngine}
13
- * middleware, each middleware will only receive requests from a particular
14
- * subject / origin. However, each middleware also requires access to some
15
- * `PermissionController` internals, which is why this "factory factory" exists.
16
+ * Creates a JSON-RPC middleware that enforces permissions for a single subject.
17
+ *
18
+ * The middleware passes through unrestricted methods, and otherwise dispatches
19
+ * restricted methods to the `PermissionController` via messenger actions. If
20
+ * the subject lacks the required permission, or if the method does not exist,
21
+ * the corresponding error is propagated to the JSON-RPC response.
22
+ *
23
+ * @deprecated Use {@link createPermissionMiddlewareV2} with `JsonRpcEngineV2`.
24
+ * @param options - Options bag.
25
+ * @param options.messenger - A messenger with the
26
+ * `PermissionController:executeRestrictedMethod` and
27
+ * `PermissionController:hasUnrestrictedMethod` actions.
28
+ * @param options.origin - The origin of the subject for which to create the middleware.
29
+ * @returns A `json-rpc-engine` middleware.
30
+ */
31
+ export declare function createPermissionMiddleware({ messenger, origin, }: CreatePermissionMiddlewareOptions): JsonRpcMiddleware<RestrictedMethodParameters, Json>;
32
+ /**
33
+ * Creates a `JsonRpcEngineV2` middleware that enforces permissions for a
34
+ * single subject.
16
35
  *
17
- * The middlewares returned by the factory will pass through requests for
18
- * unrestricted methods, and attempt to execute restricted methods. If a method
19
- * is neither restricted nor unrestricted, a "method not found" error will be
20
- * returned.
21
- * If a method is restricted, the middleware will first attempt to retrieve the
22
- * subject's permission for that method. If the permission is found, the method
23
- * will be executed. Otherwise, an "unauthorized" error will be returned.
36
+ * The middleware passes through unrestricted methods, and otherwise dispatches
37
+ * restricted methods to the `PermissionController` via messenger actions. If
38
+ * the subject lacks the required permission, or if the method does not exist,
39
+ * the corresponding error is thrown.
24
40
  *
25
41
  * @param options - Options bag.
26
- * @param options.executeRestrictedMethod - `PermissionController.executeRestrictedMethod`.
27
- * @param options.getRestrictedMethod - {@link PermissionController.getRestrictedMethod}.
28
- * @param options.isUnrestrictedMethod - A function that checks whether a
29
- * particular method is unrestricted.
30
- * @returns A permission middleware factory function.
42
+ * @param options.messenger - A messenger with the
43
+ * `PermissionController:executeRestrictedMethod` and
44
+ * `PermissionController:hasUnrestrictedMethod` actions.
45
+ * @param options.origin - The origin of the subject for which to create the middleware.
46
+ * @returns A `JsonRpcEngineV2` middleware.
31
47
  */
32
- export declare function getPermissionMiddlewareFactory({ executeRestrictedMethod, getRestrictedMethod, isUnrestrictedMethod, }: PermissionMiddlewareFactoryOptions): (subject: PermissionSubjectMetadata) => JsonRpcMiddleware<RestrictedMethodParameters, Json>;
33
- export {};
48
+ export declare function createPermissionMiddlewareV2({ messenger, origin, }: CreatePermissionMiddlewareOptions): JsonRpcMiddlewareV2;
34
49
  //# sourceMappingURL=permission-middleware.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"permission-middleware.d.cts","sourceRoot":"","sources":["../src/permission-middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAGV,iBAAiB,EAElB,kCAAkC;AACnC,OAAO,KAAK,EACV,IAAI,EAGL,wBAAwB;AAEzB,OAAO,KAAK,EACV,2BAA2B,EAC3B,yBAAyB,EACzB,gBAAgB,EAChB,0BAA0B,EAC3B,oBAAU;AAKX,KAAK,uBAAuB,GAAG,CAC7B,oBAAoB,EAAE,gBAAgB,CAAC,0BAA0B,EAAE,IAAI,CAAC,EACxE,OAAO,EAAE,yBAAyB,EAClC,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,0BAA0B,KAChC,UAAU,CAAC,gBAAgB,CAAC,0BAA0B,EAAE,IAAI,CAAC,CAAC,CAAC;AAEpE,KAAK,kCAAkC,GAAG;IACxC,uBAAuB,EAAE,uBAAuB,CAAC;IACjD,mBAAmB,EAAE,2BAA2B,CAAC,qBAAqB,CAAC,CAAC;IACxE,oBAAoB,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC;CACnD,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,8BAA8B,CAAC,EAC7C,uBAAuB,EACvB,mBAAmB,EACnB,oBAAoB,GACrB,EAAE,kCAAkC,aAExB,yBAAyB,KACjC,kBAAkB,0BAA0B,EAAE,IAAI,CAAC,CA2CvD"}
1
+ {"version":3,"file":"permission-middleware.d.cts","sourceRoot":"","sources":["../src/permission-middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAEV,iBAAiB,EAClB,kCAAkC;AACnC,OAAO,KAAK,EAAE,iBAAiB,IAAI,mBAAmB,EAAE,qCAAqC;AAC7F,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EACV,IAAI,EAGL,wBAAwB;AAEzB,OAAO,KAAK,EAAE,0BAA0B,EAAE,yBAAqB;AAC/D,OAAO,KAAK,EACV,iDAAiD,EACjD,+CAA+C,EAChD,uDAAmD;AAEpD;;GAEG;AACH,MAAM,MAAM,2BAA2B,GACnC,iDAAiD,GACjD,+CAA+C,CAAC;AAEpD,MAAM,MAAM,iCAAiC,GAAG;IAC9C,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,2BAA2B,CAAC,CAAC;IAC1D,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,0BAA0B,CAAC,EACzC,SAAS,EACT,MAAM,GACP,EAAE,iCAAiC,GAAG,iBAAiB,CACtD,0BAA0B,EAC1B,IAAI,CACL,CAsBA;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,4BAA4B,CAAC,EAC3C,SAAS,EACT,MAAM,GACP,EAAE,iCAAiC,GAAG,mBAAmB,CAezD"}
@@ -1,34 +1,49 @@
1
1
  import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
2
+ import type { JsonRpcMiddleware as JsonRpcMiddlewareV2 } from "@metamask/json-rpc-engine/v2";
3
+ import type { Messenger } from "@metamask/messenger";
2
4
  import type { Json } from "@metamask/utils";
3
- import type { GenericPermissionController, PermissionSubjectMetadata, RestrictedMethod, RestrictedMethodParameters } from "./index.mjs";
4
- type ExecuteRestrictedMethod = (methodImplementation: RestrictedMethod<RestrictedMethodParameters, Json>, subject: PermissionSubjectMetadata, method: string, params?: RestrictedMethodParameters) => ReturnType<RestrictedMethod<RestrictedMethodParameters, Json>>;
5
- type PermissionMiddlewareFactoryOptions = {
6
- executeRestrictedMethod: ExecuteRestrictedMethod;
7
- getRestrictedMethod: GenericPermissionController['getRestrictedMethod'];
8
- isUnrestrictedMethod: (method: string) => boolean;
5
+ import type { RestrictedMethodParameters } from "./Permission.mjs";
6
+ import type { PermissionControllerExecuteRestrictedMethodAction, PermissionControllerHasUnrestrictedMethodAction } from "./PermissionController-method-action-types.mjs";
7
+ /**
8
+ * The set of messenger actions required by the permission middleware.
9
+ */
10
+ export type PermissionMiddlewareActions = PermissionControllerExecuteRestrictedMethodAction | PermissionControllerHasUnrestrictedMethodAction;
11
+ export type CreatePermissionMiddlewareOptions = {
12
+ messenger: Messenger<string, PermissionMiddlewareActions>;
13
+ origin: string;
9
14
  };
10
15
  /**
11
- * Creates a permission middleware function factory. Intended for internal use
12
- * in the {@link PermissionController}. Like any {@link JsonRpcEngine}
13
- * middleware, each middleware will only receive requests from a particular
14
- * subject / origin. However, each middleware also requires access to some
15
- * `PermissionController` internals, which is why this "factory factory" exists.
16
+ * Creates a JSON-RPC middleware that enforces permissions for a single subject.
17
+ *
18
+ * The middleware passes through unrestricted methods, and otherwise dispatches
19
+ * restricted methods to the `PermissionController` via messenger actions. If
20
+ * the subject lacks the required permission, or if the method does not exist,
21
+ * the corresponding error is propagated to the JSON-RPC response.
22
+ *
23
+ * @deprecated Use {@link createPermissionMiddlewareV2} with `JsonRpcEngineV2`.
24
+ * @param options - Options bag.
25
+ * @param options.messenger - A messenger with the
26
+ * `PermissionController:executeRestrictedMethod` and
27
+ * `PermissionController:hasUnrestrictedMethod` actions.
28
+ * @param options.origin - The origin of the subject for which to create the middleware.
29
+ * @returns A `json-rpc-engine` middleware.
30
+ */
31
+ export declare function createPermissionMiddleware({ messenger, origin, }: CreatePermissionMiddlewareOptions): JsonRpcMiddleware<RestrictedMethodParameters, Json>;
32
+ /**
33
+ * Creates a `JsonRpcEngineV2` middleware that enforces permissions for a
34
+ * single subject.
16
35
  *
17
- * The middlewares returned by the factory will pass through requests for
18
- * unrestricted methods, and attempt to execute restricted methods. If a method
19
- * is neither restricted nor unrestricted, a "method not found" error will be
20
- * returned.
21
- * If a method is restricted, the middleware will first attempt to retrieve the
22
- * subject's permission for that method. If the permission is found, the method
23
- * will be executed. Otherwise, an "unauthorized" error will be returned.
36
+ * The middleware passes through unrestricted methods, and otherwise dispatches
37
+ * restricted methods to the `PermissionController` via messenger actions. If
38
+ * the subject lacks the required permission, or if the method does not exist,
39
+ * the corresponding error is thrown.
24
40
  *
25
41
  * @param options - Options bag.
26
- * @param options.executeRestrictedMethod - `PermissionController.executeRestrictedMethod`.
27
- * @param options.getRestrictedMethod - {@link PermissionController.getRestrictedMethod}.
28
- * @param options.isUnrestrictedMethod - A function that checks whether a
29
- * particular method is unrestricted.
30
- * @returns A permission middleware factory function.
42
+ * @param options.messenger - A messenger with the
43
+ * `PermissionController:executeRestrictedMethod` and
44
+ * `PermissionController:hasUnrestrictedMethod` actions.
45
+ * @param options.origin - The origin of the subject for which to create the middleware.
46
+ * @returns A `JsonRpcEngineV2` middleware.
31
47
  */
32
- export declare function getPermissionMiddlewareFactory({ executeRestrictedMethod, getRestrictedMethod, isUnrestrictedMethod, }: PermissionMiddlewareFactoryOptions): (subject: PermissionSubjectMetadata) => JsonRpcMiddleware<RestrictedMethodParameters, Json>;
33
- export {};
48
+ export declare function createPermissionMiddlewareV2({ messenger, origin, }: CreatePermissionMiddlewareOptions): JsonRpcMiddlewareV2;
34
49
  //# sourceMappingURL=permission-middleware.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"permission-middleware.d.mts","sourceRoot":"","sources":["../src/permission-middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAGV,iBAAiB,EAElB,kCAAkC;AACnC,OAAO,KAAK,EACV,IAAI,EAGL,wBAAwB;AAEzB,OAAO,KAAK,EACV,2BAA2B,EAC3B,yBAAyB,EACzB,gBAAgB,EAChB,0BAA0B,EAC3B,oBAAU;AAKX,KAAK,uBAAuB,GAAG,CAC7B,oBAAoB,EAAE,gBAAgB,CAAC,0BAA0B,EAAE,IAAI,CAAC,EACxE,OAAO,EAAE,yBAAyB,EAClC,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,0BAA0B,KAChC,UAAU,CAAC,gBAAgB,CAAC,0BAA0B,EAAE,IAAI,CAAC,CAAC,CAAC;AAEpE,KAAK,kCAAkC,GAAG;IACxC,uBAAuB,EAAE,uBAAuB,CAAC;IACjD,mBAAmB,EAAE,2BAA2B,CAAC,qBAAqB,CAAC,CAAC;IACxE,oBAAoB,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC;CACnD,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,8BAA8B,CAAC,EAC7C,uBAAuB,EACvB,mBAAmB,EACnB,oBAAoB,GACrB,EAAE,kCAAkC,aAExB,yBAAyB,KACjC,kBAAkB,0BAA0B,EAAE,IAAI,CAAC,CA2CvD"}
1
+ {"version":3,"file":"permission-middleware.d.mts","sourceRoot":"","sources":["../src/permission-middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAEV,iBAAiB,EAClB,kCAAkC;AACnC,OAAO,KAAK,EAAE,iBAAiB,IAAI,mBAAmB,EAAE,qCAAqC;AAC7F,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EACV,IAAI,EAGL,wBAAwB;AAEzB,OAAO,KAAK,EAAE,0BAA0B,EAAE,yBAAqB;AAC/D,OAAO,KAAK,EACV,iDAAiD,EACjD,+CAA+C,EAChD,uDAAmD;AAEpD;;GAEG;AACH,MAAM,MAAM,2BAA2B,GACnC,iDAAiD,GACjD,+CAA+C,CAAC;AAEpD,MAAM,MAAM,iCAAiC,GAAG;IAC9C,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,2BAA2B,CAAC,CAAC;IAC1D,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,0BAA0B,CAAC,EACzC,SAAS,EACT,MAAM,GACP,EAAE,iCAAiC,GAAG,iBAAiB,CACtD,0BAA0B,EAC1B,IAAI,CACL,CAsBA;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,4BAA4B,CAAC,EAC3C,SAAS,EACT,MAAM,GACP,EAAE,iCAAiC,GAAG,mBAAmB,CAezD"}
@@ -1,51 +1,54 @@
1
1
  import { createAsyncMiddleware } from "@metamask/json-rpc-engine";
2
- import { internalError } from "./errors.mjs";
3
2
  /**
4
- * Creates a permission middleware function factory. Intended for internal use
5
- * in the {@link PermissionController}. Like any {@link JsonRpcEngine}
6
- * middleware, each middleware will only receive requests from a particular
7
- * subject / origin. However, each middleware also requires access to some
8
- * `PermissionController` internals, which is why this "factory factory" exists.
3
+ * Creates a JSON-RPC middleware that enforces permissions for a single subject.
9
4
  *
10
- * The middlewares returned by the factory will pass through requests for
11
- * unrestricted methods, and attempt to execute restricted methods. If a method
12
- * is neither restricted nor unrestricted, a "method not found" error will be
13
- * returned.
14
- * If a method is restricted, the middleware will first attempt to retrieve the
15
- * subject's permission for that method. If the permission is found, the method
16
- * will be executed. Otherwise, an "unauthorized" error will be returned.
5
+ * The middleware passes through unrestricted methods, and otherwise dispatches
6
+ * restricted methods to the `PermissionController` via messenger actions. If
7
+ * the subject lacks the required permission, or if the method does not exist,
8
+ * the corresponding error is propagated to the JSON-RPC response.
17
9
  *
10
+ * @deprecated Use {@link createPermissionMiddlewareV2} with `JsonRpcEngineV2`.
18
11
  * @param options - Options bag.
19
- * @param options.executeRestrictedMethod - `PermissionController.executeRestrictedMethod`.
20
- * @param options.getRestrictedMethod - {@link PermissionController.getRestrictedMethod}.
21
- * @param options.isUnrestrictedMethod - A function that checks whether a
22
- * particular method is unrestricted.
23
- * @returns A permission middleware factory function.
12
+ * @param options.messenger - A messenger with the
13
+ * `PermissionController:executeRestrictedMethod` and
14
+ * `PermissionController:hasUnrestrictedMethod` actions.
15
+ * @param options.origin - The origin of the subject for which to create the middleware.
16
+ * @returns A `json-rpc-engine` middleware.
24
17
  */
25
- export function getPermissionMiddlewareFactory({ executeRestrictedMethod, getRestrictedMethod, isUnrestrictedMethod, }) {
26
- return function createPermissionMiddleware(subject) {
27
- const { origin } = subject;
28
- if (typeof origin !== 'string' || !origin) {
29
- throw new Error('The subject "origin" must be a non-empty string.');
18
+ export function createPermissionMiddleware({ messenger, origin, }) {
19
+ const permissionsMiddleware = async (request, response, next) => {
20
+ const { method, params } = request;
21
+ if (messenger.call('PermissionController:hasUnrestrictedMethod', method)) {
22
+ return next();
30
23
  }
31
- const permissionsMiddleware = async (req, res, next) => {
32
- const { method, params } = req;
33
- // Skip registered unrestricted methods.
34
- if (isUnrestrictedMethod(method)) {
35
- return next();
36
- }
37
- // This will throw if no restricted method implementation is found.
38
- const methodImplementation = getRestrictedMethod(method, origin);
39
- // This will throw if the permission does not exist.
40
- const result = await executeRestrictedMethod(methodImplementation, subject, method, params);
41
- if (result === undefined) {
42
- res.error = internalError(`Request for method "${req.method}" returned undefined result.`, { request: req });
43
- return undefined;
44
- }
45
- res.result = result;
46
- return undefined;
47
- };
48
- return createAsyncMiddleware(permissionsMiddleware);
24
+ response.result = await messenger.call('PermissionController:executeRestrictedMethod', origin, method, params);
25
+ return undefined;
26
+ };
27
+ return createAsyncMiddleware(permissionsMiddleware);
28
+ }
29
+ /**
30
+ * Creates a `JsonRpcEngineV2` middleware that enforces permissions for a
31
+ * single subject.
32
+ *
33
+ * The middleware passes through unrestricted methods, and otherwise dispatches
34
+ * restricted methods to the `PermissionController` via messenger actions. If
35
+ * the subject lacks the required permission, or if the method does not exist,
36
+ * the corresponding error is thrown.
37
+ *
38
+ * @param options - Options bag.
39
+ * @param options.messenger - A messenger with the
40
+ * `PermissionController:executeRestrictedMethod` and
41
+ * `PermissionController:hasUnrestrictedMethod` actions.
42
+ * @param options.origin - The origin of the subject for which to create the middleware.
43
+ * @returns A `JsonRpcEngineV2` middleware.
44
+ */
45
+ export function createPermissionMiddlewareV2({ messenger, origin, }) {
46
+ return async ({ request, next }) => {
47
+ const { method, params } = request;
48
+ if (messenger.call('PermissionController:hasUnrestrictedMethod', method)) {
49
+ return next();
50
+ }
51
+ return messenger.call('PermissionController:executeRestrictedMethod', origin, method, params);
49
52
  };
50
53
  }
51
54
  //# sourceMappingURL=permission-middleware.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"permission-middleware.mjs","sourceRoot":"","sources":["../src/permission-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,kCAAkC;AAmBlE,OAAO,EAAE,aAAa,EAAE,qBAAiB;AAiBzC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,8BAA8B,CAAC,EAC7C,uBAAuB,EACvB,mBAAmB,EACnB,oBAAoB,GACe;IACnC,OAAO,SAAS,0BAA0B,CACxC,OAAkC;QAElC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAC3B,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,MAAM,EAAE,CAAC;YAC1C,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;QACtE,CAAC;QAED,MAAM,qBAAqB,GAAG,KAAK,EACjC,GAA+C,EAC/C,GAA2B,EAC3B,IAAoC,EACrB,EAAE;YACjB,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;YAE/B,wCAAwC;YACxC,IAAI,oBAAoB,CAAC,MAAM,CAAC,EAAE,CAAC;gBACjC,OAAO,IAAI,EAAE,CAAC;YAChB,CAAC;YAED,mEAAmE;YACnE,MAAM,oBAAoB,GAAG,mBAAmB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;YAEjE,oDAAoD;YACpD,MAAM,MAAM,GAAG,MAAM,uBAAuB,CAC1C,oBAAoB,EACpB,OAAO,EACP,MAAM,EACN,MAAM,CACP,CAAC;YAEF,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBACzB,GAAG,CAAC,KAAK,GAAG,aAAa,CACvB,uBAAuB,GAAG,CAAC,MAAM,8BAA8B,EAC/D,EAAE,OAAO,EAAE,GAAG,EAAE,CACjB,CAAC;gBACF,OAAO,SAAS,CAAC;YACnB,CAAC;YAED,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC;YACpB,OAAO,SAAS,CAAC;QACnB,CAAC,CAAC;QAEF,OAAO,qBAAqB,CAAC,qBAAqB,CAAC,CAAC;IACtD,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport type {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n JsonRpcEngine,\n JsonRpcMiddleware,\n AsyncJsonRpcEngineNextCallback,\n} from '@metamask/json-rpc-engine';\nimport type {\n Json,\n PendingJsonRpcResponse,\n JsonRpcRequest,\n} from '@metamask/utils';\n\nimport type {\n GenericPermissionController,\n PermissionSubjectMetadata,\n RestrictedMethod,\n RestrictedMethodParameters,\n} from '.';\nimport { internalError } from './errors';\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { PermissionController } from './PermissionController';\n\ntype ExecuteRestrictedMethod = (\n methodImplementation: RestrictedMethod<RestrictedMethodParameters, Json>,\n subject: PermissionSubjectMetadata,\n method: string,\n params?: RestrictedMethodParameters,\n) => ReturnType<RestrictedMethod<RestrictedMethodParameters, Json>>;\n\ntype PermissionMiddlewareFactoryOptions = {\n executeRestrictedMethod: ExecuteRestrictedMethod;\n getRestrictedMethod: GenericPermissionController['getRestrictedMethod'];\n isUnrestrictedMethod: (method: string) => boolean;\n};\n\n/**\n * Creates a permission middleware function factory. Intended for internal use\n * in the {@link PermissionController}. Like any {@link JsonRpcEngine}\n * middleware, each middleware will only receive requests from a particular\n * subject / origin. However, each middleware also requires access to some\n * `PermissionController` internals, which is why this \"factory factory\" exists.\n *\n * The middlewares returned by the factory will pass through requests for\n * unrestricted methods, and attempt to execute restricted methods. If a method\n * is neither restricted nor unrestricted, a \"method not found\" error will be\n * returned.\n * If a method is restricted, the middleware will first attempt to retrieve the\n * subject's permission for that method. If the permission is found, the method\n * will be executed. Otherwise, an \"unauthorized\" error will be returned.\n *\n * @param options - Options bag.\n * @param options.executeRestrictedMethod - `PermissionController.executeRestrictedMethod`.\n * @param options.getRestrictedMethod - {@link PermissionController.getRestrictedMethod}.\n * @param options.isUnrestrictedMethod - A function that checks whether a\n * particular method is unrestricted.\n * @returns A permission middleware factory function.\n */\nexport function getPermissionMiddlewareFactory({\n executeRestrictedMethod,\n getRestrictedMethod,\n isUnrestrictedMethod,\n}: PermissionMiddlewareFactoryOptions) {\n return function createPermissionMiddleware(\n subject: PermissionSubjectMetadata,\n ): JsonRpcMiddleware<RestrictedMethodParameters, Json> {\n const { origin } = subject;\n if (typeof origin !== 'string' || !origin) {\n throw new Error('The subject \"origin\" must be a non-empty string.');\n }\n\n const permissionsMiddleware = async (\n req: JsonRpcRequest<RestrictedMethodParameters>,\n res: PendingJsonRpcResponse,\n next: AsyncJsonRpcEngineNextCallback,\n ): Promise<void> => {\n const { method, params } = req;\n\n // Skip registered unrestricted methods.\n if (isUnrestrictedMethod(method)) {\n return next();\n }\n\n // This will throw if no restricted method implementation is found.\n const methodImplementation = getRestrictedMethod(method, origin);\n\n // This will throw if the permission does not exist.\n const result = await executeRestrictedMethod(\n methodImplementation,\n subject,\n method,\n params,\n );\n\n if (result === undefined) {\n res.error = internalError(\n `Request for method \"${req.method}\" returned undefined result.`,\n { request: req },\n );\n return undefined;\n }\n\n res.result = result;\n return undefined;\n };\n\n return createAsyncMiddleware(permissionsMiddleware);\n };\n}\n"]}
1
+ {"version":3,"file":"permission-middleware.mjs","sourceRoot":"","sources":["../src/permission-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,kCAAkC;AA+BlE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,0BAA0B,CAAC,EACzC,SAAS,EACT,MAAM,GAC4B;IAIlC,MAAM,qBAAqB,GAAG,KAAK,EACjC,OAAmD,EACnD,QAAgC,EAChC,IAAoC,EACrB,EAAE;QACjB,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAEnC,IAAI,SAAS,CAAC,IAAI,CAAC,4CAA4C,EAAE,MAAM,CAAC,EAAE,CAAC;YACzE,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,QAAQ,CAAC,MAAM,GAAG,MAAM,SAAS,CAAC,IAAI,CACpC,8CAA8C,EAC9C,MAAM,EACN,MAAM,EACN,MAAM,CACP,CAAC;QACF,OAAO,SAAS,CAAC;IACnB,CAAC,CAAC;IAEF,OAAO,qBAAqB,CAAC,qBAAqB,CAAC,CAAC;AACtD,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,4BAA4B,CAAC,EAC3C,SAAS,EACT,MAAM,GAC4B;IAClC,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACjC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAEnC,IAAI,SAAS,CAAC,IAAI,CAAC,4CAA4C,EAAE,MAAM,CAAC,EAAE,CAAC;YACzE,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,OAAO,SAAS,CAAC,IAAI,CACnB,8CAA8C,EAC9C,MAAM,EACN,MAAM,EACN,MAAM,CACP,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport type {\n AsyncJsonRpcEngineNextCallback,\n JsonRpcMiddleware,\n} from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware as JsonRpcMiddlewareV2 } from '@metamask/json-rpc-engine/v2';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n Json,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nimport type { RestrictedMethodParameters } from './Permission';\nimport type {\n PermissionControllerExecuteRestrictedMethodAction,\n PermissionControllerHasUnrestrictedMethodAction,\n} from './PermissionController-method-action-types';\n\n/**\n * The set of messenger actions required by the permission middleware.\n */\nexport type PermissionMiddlewareActions =\n | PermissionControllerExecuteRestrictedMethodAction\n | PermissionControllerHasUnrestrictedMethodAction;\n\nexport type CreatePermissionMiddlewareOptions = {\n messenger: Messenger<string, PermissionMiddlewareActions>;\n origin: string;\n};\n\n/**\n * Creates a JSON-RPC middleware that enforces permissions for a single subject.\n *\n * The middleware passes through unrestricted methods, and otherwise dispatches\n * restricted methods to the `PermissionController` via messenger actions. If\n * the subject lacks the required permission, or if the method does not exist,\n * the corresponding error is propagated to the JSON-RPC response.\n *\n * @deprecated Use {@link createPermissionMiddlewareV2} with `JsonRpcEngineV2`.\n * @param options - Options bag.\n * @param options.messenger - A messenger with the\n * `PermissionController:executeRestrictedMethod` and\n * `PermissionController:hasUnrestrictedMethod` actions.\n * @param options.origin - The origin of the subject for which to create the middleware.\n * @returns A `json-rpc-engine` middleware.\n */\nexport function createPermissionMiddleware({\n messenger,\n origin,\n}: CreatePermissionMiddlewareOptions): JsonRpcMiddleware<\n RestrictedMethodParameters,\n Json\n> {\n const permissionsMiddleware = async (\n request: JsonRpcRequest<RestrictedMethodParameters>,\n response: PendingJsonRpcResponse,\n next: AsyncJsonRpcEngineNextCallback,\n ): Promise<void> => {\n const { method, params } = request;\n\n if (messenger.call('PermissionController:hasUnrestrictedMethod', method)) {\n return next();\n }\n\n response.result = await messenger.call(\n 'PermissionController:executeRestrictedMethod',\n origin,\n method,\n params,\n );\n return undefined;\n };\n\n return createAsyncMiddleware(permissionsMiddleware);\n}\n\n/**\n * Creates a `JsonRpcEngineV2` middleware that enforces permissions for a\n * single subject.\n *\n * The middleware passes through unrestricted methods, and otherwise dispatches\n * restricted methods to the `PermissionController` via messenger actions. If\n * the subject lacks the required permission, or if the method does not exist,\n * the corresponding error is thrown.\n *\n * @param options - Options bag.\n * @param options.messenger - A messenger with the\n * `PermissionController:executeRestrictedMethod` and\n * `PermissionController:hasUnrestrictedMethod` actions.\n * @param options.origin - The origin of the subject for which to create the middleware.\n * @returns A `JsonRpcEngineV2` middleware.\n */\nexport function createPermissionMiddlewareV2({\n messenger,\n origin,\n}: CreatePermissionMiddlewareOptions): JsonRpcMiddlewareV2 {\n return async ({ request, next }) => {\n const { method, params } = request;\n\n if (messenger.call('PermissionController:hasUnrestrictedMethod', method)) {\n return next();\n }\n\n return messenger.call(\n 'PermissionController:executeRestrictedMethod',\n origin,\n method,\n params,\n );\n };\n}\n"]}