Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added revokePermissions to PermissionController actions #708

Merged
merged 6 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
4 changes: 2 additions & 2 deletions src/permissions/PermissionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
HasApprovalRequest,
RejectRequest as RejectApprovalRequest,
} from '../approval/ApprovalController';
import { hasProperty, isPlainObject } from '../util';
import { Json } from '../BaseControllerV2';
import { ControllerMessenger } from '../ControllerMessenger';
import { hasProperty, isPlainObject } from '../util';
import * as errors from './errors';
import { EndowmentGetterParams } from './Permission';
import {
Expand Down Expand Up @@ -4408,7 +4408,7 @@ describe('PermissionController', () => {

expect(
Object.keys(
messenger.call('PermissionController:getPermissions', 'foo'),
messenger.call('PermissionController:getPermissions', 'foo') ?? {},
ritave marked this conversation as resolved.
Show resolved Hide resolved
),
).toStrictEqual(['wallet_getSecretArray']);

Expand Down
26 changes: 23 additions & 3 deletions src/permissions/PermissionController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-enable @typescript-eslint/no-unused-vars */
import deepFreeze from 'deep-freeze-strict';
import { Mutable } from '@metamask/types';
import deepFreeze from 'deep-freeze-strict';
import { castDraft, Draft, Patch } from 'immer';
import { nanoid } from 'nanoid';
import {
Expand All @@ -9,7 +9,7 @@ import {
HasApprovalRequest,
RejectRequest as RejectApprovalRequest,
} from '../approval/ApprovalController';
import { Json, BaseController, StateMetadata } from '../BaseControllerV2';
import { BaseController, Json, StateMetadata } from '../BaseControllerV2';
import { RestrictedControllerMessenger } from '../ControllerMessenger';
import {
hasProperty,
Expand Down Expand Up @@ -220,6 +220,14 @@ export type RequestPermissions = {
handler: GenericPermissionController['requestPermissions'];
};

/**
* Removes for each origin removes given permissions
ritave marked this conversation as resolved.
Show resolved Hide resolved
ritave marked this conversation as resolved.
Show resolved Hide resolved
*/
export type RevokePermissions = {
type: `${typeof controllerName}:revokePermissions`;
handler: GenericPermissionController['revokePermissions'];
};

/**
* Removes all permissions for a given origin
*/
Expand Down Expand Up @@ -255,6 +263,7 @@ export type PermissionControllerActions =
| GetPermissions
| HasPermission
| HasPermissions
| RevokePermissions
| RevokeAllPermissions
| RequestPermissions;

Expand Down Expand Up @@ -671,6 +680,11 @@ export class PermissionController<
(origin: OriginString) => this.hasPermissions(origin),
);

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

this.messagingSystem.registerActionHandler(
`${controllerName}:revokeAllPermissions` as const,
(origin: OriginString) => this.revokeAllPermissions(origin),
Expand Down Expand Up @@ -803,7 +817,13 @@ export class PermissionController<
* @param origin - The origin of the subject.
* @returns The permissions of the subject, if any.
*/
getPermissions(origin: OriginString) {
getPermissions(
origin: OriginString,
):
| SubjectPermissions<
ValidPermission<string, ExtractCaveats<ControllerCaveatSpecification>>
>
| undefined {
return this.state.subjects[origin]?.permissions;
}

Expand Down