This repository has been archived by the owner on May 10, 2019. It is now read-only.
/
json.js
428 lines (380 loc) · 10.8 KB
/
json.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* db_json is a json database driver. It is designed for use in
* local development, is intended to be extremely easy to maintain,
* have minimal dependencies on 3rd party libraries, and we could
* care less if it performs well with more than 10 or so users.
*/
const
path = require('path'),
fs = require('fs'),
secrets = require('../secrets.js'),
jsel = require('JSONSelect'),
logger = require('../logging.js').logger,
configuration = require('../configuration.js'),
temp = require('temp');
// a little alias for stringify
const ESC = JSON.stringify;
var dbPath = path.join(configuration.get('var_path'), "authdb.json");
/* The JSON database. The structure is thus:
* [
* {
* id: <numerical user id>
* password: "somepass",
* emails: {
* "lloyd@hilaiel.com": {
* type: 'secondary'
* }
* }
* }
* ]
*/
function getNextUserID() {
var max = 1;
jsel.forEach(".id", db.users, function(id) {
if (id >= max) max = id + 1;
});
return max;
};
var db = {
users: [ ],
stagedEmails: { },
staged: { }
};
function flush() {
try {
var e = fs.writeFileSync(dbPath, JSON.stringify(db));
} catch (e) {
logger.error("Cannot save database to " + dbPath);
}
}
function sync() {
// the database not existing yet just means its empty, don't log an error
if (path.existsSync(dbPath)) {
try {
db = JSON.parse(fs.readFileSync(dbPath));
// FIXME:
// at this point db might be missing some important fields
// we may want to fix this. In the meantime, delete your old json db
} catch(e) {
logger.error("Cannot read database from " + dbPath);
}
} else {
logger.debug("Database doesn't exist (yet): " + dbPath);
}
}
exports.open = function(cfg, cb) {
if (cfg && cfg.path) {
dbPath = cfg.path;
}
logger.debug("opening JSON database: " + dbPath);
sync();
setTimeout(cb, 0);
};
exports.closeAndRemove = function(cb) {
// if the file cannot be removed, it's not an error, just means it was never
// written or deleted by a different process
try { fs.unlinkSync(dbPath); } catch(e) { }
setTimeout(function() { cb(undefined); }, 0);
};
exports.close = function(cb) {
// don't flush database here to disk, the database is flushed synchronously when
// written - If we were to flush here we could overwrite changes made by
// another process - see issue #557
setTimeout(function() { cb(undefined) }, 0);
};
exports.emailKnown = function(email, cb) {
sync();
var m = jsel.match(".emails ." + ESC(email), db.users);
setTimeout(function() { cb(m.length > 0) }, 0);
};
exports.emailType = function(email, cb) {
sync();
var m = jsel.match(".emails ." + ESC(email), db.users);
process.nextTick(function() { cb(m.length ? m[0].type : undefined); });
};
exports.isStaged = function(email, cb) {
if (cb) {
setTimeout(function() {
sync();
cb(db.stagedEmails.hasOwnProperty(email));
}, 0);
}
};
exports.lastStaged = function(email, cb) {
if (cb) {
sync();
var d;
if (db.stagedEmails.hasOwnProperty(email)) {
d = new Date(db.staged[db.stagedEmails[email]].when);
}
setTimeout(function() { cb(d); }, 0);
}
};
exports.emailsBelongToSameAccount = function(lhs, rhs, cb) {
sync();
var m = jsel.match(".emails:has(."+ESC(lhs)+"):has(."+ESC(rhs)+")", db.users);
process.nextTick(function() {
cb(m && m.length == 1);
});
};
exports.emailToUID = function(email, cb) {
sync();
var m = jsel.match(":root > object:has(.emails > ." + ESC(email) + ") > .id", db.users);
if (m.length === 0) m = undefined;
else m = m[0];
process.nextTick(function() {
cb(m);
});
};
exports.userOwnsEmail = function(uid, email, cb) {
sync();
var m = jsel.match(":root > object:has(:root > .id:expr(x=" + ESC(uid) + ")):has(.emails > ." + ESC(email) + ")", db.users);
process.nextTick(function() {
cb(m && m.length == 1);
});
};
function addEmailToAccount(userID, email, type, cb) {
// validate 'type' isn't bogus
if ([ 'secondary', 'primary' ].indexOf(type) === -1) {
return process.nextTick(function() {
cb("invalid type");
});
}
process.nextTick(function() {
sync();
var emails = jsel.match(":has(.id:expr(x="+ ESC(userID) +")) > .emails", db.users);
if (emails && emails.length > 0) {
emails[0][email] = { type: type };
flush();
}
cb();
});
}
exports.stageUser = function(email, cb) {
secrets.generate(48, function(secret) {
// overwrite previously staged users
sync();
db.staged[secret] = {
type: "add_account",
email: email,
when: (new Date()).getTime()
};
db.stagedEmails[email] = secret;
flush();
setTimeout(function() { cb(secret); }, 0);
});
};
exports.stageEmail = function(existing_user, new_email, cb) {
secrets.generate(48, function(secret) {
// overwrite previously staged users
sync();
db.staged[secret] = {
type: "add_email",
existing_user: existing_user,
email: new_email,
when: (new Date()).getTime()
};
db.stagedEmails[new_email] = secret;
flush();
setTimeout(function() { cb(secret); }, 0);
});
};
exports.createUserWithPrimaryEmail = function(email, cb) {
var emailVal = { };
emailVal[email] = { type: 'primary' };
var uid = getNextUserID();
db.users.push({
id: uid,
password: null,
emails: emailVal
});
flush();
process.nextTick(function() {
cb(undefined, uid);
});
};
exports.haveVerificationSecret = function(secret, cb) {
process.nextTick(function() {
sync();
cb(!!(db.staged[secret]));
});
};
exports.emailForVerificationSecret = function(secret, cb) {
process.nextTick(function() {
sync();
if (!db.staged[secret]) return cb("no such secret");
exports.checkAuth(db.staged[secret].existing_user, function (hash) {
cb(undefined, {
email: db.staged[secret].email,
needs_password: !hash
});
});
});
};
exports.verificationSecretForEmail = function(email, cb) {
setTimeout(function() {
sync();
cb(db.stagedEmails[email]);
}, 0);
};
exports.gotVerificationSecret = function(secret, hash, cb) {
sync();
if (!db.staged.hasOwnProperty(secret)) return cb("unknown secret");
// simply move from staged over to the emails "database"
var o = db.staged[secret];
delete db.staged[secret];
delete db.stagedEmails[o.email];
flush();
if (o.type === 'add_account') {
exports.emailKnown(o.email, function(known) {
function createAccount() {
var emailVal = {};
emailVal[o.email] = { type: 'secondary' };
var uid = getNextUserID();
db.users.push({
id: uid,
password: hash,
emails: emailVal
});
flush();
cb(undefined, o.email, uid);
}
// if this email address is known and a user has completed a re-verification of this email
// address, remove the email from the old account that it was associated with, and then
// create a brand new account with only this email.
// NOTE: this might be sub-optimal, but it's a dead simple approach that mitigates many attacks
// and gives us reasonable behavior (without explicitly supporting) in the face of shared email
// addresses.
if (known) {
removeEmailNoCheck(o.email, function (err) {
if (err) cb(err);
else createAccount();
});
} else {
createAccount();
}
});
} else if (o.type === 'add_email') {
exports.emailKnown(o.email, function(known) {
function addIt() {
addEmailToAccount(o.existing_user, o.email, 'secondary', function(e) {
cb(e, o.email, o.existing_user);
});
}
if (known) {
removeEmailNoCheck(o.email, function (err) {
if (err) cb(err);
else addIt();
});
} else {
addIt();
}
});
} else {
cb("internal error");
}
};
exports.addPrimaryEmailToAccount = function(userID, emailToAdd, cb) {
sync();
exports.emailKnown(emailToAdd, function(known) {
function addIt() {
addEmailToAccount(userID, emailToAdd, 'primary', cb);
}
if (known) {
removeEmailNoCheck(emailToAdd, function (err) {
if (err) cb(err);
else addIt();
});
} else {
addIt();
}
});
};
exports.checkAuth = function(userID, cb) {
sync();
var m = undefined;
if (userID) {
m = jsel.match(":root > object:has(:root > .id:expr(x=" + ESC(userID) + ")) > .password", db.users);
if (m.length === 0) m = undefined;
else m = m[0];
}
process.nextTick(function() { cb(m) });
};
exports.userKnown = function(userID, cb) {
sync();
var m = jsel.match(":root > object:has(:root > .id:expr(x=" + ESC(userID) + "))", db.users);
if (m.length === 0) m = undefined;
else m = m[0];
process.nextTick(function() { cb(m) });
};
exports.updatePassword = function(userID, hash, cb) {
sync();
var m = jsel.match(":root > object:has(.id:expr(x=" + ESC(userID) + "))", db.users);
var err = undefined;
if (m.length === 0) err = "no such email address";
else m[0].password = hash;
flush();
process.nextTick(function() { cb(err) });
};
exports.listEmails = function(uid, cb) {
sync();
var emails = jsel.match(":has(.id:expr(x="+ ESC(uid) +")) > .emails", db.users);
process.nextTick(function() {
if (!emails || emails.length != 1) {
cb("no such user: " + uid);
return;
}
cb(null, emails[0]);
});
};
exports.removeEmail = function(authenticated_user, email, cb) {
sync();
var m = jsel.match(":has(.id:expr(x=" + ESC(authenticated_user) + ")) .emails:has(."+ESC(email)+")", db.users);
if (m.length) {
var emails = m[0];
delete emails[email];
flush();
}
setTimeout(function() { cb(); }, 0);
};
function removeEmailNoCheck(email, cb) {
sync();
var m = jsel.match(".emails:has(."+ESC(email)+")", db.users);
if (m.length) {
var emails = m[0];
delete emails[email];
flush();
}
process.nextTick(function() { cb(); });
};
exports.cancelAccount = function(authenticated_uid, cb) {
sync();
var id = undefined;
for (var i = 0; i < db.users.length; i++) {
if (db.users[i].id === authenticated_uid) break;
}
if (i < db.users.length) {
db.users.splice(i, 1);
flush();
}
process.nextTick(function() { cb(); });
};
exports.addTestUser = function(email, hash, cb) {
sync();
removeEmailNoCheck(email, function() {
var emailVal = {};
emailVal[email] = { type: 'secondary' };
db.users.push({
id: getNextUserID(),
password: hash,
emails: emailVal
});
flush();
cb();
});
};
exports.ping = function(cb) {
setTimeout(function() { cb(); }, 0);
};