Skip to content

Commit

Permalink
Merge pull request #158 from HunnySajid/refactor/background
Browse files Browse the repository at this point in the history
Refactor/background
  • Loading branch information
2byrds authored Apr 6, 2024
2 parents 945a7a6 + ad344e1 commit 3a0b726
Show file tree
Hide file tree
Showing 24 changed files with 551 additions and 494 deletions.
4 changes: 2 additions & 2 deletions src/components/credentialList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState, useEffect } from "react";
import { useIntl } from "react-intl";
import { UI_EVENTS } from "@config/event-types";
import { CredentialCard } from "@components/credentialCard";
import { Loader, Flex, Box, Text } from "@components/ui";
import { IMessage } from "@config/types";
Expand All @@ -11,8 +12,7 @@ export function CredentialList(): JSX.Element {
const fetchCredentials = async () => {
setIsLoading(true);
const { data } = await chrome.runtime.sendMessage<IMessage<void>>({
type: "fetch-resource",
subtype: "credentials",
type: UI_EVENTS.fetch_resource_credentials,
});
setCredentials(data.credentials);
setIsLoading(false);
Expand Down
7 changes: 3 additions & 4 deletions src/components/identifierList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState, useEffect } from "react";
import { useIntl } from "react-intl";
import { UI_EVENTS } from "@config/event-types";
import { IdentifierCard } from "@components/identifierCard";
import { Button, Box, Drawer, Flex, Text, Loader } from "@components/ui";
import { IMessage } from "@config/types";
Expand All @@ -19,8 +20,7 @@ export function IdentifierList(): JSX.Element {

const fetchIdentifiers = async () => {
const { data } = await chrome.runtime.sendMessage<IMessage<void>>({
type: "fetch-resource",
subtype: "identifiers",
type: UI_EVENTS.fetch_resource_identifiers
});
setAids(data.aids);
};
Expand All @@ -45,8 +45,7 @@ export function IdentifierList(): JSX.Element {
const { data, error } = await chrome.runtime.sendMessage<
IMessage<ICreateIdentifier>
>({
type: "create-resource",
subtype: "identifier",
type: UI_EVENTS.create_resource_identifier,
data: { name },
});
if (error) {
Expand Down
7 changes: 3 additions & 4 deletions src/components/selectCredential.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState, useEffect } from "react";
import { useIntl } from "react-intl";
import { UI_EVENTS } from "@config/event-types";
import { CredentialCard } from "@components/credentialCard";
import { Button, Loader, Flex, Box } from "@components/ui";
import { IMessage } from "@config/types";
Expand All @@ -11,17 +12,15 @@ export function SelectCredential(): JSX.Element {
const fetchCredentials = async () => {
setIsLoading(true);
const { data } = await chrome.runtime.sendMessage<IMessage<void>>({
type: "fetch-resource",
subtype: "credentials",
type: UI_EVENTS.fetch_resource_credentials
});
setCredentials(data.credentials);
setIsLoading(false);
};

const createSigninWithCredential = async (credential: any) => {
await chrome.runtime.sendMessage<IMessage<any>>({
type: "create-resource",
subtype: "signin",
type: UI_EVENTS.create_resource_signin,
data: {
credential,
},
Expand Down
10 changes: 4 additions & 6 deletions src/components/selectIdentifier.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState, useEffect } from "react";
import { useIntl } from "react-intl";
import { UI_EVENTS } from "@config/event-types";
import { IdentifierCard } from "@components/identifierCard";
import { Box, Button, Drawer, Flex, Text, Loader } from "@components/ui";
import { IMessage } from "@config/types";
Expand All @@ -20,8 +21,7 @@ export function SelectIdentifier(): JSX.Element {
const fetchIdentifiers = async () => {
setIsLoading(true);
const { data } = await chrome.runtime.sendMessage<IMessage<void>>({
type: "fetch-resource",
subtype: "identifiers",
type: UI_EVENTS.fetch_resource_identifiers
});
console.log("data", data);
setIsLoading(false);
Expand All @@ -30,8 +30,7 @@ export function SelectIdentifier(): JSX.Element {

const createSigninWithIdentifiers = async (aid: any) => {
await chrome.runtime.sendMessage<IMessage<any>>({
type: "create-resource",
subtype: "signin",
type: UI_EVENTS.create_resource_signin,
data: {
identifier: aid,
},
Expand Down Expand Up @@ -64,8 +63,7 @@ export function SelectIdentifier(): JSX.Element {
const { data, error } = await chrome.runtime.sendMessage<
IMessage<ISelectIdentifier>
>({
type: "create-resource",
subtype: "identifier",
type: UI_EVENTS.create_resource_identifier,
data: { name },
});
if (error) {
Expand Down
18 changes: 6 additions & 12 deletions src/components/signinList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState, useEffect } from "react";
import { useIntl } from "react-intl";
import { UI_EVENTS } from "@config/event-types";
import { SigninCard } from "@components/signinCard";
import { Loader, Flex, Box, Text } from "@components/ui";
import { IMessage, ISignin } from "@config/types";
Expand All @@ -19,19 +20,15 @@ export function SigninList(): JSX.Element {
const fetchSignins = async () => {
setIsLoading(true);
const { data } = await chrome.runtime.sendMessage<IMessage<void>>({
type: "fetch-resource",
subtype: "signins",
type: UI_EVENTS.fetch_resource_signins,
});
setSignins(data?.signins);
setIsLoading(false);
};

const deleteSignin = async (id: string) => {
const { data } = await chrome.runtime.sendMessage<
IMessage<IDeleteSignin>
>({
type: "delete-resource",
subtype: "signins",
const { data } = await chrome.runtime.sendMessage<IMessage<IDeleteSignin>>({
type: UI_EVENTS.delete_resource_signins,
data: {
id,
},
Expand All @@ -56,11 +53,8 @@ export function SigninList(): JSX.Element {
};

const updateAutoSignin = async (signin: ISignin) => {
const { data } = await chrome.runtime.sendMessage<
IMessage<IUpdateSignin>
>({
type: "update-resource",
subtype: "auto-signin",
const { data } = await chrome.runtime.sendMessage<IMessage<IUpdateSignin>>({
type: UI_EVENTS.update_resource_auto_signin,
data: {
signin,
},
Expand Down
52 changes: 52 additions & 0 deletions src/config/event-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const CS = "cs-"; // content-script
const UI = "ui-"; // pages like popup, new tab etc
const EXTERNAL = "external-"; // onExternalMessage

const EVENT_TYPE = {
action_icon: "action-icon",
authentication: "authentication",
create_resource: "create-resource",
delete_resource: "delete-resource",
fetch_resource: "fetch-resource",
update_resource: "update-resource",
vendor_info: "vendor-info",
};

export const CS_EVENTS = {
action_icon_set: `${CS}-${EVENT_TYPE.action_icon}-set`,
action_icon_unset: `${CS}-${EVENT_TYPE.action_icon}-unset`,
action_icon_set_tab: `${CS}-${EVENT_TYPE.action_icon}-set-tab`,
action_icon_unset_tab: `${CS}-${EVENT_TYPE.action_icon}-unset-tab`,

fetch_resource_auto_signin_signature: `${CS}-${EVENT_TYPE.fetch_resource}-auto-signin-signature`,
fetch_resource_tab_signin: `${CS}-${EVENT_TYPE.fetch_resource}-tab-signin`,

vendor_info_get_vendor_data: `${CS}-${EVENT_TYPE.vendor_info}-get-vendor-data`,
vendor_info_attempt_set_vendor_url: `${CS}-${EVENT_TYPE.vendor_info}-attempt-set-vendor-url`,

authentication_check_agent_connection: `${CS}-${EVENT_TYPE.authentication}-check-agent-connection`,
authentication_get_signed_headers: `${CS}-${EVENT_TYPE.authentication}-get-signed-headers`,
};

export const UI_EVENTS = {
action_icon_unset: `${UI}-${EVENT_TYPE.action_icon}-unset`,
authentication_check_agent_connection: `${UI}-${EVENT_TYPE.authentication}-check-agent-connection`,
authentication_disconnect_agent: `${UI}-${EVENT_TYPE.authentication}-disconnect-agent`,
authentication_connect_agent: `${UI}-${EVENT_TYPE.authentication}-connect-agent`,
authentication_boot_connect_agent: `${UI}-${EVENT_TYPE.authentication}-boot-connect-agent`,
authentication_generate_passcode: `${UI}-${EVENT_TYPE.authentication}-generate-passcode`,

create_resource_signin: `${UI}-${EVENT_TYPE.create_resource}-signin`,
create_resource_identifier: `${UI}-${EVENT_TYPE.create_resource}-identifier`,

fetch_resource_identifiers: `${UI}-${EVENT_TYPE.fetch_resource}-identifiers`,
fetch_resource_signins: `${UI}-${EVENT_TYPE.fetch_resource}-signins`,
fetch_resource_credentials: `${UI}-${EVENT_TYPE.fetch_resource}-credentials`,

update_resource_auto_signin: `${UI}-${EVENT_TYPE.update_resource}-auto-signin`,
delete_resource_signins: `${UI}-${EVENT_TYPE.delete_resource}-signins`,
};

export const EXTERNAL_EVENTS = {
fetch_resource_auto_signin_signature: `${EXTERNAL}-${EVENT_TYPE.fetch_resource}-auto-signin-signature`,
};
8 changes: 7 additions & 1 deletion src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ export interface ObjectOfArrays<T> {
[key: string]: T[];
}

export interface IHandler {
sendResponse: (response?: any) => void;
tabId?: number;
url?: string;
data?: any;

}
export interface IMessage<T> {
type: string;
subtype?: string;
data?: T;
}

Expand Down
25 changes: 25 additions & 0 deletions src/pages/background/handlers/actionIcon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { IHandler } from "@config/types";

export function handleSetActionIcon({ sendResponse }: IHandler) {
chrome.action.setBadgeBackgroundColor({ color: "#008000" }, () => {
chrome.action.setBadgeText({ text: "1" });
sendResponse({ data: { success: true } });
});
}

export function handleUnsetActionIcon({ sendResponse }: IHandler) {
chrome.action.setBadgeText({ text: "" });
sendResponse({ data: { success: true } });
}

export function handleSetTabActionIcon({ sendResponse, tabId }: IHandler) {
chrome.action.setBadgeBackgroundColor({ color: "#008000" }, () => {
chrome.action.setBadgeText({ tabId: tabId, text: "1" });
sendResponse({ data: { success: true } });
});
}

export function handleUnsetTabActionIcon({ sendResponse, tabId }: IHandler) {
chrome.action.setBadgeText({ tabId: tabId, text: "" });
sendResponse({ data: { success: true } });
}
79 changes: 79 additions & 0 deletions src/pages/background/handlers/authentication.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { signifyService } from "@pages/background/services/signify";
import { userService } from "@pages/background/services/user";
import { IHandler } from "@config/types";

export async function handleCheckAgentConnection({
sendResponse,
url,
}: IHandler) {
const isConnected = await signifyService.isConnected();
sendResponse({ data: { isConnected, tabUrl: url } });
}

export async function handleDisconnectAgent({ sendResponse }: IHandler) {
await signifyService.disconnect();
sendResponse({ data: { isConnected: false } });
}

export async function handleConnectAgent({ sendResponse, data }: IHandler) {
const resp = (await signifyService.connect(
data.agentUrl,
data.passcode
)) as any;
if (resp?.error) {
// TODO: improve error messages
// Current messages are not descrptive enough e.g
// bran must be 21 characters
// agent does not exist for controller <controller-id>
// using custom error message for now instead of resp?.error?.message

sendResponse({
error: {
code: 404,
message: resp?.error?.message,
},
});
} else {
await userService.setPasscode(data.passcode);
sendResponse({ data: { success: true } });
}
}

export async function handleBootConnectAgent({ sendResponse, data }: IHandler) {
const resp = (await signifyService.bootAndConnect(
data.agentUrl,
data.bootUrl,
data.passcode
)) as any;
if (resp?.error) {
sendResponse({
error: {
code: 404,
message: resp?.error?.message,
},
});
} else {
await userService.setPasscode(data.passcode);
sendResponse({ data: { success: true } });
}
}

export async function handleGeneratePasscode({ sendResponse, data }: IHandler) {
const passcode = signifyService.generatePasscode();
sendResponse({ data: { passcode } });
}

export async function handleGetSignedHeaders({
sendResponse,
url,
data,
}: IHandler) {
const resp = await signifyService.getSignedHeaders({
url: url!,
signin: data.signin,
});

sendResponse({
data: resp,
});
}
Loading

0 comments on commit 3a0b726

Please sign in to comment.