Skip to content

Commit

Permalink
Merge pull request #49 from rodolfomiranda/signature-in-webpage
Browse files Browse the repository at this point in the history
Add full flow
  • Loading branch information
rodolfomiranda committed Jan 10, 2024
2 parents 5f8b6eb + 2d69bea commit 11441c9
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 64 deletions.
8 changes: 0 additions & 8 deletions example-web/my-app/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ function App() {
<div className="App">
<header className="App-header">
<img src={logo} alt="logo" />
<input
id="aid-input"
type="password"
onChange={(e) => {
console.log(e);
setPassword(e.target.value);
}}
/>
<div>
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down
4 changes: 2 additions & 2 deletions example-web/my-app/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
// import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
Expand All @@ -14,4 +14,4 @@ root.render(
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
// reportWebVitals();
6 changes: 6 additions & 0 deletions src/components/selectIdentifier.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ export function SelectIdentifier(): JSX.Element {
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){

});
});
window.close();
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export function Signin(props: ISignin): JSX.Element {
</div>
<div className="flex flex-row justify-center">
<a href="#" className="font-medium text-blue-600 hover:underline">
Don't have account?
Don't have a KERIA agent?
</a>
</div>
<div className="flex flex-row justify-center">
Expand Down
76 changes: 40 additions & 36 deletions src/pages/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ chrome.runtime.onMessage.addListener(function (
sendResponse({ data: appData });
}

if (
message.type === "authentication" &&
message.subtype === "check-agent-connection"
) {
const isConnected = await signifyService.isConnected();
sendResponse({ data: { isConnected, meta: { tab: sender?.tab } } });
}


if (message.type === "create-resource" && message.subtype === "signin") {
const signins = await browserStorageService.getValue("signins");
Expand Down Expand Up @@ -87,15 +81,43 @@ chrome.runtime.onMessage.addListener(function (
sendResponse({ data: { credentials: credentials ?? [] } });
}

if (sender.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
message.type +
"-" +
message.subtype
);

if (
message.type === "authentication" &&
message.subtype === "check-agent-connection"
) {
const isConnected = await signifyService.isConnected();
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'),
}
sendResponse({ data: { headers: headers } });

}

if (
message.type === "fetch-resource" &&
message.subtype === "tab-signin"
Expand All @@ -104,35 +126,17 @@ chrome.runtime.onMessage.addListener(function (
sendResponse({ data: { signins: signins ?? [] } });
}

if (message.type) {
switch (message.type) {
case "getVersion":
const manifestData = chrome.runtime.getManifest();
sendResponse(manifestData.version);
break;
case "isUnlocked":
const [tab] = await chrome.tabs.query({
active: true,
lastFocusedWindow: true,
});
const response = chrome.tabs.sendMessage(tab.id!, {
type: "for_content_script",
});
// sendResponse(true);
break;
case "hasLogin":
sendResponse({ login: "AID 123" });
break;
case "authenticate":
sendResponse({ signature: "ABCD" });
break;
default:
break;
}
}
} else if (senderIsPopup(sender)) {
// handle messages from Popup
console.log("Message received from browser extension popup: " + message);
console.log("Message received from browser extension popup: " + message.type + "-" + message.subtype);

if (
message.type === "authentication" &&
message.subtype === "check-agent-connection"
) {
const isConnected = await signifyService.isConnected();
sendResponse({ data: { isConnected, meta: { tab: sender?.tab } } });
}

if (
message.type === "authentication" &&
Expand Down
4 changes: 2 additions & 2 deletions src/pages/background/services/signify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SignifyClient, Tier, ready, Authenticater } from "signify-ts";
import { userService } from "@pages/background/services/user";
import { configService } from "@pages/background/services/config";


const PASSCODE_TIMEOUT = 5 * 60 * 1000;

const Signify = () => {
let _client: SignifyClient | null;
Expand All @@ -15,7 +15,7 @@ const Signify = () => {
console.log("Timer expired, client and passcode zeroed out");
_client = null;
await userService.removePasscode();
}, 120000);
}, PASSCODE_TIMEOUT);
}

const isConnected = async () => {
Expand Down
27 changes: 21 additions & 6 deletions src/pages/content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,36 @@ window.addEventListener(
removeAlertComponent();
}
break;
case "isUnlocked":
break;
}
}
},
false
);

// Handle messages from background script
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
console.log(sender);
chrome.runtime.onMessage.addListener(async function (message, sender, sendResponse) {
if (sender.origin === "chrome-extension://" + chrome.runtime.id) {
// handle messages from Popup
console.log("Message received from browser extension pupup: " + message);
// sendResponse({ resp: "received" });
console.log("Message received from browser extension: " + message.type+" "+message.subtype);
if (message.type === "tab" && message.subtype === "reload-state") {
removeAlertComponent();
const { data } = await chrome.runtime.sendMessage<IMessage<void>>({
type: "authentication",
subtype: "check-agent-connection",
});
const tabSigninResp = await chrome.runtime.sendMessage<
IMessage<void>
>({
type: "fetch-resource",
subtype: "tab-signin",
});
insertReactComponent({
...data,
signins: tabSigninResp?.data?.signins,
});

}

}
});

Expand Down
11 changes: 7 additions & 4 deletions src/pages/dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ export default function Dialog({ isConnected, tab, signins }): JSX.Element {
const logo = chrome.runtime.getURL("src/assets/img/128_keri_logo.png");
const [showPopupPrompt, setShowPopupPrompt] = useState(false);

const handleSetState = async () => {
const handleSetState = async (state) => {
await chrome.runtime.sendMessage({
type: "tab",
subtype: "set-tab-state",
data: {
appState: APP_STATE.SELECT_IDENTIFIER,
appState: state,
},
});
};

const handleClick = () => {
handleSetState();
handleSetState(APP_STATE.SELECT_IDENTIFIER);
setShowPopupPrompt(true);
};
useEffect(() => {
if (!signins?.length) {
handleSetState();
handleSetState(APP_STATE.SELECT_IDENTIFIER);
setShowPopupPrompt(true);
} else if (!isConnected) {
handleSetState(APP_STATE.KERIA_CONNECT)
setShowPopupPrompt(true);
}
}, []);
Expand Down
29 changes: 24 additions & 5 deletions src/pages/dialog/signin.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import { APP_STATE } from "@pages/popup/constants";

export const SigninItem = ({ signin }): JSX.Element => {

const handleClick = async () => {
const headers = await chrome.runtime.sendMessage({
type: "authentication",
subtype: "get-signed-headers",
data: {
signin: "selectedAID",
},
});
console.log("Signed headers: ", headers);
alert("Signed headers received");
const element = document.getElementById("__root");
if (element) element.remove();
};

return (
<div className="flex m-2 flex-row justify-between p-2 items-start border border-black rounded">
<div>
<p className=" text-start text-sm font-bold">{signin.domain}</p>
<p className=" text-sm text-start font-normal text-gray">
<strong>{signin?.identifier?.name}</strong> (
{signin?.identifier?.prefix?.substr(0, 12)}...)
<p className=" text-start text-sm font-bold">
URL: {signin.domain}
</p>
<p className=" text-start text-sm font-bold">
AID: {signin?.identifier?.name}
</p>
<p className=" text-start text-xs font-bold">
{new Date(signin.updatedAt).toDateString()}
Last used: {new Date(signin.updatedAt).toDateString()}
</p>
</div>
<button
type="button"
onClick={handleClick}
className="text-white bg-green font-medium rounded-full text-sm px-2 py-1 text-center me-2 mb-2"
>
Sign in
Expand Down
7 changes: 7 additions & 0 deletions src/pages/popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export default function Popup(): JSX.Element {
document.body.style.width = "300px";
}
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){

});
});
}
};

useEffect(() => {
Expand Down
1 change: 1 addition & 0 deletions src/pages/popup/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export const APP_STATE = {
DEFAULT: "default",
SELECT_IDENTIFIER: "select-identifier",
SELECT_CREDENTIAL: "select-credential",
KERIA_CONNECT: "keria-connect",
}

0 comments on commit 11441c9

Please sign in to comment.