Skip to content

Commit

Permalink
signed header changes but needs testing (#9)
Browse files Browse the repository at this point in the history
* signed header changes but needs testing

Signed-off-by: 2byrds <[email protected]>

* feat: add rurl for signed_headers

---------

Signed-off-by: 2byrds <[email protected]>
Co-authored-by: Hunain Bin Sajid <[email protected]>
  • Loading branch information
2byrds and HunnySajid authored May 21, 2024
1 parent 66ae493 commit 697eb6a
Showing 1 changed file with 44 additions and 21 deletions.
65 changes: 44 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,73 @@ window.addEventListener(
return;
}

if (event.data.type && event.data.type === "signify-extension") {
if (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" &&
event.data.requestId
) {
if (event.data?.type === "signify-signature" && event.data.requestId) {
pubsub.publish(event.data.requestId, event.data.data);
}
},
false
);

const requestAid = () => {
/**
*
* @param {string} rurl resource url for which AID is requested
* @returns
*/
const requestAid = (rurl) => {
return new Promise((resolve) => {
const requestId = nanoid();
window.postMessage({ type: "select-identifier", requestId }, "*");
window.postMessage({ type: "select-identifier", requestId, rurl }, "*");
pubsub.subscribe(requestId, (_event, data) => {
resolve(data);
pubsub.unsubscribe(requestId);
});
});
};

const requestCredential = () => {
/**
*
* @param {string} rurl resource url for which credential is requested
* @returns
*/
const requestCredential = (rurl) => {

This comment has been minimized.

Copy link
@lenkan

lenkan May 23, 2024

Contributor

@2byrds I do not understand the purpose of this rurl parameter. What is it being used for in signify-browser-extension?

This comment has been minimized.

Copy link
@2byrds

2byrds May 23, 2024

Author Collaborator

previously, whenever you saw 'url' that was the webapp url (origin). we are distinguishing between wurl (webapp url) and rurl (request ur) so that we can properly sign the headers for requests.

This comment has been minimized.

Copy link
@lenkan

lenkan May 23, 2024

Contributor

But why does requestCredential need to sign headers for a request url?

This comment has been minimized.

Copy link
@2byrds

2byrds May 23, 2024

Author Collaborator

I believe we (Rodo, Hunny, and I) settled on the idea that we aren't making these calls in a vacuum. That each call is intended for a corresponding request. We discussed if we should make a call to retrieve a credential and then make a call to sign a request that contains that credential. I believe for now we decided to combine them for a single call. We would be happy to discuss that with you and modify if needed. @HunnySajid and @rodolfomiranda are welcome to weigh-in as well.

This comment has been minimized.

Copy link
@lenkan

lenkan May 23, 2024

Contributor

I thought the idea was to

  1. Web app request credential
  2. Extension responds with credential CESR stream
  3. Web app sends credential CESR to verifier

I don't understand how the request url is relevant in that flow.

For signed headers, it would be

  1. Web app requests signed headers for request url
  2. Extension responds with the signed headers
  3. Web app sends the request to the request url

This comment has been minimized.

Copy link
@rodolfomiranda

rodolfomiranda May 23, 2024

Collaborator

The credential part is actually two step, send a credential for verification, and send sign headers with the AID of the issuee of that credential so the backend can verify that who is submitting the credential is the controller of the issuee AID.
I think it's nice to have all the information to make just one call with all of it, instead of making two calls.

return new Promise((resolve) => {
const requestId = nanoid();
window.postMessage({ type: "select-credential", requestId }, "*");
window.postMessage({ type: "select-credential", requestId, rurl }, "*");
pubsub.subscribe(requestId, (_event, data) => {
resolve(data);
pubsub.unsubscribe(requestId);
});
});
};

const requestAidORCred = () => {
/**
*
* @param {string} rurl resource url for which AID or credential is requested
* @returns
*/
const requestAidORCred = (rurl) => {
return new Promise((resolve) => {
const requestId = nanoid();
window.postMessage({ type: "select-aid-or-credential", requestId }, "*");
window.postMessage({ type: "select-aid-or-credential", requestId, rurl }, "*");
pubsub.subscribe(requestId, (_event, data) => {
resolve(data);
pubsub.unsubscribe(requestId);
});
});
};

const requestAutoSignin = async () => {
/**
*
* @param {string} rurl resource url for which auto signin is requested
* @returns
*/
const requestAutoSignin = async (rurl) => {
return new Promise(async (resolve, reject) => {
/**
* In chrome or brave, chrome.runtime is accessible in webpages but in other browsers
Expand All @@ -75,10 +91,16 @@ const requestAutoSignin = async () => {
const { data, error } = await chrome.runtime.sendMessage(extensionId, {
type: "fetch-resource",
subtype: "auto-signin-signature",
data: {
rurl,
},
});
if (error) {
if (error.code === 404) {
window.postMessage({ type: "select-auto-signin", requestId }, "*");
window.postMessage(
{ type: "select-auto-signin", requestId, rurl },
"*"
);
pubsub.subscribe(requestId, (_event, data) => {
resolve(data);
pubsub.unsubscribe(requestId);
Expand All @@ -95,6 +117,7 @@ const requestAutoSignin = async () => {
type: "fetch-resource",
subtype: "auto-signin-signature",
requestId,
rurl
},
"*"
);
Expand All @@ -106,27 +129,27 @@ const requestAutoSignin = async () => {
});
};

const signifyFetch = async (url, req, fetchHeaders = false, aidName = "") => {
if (fetchHeaders && aidName) {
const signifyHeaders = async (rurl, req, aidName = "") => {
if (aidName) {
if (canCallAsync()) {
const { data, error } = await chrome.runtime.sendMessage(extensionId, {
type: "fetch-resource",
subtype: "signify-headers",
data: { aidName },
data: { aidName, rurl, reqInit: req },
});
if (error && error.message) {
throw new Error(error.message);
}
req.headers = { ...(req.headers ?? {}), ...(data ?? {}) };
req.headers = { ...(req.headers ?? {}), ...(data.headers ?? {}) };
} else {
req.headers = {
...(req.headers ?? {}),
"x-append-signify-headers": "true",
rurl,
"x-aid-name": aidName,
};
}
}
return window.fetch(url, req);
return req.headers;
};

const isExtensionInstalled = () => {
Expand Down Expand Up @@ -173,5 +196,5 @@ export {
isExtensionInstalled,
trySettingVendorUrl,
canCallAsync,
signifyFetch,
signifyHeaders,
};

0 comments on commit 697eb6a

Please sign in to comment.