-
Notifications
You must be signed in to change notification settings - Fork 5
/
popup.js
324 lines (317 loc) · 11.5 KB
/
popup.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
{
"use strict";
let isValidBase56Char = new RegExp(`^[${base56chars}]$`);
let canPrependEnterRE = new RegExp(`[${base56chars}]{4} [${base56chars}]{4} [${base56chars}]{4} [${base56chars}]{4} [${base56chars}]{4}$`);
let canPrependSpaceRE = new RegExp(`[${base56chars}]{4}$`);
function onGenerateNewIdentityClick(evt)
{
//console.log("onGenerateNewIdentityClick");
chrome.runtime.sendMessage({'action': 'createIdentity' }, result => {
//console.log("onGenerateNewIdentityClick", result);
if (result.success)
{
$('form#create textarea[name="identity"]').val(result.textualIdentity);
$('form#create input[name="rescuecode"]').val(result.rescueCode);
$('form#create input[name="enscryptedrescuecode"]').val(result.enscryptedRescueCode);
}
});
}
function onOpenImportIdentityTab(evt)
{
chrome.runtime.sendMessage({'action': 'openImportIdentityTab'}, resp => {});
}
function onVerifyrescuecodeFocus(evt)
{
$('form#create input[name="rescuecode"]').attr("type", "password");
}
function onVerifyrescuecodeBlur(evt)
{
$('form#create input[name="rescuecode"]').attr("type", "text");
}
function onInputInput(evt)
{
this.setCustomValidity("");
}
function onTextualIdentityKeyDown(evt)
{
let ta = evt.target;
let val = ta.value;
if (evt.isTrusted && !evt.ctrlKey && !evt.metaKey && isValidBase56Char.test(evt.key) && ta.selectionStart == ta.selectionEnd && ta.selectionStart == val.length)
{
if (canPrependEnterRE.test(val))
insertStringBeforeCaret(ta, "\n");
else if (canPrependSpaceRE.test(val))
insertStringBeforeCaret(ta, " ");
}
}
function onTextualIdentityKeyUp(evt)
{
let ta = evt.target;
validateTextualIdentity(ta.value).then(validationData => {
$('form#import label+b').text(new Array(validationData.lineNr + 1).join('✅ ') + (validationData.success ? '' : '❌')).attr("title", validationData.message||"");
let wasValid = ta.checkValidity();
ta.setCustomValidity(validationData.message||"");
if (validationData.message)
ta.reportValidity();
else if (!wasValid)
{
ta.blur();
ta.focus();
}
if ("chrome" in window)
{
chrome.runtime.sendMessage({'action': 'importPartialIdentity', "textualIdentity": ta.value}, result => {
if (!result || !result.success)
{
console.warn("importPartialIdentity", result);
}
});
}
}).catch(err => {
console.warn("popup.onTextualIdentityKeyUp", "ERRVA000", err);
});
}
function onCreateFormSubmit(evt)
{
evt.preventDefault();
let elems = this.elements;
if (elems.verifyrescuecode.value === elems.rescuecode.value)
{
elems.verifyrescuecode.setCustomValidity("");
if (elems.verifypassword.value === elems.password.value)
{
elems.verifypassword.setCustomValidity("");
let enscryptedRescueCode = JSON.parse(elems.enscryptedrescuecode.value);
chrome.runtime.sendMessage({'action': 'importIdentity', "textualIdentity": elems.identity.value, "rescueCode": elems.rescuecode.value, "enscryptedRescueCode": enscryptedRescueCode, "password": elems.password.value, "print": true}, result => {
memzero(enscryptedRescueCode);
elems[elems.length - 1].parentNode.className = result.success ? "success" : "failure";
if (result == null)
showGenericError("onCreateFormSubmit", "ERRCFS--1", "Problem communicating with background");
else if (result.success)
{
/*
elems.identity.value = "";
elems.rescuecode.value = "";
elems.verifyrescuecode.value = "";
elems.enscryptedrescuecode.value = "";
setPopupState();
*/
}
else if ("errorCode" in result)
showGenericError("onCreateFormSubmit", "ERRCFS--1", "Unknown error code " + result.errorCode);
else
showGenericError("onCreateFormSubmit", "ERRCFS--2", "Missing error code");
});
}
else
elems.verifypassword.setCustomValidity("Password mismatch.");
}
else
elems.verifyrescuecode.setCustomValidity("Rescue Code mismatch.\nFirst write the Rescue Code down on paper, then enter it here.");
}
function onImportFormSubmit(evt)
{
evt.preventDefault();
let btn = this.querySelector('button:not([type])');
btn.classList.add("wait");
let elems = this.elements;
if (elems.verifypassword.value === elems.password.value)
{
elems.verifypassword.setCustomValidity("");
elems[elems.length - 1].parentNode.className = "";
chrome.runtime.sendMessage({'action': 'importIdentity', "textualIdentity": elems.identity.value, "rescueCode": elems.rescuecode.value, "password": elems.password.value, "print": false}, result => {
//console.log("onImportFormSubmit", result);
elems[elems.length - 1].parentNode.className = result.success ? "success" : "failure";
btn.classList.remove("wait");
if (result.success)
{
elems.identity.value = "";
elems.rescuecode.value = "";
$('form#import label+b').text("").attr("title", "");
setPopupState();
$('#tab2')[0].checked = false;
}
else if ("errorCode" in result)
{
if (result.errorCode == "ERRII004")
showGenericError("", "", "Invalid Rescue Code");
else
showGenericError("onCreateFormSubmit", "ERRIFS--2", "Unknown error code " + result.errorCode);
}
});
}
else
elems.verifypassword.setCustomValidity("Password mismatch.");
}
function onChangepasswordFormSubmit(evt)
{
evt.preventDefault();
//console.log(this, evt);
}
function onDeletepasswordFormSubmit(evt)
{
evt.preventDefault();
//console.log(this, evt);
}
function onEraseidentityFormSubmit(evt)
{
evt.preventDefault();
chrome.runtime.sendMessage({'action': 'eraseIdentity' }, error => {
let btn = $('form#eraseidentity button')[0];
btn.className = error ? "failure" : "success";
btn.title = error || "";
setPopupState();
});
}
function onSettingsFormSubmit(evt)
{
evt.preventDefault();
let elems = this.elements;
chrome.runtime.sendMessage({'action': 'savePassword', 'savepwd': elems.savepwd.checked }, resp => {
$('form#settings button').addClass("success");
});
}
function onPasswdFormSubmit(evt)
{
evt.preventDefault();
let btn = this.querySelector('button');
btn.classList.add("wait");
let elems = this.elements;
chrome.runtime.sendMessage({"action": "sendPostDataToActiveTab", "password": document.querySelector('form#passwd>label.pwd').classList.contains("hasPassword") == "none" ? null : elems.password.value}, resp => {
//console.log("popup.onPasswdFormSubmit", "sendPostDataToActiveTab", JSON.stringify(resp));
btn.classList.remove("wait");
if (resp == null)
showGenericError("onPasswdFormSubmit", "ERRPFS--1", "Problem communicating with background");
else
{
if (resp.success)
{
if (resp.hasOpenRequest)
window.close();
else
elems.password.value = "";
}
else if ("errorCode" in resp)
{
if (resp.errorCode == "ERRGI003")//"ERRPD008")
showPasswordError(elems.password);
else if (resp.errorCode == "ERRFE012")
showGenericError("IP MISMATCH", "", "The SQRL server reported that the IP address of the website visitor was different from the IP address of the SQRL client.\nThis may indicate a Man-In-The-Middle (MITM) interception attack.");
else
showGenericError("onPasswdFormSubmit", "ERRPFS--2", "Unknown error code " + resp.errorCode);
}
else
showGenericError("onPasswdFormSubmit", "ERRPFS--3", "Missing error code");
}
});
}
function setPopupState()
{
chrome.runtime.sendMessage({'action': 'hasIdentity' }, result => {
//$('form#passwd input').enable(result.hasIdentity).val(result.hasPassword ? "........" : "");
$('form#settings input[name="savepwd"]').val(result.isSavepwd);
$('#tab1,#tab2').enable(!result.hasIdentity);
$('#tab3,#tab4,#tab5,#tab6').enable(result.hasIdentity);
$('#identityhash').text(result.hasIdentity && "textualIdentity" in result ? result.textualIdentity.substr(0, 4) : "");
$('form#export textarea[name="identity"]').val(result.hasIdentity && "textualIdentity" in result ? result.textualIdentity : "");
if (result.hasIdentity)
{
if ("textualIdentity" in result)
{
let identity = parseTextualIdentity(result.textualIdentity);
let qrData = new Uint8Array(identity.length + 8);
qrData.set([115,113,114,108,100,97,116,97], 0);//'sqrldata'
qrData.set(identity, 8);//'sqrldata'
makeQR(document.querySelector('form#export img#exportqr'), qrData);
}
}
else
{
$('#tab9').trigger("click");
}
if ("partialTextualIdentity" in result && result.partialTextualIdentity != null && result.partialTextualIdentity != "")
{
let ta = document.querySelector('form#import textarea[name="identity"]');
ta.value = result.partialTextualIdentity;
document.querySelector('#tab2').checked = true;
onTextualIdentityKeyUp({"target": ta});
}
});
}
function onInputInputValidate(evt)
{
evt.target.setCustomValidity('');
evt.target.checkValidity();
}
function onInputInvalidValidate(evt)
{
evt.target.setCustomValidity(evt.target.getAttribute("data-errormessage"));
}
function init()
{
if ("chrome" in window && "runtime" in chrome)
{
chrome.runtime.sendMessage({'action': 'hasPendingRequest' }, result => { //{"success": true, "hasPendingRequest": true, "hasPassword": true, "isSameOrigin": false, "linkOrigin": "http://a.com:8080"}
if (chrome.runtime.lastError)
console.warn("popup.init", "ERRIN000", "Could not sendMessage hasPendingRequest");
else if (result && result.hasPendingRequest)
{
document.body.classList.add("passwdOnly");
$('form#passwd>code').text(result.linkOrigin.replace(/^https:\/\//, "")).toggleClass("warn", !result.isSameOrigin);
$('form#passwd>label.pwd').toggleClass("hasPassword", result.hasPassword);
if (result.isSameOrigin)
{
if (result.hasPassword) //password is known to background
{
chrome.runtime.sendMessage({"action": "sendPostDataToActiveTab", "password": null}, resp => {
//console.log("popup.init", "sendPostDataToActiveTab", resp);
if (resp != null && resp.success && resp.hasOpenRequest)
{
window.close();
}
});
}
else
{
document.querySelector('form#passwd input[name="password"]').focus();
}
}
}
});
setPopupState();
$('#version').text(chrome.runtime.getManifest().version);
}
// [ form#create, form#import, form#changepassword, form#deletepassword, form#eraseidentity, form#settings ]
$('form#create').submit(onCreateFormSubmit);
$('button#generateNewIdentity').click(onGenerateNewIdentityClick);
$('form#import').submit(onImportFormSubmit);
$('button#openImportIdentityTab').click(onOpenImportIdentityTab);
$('form#changepassword').submit(onChangepasswordFormSubmit);
$('form#deletepassword').submit(onDeletepasswordFormSubmit);
$('form#eraseidentity').submit(onEraseidentityFormSubmit);
$('form#create input[name="verifyrescuecode"]').focus(onVerifyrescuecodeFocus).blur(onVerifyrescuecodeBlur).bind("input", onInputInput);
$('form input[name="verifypassword"]').bind("input", onInputInput);
$('form#import textarea[name="identity"]').keydown(onTextualIdentityKeyDown).keyup(onTextualIdentityKeyUp);
$('form#settings').submit(onSettingsFormSubmit);
$('form#passwd').submit(onPasswdFormSubmit);
$('input[data-errormessage]').bind("input", onInputInputValidate).bind("invalid", onInputInvalidValidate);
}
if ("chrome" in window && "runtime" in chrome)
{
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action == "createIdentity.enscryptUpdate")
{
let prgrss = $('form#create progress').val(request.step)[0];
if (prgrss && prgrss.max != request.max)
prgrss.max = request.max;
}
else if (request.action == "importIdentity.enscryptUpdate")
{
let prgrss = $('form#import progress').val(request.step)[0];
if (prgrss && prgrss.max != request.max)
prgrss.max = request.max;
}
});
}
document.addEventListener("DOMContentLoaded", init);
}