-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.js
164 lines (152 loc) · 4.92 KB
/
background.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
var lnAuths = {};
var webClients = localStorage["clients"] ? JSON.parse(localStorage["clients"]) : {};
const LNREALMS_REGEXP = "^ln(payreq|sign|signpayreq):";
function getWebClientFromUrl(url) {
for (var endpointProp in webClients) {
if (webClients.hasOwnProperty(endpointProp)) {
if (url.startsWith(endpointProp)) {
return webClients[endpointProp];
}
}
}
return null;
}
function webClientSocketHandshakeListenerFunction (details) {
console.log("onBeforeSendHeaders webclient", details);
var webClient = getWebClientFromUrl(details.url);
if (webClient) {
details.requestHeaders.push({
name: "Authorization",
value: "Basic " + btoa(webClient.login + ":" + webClient.password)
});
return { requestHeaders: details.requestHeaders };
}
}
function webClientAuthenticateListenerFunction (details, callback) {
console.log("onAuthRequired webclient", details);
var re = new RegExp(LNREALMS_REGEXP, "i");
if (re.test(details.realm)) { // handled by other listener
callback();
} else {
var webClient = getWebClientFromUrl(details.url);
if (webClient) {
callback({ authCredentials: { username: webClient.login, password: webClient.password }});
} else {
callback();
}
}
}
function getWebClientListenerUrls (suffix) {
var urls = [];
for (var endpointProp in webClients) {
if (webClients.hasOwnProperty(endpointProp)) {
urls.push(endpointProp + suffix);
}
}
return urls;
}
function refreshWebClientContext () {
webClients = localStorage["clients"] ? JSON.parse(localStorage["clients"]) : {};
chrome.webRequest.onBeforeSendHeaders.removeListener(
webClientSocketHandshakeListenerFunction); // Remove existing listener
chrome.webRequest.onBeforeSendHeaders.addListener(
webClientSocketHandshakeListenerFunction,
{ urls: getWebClientListenerUrls("/socket.io/*") },
[ "blocking", "requestHeaders" ]);
chrome.webRequest.onAuthRequired.removeListener(
webClientAuthenticateListenerFunction); // Remove existing listener
chrome.webRequest.onAuthRequired.addListener(
webClientAuthenticateListenerFunction,
{ urls: getWebClientListenerUrls("/*") },
[ "asyncBlocking", "responseHeaders" ]);
}
refreshWebClientContext ();
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function waitForAuthValidation(requestId, callback) {
var lnAuth;
while (true) {
lnAuth = lnAuths[requestId];
if (!lnAuth || !lnAuth.running) break;
await sleep(100); // sleep 100 ms
}
if (lnAuth && lnAuth.proof) {
callback({ authCredentials: { username: lnAuth.proof[0], password: lnAuth.proof[1] }});
} else {
callback({ cancel: true });
}
}
chrome.webRequest.onAuthRequired.addListener(function (details, callback) {
console.log("onAuthRequired default", details);
var re = new RegExp(LNREALMS_REGEXP, "i");
if (re.test(details.realm)) {
var requestId = details.requestId;
if (details.realm.startsWith("lnpayreq:")) {
lnAuths[requestId] = { requestId: requestId, running: true };
var payreq = details.realm.substr(9);
var url = "payment.html?payreq=" + encodeURIComponent(payreq) + "&request=" + requestId;
chrome.windows.create({
url: url,
type: "popup",
width:600,
height: 480,
left: 300,
top: 150,
focused: true
});
waitForAuthValidation(requestId, callback);
return;
} else if (details.realm.startsWith("lnsign:")) {
lnAuths[requestId] = { requestId: requestId, running: true };
var message = details.realm.substr(7);
var url = "signature.html?message=" + encodeURIComponent(message) + "&request=" + requestId;
chrome.windows.create({
url: url,
type: "popup",
width:600,
height: 400,
left: 300,
top: 150,
focused: true
});
waitForAuthValidation(requestId, callback);
return;
} else if (details.realm.startsWith("lnsignpayreq:")) {
lnAuths[requestId] = { requestId: requestId, running: true };
var payreq = details.realm.substr(13);
var url = "signpayreq.html?payreq=" + encodeURIComponent(payreq) + "&request=" + requestId;
chrome.windows.create({
url: url,
type: "popup",
width:600,
height: 620,
left: 300,
top: 120,
focused: true
});
waitForAuthValidation(requestId, callback);
return;
}
}
callback();
}, { urls: [ "*://*/*" ] }, [ "asyncBlocking", "responseHeaders" ]);
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
console.log("onMessage (request, sender)", request, sender);
if (request.state === "validated") {
lnAuths[request.reqid].proof = request.proof;
lnAuths[request.reqid].running = false;
sendResponse({ state: "success" });
} else if (request.state === "canceled") {
delete lnAuths[request.reqid];
sendResponse({ state: "success" });
} else if (request.state === "closed") {
if (lnAuths[request.reqid] && !lnAuths[request.reqid].proof) { // not validating
delete lnAuths[request.reqid];
}
} else if (request.state === "refresh") {
refreshWebClientContext();
}
}
);