Skip to content

Commit

Permalink
Add PermissionController.revokePermissionForAllSubjects action (#764)
Browse files Browse the repository at this point in the history
Adds a controller messenger action for `PermissionController.revokePermissionForAllSubjects`. We currently call the underlying function directly in the extension, but we'd like to encapsulate that call in the `SnapController`, which is where it should occur.
  • Loading branch information
rekmarks authored Apr 7, 2022
1 parent f4404bc commit 3391089
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
40 changes: 40 additions & 0 deletions src/permissions/PermissionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4452,6 +4452,46 @@ describe('PermissionController', () => {
expect(revokeAllPermissionsSpy).toHaveBeenNthCalledWith(1, 'foo');
});

it('action: PermissionController:revokePermissionForAllSubjects', () => {
const messenger = getUnrestrictedMessenger();
const options = getPermissionControllerOptions({
messenger: getPermissionControllerMessenger(messenger),
});
const controller = new PermissionController<
DefaultPermissionSpecifications,
DefaultCaveatSpecifications
>(options);

controller.grantPermissions({
subject: { origin: 'foo' },
approvedPermissions: {
wallet_getSecretArray: {},
},
});
const revokePermissionForAllSubjectsSpy = jest.spyOn(
controller,
'revokePermissionForAllSubjects',
);

expect(
controller.hasPermission('foo', 'wallet_getSecretArray'),
).toStrictEqual(true);

messenger.call(
'PermissionController:revokePermissionForAllSubjects',
'wallet_getSecretArray',
);

expect(
controller.hasPermission('foo', 'wallet_getSecretArray'),
).toStrictEqual(false);
expect(revokePermissionForAllSubjectsSpy).toHaveBeenCalledTimes(1);
expect(revokePermissionForAllSubjectsSpy).toHaveBeenNthCalledWith(
1,
'wallet_getSecretArray',
);
});

it('action: PermissionsController:requestPermissions', async () => {
const messenger = getUnrestrictedMessenger();
const options = getPermissionControllerOptions({
Expand Down
34 changes: 27 additions & 7 deletions src/permissions/PermissionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ export type RevokeAllPermissions = {
handler: GenericPermissionController['revokeAllPermissions'];
};

/**
* Revokes all permissions corresponding to the specified target for all subjects.
* Does nothing if no subjects or no such permission exists.
*/
export type RevokePermissionForAllSubjects = {
type: `${typeof controllerName}:revokePermissionForAllSubjects`;
handler: GenericPermissionController['revokePermissionForAllSubjects'];
};

/**
* Clears all permissions from the {@link PermissionController}.
*/
Expand Down Expand Up @@ -263,9 +272,10 @@ export type PermissionControllerActions =
| GetPermissions
| HasPermission
| HasPermissions
| RevokePermissions
| RequestPermissions
| RevokeAllPermissions
| RequestPermissions;
| RevokePermissionForAllSubjects
| RevokePermissions;

/**
* The generic state change event of the {@link PermissionController}.
Expand Down Expand Up @@ -681,8 +691,9 @@ export class PermissionController<
);

this.messagingSystem.registerActionHandler(
`${controllerName}:revokePermissions` as const,
this.revokePermissions.bind(this),
`${controllerName}:requestPermissions` as const,
(subject: PermissionSubjectMetadata, permissions: RequestedPermissions) =>
this.requestPermissions(subject, permissions),
);

this.messagingSystem.registerActionHandler(
Expand All @@ -691,9 +702,18 @@ export class PermissionController<
);

this.messagingSystem.registerActionHandler(
`${controllerName}:requestPermissions` as const,
(subject: PermissionSubjectMetadata, permissions: RequestedPermissions) =>
this.requestPermissions(subject, permissions),
`${controllerName}:revokePermissionForAllSubjects` as const,
(
target: ExtractPermission<
ControllerPermissionSpecification,
ControllerCaveatSpecification
>['parentCapability'],
) => this.revokePermissionForAllSubjects(target),
);

this.messagingSystem.registerActionHandler(
`${controllerName}:revokePermissions` as const,
this.revokePermissions.bind(this),
);
}

Expand Down

0 comments on commit 3391089

Please sign in to comment.