diff --git a/example-web/my-app/package-lock.json b/example-web/my-app/package-lock.json index cd1f97ea..6acc438e 100644 --- a/example-web/my-app/package-lock.json +++ b/example-web/my-app/package-lock.json @@ -16030,7 +16030,7 @@ }, "node_modules/signify-polaris-web": { "version": "1.0.2", - "resolved": "git+ssh://git@github.com/roots-id/polaris-web.git#e2ff948985e523bb1aed1e7489476a32328480a1", + "resolved": "git+ssh://git@github.com/roots-id/polaris-web.git#e240dd3d0e5d4ad4aa5bbe31d464feb8f7515c5a", "license": "ISC" }, "node_modules/sisteransi": { diff --git a/example-web/my-app/src/App.js b/example-web/my-app/src/App.js index 08fba4dd..9956c4cc 100644 --- a/example-web/my-app/src/App.js +++ b/example-web/my-app/src/App.js @@ -8,7 +8,8 @@ import { requestCredential, requestAidORCred, trySettingVendorUrl, -} from "./signify-polaris-web"; + canCallAsync, +} from "signify-polaris-web"; import Button from "@mui/material/Button"; import Alert from "@mui/material/Alert"; import CircularProgress from "@mui/material/CircularProgress"; @@ -66,6 +67,18 @@ function App() { setParsedSignifyData(null); }; + const handleRequestAutoSignin = async () => { + console.log("canCallAsync()", canCallAsync()); + if (canCallAsync()) { + const resp = await requestAutoSignin(); + if (resp) { + handleSignifyData(resp); + } + } else { + requestAutoSignin(); + } + }; + const renderData = () => { if (extensionInstalled === null) return ; @@ -93,7 +106,7 @@ function App() { diff --git a/example-web/my-app/src/signify-polaris-web/index.js b/example-web/my-app/src/signify-polaris-web/index.js deleted file mode 100644 index 4bd28d19..00000000 --- a/example-web/my-app/src/signify-polaris-web/index.js +++ /dev/null @@ -1,105 +0,0 @@ -import { pubsub } from "./pubsub"; - -var extensionId = ""; - -window.addEventListener( - "message", - async (event) => { - // Accept messages only from same window - if (event.source !== window) { - return; - } - - if (event.data.type && event.data.type === "signify-extension") { - console.log("Content script loaded from polaris-web"); - extensionId = event.data.data.extensionId; - pubsub.publish("signify-extension-loaded", extensionId); - } - - if (event.data.type && event.data.type === "signify-signature") { - pubsub.publish("signify-signature", event.data.data); - } - }, - false -); - -const requestAid = () => { - window.postMessage({ type: "select-identifier" }, "*"); -}; - -const requestCredential = () => { - window.postMessage({ type: "select-credential" }, "*"); -}; - -const requestAidORCred = () => { - window.postMessage({ type: "select-aid-or-credential" }, "*"); -}; - -const requestAutoSignin = async () => { - window.postMessage( - { - type: "fetch-resource", - subtype: "auto-signin-signature", - }, - "*" - ); - // const { data, error } = await chrome.runtime.sendMessage(extensionId, { - // type: "fetch-resource", - // subtype: "auto-signin-signature", - // }); - // if (error) { - // window.postMessage({ type: "select-auto-signin" }, "*"); - // } else { - // pubsub.publish("signify-signature", data); - // } -}; - -const isExtensionInstalled = (func) => { - const timeout = setTimeout(() => { - func(false); - }, 1000); - pubsub.subscribe("signify-extension-loaded", (_event, data) => { - func(data); - clearTimeout(timeout); - pubsub.unsubscribe("signify-extension-loaded"); - }); -}; - -const trySettingVendorUrl = async (vendorUrl) => { - window.postMessage( - { - type: "vendor-info", - subtype: "attempt-set-vendor-url", - data: { - vendorUrl, - }, - }, - "*" - ); - // await chrome.runtime.sendMessage(extensionId, { - // type: "vendor-info", - // subtype: "attempt-set-vendor-url", - // data: { - // vendorUrl, - // }, - // }); -}; - -const subscribeToSignature = (func) => { - pubsub.subscribe("signify-signature", (_event, data) => func(data)); -}; - -const unsubscribeFromSignature = () => { - pubsub.unsubscribe("signify-signature"); -}; - -export { - requestAid, - requestCredential, - requestAidORCred, - requestAutoSignin, - subscribeToSignature, - unsubscribeFromSignature, - isExtensionInstalled, - trySettingVendorUrl, -}; diff --git a/example-web/my-app/src/signify-polaris-web/pubsub.js b/example-web/my-app/src/signify-polaris-web/pubsub.js deleted file mode 100644 index b9cfba97..00000000 --- a/example-web/my-app/src/signify-polaris-web/pubsub.js +++ /dev/null @@ -1,51 +0,0 @@ -export const pubsub = (() => { - const events = {}; - - let subscribersId = -1; - - function publish(event, data) { - if (!events[event]) { - return false; - } - - const subscribers = events[event]; - subscribers.forEach((subscriber) => { - subscriber.func(event, data); - }); - return true; - } - - function subscribe(event, func) { - if (!events[event]) { - events[event] = []; - } - - subscribersId += 1; - const token = subscribersId.toString(); - events[event].push({ - token, - func, - }); - return token; - } - - function unsubscribe(token) { - const found = Object.keys(events).some((event) => - events[event].some((subscriber, index) => { - const areEqual = subscriber.token === token.toString(); - if (areEqual) { - events[event].splice(index, 1); - } - return areEqual; - }) - ); - - return found ? token : null; - } - - return { - publish, - subscribe, - unsubscribe, - }; -})(); diff --git a/src/components/credentialList.tsx b/src/components/credentialList.tsx index 55aeb6ad..1ad70007 100644 --- a/src/components/credentialList.tsx +++ b/src/components/credentialList.tsx @@ -35,7 +35,7 @@ export function CredentialList(): JSX.Element { ))} {!isLoading && !credentials?.length ? ( -

{formatMessage({ id: "message.noItems" })}

+

{formatMessage({ id: "message.noItems" })}

) : ( <> )} diff --git a/src/components/identifierList.tsx b/src/components/identifierList.tsx index 50d6616e..0b34656b 100644 --- a/src/components/identifierList.tsx +++ b/src/components/identifierList.tsx @@ -94,7 +94,7 @@ export function IdentifierList(): JSX.Element { ))} {!isLoading && !aids?.length ? ( -

{formatMessage({ id: "message.noItems" })}

+

{formatMessage({ id: "message.noItems" })}

) : ( <> )} diff --git a/src/components/signinList.tsx b/src/components/signinList.tsx index 552ce26d..c902f431 100644 --- a/src/components/signinList.tsx +++ b/src/components/signinList.tsx @@ -91,7 +91,7 @@ export function SigninList(): JSX.Element { ))} {!isLoading && !signins?.length ? ( -

{formatMessage({ id: "message.noItems" })}

+

{formatMessage({ id: "message.noItems" })}

) : ( <> )} diff --git a/src/pages/background/index.ts b/src/pages/background/index.ts index 135b0355..9c1590e4 100644 --- a/src/pages/background/index.ts +++ b/src/pages/background/index.ts @@ -83,7 +83,6 @@ chrome.runtime.onMessage.addListener(function ( // Validate that message comes from a page that has a signin const origin = removeSlash(sender.url); const signins = await getSigninsByDomain(origin); - console.log("signins", signins); const autoSignin = signins?.find((signin) => signin.autoSignin); if (!signins?.length || !autoSignin) { sendResponse({ @@ -318,63 +317,35 @@ chrome.runtime.onMessageExternal.addListener(function ( console.log("Message received from external source: ", sender); console.log("Message received from external request: ", message); - // if ( - // message.type === "fetch-resource" && - // message.subtype === "auto-signin-signature" - // ) { - // // Validate that message comes from a page that has a signin - // const origin = removeSlash(sender.url); - // const signins = await getSigninsByDomain(origin); - // console.log("signins", signins); - // const autoSignin = signins?.find((signin) => signin.autoSignin); - // if (!signins?.length || !autoSignin) { - // sendResponse({ - // error: { code: 404, message: "auto signin not found" }, - // }); - // return; - // } - - // const signedHeaders = await signifyService.signHeaders( - // // sigin can either have identifier or credential - // autoSignin?.identifier - // ? autoSignin?.identifier?.name - // : autoSignin?.credential?.issueeName, - // origin - // ); - // let jsonHeaders: { [key: string]: string } = {}; - // for (const pair of signedHeaders.entries()) { - // jsonHeaders[pair[0]] = pair[1]; - // } - // sendResponse({ data: { headers: jsonHeaders } }); - // } - - - // if ( - // message.type === "vendor-info" && - // message.subtype === "attempt-set-vendor-url" - // ) { - // const { vendorUrl } = message?.data ?? {}; - // if (!vendorUrl) { - // return; - // } - - // const _vendorUrl = await configService.getUrl(); - // if (!_vendorUrl) { - // try { - // const resp = await (await fetch(vendorUrl)).json(); - // if (resp?.agentUrl) { - // await configService.setAgentUrl(resp?.agentUrl); - // } - // await configService.setData(resp); - // if (resp?.icon) { - // await setActionIcon(resp?.icon); - // } - // await configService.setUrl(vendorUrl); - // } catch (error) {} - // } + if ( + message.type === "fetch-resource" && + message.subtype === "auto-signin-signature" + ) { + // Validate that message comes from a page that has a signin + const origin = removeSlash(sender.url); + const signins = await getSigninsByDomain(origin); + console.log("signins", signins); + const autoSignin = signins?.find((signin) => signin.autoSignin); + if (!signins?.length || !autoSignin) { + sendResponse({ + error: { code: 404, message: "auto signin not found" }, + }); + return; + } - // sendResponse({ data: { _vendorUrl } }); - // } + const signedHeaders = await signifyService.signHeaders( + // sigin can either have identifier or credential + autoSignin?.identifier + ? autoSignin?.identifier?.name + : autoSignin?.credential?.issueeName, + origin + ); + let jsonHeaders: { [key: string]: string } = {}; + for (const pair of signedHeaders.entries()) { + jsonHeaders[pair[0]] = pair[1]; + } + sendResponse({ data: { headers: jsonHeaders } }); + } })(); // return true to indicate chrome api to send a response asynchronously diff --git a/src/pages/content/index.tsx b/src/pages/content/index.tsx index 22dafd4e..46ef9fa2 100644 --- a/src/pages/content/index.tsx +++ b/src/pages/content/index.tsx @@ -110,7 +110,6 @@ window.addEventListener( { type: "signify-signature", data: data }, "*" ); - // pubsub.publish("signify-signature", data); } }