Skip to content

Commit

Permalink
Merge pull request #51 from rodolfomiranda/print-signatures-on-alert-…
Browse files Browse the repository at this point in the history
…for-demo

Latest changes for demo
  • Loading branch information
rodolfomiranda committed Jan 11, 2024
2 parents 00a552c + 9fbfcdb commit c80c927
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 102 deletions.
4 changes: 3 additions & 1 deletion src/components/identifierCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export function IdentifierCard({ aid }): JSX.Element {
{obfuscateString(aid.prefix)}
</p>
</div>

{/* COMMENTED OUT FOR THE DEMO
<div className="flex flex-row justify-between">
<div className="">
<p className="font-bold text-gray-dark">Credentials Received: </p>
Expand All @@ -41,7 +43,7 @@ export function IdentifierCard({ aid }): JSX.Element {
<p className="font-bold text-gray-dark">Last Used: </p>
<p className="font-normal text-gray">November 08, 2023</p>
</div>
</div>
</div> */}
</div>
);
}
1 change: 0 additions & 1 deletion src/components/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export function Main(props: IMain): JSX.Element {
subtype: "get-tab-state",
});

console.log("tab state data", data);
if (!data) return;

if (data?.appState) {
Expand Down
160 changes: 76 additions & 84 deletions src/pages/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,84 +16,14 @@ chrome.runtime.onMessage.addListener(function (
sendResponse
) {
(async () => {
console.log("sender", sender);

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");
const currentDomain = await getCurrentDomain();

const { identifier, credential } = message.data;
const signinObj = {
identifier,
credential,
createdAt: new Date().getTime(),
updatedAt: new Date().getTime(),
domain: currentDomain.origin,
};
if (signins && signins?.length) {
await browserStorageService.setValue("signins", [
...signins,
signinObj,
]);
} else {
await browserStorageService.setValue("signins", [signinObj]);
}
const storageSignins = await browserStorageService.getValue("signins");
sendResponse({ data: { signins: storageSignins } });
}

if (
message.type === "fetch-resource" &&
message.subtype === "identifiers"
) {
const identifiers = await signifyService.listIdentifiers();
sendResponse({ data: { aids: identifiers?.aids ?? [] } });
}

if (message.type === "fetch-resource" && message.subtype === "signins") {
const signins = await browserStorageService.getValue("signins");
sendResponse({
data: {
signins: signins ?? [],
},
});
}

if (
message.type === "fetch-resource" &&
message.subtype === "credentials"
) {
const credentials = await signifyService.listCredentials();
sendResponse({ data: { credentials: credentials ?? [] } });
}





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

console.log(
"Message received from content script at " +
sender.tab.url +
": " +
message.type +
"-" +
sender.tab.url + " " +
message.type + ":" +
message.subtype
);

Expand All @@ -105,18 +35,18 @@ chrome.runtime.onMessage.addListener(function (
sendResponse({ data: { isConnected, meta: { tab: sender?.tab } } });
}


if (
message.type === "authentication" &&
message.subtype === "get-signed-headers"
) {
// TODO get real signed headers from signify
const headers = {
'Signify-Resource': "asdasd",
'Signify-Timestamp': new Date().toISOString().replace('Z', '000+00:00'),
const origin = sender.tab.url!;
// TODO method and path should be passed from web page
const signedHeaders = await signifyService.signHeaders(message.data.signin.identifier.name, "GET", "/", origin);
let jsonHeaders: { [key: string]: string; } = {};
for (const pair of signedHeaders.entries()) {
jsonHeaders[pair[0]] = pair[1];
}
sendResponse({ data: { headers: headers } });

sendResponse({ data: { headers: jsonHeaders } });
}

if (
Expand All @@ -126,10 +56,10 @@ chrome.runtime.onMessage.addListener(function (
const signins = await browserStorageService.getValue("signins");
sendResponse({ data: { signins: signins ?? [] } });
}


// Handle messages from Popup
} else if (senderIsPopup(sender)) {
// handle messages from Popup
console.log("Message received from browser extension popup: " + message.type + "-" + message.subtype);
console.log("Message received from browser extension: " + message.type + "-" + message.subtype);

if (
message.type === "authentication" &&
Expand Down Expand Up @@ -162,6 +92,68 @@ chrome.runtime.onMessage.addListener(function (
sendResponse({ data: { state } });
}
}

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");
const currentDomain = await getCurrentDomain();

const { identifier, credential } = message.data;
const signinObj = {
identifier,
credential,
createdAt: new Date().getTime(),
updatedAt: new Date().getTime(),
domain: currentDomain.origin,
};
if (signins && signins?.length) {
await browserStorageService.setValue("signins", [
...signins,
signinObj,
]);
} else {
await browserStorageService.setValue("signins", [signinObj]);
}
const storageSignins = await browserStorageService.getValue("signins");
sendResponse({ data: { signins: storageSignins } });
}
if (
message.type === "fetch-resource" &&
message.subtype === "identifiers"
) {
const identifiers = await signifyService.listIdentifiers();
sendResponse({ data: { aids: identifiers?.aids ?? [] } });
}

if (message.type === "fetch-resource" && message.subtype === "signins") {
const signins = await browserStorageService.getValue("signins");
sendResponse({
data: {
signins: signins ?? [],
},
});
}

if (
message.type === "fetch-resource" &&
message.subtype === "credentials"
) {
const credentials = await signifyService.listCredentials();
sendResponse({ data: { credentials: credentials ?? [] } });
}

})();

// return true to indicate chrome api to send a response asynchronously
Expand Down
1 change: 1 addition & 0 deletions src/pages/background/services/signify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const Signify = () => {
path,
fields
);

return signed_headers;
};

Expand Down
9 changes: 4 additions & 5 deletions src/pages/content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ window.addEventListener(
eventType: event.data.type,
});
} else {
removeAlertComponent();
removeDialog();
}
break;
}
Expand All @@ -51,15 +51,14 @@ chrome.runtime.onMessage.addListener(async function (
sendResponse
) {
if (sender.origin === "chrome-extension://" + chrome.runtime.id) {
// handle messages from Popup
console.log(
"Message received from browser extension: " +
message.type +
" " +
":" +
message.subtype
);
if (message.type === "tab" && message.subtype === "reload-state") {
removeAlertComponent();
removeDialog();
const { data } = await chrome.runtime.sendMessage<IMessage<void>>({
type: "authentication",
subtype: "check-agent-connection",
Expand Down Expand Up @@ -95,7 +94,7 @@ function insertDialog(data: any) {
);
}

function removeAlertComponent() {
function removeDialog() {
const element = document.getElementById("__root");
if (element) element.remove();
}
6 changes: 3 additions & 3 deletions src/pages/dialog/signin.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { APP_STATE } from "@pages/popup/constants";
import { sign } from "crypto";

export const SigninItem = ({ signin }): JSX.Element => {
const handleClick = async () => {
const headers = await chrome.runtime.sendMessage({
type: "authentication",
subtype: "get-signed-headers",
data: {
signin: "selectedAID",
signin: signin,
},
});
console.log("Signed headers: ", headers);
alert("Signed headers received");
alert("Signed headers received\n"+ JSON.stringify(headers.data.headers, null, 2));
const element = document.getElementById("__root");
if (element) element.remove();
};
Expand Down
17 changes: 9 additions & 8 deletions src/pages/popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { Signin } from "@src/components/signin";
import { Loader } from "@components/loader";
import { Main } from "@components/main";

// TODO Harcoded for initial development. Must be removed soon
const url = "https://keria-dev.rootsid.cloud/admin";
const boot_url = "https://keria-dev.rootsid.cloud";
const password = "CdsUqv_8-F5otMp5feNpps";
const password2 = "j2H9kCTbGybPkrTs9cGQA";
const password = "CqjYb60NT9gZl8itwuttD9";

interface IConnect {
passcode?: string;
Expand All @@ -22,7 +22,6 @@ export default function Popup(): JSX.Element {
const [isLoading, setIsLoading] = useState(false);
const [isCheckingInitialConnection, setIsCheckingInitialConnection] =
useState(false);

const getVendorUrl = async () => {
const _vendorUrl = await configService.getUrl();
setVendorUrl(_vendorUrl);
Expand All @@ -43,11 +42,13 @@ export default function Popup(): JSX.Element {
setIsConnected(!!data.isConnected);
if (data.isConnected) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(
tabs[0].id,
{ type: "tab", subtype: "reload-state" },
function (response) {}
);
if (tabs.length === 1) {
chrome.tabs.sendMessage(
tabs[0].id!,
{ type: "tab", subtype: "reload-state" },
function (response) {}
);
}
});
}
};
Expand Down

0 comments on commit c80c927

Please sign in to comment.