Skip to content

Commit

Permalink
Merge pull request #1819 from getAlby/feature/permission-actions
Browse files Browse the repository at this point in the history
feat: bring back permission actions
  • Loading branch information
bumi authored Dec 12, 2022
2 parents 76e4cee + eba1e0a commit f122766
Show file tree
Hide file tree
Showing 12 changed files with 356 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import db from "~/extension/background-script/db";
import { allowanceFixture } from "~/fixtures/allowances";
import { permissionsFixture } from "~/fixtures/permissions";
import type { DbAllowance, MessagePermissionAdd } from "~/types";

import addPermission from "../add";

Date.now = jest.fn(() => 1487076708000);

const mockAllowances: DbAllowance[] = [allowanceFixture[0]];

beforeEach(async () => {
// fill the DB first
await db.allowances.bulkAdd(mockAllowances);
});

afterEach(() => {
jest.clearAllMocks();
});

describe("add permission", () => {
test("saves permissions", async () => {
const message: MessagePermissionAdd = {
application: "LBE",
prompt: true,
action: "addPermission",
origin: {
internal: true,
},
args: {
host: mockAllowances[0].host,
method: "the-request-method-1",
enabled: true,
blocked: false,
},
};

await addPermission(message);

const dbPermissions = await db.permissions.toArray();

expect(dbPermissions).toStrictEqual([permissionsFixture[0]]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import db from "~/extension/background-script/db";
import { permissionsFixture } from "~/fixtures/permissions";
import type { DbPermission, MessagePermissionDelete } from "~/types";

import deletePermission from "../delete";

const mockNow = 1487076708000;
Date.now = jest.fn(() => mockNow);

const mockPermissions: DbPermission[] = permissionsFixture;

const resultPermissions: DbPermission[] = permissionsFixture.filter(
(permission) => permission.id !== 2
);

beforeEach(async () => {
// fill the DB first
await db.permissions.bulkAdd(mockPermissions);
});

afterEach(() => {
jest.clearAllMocks();
});

describe("delete permission", () => {
test("removes permission method from allowance and deletes permission", async () => {
const message: MessagePermissionDelete = {
application: "LBE",
prompt: true,
action: "deletePermission",
origin: {
internal: true,
},
args: {
host: mockPermissions[1].host,
method: "the-request-method-2",
},
};

await deletePermission(message);

const dbPermissions = await db.permissions.toArray();

expect(dbPermissions).toStrictEqual(resultPermissions);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import db from "~/extension/background-script/db";
import { permissionsFixture } from "~/fixtures/permissions";
import type { DbPermission, MessagePermissionsDelete } from "~/types";

import deleteByIds from "../deleteByIds";

const mockNow = 1487076708000;
Date.now = jest.fn(() => mockNow);

const mockPermissions: DbPermission[] = permissionsFixture;

beforeEach(async () => {
// fill the DB first
await db.permissions.bulkAdd(mockPermissions);
});

afterEach(() => {
jest.clearAllMocks();
});

describe("delete permissions by id", () => {
test("bulk deletes permissions using keys", async () => {
const message: MessagePermissionsDelete = {
application: "LBE",
prompt: true,
action: "deletePermissions",
origin: {
internal: true,
},
args: {
ids: [2, 3],
},
};

await deleteByIds(message);

const dbPermissions = await db.permissions.toArray();

expect(dbPermissions).toStrictEqual([mockPermissions[0]]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import db from "~/extension/background-script/db";
import { permissionsFixture } from "~/fixtures/permissions";
import type { DbPermission, Permission, MessagePermissionsList } from "~/types";

import listByAllowance from "../list";

const mockNow = 1487076708000;
Date.now = jest.fn(() => mockNow);

const mockPermissions: DbPermission[] = permissionsFixture;

const resultPermissions: Permission[] = [
{
...mockPermissions[0],
id: 1,
},
{
...mockPermissions[1],
id: 2,
},
];

beforeEach(async () => {
// fill the DB first
await db.permissions.bulkAdd(mockPermissions);
});

afterEach(() => {
jest.clearAllMocks();
});

describe("delete permissions by id", () => {
test("bulk deletes permissions using keys", async () => {
const message: MessagePermissionsList = {
application: "LBE",
prompt: true,
action: "listPermissions",
origin: {
internal: true,
},
args: {
id: 1,
},
};

expect(await listByAllowance(message)).toStrictEqual({
data: {
permissions: resultPermissions,
},
});
});
});
31 changes: 31 additions & 0 deletions src/extension/background-script/actions/permissions/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { MessagePermissionAdd } from "~/types";

import db from "../../db";

const addPermission = async (message: MessagePermissionAdd) => {
const { host, method, enabled, blocked } = message.args;

const matchingAllowance = await db.allowances
.where("host")
.equalsIgnoreCase(host)
.first();

if (!matchingAllowance?.id) {
return { error: "No Allowance set for this host" };
}

const added = await db.permissions.add({
createdAt: Date.now().toString(),
allowanceId: matchingAllowance.id,
host,
method,
enabled,
blocked,
});

await db.saveToStorage();

return { data: added };
};

export default addPermission;
19 changes: 19 additions & 0 deletions src/extension/background-script/actions/permissions/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { MessagePermissionDelete } from "~/types";

import db from "../../db";

const deletePermission = async (message: MessagePermissionDelete) => {
const { host, method } = message.args;

const deleteCount = await db.permissions
.where("host")
.equalsIgnoreCase(host)
.and((p) => p.method === method)
.delete();

await db.saveToStorage();

return { data: !!deleteCount };
};

export default deletePermission;
13 changes: 13 additions & 0 deletions src/extension/background-script/actions/permissions/deleteByIds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import db from "~/extension/background-script/db";
import type { MessagePermissionsDelete } from "~/types";

const deleteByIds = async (message: MessagePermissionsDelete) => {
const { ids } = message.args;

await db.permissions.bulkDelete(ids);
await db.saveToStorage();

return { data: true };
};

export default deleteByIds;
6 changes: 6 additions & 0 deletions src/extension/background-script/actions/permissions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import add from "./add";
import deletePermission from "./delete";
import deleteByIds from "./deleteByIds";
import listByAllowance from "./list";

export { add, deletePermission, deleteByIds, listByAllowance };
32 changes: 32 additions & 0 deletions src/extension/background-script/actions/permissions/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import db from "~/extension/background-script/db";
import type { MessagePermissionsList, Permission } from "~/types";

const listByAllowance = async (message: MessagePermissionsList) => {
const { id } = message.args;
const dbPermissions = await db.permissions
.toCollection()
.filter((permission) => permission.allowanceId === id)
.toArray();

const permissions: Permission[] = [];

for (const dbPermission of dbPermissions) {
if (dbPermission.id) {
const { id } = dbPermission;
const tmpPermission: Permission = {
...dbPermission,
id,
};

permissions.push(tmpPermission);
}
}

return {
data: {
permissions,
},
};
};

export default listByAllowance;
5 changes: 5 additions & 0 deletions src/extension/background-script/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as ln from "./actions/ln";
import lnurl, { auth } from "./actions/lnurl";
import * as nostr from "./actions/nostr";
import * as payments from "./actions/payments";
import * as permissions from "./actions/permissions";
import * as settings from "./actions/settings";
import * as setup from "./actions/setup";
import * as webln from "./actions/webln";
Expand All @@ -17,6 +18,10 @@ const routes = {
listAllowances: allowances.list,
deleteAllowance: allowances.deleteAllowance,
updateAllowance: allowances.updateAllowance,
addPermission: permissions.add,
deletePermission: permissions.deletePermission,
deletePermissionsById: permissions.deleteByIds,
listPermissions: permissions.listByAllowance,
lock: accounts.lock,
unlock: accounts.unlock,
getInfo: ln.getInfo,
Expand Down
31 changes: 31 additions & 0 deletions src/fixtures/permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { DbPermission } from "~/types";

export const permissionsFixture: DbPermission[] = [
{
id: 1,
allowanceId: 1,
createdAt: "1487076708000",
host: "pro.kollider.xyz",
method: "the-request-method-1",
blocked: false,
enabled: true,
},
{
id: 2,
allowanceId: 1,
createdAt: "1487076708000",
host: "pro.kollider.xyz",
method: "the-request-method-2",
blocked: false,
enabled: true,
},
{
id: 3,
allowanceId: 2,
createdAt: "1487076708000",
host: "lnmarkets.com",
method: "the-request-method-3",
blocked: false,
enabled: true,
},
];
36 changes: 36 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,38 @@ export interface MessageAccountAll extends MessageDefault {
action: "getAccounts";
}

export interface MessagePermissionAdd extends MessageDefault {
args: {
host: Permission["host"];
method: Permission["method"];
enabled: Permission["enabled"];
blocked: Permission["blocked"];
};
action: "addPermission";
}

export interface MessagePermissionDelete extends MessageDefault {
args: {
host: Permission["host"];
method: Permission["method"];
};
action: "deletePermission";
}

export interface MessagePermissionsList extends MessageDefault {
args: {
id: Allowance["id"];
};
action: "listPermissions";
}

export interface MessagePermissionsDelete extends MessageDefault {
args: {
ids: Permission["id"][];
};
action: "deletePermissions";
}

export interface MessageBlocklistAdd extends MessageDefault {
args: {
host: string;
Expand Down Expand Up @@ -516,6 +548,10 @@ export interface DbPermission {
blocked: boolean;
}

export interface Permission extends Omit<DbPermission, "id"> {
id: number;
}

export interface PaymentResponse
extends Pick<Payment, "destination" | "preimage" | "paymentHash"> {
route: {
Expand Down

0 comments on commit f122766

Please sign in to comment.