2016-04-17 16:22:39 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
get: get,
|
|
|
|
|
update: update,
|
2018-04-25 19:08:15 +02:00
|
|
|
changePassword: changePassword,
|
|
|
|
|
setTwoFactorAuthenticationSecret: setTwoFactorAuthenticationSecret,
|
|
|
|
|
enableTwoFactorAuthentication: enableTwoFactorAuthentication,
|
|
|
|
|
disableTwoFactorAuthentication: disableTwoFactorAuthentication
|
2016-04-17 16:22:39 +02:00
|
|
|
};
|
|
|
|
|
|
2018-06-17 15:25:41 -07:00
|
|
|
var assert = require('assert'),
|
2016-04-17 16:22:39 +02:00
|
|
|
HttpError = require('connect-lastmile').HttpError,
|
|
|
|
|
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
2018-04-29 10:58:45 -07:00
|
|
|
users = require('../users.js'),
|
2018-04-29 17:37:53 -07:00
|
|
|
UsersError = users.UsersError,
|
2016-06-02 23:53:06 -07:00
|
|
|
_ = require('underscore');
|
2016-04-17 16:22:39 +02:00
|
|
|
|
2016-05-01 20:09:31 -07:00
|
|
|
function auditSource(req) {
|
|
|
|
|
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || null;
|
|
|
|
|
return { ip: ip, username: req.user ? req.user.username : null, userId: req.user ? req.user.id : null };
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-17 16:22:39 +02:00
|
|
|
function get(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.user, 'object');
|
|
|
|
|
|
2016-06-23 13:03:39 +02:00
|
|
|
next(new HttpSuccess(200, {
|
|
|
|
|
id: req.user.id,
|
|
|
|
|
username: req.user.username,
|
|
|
|
|
email: req.user.email,
|
2018-01-21 14:50:24 +01:00
|
|
|
fallbackEmail: req.user.fallbackEmail,
|
2018-04-26 16:29:40 +02:00
|
|
|
displayName: req.user.displayName,
|
2018-08-03 09:28:58 -07:00
|
|
|
twoFactorAuthenticationEnabled: req.user.twoFactorAuthenticationEnabled,
|
|
|
|
|
admin: req.user.admin
|
2016-06-23 13:03:39 +02:00
|
|
|
}));
|
2016-04-17 16:22:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function update(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.user, 'object');
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if ('email' in req.body && typeof req.body.email !== 'string') return next(new HttpError(400, 'email must be string'));
|
2018-01-22 15:55:55 +01:00
|
|
|
if ('fallbackEmail' in req.body && typeof req.body.fallbackEmail !== 'string') return next(new HttpError(400, 'fallbackEmail must be string'));
|
2016-04-17 16:22:39 +02:00
|
|
|
if ('displayName' in req.body && typeof req.body.displayName !== 'string') return next(new HttpError(400, 'displayName must be string'));
|
|
|
|
|
|
2018-01-22 15:55:55 +01:00
|
|
|
var data = _.pick(req.body, 'email', 'fallbackEmail', 'displayName');
|
2016-06-02 23:53:06 -07:00
|
|
|
|
2018-04-29 10:58:45 -07:00
|
|
|
users.update(req.user.id, data, auditSource(req), function (error) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === UsersError.BAD_FIELD) return next(new HttpError(400, error.message));
|
|
|
|
|
if (error && error.reason === UsersError.ALREADY_EXISTS) return next(new HttpError(409, error.message));
|
|
|
|
|
if (error && error.reason === UsersError.NOT_FOUND) return next(new HttpError(404, 'User not found'));
|
2016-04-17 16:22:39 +02:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(204));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function changePassword(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
assert.strictEqual(typeof req.user, 'object');
|
|
|
|
|
|
2016-06-02 00:31:41 -07:00
|
|
|
if (typeof req.body.newPassword !== 'string') return next(new HttpError(400, 'newPassword must be a string'));
|
2016-04-17 16:22:39 +02:00
|
|
|
|
2018-04-29 10:58:45 -07:00
|
|
|
users.setPassword(req.user.id, req.body.newPassword, function (error) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === UsersError.BAD_FIELD) return next(new HttpError(400, error.message));
|
2018-06-15 20:51:26 -07:00
|
|
|
if (error && error.reason === UsersError.NOT_FOUND) return next(new HttpError(404, 'User not found'));
|
2016-04-17 16:22:39 +02:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(204));
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-04-25 19:08:15 +02:00
|
|
|
|
|
|
|
|
function setTwoFactorAuthenticationSecret(req, res, next) {
|
2018-04-26 15:12:14 +02:00
|
|
|
assert.strictEqual(typeof req.user, 'object');
|
2018-04-25 19:08:15 +02:00
|
|
|
|
2018-04-29 10:58:45 -07:00
|
|
|
users.setTwoFactorAuthenticationSecret(req.user.id, function (error, result) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === UsersError.ALREADY_EXISTS) return next(new HttpError(409, 'TwoFactor Authentication is enabled, disable first'));
|
2018-04-25 19:08:15 +02:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
2018-04-27 08:20:12 +02:00
|
|
|
next(new HttpSuccess(201, { secret: result.secret, qrcode: result.qrcode }));
|
2018-04-25 19:08:15 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enableTwoFactorAuthentication(req, res, next) {
|
2018-04-26 15:12:14 +02:00
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
assert.strictEqual(typeof req.user, 'object');
|
2018-04-25 19:08:15 +02:00
|
|
|
|
|
|
|
|
if (!req.body.totpToken || typeof req.body.totpToken !== 'string') return next(new HttpError(400, 'totpToken must be a nonempty string'));
|
|
|
|
|
|
2018-04-29 10:58:45 -07:00
|
|
|
users.enableTwoFactorAuthentication(req.user.id, req.body.totpToken, function (error) {
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === UsersError.NOT_FOUND) return next(new HttpError(404, 'User not found'));
|
2018-06-15 20:51:26 -07:00
|
|
|
if (error && error.reason === UsersError.BAD_TOKEN) return next(new HttpError(401, 'Invalid token'));
|
2018-04-29 17:37:53 -07:00
|
|
|
if (error && error.reason === UsersError.ALREADY_EXISTS) return next(new HttpError(409, 'TwoFactor Authentication is already enabled'));
|
2018-04-25 19:08:15 +02:00
|
|
|
if (error) return next(new HttpError(500, error));
|
2018-04-26 16:29:40 +02:00
|
|
|
|
2018-04-25 19:08:15 +02:00
|
|
|
next(new HttpSuccess(202, {}));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function disableTwoFactorAuthentication(req, res, next) {
|
2018-04-26 15:12:14 +02:00
|
|
|
assert.strictEqual(typeof req.user, 'object');
|
2018-04-25 19:08:15 +02:00
|
|
|
|
2018-04-29 10:58:45 -07:00
|
|
|
users.disableTwoFactorAuthentication(req.user.id, function (error) {
|
2018-04-25 19:08:15 +02:00
|
|
|
if (error) return next(new HttpError(500, error));
|
2018-04-26 16:29:40 +02:00
|
|
|
|
2018-04-25 19:08:15 +02:00
|
|
|
next(new HttpSuccess(202, {}));
|
|
|
|
|
});
|
|
|
|
|
}
|