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

feat: credential checks #1110

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ describe("background/controllers/handler", () => {
test("should throw error if there is no registered method", () => {
const handler = new Handler();

expect(handler.handle({ method: "unknown" }, defaultOptions)).rejects.toThrowError(
"method: unknown is not detected",
);
expect(handler.handle({ method: "unknown" }, defaultOptions)).rejects.toThrow("method: unknown is not detected");
});
});
55 changes: 25 additions & 30 deletions packages/app/src/background/cryptKeeper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import RequestManager from "@src/background/controllers/requestManager";
import ApprovalService from "@src/background/services/approval";
import BackupService from "@src/background/services/backup";
import ConnectionService from "@src/background/services/connection";
import VerifiableCredentialsService from "@src/background/services/credentials";
import { VerifiableCredentialService, VerifiablePresentationService } from "@src/background/services/credentials";
import GroupService from "@src/background/services/group";
import HistoryService from "@src/background/services/history";
import { InjectorService } from "@src/background/services/injector";
Expand All @@ -18,7 +18,7 @@ import WalletService from "@src/background/services/wallet";
import ZkIdentityService from "@src/background/services/zkIdentity";
import { RPCInternalAction } from "@src/constants";
import { BackupableServices } from "@src/types";
import { validateSerializedVerifiableCredential } from "@src/util/credentials";
import { validateSerializedVC } from "@src/util/credentials";

import type { Runtime } from "webextension-polyfill";

Expand All @@ -37,7 +37,6 @@ const RPC_METHOD_ACCESS: Record<RPCExternalAction, boolean> = {
[RPCExternalAction.GENERATE_GROUP_MERKLE_PROOF]: true,
[RPCExternalAction.IMPORT_IDENTITY]: true,
[RPCExternalAction.REVEAL_CONNECTED_IDENTITY_COMMITMENT]: true,
// TODO: Please note that the following 2 actions will be refactored in another PR
[RPCExternalAction.ADD_VERIFIABLE_CREDENTIAL]: true,
[RPCExternalAction.GENERATE_VERIFIABLE_PRESENTATION]: true,
};
Expand Down Expand Up @@ -67,7 +66,9 @@ export default class CryptKeeperController {

private walletService: WalletService;

private verifiableCredentialsService: VerifiableCredentialsService;
private verifiableCredentialService: VerifiableCredentialService;

private verifiablePresentationService: VerifiablePresentationService;

private groupService: GroupService;

Expand All @@ -86,8 +87,8 @@ export default class CryptKeeperController {
this.browserService = BrowserUtils.getInstance();
this.historyService = HistoryService.getInstance();
this.walletService = WalletService.getInstance();
this.verifiableCredentialsService = VerifiableCredentialsService.getInstance();
this.verifiableCredentialsService = VerifiableCredentialsService.getInstance();
this.verifiableCredentialService = VerifiableCredentialService.getInstance();
this.verifiablePresentationService = VerifiablePresentationService.getInstance();
this.groupService = GroupService.getInstance();
this.protocolService = ProtocolService.getInstance();
this.connectionService = ConnectionService.getInstance();
Expand All @@ -96,7 +97,7 @@ export default class CryptKeeperController {
.add(BackupableServices.WALLET, this.walletService)
.add(BackupableServices.APPROVAL, this.approvalService)
.add(BackupableServices.IDENTITY, this.zkIdentityService)
.add(BackupableServices.VERIFIABLE_CREDENTIALS, this.verifiableCredentialsService)
.add(BackupableServices.VERIFIABLE_CREDENTIALS, this.verifiableCredentialService)
.add(BackupableServices.CONNECTIONS, this.connectionService);
}

Expand Down Expand Up @@ -282,60 +283,54 @@ export default class CryptKeeperController {

// Credentials
this.handler.add(
RPCInternalAction.ADD_VERIFIABLE_CREDENTIAL,
RPCExternalAction.ADD_VERIFIABLE_CREDENTIAL,
this.lockService.ensure,
this.verifiableCredentialsService.addVerifiableCredential,
this.approvalService.isOriginApproved,
this.connectionService.isOriginConnected,
validateSerializedVC,
this.verifiableCredentialService.addRequest,
);
this.handler.add(
RPCExternalAction.ADD_VERIFIABLE_CREDENTIAL,
RPCExternalAction.GENERATE_VERIFIABLE_PRESENTATION,
this.lockService.ensure,
validateSerializedVerifiableCredential,
this.verifiableCredentialsService.addVerifiableCredentialRequest,
this.approvalService.isOriginApproved,
this.connectionService.isOriginConnected,
this.verifiablePresentationService.generateRequest,
);
this.handler.add(
RPCInternalAction.REJECT_VERIFIABLE_CREDENTIAL_REQUEST,
RPCInternalAction.ADD_VERIFIABLE_CREDENTIAL,
this.lockService.ensure,
this.verifiableCredentialsService.rejectVerifiableCredentialRequest,
this.verifiableCredentialService.add,
);
this.handler.add(
RPCInternalAction.RENAME_VERIFIABLE_CREDENTIAL,
this.lockService.ensure,
this.verifiableCredentialsService.renameVerifiableCredential,
this.verifiableCredentialService.rename,
);
this.handler.add(
RPCInternalAction.GET_ALL_VERIFIABLE_CREDENTIALS,
this.lockService.ensure,
this.verifiableCredentialsService.getAllVerifiableCredentials,
this.verifiableCredentialService.getAll,
);
this.handler.add(
RPCInternalAction.DELETE_VERIFIABLE_CREDENTIAL,
this.lockService.ensure,
this.verifiableCredentialsService.deleteVerifiableCredential,
this.verifiableCredentialService.delete,
);
this.handler.add(
RPCInternalAction.DELETE_ALL_VERIFIABLE_CREDENTIALS,
this.lockService.ensure,
this.verifiableCredentialsService.deleteAllVerifiableCredentials,
this.verifiableCredentialService.deleteAll,
);
this.handler.add(
RPCInternalAction.GENERATE_VERIFIABLE_PRESENTATION,
this.lockService.ensure,
this.verifiableCredentialsService.generateVerifiablePresentation,
this.verifiablePresentationService.generate,
);
this.handler.add(
RPCInternalAction.GENERATE_VERIFIABLE_PRESENTATION_WITH_CRYPTKEEPER,
this.lockService.ensure,
this.verifiableCredentialsService.generateVerifiablePresentationWithCryptkeeper,
);
this.handler.add(
RPCExternalAction.GENERATE_VERIFIABLE_PRESENTATION,
this.lockService.ensure,
this.verifiableCredentialsService.generateVerifiablePresentationRequest,
);
this.handler.add(
RPCInternalAction.REJECT_VERIFIABLE_PRESENTATION_REQUEST,
this.lockService.ensure,
this.verifiableCredentialsService.rejectVerifiablePresentationRequest,
this.verifiablePresentationService.generateWithCryptkeeper,
);

// Approvals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,15 @@ describe("background/services/approval", () => {
test("should throw error if origin is not approved", async () => {
await approvalService.unlock();

expect(() => approvalService.isOriginApproved({}, { urlOrigin: "unknown" })).toThrowError(
expect(() => approvalService.isOriginApproved({}, { urlOrigin: "unknown" })).toThrow(
"CryptKeeper: origin is not approved",
);
});

test("should throw error if origin is not provided", async () => {
await approvalService.unlock();

expect(() => approvalService.isOriginApproved({}, { urlOrigin: "" })).toThrowError(
"CryptKeeper: origin is not set",
);
expect(() => approvalService.isOriginApproved({}, { urlOrigin: "" })).toThrow("CryptKeeper: origin is not set");
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe("background/services/backup/BackupService", () => {
const invalidFileContent = JSON.stringify({ unknown: "encrypted" }, null, 4);
const fileContent = JSON.stringify({ lock: "encrypted", wallet: "encrypted", approval: "encrypted" }, null, 4);

await expect(backupService.upload({ content: invalidFileContent, ...defaultPasswords })).rejects.toThrowError(
await expect(backupService.upload({ content: invalidFileContent, ...defaultPasswords })).rejects.toThrow(
"File content is corrupted",
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,22 @@ describe("background/services/bandada/BandadaService", () => {
} as Response);
const service = BandadaService.getInstance();

await expect(service.addMember(defaultAddMemberArgs)).rejects.toThrowError("Error 1,Error 2");
await expect(service.addMember(defaultAddMemberArgs)).rejects.toThrow("Error 1,Error 2");
expect(fetchSpy).toHaveBeenCalledTimes(1);
});

test("should throw error if add member is called without required params", async () => {
const args = { ...defaultAddMemberArgs, apiKey: undefined, inviteCode: undefined };
const service = BandadaService.getInstance();

await expect(service.addMember(args)).rejects.toThrowError("Provide api key or invide code");
await expect(service.addMember(args)).rejects.toThrow("Provide api key or invide code");
});

test("should throw error if add member is called with both required params", async () => {
const args = { ...defaultAddMemberArgs, apiKey: "key", inviteCode: "code" };
const service = BandadaService.getInstance();

await expect(service.addMember(args)).rejects.toThrowError("Don't provide both api key and invide code");
await expect(service.addMember(args)).rejects.toThrow("Don't provide both api key and invide code");
});

test("should generate merkle proof properly", async () => {
Expand All @@ -125,7 +125,7 @@ describe("background/services/bandada/BandadaService", () => {
} as Response);
const service = BandadaService.getInstance();

await expect(service.generateMerkleProof(defaultGenerateProofArgs)).rejects.toThrowError("Error");
await expect(service.generateMerkleProof(defaultGenerateProofArgs)).rejects.toThrow("Error");
expect(fetchSpy).toHaveBeenCalledTimes(1);
});

Expand Down Expand Up @@ -162,7 +162,7 @@ describe("background/services/bandada/BandadaService", () => {
} as Response);
const service = BandadaService.getInstance();

await expect(service.checkGroupMembership(defaultCheckMembershipArgs)).rejects.toThrowError("Error");
await expect(service.checkGroupMembership(defaultCheckMembershipArgs)).rejects.toThrow("Error");
expect(fetchSpy).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ describe("background/services/connection", () => {
});

test("should throw error if there is no url origin", async () => {
await expect(connectionService.connect(defaultArgs, { urlOrigin: "" })).rejects.toThrowError(
await expect(connectionService.connect(defaultArgs, { urlOrigin: "" })).rejects.toThrow(
"CryptKeeper: origin is not provided",
);
});

test("should throw error if there is no idenity found", async () => {
await expect(connectionService.connect({ commitment: "unknown" }, defaultMetadata)).rejects.toThrowError(
await expect(connectionService.connect({ commitment: "unknown" }, defaultMetadata)).rejects.toThrow(
"CryptKeeper: identity is not found",
);
});
Expand Down Expand Up @@ -273,13 +273,13 @@ describe("background/services/connection", () => {
});

test("should throw error if there is no url origin", () => {
expect(() => connectionService.isOriginConnected({}, { urlOrigin: "" })).toThrowError(
expect(() => connectionService.isOriginConnected({}, { urlOrigin: "" })).toThrow(
"CryptKeeper: origin is not provided",
);
});

test("should throw error if there is no connection for url origin", () => {
expect(() => connectionService.isOriginConnected({}, { urlOrigin: "unknown" })).toThrowError(
expect(() => connectionService.isOriginConnected({}, { urlOrigin: "unknown" })).toThrow(
"CryptKeeper: origin is not connected",
);
});
Expand Down
Loading