forked from ericwoodruff/passwordhasherplus
-
Notifications
You must be signed in to change notification settings - Fork 4
/
storagearea.js
490 lines (462 loc) · 18.6 KB
/
storagearea.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
<!--
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Password Hasher Plus
*
* The Initial Developer of the Original Code is Simon Gerber.
* Portions created by the Initial Developer are Copyright (C) 2017
* the Initial Developer. All Rights Reserved.
*
* Contributors: (none)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
-->
var debug = false;
// TODO: update to storage version 7?
var CURRENT_STORAGE_VER = 6;
function onError(error) {
console.log(`Error: ${error}`);
}
function StorageArea(){
"use strict"
};
StorageArea.prototype.loadTags = function (resultHandler) {
select_storage_area().then(storagearea => {
storagearea.get('tag').then(results => {
resultHandler(results['tag']);
});
});
}
StorageArea.prototype.saveOptions = function (options, doneHandler) {
if ('sync' in options) {
delete options['sync'];
}
select_storage_area().then(storagearea => {
storagearea.set({'options': options}).then(doneHandler, onError);
});
}
function initOptions(settings) {
var options;
if (settings && settings.options !== undefined) {
options = settings['options'];
} else {
options = new Object();
}
var dirty = false;
if (null == options.privateSeed) {
options.privateSeed = generateGuid ();
dirty = true;
}
if (null == options.defaultLength) {
options.defaultLength = default_length;
dirty = true;
}
if (null == options.defaultStrength) {
options.defaultStrength = default_strength;
dirty = true;
}
if (null == options.backedUp) {
options.backedUp = false;
dirty = true;
}
if (null == options.compatibilityMode) {
options.compatibilityMode = false;
dirty = true;
}
if (null == options.hashKey) {
options.hashKey = default_hashkey;
dirty = true;
}
if (null == options.maskKey) {
options.maskKey = default_maskkey;
dirty = true;
}
if (settings) {
settings['options'] = options;
}
return dirty;
}
StorageArea.prototype.loadOptions = function (resultHandler) {
select_storage_area().then(storagearea => {
storagearea.get(['options', 'sync']).then(results => {
if (debug) {
console.log("[storagearea.js:loadOptions] sync="+results.sync);
console.dir(results);
}
var dirty = initOptions(results);
if (debug) console.log('[loadOptions] initOptions -> ' + dirty);
var options = results['options'];
console.assert(options !== undefined);
// inject sync for options page and stuff
if (results.sync === undefined) {
if (options.sync !== undefined) {
results.sync = options.sync;
delete options.sync;
} else {
results.sync = false;
}
dirty = true;
console.log("[loadOptions] sync was undefined, setting sync to " + results.sync);
browser.storage.local.set({sync: results.sync}).then(() => {
browser.storage.sync.set({sync: results.sync});
});
}
if (dirty) {
if (debug) console.log("options updated, saving");
this.saveOptions (options);
}
options.sync = results['sync'];
if (debug) console.log('[storageLoadOptions] calling resultHandler with options='+JSON.stringify(options, null, 2));
resultHandler(options);
});
});
}
StorageArea.prototype.saveConfig = function (url, config, doneHandler) {
if (debug) console.log ("[storagearea.js] Saving " + url + " " + JSON.stringify (config, null, 2));
config.fields = toArray (config.fields);
// grab settings from storage area and update
select_storage_area().then(storagearea => {
storagearea.get(['url', 'tag']).then(results => {
if (!('tag' in results)) {
results.tag = new Object();
}
results.tag[config.tag] = config.policy;
delete config.policy;
delete config.options;
if (!('url' in results)) {
results.url = new Object();
}
results.url[url] = config;
//if (debug) console.log ("Writing " + JSON.stringify (results, null, 2));
storagearea.set(results).then(doneHandler, onError);
});
});
}
StorageArea.prototype.loadConfig = function (url, resultHandler) {
var config = new Object();
config.tag = url;
config.fields = new Array();
function configSetOptions(options) {
config.options = options;
select_storage_area().then(storagearea => {
storagearea.get('tag').then(results => {
if ('tag' in results && config.tag in results['tag']) {
// found tag settings
config.policy = results.tag[config.tag];
} else {
// init default tag settings
config.policy = new Object();
config.policy.seed = config.options.privateSeed;
config.policy.length = config.options.defaultLength;
config.policy.strength = config.options.defaultStrength;
}
if (debug) console.log('calling result handler with config='+JSON.stringify(config, null, 2));
resultHandler(config);
}, onError);
});
};
if (debug) console.log('reading config for url='+url+' from storagearea');
select_storage_area().then(storagearea => {
storagearea.get('url').then(results => {
if ('url' in results && url in results['url']) {
config = results.url[url];
} else {
config = new Object ();
config.tag = url;
config.fields = new Array ();
}
this.loadOptions(configSetOptions);
}, onError);
});
}
StorageArea.prototype.collectGarbage = function (doneHandler) {
function isTagReferenced (urls, tagn) {
for (var urln in urls) {
var url = urls[urln];
if (url.tag == tagn) {
return true;
}
}
return false;
}
// remove unreferenced tags
select_storage_area().then(storagearea => {
storagearea.get(['tag', 'url']).then(results => {
console.dir(results.url);
for (var tag in results.tag) {
if (!isTagReferenced (results.url, tag)) {
console.log("tag '"+ tag+ "' not referenced, deleting it");
delete results.tag[tag];
}
}
if (debug) {
console.log("[storagearea:collectGarbage] writing back clean settings");
console.dir(results);
}
storagearea.set(results).then(doneHandler);
});
});
}
StorageArea.prototype.migrateLocalStorage = function () {
if (debug) console.log('migrating settings from localStorage to webext storage');
// first make sure that we're on latest config version
localStorage.migrate();
// cleanup unused tags
localStorage.collectGarbage();
var settings = new Object();
// now construct settings object which we will store in this storage area
settings['version'] = 6;
// 1. grab options from localStorage
var options = localStorage.loadOptions();
settings['options'] = options;
settings['url'] = new Object();
settings['tag'] = new Object();
// 2. go through all URLs in localStorage
var keys = toArray(localStorage);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (key.startsWith('url:')) {
if (debug) console.log('migrating settings for: '+ key);
var url = key.substringAfter('url:');
var config = localStorage.getObject(key);
settings['url'][url] = config;
var policy = localStorage.getObject('tag:'+config.tag);
settings['tag'][config.tag] = policy;
}
}
return settings;
}
// Possible syncopt values:
// 0: use existing sync settings if available, move local settings to sync otherwise
// 1: Overwrite existing sync settings
// 2: merge local settings with sync settings -- local options take precedence
// 3: merge local settings with sync settings -- sync options take precedence
StorageArea.prototype.migrateArea = function (sync, syncopt, doneHandler) {
console.log("[storagearea.js:migrateArea] sync="+sync+", syncopt="+syncopt);
function saveToArea(area, syncval, settings, mDoneHandler) {
browser.storage.local.set({sync: syncval}).then(() => {
browser.storage.sync.set({sync: syncval}).then(() => {
area.set(settings).then(() => {
storage.collectGarbage(() => {
storage.saveOptions(settings.options, mDoneHandler);
});
});
});
});
}
function doMigration(oldarea, area, syncval, mDoneHandler) {
oldarea.get(null).then(results => {
// update sync field
results.sync = syncval;
if (debug) {
console.log('[migrateArea] storing');
console.dir(results);
}
saveToArea(area, syncval, results, mDoneHandler);
});
}
function doMerge(area, syncval, syncoptval, mDoneHandler) {
if (debug) console.log('[migrateArea:doMerge] sync='+syncval+' syncopt='+syncoptval);
var masterarea = null;
var slavearea = null;
var masterareaname = null;
if (syncoptval == 2) {
masterarea = browser.storage.local;
slavearea = browser.storage.sync;
masterareaname = 'local';
} else if (syncoptval == 3) {
masterarea = browser.storage.sync;
slavearea = browser.storage.local;
masterareaname = 'sync';
} else {
console.warn("[migrateArea:doMerge] unknown syncopt: " + syncoptval);
console.assert(!"Fallback NYI");
return;
}
masterarea.get(null).then(results => {
slavearea.get(null).then(slaveres => {
if (results.options === null) {
if (slaveres.options !== null) {
console.log('no options in preferred storage, using options from other storage');
results.options = slaveres.options;
} else {
console.log("no options available, generating initial options");
initOptions(results);
}
}
// now resuls.options == requested options
// merge urls in slaveres.url which don't exist in results.url
if (results.url === undefined) {
if (debug) console.log("no urls in "+masterareaname);
results.url = slaveres.url;
} else {
for (var url in slaveres.url) {
if (!(url in results.url)) {
results.url[url] = slaveres.url[url];
}
}
}
// merge tags in slaveres.tag which don't exist in results.tag
if (results.tag === undefined) {
if (debug) console.log("no tags in "+masterareaname);
results.tag = slaveres.tag;
} else {
for (var tag in slaveres.tag) {
if (!(tag in results.tag)) {
results.tag[tag] = slaveres.tag[tag];
}
}
}
if (debug) {
console.log('merged settings:');
console.dir(results);
}
saveToArea(area, syncval, results, mDoneHandler);
});
});
}
// check if we need to do anything:
// storage area, sync flag, op
// sync true noop
// sync false migrate to local
// local true migrate to sync
// local false noop
select_storage_area().then(oldstoragearea => {
if ((oldstoragearea == browser.storage.sync) != sync) {
var storagearea = null;
var areaname = null;
// select new storagearea
if (sync) {
storagearea = browser.storage.sync;
areaname = 'sync';
} else {
storagearea = browser.storage.local;
areaname = 'local';
}
// use the same logic now that we have properly set storageareas
console.log("checking if "+areaname+" already contains settings");
storagearea.get(['version', 'options']).then(results => {
if(results['version'] == CURRENT_STORAGE_VER) {
console.log(areaname + ' already has config');
if (syncopt == 0) {
console.log('user requested to use settings in ' + areaname);
console.log('setting both sync flags to ' + sync);
browser.storage.local.set({sync: sync}).then(() => {
browser.storage.sync.set({sync: sync}).then(() => {
storage.collectGarbage(() => {
storage.saveOptions(results.options, doneHandler);
});
});
});
} else if (syncopt == 1) {
console.log('user requested to overwrite ' + areaname);
doMigration(oldstoragearea, storagearea, sync, doneHandler);
} else if (syncopt == 2 || syncopt == 3) {
console.log('user requested to merge configs');
console.log('merge: user requested to keep ' + (syncopt == 3 ? 'sync' : 'local') + ' options');
doMerge(storagearea, sync, syncopt, doneHandler);
} else {
console.warn("syncopt = " + syncopt + " unknown");
}
return;
} else {
console.log('No settings present in ' + area +', writing current settings to ' + area);
doMigration(oldstoragearea, storagearea, sync, doneHandler);
}
});
} else {
if (syncopt == 2 || syncopt == 3) {
console.log('user requested merge without switching area');
doMerge(oldstoragearea, sync, syncopt, doneHandler);
} else {
// nothing to do, make sure sync flag is correct
browser.storage.local.set({sync: sync}).then(() => {
browser.storage.sync.set({sync: sync}).then(doneHandler);
});
}
}
});
}
StorageArea.prototype.migrate = function (area) {
console.assert(area !== null);
area.get(null).then(results => {
if ('version' in results && results['version'] == CURRENT_STORAGE_VER)
{
if (debug) console.log('webext storage already contains up-to-date settings');
return;
}
// we need to do some work
var settings;
if ('version' in results && results['version'] == CURRENT_STORAGE_VER) {
if (debug) console.log('migrating settings to ' + area);
settings = results;
} else {
settings = this.migrateLocalStorage();
}
if (!('options' in settings)) {
console.log('no options in settings to migrate?');
settings['options'] = initOptions(settings);
}
if (debug) console.log('setting webext storage settings: '+ JSON.stringify(settings, null, 2));
area.set(settings).then(null, onError);
});
}
StorageArea.prototype.init = function (settings) {
if (settings['sync']) {
storagearea = browser.storage.sync;
} else {
storagearea = browser.storage.local;
}
storagearea.clear ();
return browser.storage.local.set({sync: settings['sync']}).then(() => {
// clear selected storagearea
return storagearea.set(settings)
});
}
var storage = new StorageArea();
// make sure settings are available in webext storage after plugin install / plugin reload
function install_handler() {
if (debug) console.log('Initializing webext storage...');
browser.storage.local.get('sync').then(
results => {
var sync = 'sync' in results && results['sync'] > 0;
if (debug) {
console.log("storage.local.sync = " + sync);
browser.storage.sync.get(null).then(results => {
console.log("sync: "+JSON.stringify(results, null, 2));
});
}
if (sync) {
storage.migrate(browser.storage.sync);
} else {
storage.migrate(browser.storage.local);
}
});
}
browser.runtime.onInstalled.addListener(install_handler);
if (typeof exports !== 'undefined') {
exports.StorageArea = StorageArea;
}