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

Move tab state to content script and synchronous authentications #61

Merged
merged 3 commits into from
Jan 14, 2024
Merged
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
15 changes: 8 additions & 7 deletions example-web/my-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion example-web/my-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"eslint": "^8.56.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
Expand All @@ -24,7 +25,10 @@
"extends": [
"react-app",
"react-app/jest"
]
],
"globals": {
"chrome": true
}
},
"browserslist": {
"production": [
Expand Down
13 changes: 13 additions & 0 deletions example-web/my-app/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ function App() {
window.postMessage({ type: "init-req-credential" }, "*");
};

const handleSyncRequest = () => {
// TODO extension Id harcoded just for testing, need to find a way to get it dynamically
chrome.runtime.sendMessage("fklmfbmpaimbgjplbambkdjphdadbmed", {data: "test"},
function(response) {
if (!response.success)
console.log(response.data)
alert("Signed headers received\n"+ JSON.stringify(response.data.headers, null, 2));
});
};



return (
Expand All @@ -25,6 +35,9 @@ function App() {
<Button variant="contained" onClick={handleRequestCredential}>
Authenticate with Credential
</Button>
<Button variant="contained" onClick={handleSyncRequest}>
Synchronous Authentication
</Button>
</div>
</header>
</div>
Expand Down
18 changes: 6 additions & 12 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,14 @@
"permissions": [
"activeTab",
"storage",
"tabs",
"alarms"
],
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*",
"<all_urls>"
],
"matches": ["<all_urls>"],
"run_at": "document_end",
"js": [
"src/pages/content/index.tsx"
],
"css": [
"contentStyle.css"
]
"js": ["src/pages/content/index.tsx"],
"css": ["contentStyle.css"]
}
],
"web_accessible_resources": [
Expand All @@ -48,5 +39,8 @@
],
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval'"
},
"externally_connectable": {
"matches": ["<all_urls>"]
}
}
30 changes: 16 additions & 14 deletions src/components/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, useState } from "react";
import { Sidebar } from "@components/sidebar";
import { SelectIdentifier } from "@components/selectIdentifier";
import { SelectCredential } from "@components/selectCredential";
import { APP_STATE } from "@pages/popup/constants";
import { TAB_STATE } from "@pages/popup/constants";
import { IdentifierList } from "@components/identifierList";
import { CredentialList } from "@components/credentialList";
import { SigninList } from "@components/signinList";
Expand All @@ -13,39 +13,41 @@ interface IMain {

export function Main(props: IMain): JSX.Element {
const [activeSidebar, setActiveSidebar] = useState("Identifiers");
const [tabState, setTabState] = useState(APP_STATE.DEFAULT);
const [tabState, setTabState] = useState(TAB_STATE.DEFAULT);

const fetchTabState = async () => {
const { data } = await chrome.runtime.sendMessage({
type: "tab",
subtype: "get-tab-state",
});

if (!data) return;
chrome.tabs.query({ active: true, currentWindow: true }, async function (tabs) {
const { data } = await chrome.tabs.sendMessage(
tabs[0].id!,
{ type: "tab", subtype: "get-tab-state" }
);
if (!data) return;

if (data?.appState) {
setTabState(data?.appState);
if (
data?.appState === APP_STATE.SELECT_IDENTIFIER ||
data?.appState === APP_STATE.SELECT_CREDENTIAL
data?.appState === TAB_STATE.SELECT_IDENTIFIER ||
data?.appState === TAB_STATE.SELECT_CREDENTIAL
) {
setActiveSidebar(
data?.appState === APP_STATE.SELECT_IDENTIFIER
data?.appState === TAB_STATE.SELECT_IDENTIFIER
? "Identifiers"
: "Credentials"
);
}
}
});
};

useEffect(() => {
fetchTabState();
}, []);

const renderItems = () => {
if (tabState === APP_STATE.SELECT_IDENTIFIER) return <SelectIdentifier />;
if (tabState === TAB_STATE.SELECT_IDENTIFIER) return <SelectIdentifier />;

if (tabState === APP_STATE.SELECT_CREDENTIAL) return <SelectCredential />;
if (tabState === TAB_STATE.SELECT_CREDENTIAL) return <SelectCredential />;

switch (activeSidebar) {
case "Credentials":
Expand All @@ -60,8 +62,8 @@ export function Main(props: IMain): JSX.Element {

const isSidebarDisabled = () => {
return (
tabState === APP_STATE.SELECT_IDENTIFIER ||
tabState === APP_STATE.SELECT_CREDENTIAL
tabState === TAB_STATE.SELECT_IDENTIFIER ||
tabState === TAB_STATE.SELECT_CREDENTIAL
);
};

Expand Down
15 changes: 6 additions & 9 deletions src/components/selectCredential.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState, useEffect } from "react";
import { CredentialCard } from "@components/credentialCard";
import { IMessage } from "@pages/background/types";
import { APP_STATE } from "@pages/popup/constants";

export function SelectCredential(): JSX.Element {
const [credentials, setCredentials] = useState([]);
Expand All @@ -13,20 +12,18 @@ export function SelectCredential(): JSX.Element {
setCredentials(data.credentials);
};

const createSigninWithCredential = async (credential) => {
const { data } = await chrome.runtime.sendMessage<IMessage<void>>({
const createSigninWithCredential = async (credential: any) => {
await chrome.runtime.sendMessage<IMessage<any>>({
type: "create-resource",
subtype: "signin",
data: {
credential,
},
});
await chrome.runtime.sendMessage({
type: "tab",
subtype: "set-app-state",
data: {
appState: APP_STATE.DEFAULT,
},
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(
tabs[0].id!,
{ type: "tab", subtype: "reload-state" });
});
window.close();
};
Expand Down
19 changes: 4 additions & 15 deletions src/components/selectIdentifier.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState, useEffect } from "react";
import { IdentifierCard } from "@components/identifierCard";
import { IMessage } from "@pages/background/types";
import { APP_STATE } from "@pages/popup/constants";

export function SelectIdentifier(): JSX.Element {
const [aids, setAids] = useState([]);
Expand All @@ -14,29 +13,19 @@ export function SelectIdentifier(): JSX.Element {
setAids(data.aids);
};

const createSigninWithIdentifiers = async (aid) => {
const { data } = await chrome.runtime.sendMessage<IMessage<void>>({
const createSigninWithIdentifiers = async (aid: any) => {
await chrome.runtime.sendMessage<IMessage<any>>({
type: "create-resource",
subtype: "signin",
data: {
identifier: aid,
},
});
await chrome.runtime.sendMessage({
type: "tab",
subtype: "set-app-state",
data: {
appState: APP_STATE.DEFAULT,
},
});

console.log("data.signins", data.signins);
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(
tabs[0].id,
{ type: "tab", subtype: "reload-state" },
function (response) {}
);
tabs[0].id!,
{ type: "tab", subtype: "reload-state" });
});
window.close();
};
Expand Down
45 changes: 30 additions & 15 deletions src/pages/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { browserStorageService } from "@pages/background/services/browser-storage";
import { webappService } from "@pages/background/services/webapp";
import { configService } from "@pages/background/services/config";
import { userService } from "@pages/background/services/user";
import { signifyService } from "@pages/background/services/signify";
Expand All @@ -9,6 +8,12 @@ import { getCurrentDomain } from "@pages/background/utils";

console.log("Background script loaded");

chrome.runtime.onInstalled.addListener(function (object) {
if (object.reason === chrome.runtime.OnInstalledReason.INSTALL) {
console.log("Signify Browser Extension installed");
}
});

// Handle messages
chrome.runtime.onMessage.addListener(function (
message: IMessage<any>,
Expand All @@ -18,7 +23,7 @@ chrome.runtime.onMessage.addListener(function (
(async () => {

// Handle mesages from content script on active tab
if (sender.tab && sender.tab.active) {
if (sender.tab && sender.tab.active) {

console.log(
"Message received from content script at " +
Expand Down Expand Up @@ -92,19 +97,6 @@ chrome.runtime.onMessage.addListener(function (
}
}

if (message.type === "tab" && message.subtype === "get-tab-state") {
const currentDomain = await getCurrentDomain();
const appData = await webappService.getAppData(currentDomain!.origin);
sendResponse({ data: appData });
}

if (message.type === "tab" && message.subtype === "set-app-state") {
const currentDomain = await getCurrentDomain();
await webappService.setAppData(currentDomain!.origin, message.data);
const appData = await webappService.getAppData(currentDomain!.origin);
sendResponse({ data: appData });
}

if (message.type === "create-resource" && message.subtype === "signin") {
const signins = await browserStorageService.getValue("signins") as any[];
const currentDomain = await getCurrentDomain();
Expand Down Expand Up @@ -158,3 +150,26 @@ chrome.runtime.onMessage.addListener(function (
// return true to indicate chrome api to send a response asynchronously
return true;
});

chrome.runtime.onMessageExternal.addListener(
async function(request, sender, sendResponse) {
console.log("Message received from external source: " + sender.url);
// if (sender.url === blocklistedWebsite)
// return; // don't allow this web page access
// if (request.openUrlInEditor)
// openUrl(request.openUrlInEditor);
// sendResponse({data: "received"})
const origin = sender.url!;
const signins = await browserStorageService.getValue("signins") as any;
// Validate that message comes from a page that has a signin
if (origin.startsWith(signins[0].domain)) {
const signedHeaders = await signifyService.signHeaders(signins[0].identifier.name, origin);
let jsonHeaders: { [key: string]: string; } = {};
for (const pair of signedHeaders.entries()) {
jsonHeaders[pair[0]] = pair[1];
}
sendResponse({ data: { headers: jsonHeaders } });

}

});
2 changes: 1 addition & 1 deletion src/pages/background/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const CONFIG_ENUMS = {

const Config = () => {
const getUrl = async (): Promise<string> => {
return await browserStorageService.getValue(CONFIG_ENUMS.KERIA_URL);
return await browserStorageService.getValue(CONFIG_ENUMS.KERIA_URL) as string;
}

const removeUrl = async () => {
Expand Down
20 changes: 0 additions & 20 deletions src/pages/background/services/webapp.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/pages/background/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const getCurrentTab = (): Promise<chrome.tabs.Tab> => {

export const getCurrentDomain = async () => {
const currentTab = await getCurrentTab();
console.log("Current tab: ", currentTab)
return currentTab ? new URL(currentTab.url!) : null;
};

Expand Down
Loading