Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
41a23c9
add side-effects to permissions
GuillaumeRx Jan 18, 2023
d2bc24c
add missing origin parameter
GuillaumeRx Jan 18, 2023
07df8f9
add logic to handle failure and success of side effects
GuillaumeRx Feb 6, 2023
a63bf21
update error message
GuillaumeRx Feb 6, 2023
5f09017
move side effects handling to approval controller
GuillaumeRx Feb 7, 2023
1a9f796
add tests to approval controller and improve logic
GuillaumeRx Feb 8, 2023
87d6bda
fix failure logic
GuillaumeRx Feb 9, 2023
e496471
pass requestData to side-effect hooks and revert changes to grantPerm…
GuillaumeRx Feb 15, 2023
6d818df
revert more changes in PermissionController
GuillaumeRx Feb 15, 2023
bac36d8
remove useless type
GuillaumeRx Feb 15, 2023
018ff3d
re-add passing of side-effects to approvals
GuillaumeRx Feb 15, 2023
87a9ede
fix approval resolving
GuillaumeRx Feb 15, 2023
9810c15
add tests to PermissionController
GuillaumeRx Feb 16, 2023
c9542e9
adjust branch coverage threshold for permission controller
GuillaumeRx Feb 16, 2023
d3ffd3e
pass messenger call method to side effect handler params
GuillaumeRx Feb 21, 2023
8e54321
rework types
GuillaumeRx Feb 21, 2023
e5ed6dc
display error on failure
GuillaumeRx Feb 22, 2023
b3bf4fd
return values from side-effects
GuillaumeRx Mar 21, 2023
4c2ec64
add missing test
GuillaumeRx Mar 22, 2023
85dc902
pass entire messenger
GuillaumeRx Mar 22, 2023
6d56a40
update type
GuillaumeRx Mar 22, 2023
0ac9add
update side-effect messenger type
GuillaumeRx Mar 22, 2023
f046d83
address requested changes and impove sideEffect types
GuillaumeRx Mar 22, 2023
2442055
add missing parameter to `add` jsdoc
GuillaumeRx Mar 22, 2023
d051bad
move side-effect handling to `PermissionController`
GuillaumeRx Mar 23, 2023
009ad82
fixing tests
GuillaumeRx Mar 23, 2023
3a048ad
add tests
GuillaumeRx Mar 23, 2023
04f051d
revert useless change
GuillaumeRx Mar 23, 2023
75baa19
add JSDocs and address requested changes
GuillaumeRx Mar 23, 2023
13327bf
add more JSDocs
GuillaumeRx Mar 23, 2023
e229a9b
pass the entire approved request to side-effects
GuillaumeRx Mar 24, 2023
ff1dffb
fix side effect return mappin unexpected behaviour
GuillaumeRx Mar 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/permission-controller/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 99.6,
branches: 98.8,
functions: 100,
lines: 99.78,
statements: 99.78,
Expand Down
54 changes: 52 additions & 2 deletions packages/permission-controller/src/Permission.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Json } from '@metamask/types';
import { nanoid } from 'nanoid';
import { NonEmptyArray } from '@metamask/controller-utils';
import { ActionConstraint, EventConstraint } from '@metamask/base-controller';
import { CaveatConstraint } from './Caveat';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import type { PermissionController } from './PermissionController';

import type {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
PermissionController,
PermissionsRequest,
SideEffectMessenger,
} from './PermissionController';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import type { Caveat } from './Caveat';

Expand Down Expand Up @@ -349,6 +355,42 @@ export type PermissionValidatorConstraint = (
target?: string,
) => void;

/**
* The parameters passed to the side-effect function.
*/
export type SideEffectParams<
Actions extends ActionConstraint,
Events extends EventConstraint,
> = {
requestData: PermissionsRequest;
messagingSystem: SideEffectMessenger<Actions, Events>;
};

/**
* A function that will execute actions as a permission side-effect.
*/
export type SideEffectHandler<
Actions extends ActionConstraint,
Events extends EventConstraint,
> = (params: SideEffectParams<Actions, Events>) => Promise<unknown>;

/**
* The permissions side effects.
*/
export type PermissionSideEffect<
Actions extends ActionConstraint,
Events extends EventConstraint,
> = {
/**
* A method triggered when the permission is accepted by the user
*/
onPermitted: SideEffectHandler<Actions, Events>;
/**
* A method triggered if a `onPermitted` method rejected.
*/
onFailure?: SideEffectHandler<Actions, Events>;
};

/**
* A utility type for ensuring that the given permission target key conforms to
* our naming conventions.
Expand Down Expand Up @@ -432,6 +474,14 @@ type PermissionSpecificationBase<Type extends PermissionType> = {
* The validator should throw an appropriate JSON-RPC error if validation fails.
*/
validator?: PermissionValidatorConstraint;

/**
* The side-effect triggered by the {@link PermissionController} once the user approved it.
* The side-effect can only be an action allowed to be called inside the {@link PermissionController}.
*
* If the side-effect action fails, the permission that triggered it is revoked.
*/
sideEffect?: PermissionSideEffect<any, any>;
};

/**
Expand Down
Loading