-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
200 lines (186 loc) · 4.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { nanoid } from "nanoid";
import { pubsub } from "./pubsub";
import { canCallAsync } from "./utils";
var extensionId = "";
window.addEventListener(
"message",
async (event) => {
// Accept messages only from same window
if (event.source !== window) {
return;
}
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 === "signify-signature" && event.data.requestId) {
pubsub.publish(event.data.requestId, event.data.data);
}
},
false
);
/**
*
* @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, rurl }, "*");
pubsub.subscribe(requestId, (_event, data) => {
resolve(data);
pubsub.unsubscribe(requestId);
});
});
};
/**
*
* @param {string} rurl resource url for which credential is requested
* @returns
*/
const requestCredential = (rurl) => {
return new Promise((resolve) => {
const requestId = nanoid();
window.postMessage({ type: "select-credential", requestId, rurl }, "*");
pubsub.subscribe(requestId, (_event, data) => {
resolve(data);
pubsub.unsubscribe(requestId);
});
});
};
/**
*
* @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, rurl }, "*");
pubsub.subscribe(requestId, (_event, data) => {
resolve(data);
pubsub.unsubscribe(requestId);
});
});
};
/**
*
* @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
* like firefox chrome.runtime can only be accesed by content script.
* canCallAsync() means sendMessage api can be used otherwise we postMessage to content script that
* communicates with service-worker.
*/
const requestId = nanoid();
if (canCallAsync()) {
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, rurl },
"*"
);
pubsub.subscribe(requestId, (_event, data) => {
resolve(data);
pubsub.unsubscribe(requestId);
});
} else {
reject(error);
}
} else {
resolve(data);
}
} else {
window.postMessage(
{
type: "fetch-resource",
subtype: "auto-signin-signature",
requestId,
rurl
},
"*"
);
pubsub.subscribe(requestId, (_event, data) => {
resolve(data);
pubsub.unsubscribe(requestId);
});
}
});
};
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, rurl, reqInit: req },
});
if (error && error.message) {
throw new Error(error.message);
}
req.headers = { ...(req.headers ?? {}), ...(data.headers ?? {}) };
} else {
req.headers = {
...(req.headers ?? {}),
rurl,
"x-aid-name": aidName,
};
}
}
return req.headers;
};
const isExtensionInstalled = () => {
return new Promise((resolve) => {
const timeout = setTimeout(() => {
resolve(false);
}, 1000);
pubsub.subscribe("signify-extension-loaded", (_event, extensionId) => {
resolve(extensionId);
clearTimeout(timeout);
pubsub.unsubscribe("signify-extension-loaded");
});
});
};
const trySettingVendorUrl = async (vendorUrl) => {
window.postMessage(
{
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,
canCallAsync,
signifyHeaders,
};