2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2018-04-29 17:37:53 -07:00
|
|
|
UsersError: UsersError,
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-03-02 11:24:06 +01:00
|
|
|
removePrivateFields: removePrivateFields,
|
2018-06-25 15:54:24 -07:00
|
|
|
removeRestrictedFields: removeRestrictedFields,
|
2018-03-02 11:24:06 +01:00
|
|
|
|
2019-01-14 16:39:20 +01:00
|
|
|
getAll: getAll,
|
|
|
|
|
getAllPaged: getAllPaged,
|
2018-06-11 11:39:49 -07:00
|
|
|
create: create,
|
2018-11-10 18:08:08 -08:00
|
|
|
isActivated: isActivated,
|
2015-07-20 00:09:47 -07:00
|
|
|
verify: verify,
|
2016-04-05 16:27:04 +02:00
|
|
|
verifyWithUsername: verifyWithUsername,
|
2015-07-20 00:09:47 -07:00
|
|
|
verifyWithEmail: verifyWithEmail,
|
|
|
|
|
remove: removeUser,
|
2018-06-11 11:39:49 -07:00
|
|
|
get: get,
|
2015-07-20 00:09:47 -07:00
|
|
|
getByResetToken: getByResetToken,
|
2019-03-18 21:15:50 -07:00
|
|
|
getByUsername: getByUsername,
|
2016-01-15 16:04:33 +01:00
|
|
|
getAllAdmins: getAllAdmins,
|
2015-07-20 00:09:47 -07:00
|
|
|
resetPasswordByIdentifier: resetPasswordByIdentifier,
|
|
|
|
|
setPassword: setPassword,
|
|
|
|
|
update: updateUser,
|
2016-01-13 12:28:38 -08:00
|
|
|
createOwner: createOwner,
|
2016-01-18 15:16:18 +01:00
|
|
|
getOwner: getOwner,
|
2018-08-17 09:49:58 -07:00
|
|
|
createInvite: createInvite,
|
2016-02-09 15:47:02 -08:00
|
|
|
sendInvite: sendInvite,
|
2018-06-18 13:57:17 -07:00
|
|
|
setMembership: setMembership,
|
2018-04-25 19:08:15 +02:00
|
|
|
setTwoFactorAuthenticationSecret: setTwoFactorAuthenticationSecret,
|
|
|
|
|
enableTwoFactorAuthentication: enableTwoFactorAuthentication,
|
2018-06-28 16:48:04 -07:00
|
|
|
disableTwoFactorAuthentication: disableTwoFactorAuthentication,
|
2019-07-02 20:22:17 -07:00
|
|
|
|
|
|
|
|
count: count
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
2019-07-02 20:22:17 -07:00
|
|
|
let assert = require('assert'),
|
2015-07-20 00:09:47 -07:00
|
|
|
crypto = require('crypto'),
|
2016-08-31 21:20:48 -07:00
|
|
|
config = require('./config.js'),
|
2016-07-12 10:07:55 -07:00
|
|
|
constants = require('./constants.js'),
|
2016-05-29 23:15:55 -07:00
|
|
|
debug = require('debug')('box:user'),
|
2015-07-20 00:09:47 -07:00
|
|
|
DatabaseError = require('./databaseerror.js'),
|
2016-05-01 20:01:34 -07:00
|
|
|
eventlog = require('./eventlog.js'),
|
2016-02-08 15:16:59 -08:00
|
|
|
groups = require('./groups.js'),
|
2018-04-29 11:21:01 -07:00
|
|
|
GroupsError = groups.GroupsError,
|
2018-06-11 12:38:15 -07:00
|
|
|
hat = require('./hat.js'),
|
2016-02-08 15:15:42 -08:00
|
|
|
mailer = require('./mailer.js'),
|
2018-04-25 19:08:15 +02:00
|
|
|
qrcode = require('qrcode'),
|
2016-07-12 10:07:55 -07:00
|
|
|
safe = require('safetydance'),
|
2018-04-25 19:08:15 +02:00
|
|
|
speakeasy = require('speakeasy'),
|
2016-02-08 15:15:42 -08:00
|
|
|
userdb = require('./userdb.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
util = require('util'),
|
2017-08-13 17:44:31 -07:00
|
|
|
uuid = require('uuid'),
|
2015-07-20 00:09:47 -07:00
|
|
|
validator = require('validator'),
|
|
|
|
|
_ = require('underscore');
|
|
|
|
|
|
|
|
|
|
var CRYPTO_SALT_SIZE = 64; // 512-bit salt
|
|
|
|
|
var CRYPTO_ITERATIONS = 10000; // iterations
|
|
|
|
|
var CRYPTO_KEY_LENGTH = 512; // bits
|
2017-01-04 14:53:21 +01:00
|
|
|
var CRYPTO_DIGEST = 'sha1'; // used to be the default in node 4.1.1 cannot change since it will affect existing db records
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
// http://dustinsenos.com/articles/customErrorsInNode
|
|
|
|
|
// http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
|
2018-04-29 17:37:53 -07:00
|
|
|
function UsersError(reason, errorOrMessage) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof reason, 'string');
|
|
|
|
|
assert(errorOrMessage instanceof Error || typeof errorOrMessage === 'string' || typeof errorOrMessage === 'undefined');
|
|
|
|
|
|
|
|
|
|
Error.call(this);
|
|
|
|
|
Error.captureStackTrace(this, this.constructor);
|
|
|
|
|
|
|
|
|
|
this.name = this.constructor.name;
|
|
|
|
|
this.reason = reason;
|
|
|
|
|
if (typeof errorOrMessage === 'undefined') {
|
|
|
|
|
this.message = reason;
|
|
|
|
|
} else if (typeof errorOrMessage === 'string') {
|
|
|
|
|
this.message = errorOrMessage;
|
|
|
|
|
} else {
|
|
|
|
|
this.message = 'Internal error';
|
|
|
|
|
this.nestedError = errorOrMessage;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-29 17:37:53 -07:00
|
|
|
util.inherits(UsersError, Error);
|
|
|
|
|
UsersError.INTERNAL_ERROR = 'Internal Error';
|
|
|
|
|
UsersError.ALREADY_EXISTS = 'Already Exists';
|
|
|
|
|
UsersError.NOT_FOUND = 'Not Found';
|
|
|
|
|
UsersError.WRONG_PASSWORD = 'Wrong User or Password';
|
|
|
|
|
UsersError.BAD_FIELD = 'Bad field';
|
|
|
|
|
UsersError.BAD_TOKEN = 'Bad token';
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-01-25 18:03:26 +01:00
|
|
|
// keep this in sync with validateGroupname and validateAlias
|
2015-07-20 00:09:47 -07:00
|
|
|
function validateUsername(username) {
|
|
|
|
|
assert.strictEqual(typeof username, 'string');
|
2016-04-01 16:48:34 +02:00
|
|
|
|
2018-04-29 17:37:53 -07:00
|
|
|
if (username.length < 1) return new UsersError(UsersError.BAD_FIELD, 'Username must be atleast 1 char');
|
|
|
|
|
if (username.length >= 200) return new UsersError(UsersError.BAD_FIELD, 'Username too long');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-04-29 17:37:53 -07:00
|
|
|
if (constants.RESERVED_NAMES.indexOf(username) !== -1) return new UsersError(UsersError.BAD_FIELD, 'Username is reserved');
|
2016-04-13 16:50:20 -07:00
|
|
|
|
2018-05-05 09:55:46 -07:00
|
|
|
// also need to consider valid LDAP characters here (e.g '+' is reserved)
|
|
|
|
|
if (/[^a-zA-Z0-9.-]/.test(username)) return new UsersError(UsersError.BAD_FIELD, 'Username can only contain alphanumerals, dot and -');
|
2016-05-25 21:36:20 -07:00
|
|
|
|
2016-05-30 01:32:18 -07:00
|
|
|
// app emails are sent using the .app suffix
|
2018-04-29 17:37:53 -07:00
|
|
|
if (username.indexOf('.app') !== -1) return new UsersError(UsersError.BAD_FIELD, 'Username pattern is reserved for apps');
|
2016-05-18 21:45:02 -07:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function validateEmail(email) {
|
|
|
|
|
assert.strictEqual(typeof email, 'string');
|
|
|
|
|
|
2018-04-29 17:37:53 -07:00
|
|
|
if (!validator.isEmail(email)) return new UsersError(UsersError.BAD_FIELD, 'Invalid email');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function validateToken(token) {
|
|
|
|
|
assert.strictEqual(typeof token, 'string');
|
|
|
|
|
|
2018-04-29 17:37:53 -07:00
|
|
|
if (token.length !== 64) return new UsersError(UsersError.BAD_TOKEN, 'Invalid token'); // 256-bit hex coded token
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-19 23:34:49 -08:00
|
|
|
function validateDisplayName(name) {
|
|
|
|
|
assert.strictEqual(typeof name, 'string');
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-11 12:55:24 -07:00
|
|
|
function validatePassword(password) {
|
|
|
|
|
assert.strictEqual(typeof password, 'string');
|
|
|
|
|
|
|
|
|
|
if (password.length < 8) return new UsersError(UsersError.BAD_FIELD, 'Password must be atleast 8 characters');
|
2019-02-08 09:47:36 -08:00
|
|
|
if (password.length > 256) return new UsersError(UsersError.BAD_FIELD, 'Password cannot be more than 256 characters');
|
2018-06-11 12:55:24 -07:00
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-25 15:54:24 -07:00
|
|
|
// remove all fields that should never be sent out via REST API
|
2018-03-02 11:24:06 +01:00
|
|
|
function removePrivateFields(user) {
|
|
|
|
|
return _.pick(user, 'id', 'username', 'email', 'fallbackEmail', 'displayName', 'groupIds', 'admin');
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-25 15:54:24 -07:00
|
|
|
// remove all fields that Non-privileged users must not see
|
|
|
|
|
function removeRestrictedFields(user) {
|
|
|
|
|
return _.pick(user, 'id', 'username', 'email', 'displayName');
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-11 14:47:24 -07:00
|
|
|
function create(username, password, email, displayName, options, auditSource, callback) {
|
2017-02-02 00:23:11 -08:00
|
|
|
assert(username === null || typeof username === 'string');
|
2018-06-11 12:59:52 -07:00
|
|
|
assert(password === null || typeof password === 'string');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof email, 'string');
|
2016-01-19 23:34:49 -08:00
|
|
|
assert.strictEqual(typeof displayName, 'string');
|
2018-06-11 14:47:24 -07:00
|
|
|
assert(options && typeof options === 'object');
|
2019-01-23 11:18:31 +01:00
|
|
|
assert(auditSource && typeof auditSource === 'object');
|
2016-02-08 21:05:02 -08:00
|
|
|
|
2018-08-17 09:49:58 -07:00
|
|
|
const isOwner = !!options.owner;
|
2018-08-21 16:41:42 +02:00
|
|
|
const isAdmin = !!options.admin;
|
2018-08-17 09:49:58 -07:00
|
|
|
const invitor = options.invitor || null;
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2017-02-02 00:23:11 -08:00
|
|
|
var error;
|
2016-04-14 16:25:46 +02:00
|
|
|
|
2017-02-02 00:23:11 -08:00
|
|
|
if (username !== null) {
|
|
|
|
|
username = username.toLowerCase();
|
|
|
|
|
error = validateUsername(username);
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-06-11 12:59:52 -07:00
|
|
|
if (password !== null) {
|
|
|
|
|
error = validatePassword(password);
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.BAD_FIELD, error.message));
|
|
|
|
|
} else {
|
|
|
|
|
password = hat(8 * 8);
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2017-02-02 00:23:11 -08:00
|
|
|
email = email.toLowerCase();
|
2015-07-20 00:09:47 -07:00
|
|
|
error = validateEmail(email);
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2016-01-19 23:34:49 -08:00
|
|
|
error = validateDisplayName(displayName);
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
crypto.randomBytes(CRYPTO_SALT_SIZE, function (error, salt) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2017-01-04 14:53:21 +01:00
|
|
|
crypto.pbkdf2(password, salt, CRYPTO_ITERATIONS, CRYPTO_KEY_LENGTH, CRYPTO_DIGEST, function (error, derivedKey) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-09-17 13:50:20 -07:00
|
|
|
var now = (new Date()).toISOString();
|
2015-07-20 00:09:47 -07:00
|
|
|
var user = {
|
2016-04-01 16:48:34 +02:00
|
|
|
id: 'uid-' + uuid.v4(),
|
2015-07-20 00:09:47 -07:00
|
|
|
username: username,
|
|
|
|
|
email: email,
|
2018-01-21 14:25:39 +01:00
|
|
|
fallbackEmail: email, // for new users the fallbackEmail is also the default email
|
2019-03-21 20:06:14 -07:00
|
|
|
password: Buffer.from(derivedKey, 'binary').toString('hex'),
|
2015-07-20 00:09:47 -07:00
|
|
|
salt: salt.toString('hex'),
|
|
|
|
|
createdAt: now,
|
|
|
|
|
modifiedAt: now,
|
2018-08-17 09:49:58 -07:00
|
|
|
resetToken: '',
|
2018-07-26 17:17:52 -07:00
|
|
|
displayName: displayName,
|
2018-08-21 16:41:42 +02:00
|
|
|
admin: isOwner || isAdmin
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
2017-02-14 10:42:32 -08:00
|
|
|
userdb.add(user.id, user, function (error) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.ALREADY_EXISTS) return callback(new UsersError(UsersError.ALREADY_EXISTS, error.message));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2019-01-19 14:34:49 +01:00
|
|
|
// when this is used to create the owner, then we have to patch the auditSource to contain himself
|
|
|
|
|
if (isOwner) {
|
|
|
|
|
auditSource.userId = user.id;
|
|
|
|
|
auditSource.username = user.username;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 09:49:58 -07:00
|
|
|
eventlog.add(eventlog.ACTION_USER_ADD, auditSource, { userId: user.id, email: user.email, user: removePrivateFields(user), invitor: invitor });
|
2016-09-22 15:55:18 -07:00
|
|
|
|
2019-01-17 13:12:26 +01:00
|
|
|
callback(null, user);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:07:55 -07:00
|
|
|
// returns true if ghost user was matched
|
|
|
|
|
function verifyGhost(username, password) {
|
|
|
|
|
assert.strictEqual(typeof username, 'string');
|
|
|
|
|
assert.strictEqual(typeof password, 'string');
|
|
|
|
|
|
2018-02-27 10:30:57 -08:00
|
|
|
var ghostData = safe.JSON.parse(safe.fs.readFileSync(constants.GHOST_USER_FILE, 'utf8'));
|
2016-07-12 10:07:55 -07:00
|
|
|
if (!ghostData) return false;
|
|
|
|
|
|
|
|
|
|
if (username in ghostData && ghostData[username] === password) {
|
|
|
|
|
debug('verifyGhost: matched ghost user');
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-05 16:27:04 +02:00
|
|
|
function verify(userId, password, callback) {
|
|
|
|
|
assert.strictEqual(typeof userId, 'string');
|
|
|
|
|
assert.strictEqual(typeof password, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2018-06-11 11:39:49 -07:00
|
|
|
get(userId, function (error, user) {
|
2016-09-26 16:28:52 +02:00
|
|
|
if (error) return callback(error);
|
2016-04-05 16:27:04 +02:00
|
|
|
|
2017-02-20 21:48:31 +01:00
|
|
|
// for just invited users the username may be still null
|
2018-05-14 14:49:31 -07:00
|
|
|
if (user.username && verifyGhost(user.username, password)) {
|
|
|
|
|
user.ghost = true;
|
|
|
|
|
return callback(null, user);
|
|
|
|
|
}
|
2016-07-12 10:07:55 -07:00
|
|
|
|
2019-03-21 20:06:14 -07:00
|
|
|
var saltBinary = Buffer.from(user.salt, 'hex');
|
2017-01-04 14:53:21 +01:00
|
|
|
crypto.pbkdf2(password, saltBinary, CRYPTO_ITERATIONS, CRYPTO_KEY_LENGTH, CRYPTO_DIGEST, function (error, derivedKey) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2016-04-05 16:27:04 +02:00
|
|
|
|
2019-03-21 20:06:14 -07:00
|
|
|
var derivedKeyHex = Buffer.from(derivedKey, 'binary').toString('hex');
|
2018-04-29 17:37:53 -07:00
|
|
|
if (derivedKeyHex !== user.password) return callback(new UsersError(UsersError.WRONG_PASSWORD));
|
2016-04-05 16:27:04 +02:00
|
|
|
|
|
|
|
|
callback(null, user);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function verifyWithUsername(username, password, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof username, 'string');
|
|
|
|
|
assert.strictEqual(typeof password, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-04-14 16:25:46 +02:00
|
|
|
userdb.getByUsername(username.toLowerCase(), function (error, user) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason == DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-06-23 11:58:05 +02:00
|
|
|
verify(user.id, password, callback);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function verifyWithEmail(email, password, callback) {
|
|
|
|
|
assert.strictEqual(typeof email, 'string');
|
|
|
|
|
assert.strictEqual(typeof password, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2018-01-22 16:12:13 +01:00
|
|
|
userdb.getByEmail(email.toLowerCase(), function (error, user) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason == DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2016-09-27 15:41:34 +02:00
|
|
|
|
2018-01-22 16:12:13 +01:00
|
|
|
verify(user.id, password, callback);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-02 22:25:48 -07:00
|
|
|
function removeUser(userId, auditSource, callback) {
|
|
|
|
|
assert.strictEqual(typeof userId, 'string');
|
2019-01-23 11:18:31 +01:00
|
|
|
assert(auditSource && typeof auditSource === 'object');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2018-06-11 11:39:49 -07:00
|
|
|
get(userId, function (error, user) {
|
2016-06-02 22:25:48 -07:00
|
|
|
if (error) return callback(error);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-04-29 17:37:53 -07:00
|
|
|
if (config.isDemo() && user.username === constants.DEMO_USERNAME) return callback(new UsersError(UsersError.BAD_FIELD, 'Not allowed in demo mode'));
|
2016-08-31 23:41:28 -07:00
|
|
|
|
2018-04-02 09:45:46 -07:00
|
|
|
userdb.del(userId, function (error) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2016-05-01 20:09:31 -07:00
|
|
|
|
2019-01-17 13:12:26 +01:00
|
|
|
eventlog.add(eventlog.ACTION_USER_REMOVE, auditSource, { userId: userId, user: removePrivateFields(user) }, callback);
|
2016-06-02 22:25:48 -07:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 16:39:20 +01:00
|
|
|
function getAll(callback) {
|
2016-02-09 09:25:17 -08:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-06-03 00:04:17 -07:00
|
|
|
userdb.getAllWithGroupIds(function (error, results) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2016-02-09 09:25:17 -08:00
|
|
|
|
2018-01-21 14:50:24 +01:00
|
|
|
return callback(null, results);
|
2016-02-09 09:25:17 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 17:21:40 +01:00
|
|
|
function getAllPaged(search, page, perPage, callback) {
|
|
|
|
|
assert(typeof search === 'string' || search === null);
|
2019-01-14 16:39:20 +01:00
|
|
|
assert.strictEqual(typeof page, 'number');
|
|
|
|
|
assert.strictEqual(typeof perPage, 'number');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2019-01-15 17:21:40 +01:00
|
|
|
userdb.getAllWithGroupIdsPaged(search, page, perPage, function (error, results) {
|
2019-01-14 16:39:20 +01:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
return callback(null, results);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 09:59:29 -07:00
|
|
|
function count(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
userdb.count(function (error, count) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2016-06-07 09:59:29 -07:00
|
|
|
|
|
|
|
|
callback(null, count);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-10 18:08:08 -08:00
|
|
|
function isActivated(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
count(function (error, count) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
callback(null, count !== 0);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-11 11:39:49 -07:00
|
|
|
function get(userId, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof userId, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
userdb.get(userId, function (error, result) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-06-18 13:57:17 -07:00
|
|
|
groups.getMembership(userId, function (error, groupIds) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2016-02-08 20:38:50 -08:00
|
|
|
|
|
|
|
|
result.groupIds = groupIds;
|
|
|
|
|
|
2018-01-21 14:50:24 +01:00
|
|
|
return callback(null, result);
|
2016-02-08 20:38:50 -08:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-12 17:22:41 -07:00
|
|
|
function getByResetToken(email, resetToken, callback) {
|
|
|
|
|
assert.strictEqual(typeof email, 'string');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof resetToken, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2018-06-12 17:22:41 -07:00
|
|
|
var error = validateEmail(email);
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
error = validateToken(resetToken);
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2018-06-12 17:22:41 -07:00
|
|
|
userdb.getByResetToken(email, resetToken, function (error, result) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-06-15 16:22:28 -07:00
|
|
|
callback(null, result);
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-18 21:15:50 -07:00
|
|
|
function getByUsername(username, callback) {
|
|
|
|
|
assert.strictEqual(typeof username, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
userdb.getByUsername(username.toLowerCase(), function (error, result) {
|
|
|
|
|
if (error && error.reason == DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
get(result.id, callback);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-02 23:53:06 -07:00
|
|
|
function updateUser(userId, data, auditSource, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof userId, 'string');
|
2016-06-02 23:53:06 -07:00
|
|
|
assert.strictEqual(typeof data, 'object');
|
2019-01-23 11:18:31 +01:00
|
|
|
assert(auditSource && typeof auditSource === 'object');
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-06-02 23:53:06 -07:00
|
|
|
var error;
|
2018-07-26 17:17:52 -07:00
|
|
|
data = _.pick(data, 'email', 'fallbackEmail', 'displayName', 'username', 'admin');
|
2016-04-14 16:25:46 +02:00
|
|
|
|
2016-06-02 23:53:06 -07:00
|
|
|
if (_.isEmpty(data)) return callback();
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-06-02 23:53:06 -07:00
|
|
|
if (data.username) {
|
|
|
|
|
data.username = data.username.toLowerCase();
|
|
|
|
|
error = validateUsername(data.username);
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.email) {
|
|
|
|
|
data.email = data.email.toLowerCase();
|
|
|
|
|
error = validateEmail(data.email);
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-01-21 14:25:39 +01:00
|
|
|
if (data.fallbackEmail) {
|
|
|
|
|
data.fallbackEmail = data.fallbackEmail.toLowerCase();
|
|
|
|
|
error = validateEmail(data.fallbackEmail);
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-26 17:17:52 -07:00
|
|
|
userdb.get(userId, function (error, oldUser) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2016-05-01 20:09:31 -07:00
|
|
|
|
2017-02-15 23:19:54 -08:00
|
|
|
userdb.update(userId, data, function (error) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.ALREADY_EXISTS) return callback(new UsersError(UsersError.ALREADY_EXISTS, error.message));
|
|
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND, error));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2017-02-15 23:19:54 -08:00
|
|
|
|
2018-07-26 17:17:52 -07:00
|
|
|
callback(null);
|
2018-03-02 11:02:32 +01:00
|
|
|
|
2018-06-11 11:39:49 -07:00
|
|
|
get(userId, function (error, result) {
|
2018-10-29 14:43:43 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2018-03-02 11:02:32 +01:00
|
|
|
|
2019-01-17 13:12:26 +01:00
|
|
|
eventlog.add(eventlog.ACTION_USER_UPDATE, auditSource, {
|
|
|
|
|
userId: userId,
|
|
|
|
|
user: removePrivateFields(result),
|
|
|
|
|
adminStatusChanged: ((result.admin && !oldUser.admin) || (!result.admin && oldUser.admin))
|
|
|
|
|
});
|
2018-03-02 11:02:32 +01:00
|
|
|
});
|
2017-02-15 23:19:54 -08:00
|
|
|
});
|
2016-09-21 15:34:58 -07:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2018-06-18 13:57:17 -07:00
|
|
|
function setMembership(userId, groupIds, callback) {
|
2016-02-09 15:47:02 -08:00
|
|
|
assert.strictEqual(typeof userId, 'string');
|
|
|
|
|
assert(Array.isArray(groupIds));
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2018-07-26 17:17:52 -07:00
|
|
|
groups.setMembership(userId, groupIds, function (error) {
|
|
|
|
|
if (error && error.reason === GroupsError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND, 'One or more groups not found'));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2016-05-04 13:54:32 +02:00
|
|
|
|
2018-07-26 17:17:52 -07:00
|
|
|
callback(null);
|
2016-02-09 15:47:02 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-15 16:04:33 +01:00
|
|
|
function getAllAdmins(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
userdb.getAllAdmins(function (error, admins) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2016-09-27 14:52:15 +02:00
|
|
|
|
2018-01-21 14:50:24 +01:00
|
|
|
callback(null, admins);
|
2016-01-15 16:04:33 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
function resetPasswordByIdentifier(identifier, callback) {
|
|
|
|
|
assert.strictEqual(typeof identifier, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
var getter;
|
|
|
|
|
if (identifier.indexOf('@') === -1) getter = userdb.getByUsername;
|
|
|
|
|
else getter = userdb.getByEmail;
|
|
|
|
|
|
2016-04-14 16:25:46 +02:00
|
|
|
getter(identifier.toLowerCase(), function (error, result) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
result.resetToken = hat(256);
|
|
|
|
|
|
|
|
|
|
userdb.update(result.id, result, function (error) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
mailer.passwordReset(result);
|
|
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setPassword(userId, newPassword, callback) {
|
|
|
|
|
assert.strictEqual(typeof userId, 'string');
|
|
|
|
|
assert.strictEqual(typeof newPassword, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
var error = validatePassword(newPassword);
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.BAD_FIELD, error.message));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
userdb.get(userId, function (error, user) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-04-29 17:37:53 -07:00
|
|
|
if (config.isDemo() && user.username === constants.DEMO_USERNAME) return callback(new UsersError(UsersError.BAD_FIELD, 'Not allowed in demo mode'));
|
2016-08-31 23:41:28 -07:00
|
|
|
|
2019-03-21 20:06:14 -07:00
|
|
|
var saltBuffer = Buffer.from(user.salt, 'hex');
|
2017-01-04 14:53:21 +01:00
|
|
|
crypto.pbkdf2(newPassword, saltBuffer, CRYPTO_ITERATIONS, CRYPTO_KEY_LENGTH, CRYPTO_DIGEST, function (error, derivedKey) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-09-17 13:50:20 -07:00
|
|
|
user.modifiedAt = (new Date()).toISOString();
|
2019-03-21 20:06:14 -07:00
|
|
|
user.password = Buffer.from(derivedKey, 'binary').toString('hex');
|
2015-07-20 00:09:47 -07:00
|
|
|
user.resetToken = '';
|
|
|
|
|
|
|
|
|
|
userdb.update(userId, user, function (error) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-04-30 14:30:41 -07:00
|
|
|
callback();
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-01 20:01:34 -07:00
|
|
|
function createOwner(username, password, email, displayName, auditSource, callback) {
|
2016-04-04 15:14:00 +02:00
|
|
|
assert.strictEqual(typeof username, 'string');
|
|
|
|
|
assert.strictEqual(typeof password, 'string');
|
|
|
|
|
assert.strictEqual(typeof email, 'string');
|
|
|
|
|
assert.strictEqual(typeof displayName, 'string');
|
2019-01-23 11:18:31 +01:00
|
|
|
assert(auditSource && typeof auditSource === 'object');
|
2016-04-04 15:14:00 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-04-14 16:25:46 +02:00
|
|
|
// This is only not allowed for the owner
|
2018-04-29 17:37:53 -07:00
|
|
|
if (username === '') return callback(new UsersError(UsersError.BAD_FIELD, 'Username cannot be empty'));
|
2016-04-04 15:16:16 +02:00
|
|
|
|
2019-07-02 20:22:17 -07:00
|
|
|
count(function (error, count) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
|
|
|
|
if (count !== 0) return callback(new UsersError(UsersError.ALREADY_EXISTS, 'Owner already exists'));
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2018-07-26 17:17:52 -07:00
|
|
|
create(username, password, email, displayName, { owner: true }, auditSource, function (error, user) {
|
|
|
|
|
if (error) return callback(error);
|
2016-02-08 15:16:59 -08:00
|
|
|
|
2018-07-26 17:17:52 -07:00
|
|
|
callback(null, user);
|
2016-02-08 15:16:59 -08:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-13 12:28:38 -08:00
|
|
|
function getOwner(callback) {
|
|
|
|
|
userdb.getOwner(function (error, owner) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2016-01-13 12:28:38 -08:00
|
|
|
|
2018-01-21 14:50:24 +01:00
|
|
|
return callback(null, owner);
|
2016-01-13 12:28:38 -08:00
|
|
|
});
|
|
|
|
|
}
|
2016-01-18 15:16:18 +01:00
|
|
|
|
2018-08-17 09:49:58 -07:00
|
|
|
function createInvite(userId, callback) {
|
2016-01-18 15:16:18 +01:00
|
|
|
assert.strictEqual(typeof userId, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
userdb.get(userId, function (error, userObject) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2016-01-18 15:16:18 +01:00
|
|
|
|
|
|
|
|
userObject.resetToken = hat(256);
|
|
|
|
|
|
|
|
|
|
userdb.update(userId, userObject, function (error) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2016-01-18 15:16:18 +01:00
|
|
|
|
2016-04-04 18:14:07 +02:00
|
|
|
callback(null, userObject.resetToken);
|
2016-01-18 15:16:18 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-05-06 13:56:26 +02:00
|
|
|
|
2018-08-17 09:49:58 -07:00
|
|
|
function sendInvite(userId, options, callback) {
|
|
|
|
|
assert.strictEqual(typeof userId, 'string');
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
userdb.get(userId, function (error, userObject) {
|
|
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
if (!userObject.resetToken) return callback(new UsersError(UsersError.BAD_FIELD, 'Must generate resetToken to send inivitation'));
|
|
|
|
|
|
|
|
|
|
mailer.sendInvite(userObject, options.invitor || null);
|
|
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 19:08:15 +02:00
|
|
|
function setTwoFactorAuthenticationSecret(userId, callback) {
|
|
|
|
|
assert.strictEqual(typeof userId, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
userdb.get(userId, function (error, result) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2018-04-25 19:08:15 +02:00
|
|
|
|
2018-04-29 17:37:53 -07:00
|
|
|
if (result.twoFactorAuthenticationEnabled) return callback(new UsersError(UsersError.ALREADY_EXISTS));
|
2018-04-25 19:08:15 +02:00
|
|
|
|
2018-06-04 15:54:34 -07:00
|
|
|
var secret = speakeasy.generateSecret({ name: `Cloudron ${config.adminFqdn()} (${result.username})` });
|
2018-04-25 19:08:15 +02:00
|
|
|
|
|
|
|
|
userdb.update(userId, { twoFactorAuthenticationSecret: secret.base32, twoFactorAuthenticationEnabled: false }, function (error) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2018-04-25 19:08:15 +02:00
|
|
|
|
|
|
|
|
qrcode.toDataURL(secret.otpauth_url, function (error, dataUrl) {
|
2018-10-29 14:43:43 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2018-04-25 19:08:15 +02:00
|
|
|
|
|
|
|
|
callback(null, { secret: secret.base32, qrcode: dataUrl });
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enableTwoFactorAuthentication(userId, totpToken, callback) {
|
|
|
|
|
assert.strictEqual(typeof userId, 'string');
|
|
|
|
|
assert.strictEqual(typeof totpToken, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
userdb.get(userId, function (error, result) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new UsersError(UsersError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2018-04-25 19:08:15 +02:00
|
|
|
|
2019-07-23 14:42:03 -07:00
|
|
|
var verified = speakeasy.totp.verify({ secret: result.twoFactorAuthenticationSecret, encoding: 'base32', token: totpToken, window: 2 });
|
2018-04-29 17:37:53 -07:00
|
|
|
if (!verified) return callback(new UsersError(UsersError.BAD_TOKEN));
|
2018-04-25 19:08:15 +02:00
|
|
|
|
2018-04-29 17:37:53 -07:00
|
|
|
if (result.twoFactorAuthenticationEnabled) return callback(new UsersError(UsersError.ALREADY_EXISTS));
|
2018-04-25 19:08:15 +02:00
|
|
|
|
|
|
|
|
userdb.update(userId, { twoFactorAuthenticationEnabled: true }, function (error) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2018-04-25 19:08:15 +02:00
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function disableTwoFactorAuthentication(userId, callback) {
|
|
|
|
|
assert.strictEqual(typeof userId, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
userdb.update(userId, { twoFactorAuthenticationEnabled: false, twoFactorAuthenticationSecret: '' }, function (error) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error) return callback(new UsersError(UsersError.INTERNAL_ERROR, error));
|
2018-04-25 19:08:15 +02:00
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
|
|
|
|
}
|