-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathoptions.js
595 lines (467 loc) · 19.2 KB
/
options.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
shadowCrypt = {};
shadowCrypt.KeyManagement = new function () {
// Modal UI helpers
var dialogCallback;
var keyDialogCallback;
var keyDialogDeleteCallback;
// Generic function to display a modal element
function displayModal(elementSelector, fadeOut) {
var element = $(elementSelector);
var modal = element.parents(".modal");
modal.add(element).addClass("displayed");
if (fadeOut === undefined) fadeOut = true;
if (fadeOut) {
modal.addClass("fade-out");
} else {
modal.removeClass("fade-out");
}
}
// Generic function to hide a modal element
function hideModal(modal) {
var displayedElements = modal.children(".displayed");
modal.add(displayedElements).removeClass("displayed");
}
// Displaying the normal dialogs
function displayOKCancelDialog(content, callback) {
dialogCallback = callback;
$("#ok-cancel-dialog").find(".dialog-content").html(content);
displayModal("#ok-cancel-dialog");
}
function displayCloseDialog(content) {
$("#close-dialog").find(".dialog-content").html(content);
displayModal("#close-dialog");
}
function hideDialog() {
hideModal($("#dialog-box"));
}
// Displaying the key dialog
var activeDialogClass = null;
function displayKeyDialog(dialogClass, callback, deleteCallback) {
keyDialogCallback = callback;
keyDialogDeleteCallback = deleteCallback;
var dialog = $("#key-dialog");
if (activeDialogClass) {
dialog.removeClass(activeDialogClass);
}
activeDialogClass = dialogClass;
dialog.addClass(activeDialogClass);
displayModal("#key-dialog");
}
function hideKeyDialog() {
hideModal($("#key-dialog-modal"));
}
// Key icon color.
var activeEditKeyColor = 0;
function changeEditKeyColor(newColor) {
var keyIcon = $("#edit-key-title").find(".shadowcrypt-key");
keyIcon.removeClass("shadowcrypt-color-" + activeEditKeyColor);
activeEditKeyColor = newColor;
keyIcon.addClass("shadowcrypt-color-" + activeEditKeyColor);
}
function resetEditKeyColor(newColor) {
changeEditKeyColor(newColor);
$("#key-dialog-color" + activeEditKeyColor).prop("checked", true);
}
// Callback for when something is changed in the edit key dialog.
var onKeyChangeCallback;
// Key Management Main Initialize
this.initialize = function (domains, newKeyCallback, editKeyCallback, deleteKeyCallback, exportKeyFunction, importKeyFunction, defaultChangedCallback) {
// Populate the table of keys.
var keysTable = $("#domain-list").find(".keys-table");
var globalHoverCounter = 0;
var tooltipActive = false;
function createKeyUI(domain, key, defaultKey, url) {
var keyButton = $("<div class='key-holder" + (defaultKey ? " default-key" : "") +
"'><div class='shadowcrypt-key shadowcrypt-color-" + key.color +
"'></div><div class='key-name-wrapper'><div class='shadowcrypt-key-name shadowcrypt-color-" + key.color +
"'>" + key.name + "</div></div></div>");
// Edit key button.
keyButton.click(function () {
originLength = "origin-".length;
$("#edit-key-domain").text(url.slice(originLength));
$("#key-dialog-name").val(key.name);
$("#key-dialog-passphrase").val(key.passphrase);
$("#key-dialog-note").val(key.note);
for (var keyFingerprint in domain.keys) {
if (domain.keys[keyFingerprint] == key) break;
}
$("#key-dialog-default").prop("checked", domain.defaultFingerprint == keyFingerprint);
$("#key-dialog-export").text(exportKeyFunction(url, key));
var keyCopy = jQuery.extend({}, key);
onKeyChangeCallback = function() {
keyCopy.name = $("#key-dialog-name").val();
keyCopy.color = activeEditKeyColor;
$("#key-dialog-export").text(exportKeyFunction(url, keyCopy));
};
resetEditKeyColor(key.color);
displayKeyDialog("edit-key", function() {
// OK action at the end of edit key.
var newName = $("#key-dialog-name").val();
var newColor = activeEditKeyColor;
var newNote = $("#key-dialog-note").val();
var newPassphrase = $("#key-dialog-passphrase").val();
var isDefault = $("#key-dialog-default").prop("checked");
// Verify new data.
if (!verifyKeyName(newName, domain, key, url)) {
return false;
}
// Update the UI.
var label = keyButton.find(".shadowcrypt-key-name");
keyButton.find(".shadowcrypt-key").add(label).removeClass("shadowcrypt-color-" + key.color).addClass("shadowcrypt-color-" + newColor);
label.text(newName);
if (isDefault) {
changeDefaultKey(domain, key);
}
// Update the object.
key.name = newName;
key.color = newColor;
key.note = newNote;
key.passphrase = newPassphrase
var updateStorage = {};
updateStorage[url] = domain;
// Report to callback.
editKeyCallback(domain, key, url);
// Return true if everything is OK and dialog should close.
return true;
}, function(deletedCallback){
// Delete key action.
var name = $("#key-dialog-name").val();
displayOKCancelDialog("<p>Delete key " + name + "?</p>", function(){
deleteKey(domain, key, url);
hideKeyDialog();
});
});
});
var hover = false;
keyButton.hover(function(){
hover=true;
globalHoverCounter++;
setTimeout(function(){
if (hover) {
keysTable.addClass("tooltip-active");
}
}, 1000);
}, function(){
hover=false;
globalHoverCounter--;
setTimeout(function(){
if (globalHoverCounter == 0 ) {
keysTable.removeClass("tooltip-active");
}
}, 200);
});
domain.keysHolder.append(keyButton);
}
function createDomainUI(domain, url){
// Create domain editor row.
var protocolIndex = url.indexOf("://");
if (protocolIndex > -1) {
protocolIndex += 3;
} else {
protocolIndex = 0;
}
var displayName = url.substr(protocolIndex);
var row = $("<tr><td class='domain-name'>" + displayName + "</td></tr>");
domain.row = row;
// Create keys cell.
var keysCell = $("<td>");
// Delete button
var deleteButton = $("<div class='delete-button'>");
deleteButton.click(function() {
displayOKCancelDialog("<p>Delete all the keys for domain " + displayName + "?</p>", function(){
var keys = $.extend({}, domain.keys);
for (var key in keys) {
deleteKey(domain, keys[key], url);
}
});
});
keysCell.append(deleteButton);
// Create keys holder.
var keysHolder = $("<div>");
keysCell.append(keysHolder);
row.append(keysCell);
domain.keysHolder = keysHolder;
for (var fingerprint in domain.keys) {
var key = domain.keys[fingerprint];
createKeyUI(domain, key, domain.defaultFingerprint == fingerprint, url);
}
// Insert row.
keysTable.append(row);
}
for (var origin in domains) {
createDomainUI(domains[origin], origin);
};
// Helper for updating the UI with a new key
function addNewKeyUI(url, key, fingerprint, isDefault) {
// Is this an existing domain?
// Add the key to the database and create the UI.
if (url.slice(0, "origin-".length) != "origin-") {
url = 'origin-' + url;
}
var domain = domains[url];
if (domain) {
// Verify data.
if (!verifyKeyName(key.name, domain, null, url)) {
return false;
}
domain.keys[fingerprint] = key;
createKeyUI(domain, key, isDefault, url);
} else {
domain = {
keys: {},
"defaultFingerprint": fingerprint,
rules: []
};
domain.keys[fingerprint] = key;
domains[url] = domain;
createDomainUI(domain, url);
}
var newDomain = {};
newDomain[url] = domain;
newKeyCallback(domain, key, url);
if (isDefault) {
changeDefaultKey(domain, url);
}
// Return true if key was added OK.
return true;
}
// Helper for deleting a key from the UI
function deleteKey(domain, key, url) {
var keys = Object.keys(domain.keys),
eyIndex,
isEmpty,
fingerprint;
for (var i = 0; i < keys.length; i++) {
if (domain.keys[keys[i]] == key) {
fingerprint = keys[i];
keyIndex = i;
break;
}
}
delete domain.keys[fingerprint];
domain.keysHolder.find(".key-holder")[keyIndex].remove();
// If the domain is empty, remove it.
if ($.isEmptyObject(domain.keys)) {
domain.row.remove();
for (var site in domains) {
if ($.isEmptyObject(domains[site].keys)) {
delete domains[site];
isEmpty = true;
}
}
} else {
// Make sure to reassign the default if needed.
if (domain.defaultFingerprint == fingerprint) {
// Default was deleted, reassign to the end.
var keys = Object.keys(domain.keys),
newDefault = keys[keys.length - 1];
changeDefaultKey(domain, domain.keys[newDefault]);
}
}
// Report to callback.
deleteKeyCallback(domain, key, url, isEmpty);
}
// Helper for making sure the key name is unique
function verifyKeyName(name, domain, existing, url) {
var nameOK = true;
for (fingerprint in domain.keys) {
var key = domain.keys[fingerprint];
if (key.name == name && key != existing) {
displayCloseDialog("<p>The name " + name + " is already in use for " + url +". Please choose another key name.</p>");
nameOK = false;
}
}
return nameOK;
}
// Helper for changing the default key of the domain
function changeDefaultKey(domain, key) {
var oldDefault = domain.defaultFingerprint;
// Remove old default key.
// Add new one.
var index = 0,
oldIndex = 0;
for (var fingerprint in domain.keys) {
if (fingerprint == oldDefault) break;
oldIndex++;
}
//After loop, fingerprint is current key's fingerprint
for (fingerprint in domain.keys) {
if (domain.keys[fingerprint] == key) break;
index++;
}
for (var url in domains) {
if (domains[url] == domain) break;
}
domain.keysHolder.find(".key-holder").eq(oldIndex).removeClass("default-key");
domain.defaultFingerprint = fingerprint;
domain.keysHolder.find(".key-holder").eq(index).addClass("default-key");
// Report to callack if default has changed.
if (domain.defaultFingerprint != oldDefault) {
defaultChangedCallback(domain, key, url);
}
}
// Hook up the main toolbar buttons.
// New key
$("#new-key-button").click(function(){
var url = "",
secret = Array.prototype.slice.call(window.crypto.getRandomValues(new Uint32Array(4))),
fingerprint = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(secret)),
key = {
name: "Default",
color: Math.floor(Math.random() * 7),
note: "",
passphrase: "",
secret: secret,
};
$("#key-dialog-url").val(url);
$("#key-dialog-name").val(key.name);
$("#key-dialog-note").val(key.note);
$("#key-dialog-passphrase").val(key.passphrase);
$("#key-dialog-default").prop("checked", true);
$("#key-dialog-export").text(exportKeyFunction(url, key));
onKeyChangeCallback = function() {
key.name = $("#key-dialog-name").val();
key.color = activeEditKeyColor;
url = $("#key-dialog-url").val();
$("#key-dialog-export").text(exportKeyFunction(url, key));
};
resetEditKeyColor(key.color);
displayKeyDialog("new-key", function(){
// OK action at the end of add new key.
onKeyChangeCallback();
// Update the UI.
var changeDefault = $("#key-dialog-default").prop("checked");
var success = addNewKeyUI(url, key, fingerprint, changeDefault);
if (!success) {
return false;
}
// Return true if everything is OK and dialog should close.
setTimeout(function () {
displayCloseDialog('<p> Key added for ' + url + '</p>');
}, 100);
return true;
});
});
// Import key
$("#import-key-button").click(function () {
$("#key-dialog-import").val("");
displayKeyDialog("import-key", function(){
var string = $("#key-dialog-import").val();
var entry = importKeyFunction(string);
if (! string) {
displayCloseDialog('<p> Wrong key format. Try recopying the key </p>');
return false;
}
var fingerprint = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(entry.key.secret));
var success = addNewKeyUI(entry.url, entry.key, fingerprint, false)
if (!success) return false;
displayCloseDialog('<p> New key added for ' + entry.url + ' </p>');
return true;
});
});
// Hook up dialog buttons.
$("#key-dialog-delete").click(function(){
var deleted = false;
if (keyDialogDeleteCallback) {
keyDialogDeleteCallback();
}
return false;
});
$("#key-dialog-cancel").click(function(){
hideKeyDialog();
return false;
});
function keyDialogConfirmed(){
var success = true;
if (keyDialogCallback) {
success = keyDialogCallback();
}
if (success) hideKeyDialog();
return false;
}
$("#key-dialog-ok").click(keyDialogConfirmed);
$("#key-dialog").submit(keyDialogConfirmed);
$("#dialog-cancel").add("#dialog-close").click(hideDialog);
$("#dialog-ok").click(function(){
if (dialogCallback) {
dialogCallback();
dialogCallback = null;
}
hideDialog();
});
// Hook up color selection buttons.
for (var i=0;i<7;i++) {
(function(i){
$("#key-dialog-color" + i).click(function(){
changeEditKeyColor(i);
onKeyChangeCallback();
});})(i);
}
// Hook up input box changes.
$("#key-dialog-name").add("#key-dialog-url").change(function(){
onKeyChangeCallback();
});
$("#key-dialog-name").add("#key-dialog-url").keyup(function(){
onKeyChangeCallback();
});
};
};
function prepareSyncData(domain, url) {
var newDoamin = {};
newDoamin[url] = domain;
return newDoamin;
}
function onNewKey(domain, key, url) {
chrome.storage.sync.set(prepareSyncData(domain, url));
console.log("Key " + key.name + " created for URL " + url);
return key;
}
function onEditKey(domain, key, url) {
chrome.storage.sync.set(prepareSyncData(domain, url));
console.log("Key " + key.name + " at URL " + url + " was edited.");
}
function onDeletedKey(domain, key, url, isEmpty) {
if (isEmpty) {
chrome.storage.sync.remove(url);
} else {
chrome.storage.sync.set(prepareSyncData(domain, url));
}
console.log("Key " + key.name + " deleted for URL " + url);
};
function onDefaultChange(domain, key, url) {
chrome.storage.sync.set(prepareSyncData(domain, url));
console.log("Key " + key.name + " is now the default for URL " + url + ".");
}
// On load initialize the UI
$(function () {
chrome.storage.sync.get(function (domains) {
shadowCrypt.KeyManagement.initialize(
domains
, onNewKey
, onEditKey
, onDeletedKey
, function(url, key){
// This is a function that transforms a key object into an export string.
return url + " [" + key.name + "] " + sjcl.codec.hex.fromBits(key.secret);
}, function(string){
// This is a function that transforms a key string into a key object.
try {
var parts = string.split(" ");
var url = parts[0];
var name = parts[1].substr(1, parts[1].length-2);
var secret = sjcl.codec.hex.toBits(parts[2]);
return {
url: url,
key: {
name: name,
color: Math.floor(Math.random() * 7),
secret: secret,
}
};
} catch (e) {
return false;
}
}, onDefaultChange
);
});
});