forked from ericwoodruff/passwordhasherplus
-
Notifications
You must be signed in to change notification settings - Fork 4
/
storage.js
314 lines (288 loc) · 8.61 KB
/
storage.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
<!--
/* ***** 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 Eric Woodruff.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s): Oren Ben-Kiki
*
* 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 ***** */
-->
Storage.prototype.setObject = function (key, value) {
var str = JSON.stringify (value);
if (debug) console.log ("Set " + key + " " + str);
this.setItem (key, str);
}
Storage.prototype.getObject = function (key) {
var str = this.getItem (key);
if (null == str) {
if (debug) console.log ("Get " + key + " null");
return null;
}
if (debug) console.log ("Get " + key + " " + str);
return JSON.parse (str);
}
Storage.prototype.getBoolean = function (key) {
if ("true" == localStorage[key]) {
return true;
}
return false;
}
Storage.prototype.saveOptions = function (options) {
localStorage.setObject ("options", options);
}
Storage.prototype.loadOptions = function () {
var dirty = false;
var options;
options = localStorage.getObject ("options");
if (null == options) {
options = new Object ();
dirty = true;
}
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.showMaskButton) {
options.showMaskButton = true;
dirty = true;
}
if (null == options.hashKey) {
options.hashKey = default_hashkey;
dirty = true;
}
if (null == options.maskKey) {
options.maskKey = default_maskkey;
dirty = true;
}
if (dirty) {
this.saveOptions (options);
}
return options;
}
Storage.prototype.saveConfig = function (url, config) {
if (debug) console.log ("Saving " + url + " " + JSON.stringify (config));
config.fields = toArray (config.fields);
localStorage.setObject ("tag:" + config.tag, config.policy);
var options = config.options;
var policy = config.policy;
delete config.policy;
delete config.options;
localStorage.setObject ("url:" + url, config);
config.policy = policy;
config.options = options;
}
Storage.prototype.loadTags = function () {
var tags = [];
var keys = toArray (localStorage);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (key.startsWith ("tag:")) {
tags[tags.length] = key.substringAfter ("tag:");
}
}
return tags;
}
Storage.prototype.isTagReferenced = function (keys, tag) {
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (key.startsWith ("url:")) {
var config = localStorage.getObject (key);
if (config.tag == tag) {
return true;
}
}
}
return false;
}
Storage.prototype.collectGarbage = function () {
// remove unreferenced tags
var keys = toArray (localStorage);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (key.startsWith ("tag:")) {
if (!this.isTagReferenced (keys, key.substringAfter ("tag:"))) {
delete localStorage[key];
}
}
}
}
Storage.prototype.loadConfig = function (url) {
var config = localStorage.getObject ("url:" + url);
if (null == config) {
config = new Object ();
config.tag = url;
config.fields = new Array ();
}
config.options = this.loadOptions ();
config.policy = localStorage.getObject ("tag:" + config.tag);
if (null == config.policy) {
config.policy = new Object ();
config.policy.seed = config.options.privateSeed;
config.policy.length = config.options.defaultLength;
config.policy.strength = config.options.defaultStrength;
}
return config;
}
Storage.prototype.migrate = function () {
if (null == localStorage["version"]) {
if (null != localStorage["option:private_seed"]) {
// Migrate Password Hasher Plus Plus databases
localStorage["version"] = "1";
} else {
localStorage["version"] = "0";
this.runMigration (1, this.migrate_v1);
}
}
this.runMigration (2, this.migrate_v2);
this.runMigration (3, this.migrate_v3);
this.runMigration (4, this.migrate_v4);
this.runMigration (5, this.migrate_v5);
}
Storage.prototype.migrate_v5 = function () {
var keys = toArray (localStorage);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (key.startsWith ("url:")) {
var config = localStorage.getObject (key);
var tagName = config.tag;
var tag = new Object ();
tag.strength = config.strength;
tag.length = config.length;
tag.seed = config.seed;
delete config.strength;
delete config.length;
delete config.seed;
localStorage.setObject (key, config);
localStorage.setObject ("tag:" + tagName, tag);
}
}
}
Storage.prototype.migrate_v4 = function () {
var options = new Object ();
options.defaultLength = localStorage["option:default_length"];
options.defaultStrength = localStorage["option:default_strength"];
options.privateSeed = localStorage["option:private_seed"];
options.compatibilityMode = localStorage.getBoolean ("option:compatibility_mode");
options.backedUp = localStorage.getBoolean ("option:backed_up");
var keys = toArray (localStorage);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (key.startsWith ("option:")) {
delete localStorage[key];
} else if (key.startsWith ("url:")) {
var config = localStorage.getObject (key);
try {
if (config.tag.startsWith ("compatible:")) {
delete config.seed;
config.tag = config.tag.substringAfter ("compatible:");
}
delete config.compatibilitymode;
delete config.backedup;
localStorage.setObject (key, config);
} catch (e) {
console.log ("failed to migrate " + key);
}
}
}
localStorage.saveOptions (options);
}
Storage.prototype.migrate_v3 = function () {
var keys = toArray (localStorage);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (!key.startsWith ("url:")) {
continue;
}
try {
var config = localStorage.getObject (key);
var config2 = new Object ();
config2.tag = config.site;
for (var property in config) {
config2[property] = config[property];
}
delete config2.site;
localStorage.setObject (key, config2);
} catch (e) {
console.log ("failed to migrate " + key);
}
}
}
Storage.prototype.migrate_v2 = function () {
var keys = toArray (localStorage);
var reg = new RegExp ("^site:.*$");
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (reg.test (key)) {
var to = "url";
localStorage[to + key.slice(4)] = localStorage[key];
delete localStorage[key];
}
}
}
Storage.prototype.migrate_v1 = function () {
var keys = toArray (localStorage);
var reg = new RegExp ("^compatibility_mode|default_length|default_strength|private_seed|backed_up$");
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
var to = "site";
if ("version" == key) {
continue;
} else if (reg.test (key)) {
to = "option";
}
localStorage[to + ":" + key] = localStorage[key];
delete localStorage[key];
}
localStorage["version"] = "1";
}
Storage.prototype.runMigration = function (toVersion, func) {
var version = parseInt (localStorage["version"]);
if (toVersion <= version) {
return;
}
func ();
localStorage["version"] = toVersion;
}