You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/controllers/authentication.js

439 lines
12 KiB
JavaScript

10 years ago
"use strict";
9 years ago
var async = require('async');
var winston = require('winston');
var passport = require('passport');
var nconf = require('nconf');
var validator = require('validator');
var _ = require('underscore');
9 years ago
var url = require('url');
9 years ago
var db = require('../database');
var meta = require('../meta');
var user = require('../user');
var plugins = require('../plugins');
var utils = require('../../public/src/utils');
var Password = require('../password');
var sockets = require('../socket.io');
9 years ago
var authenticationController = {};
10 years ago
authenticationController.register = function (req, res, next) {
10 years ago
var registrationType = meta.config.registrationType || 'normal';
if (registrationType === 'disabled') {
10 years ago
return res.sendStatus(403);
}
var userData = {};
for (var key in req.body) {
if (req.body.hasOwnProperty(key)) {
userData[key] = req.body[key];
}
}
async.waterfall([
function (next) {
9 years ago
if (registrationType === 'invite-only' || registrationType === 'admin-invite-only') {
10 years ago
user.verifyInvitation(userData, next);
} else {
next();
}
},
function (next) {
10 years ago
if (!userData.email) {
return next(new Error('[[error:invalid-email]]'));
}
if (!userData.username || userData.username.length < meta.config.minimumUsernameLength) {
return next(new Error('[[error:username-too-short]]'));
}
if (userData.username.length > meta.config.maximumUsernameLength) {
return next(new Error('[[error:username-too-long'));
}
10 years ago
user.isPasswordValid(userData.password, next);
10 years ago
},
function (next) {
9 years ago
if (registrationType === 'normal' || registrationType === 'invite-only' || registrationType === 'admin-invite-only') {
next(null, false);
10 years ago
} else if (registrationType === 'admin-approval') {
next(null, true);
} else if (registrationType === 'admin-approval-ip') {
db.sortedSetCard('ip:' + req.ip + ':uid', function (err, count) {
if (err) {
next(err);
} else {
next(null, !!count);
}
});
10 years ago
}
},
function (queue, next) {
res.locals.processLogin = true; // set it to false in plugin if you wish to just register only
plugins.fireHook('filter:register.check', {req: req, res: res, userData: userData, queue: queue}, next);
},
function (data, next) {
if (data.queue) {
addToApprovalQueue(req, userData, next);
} else {
registerAndLoginUser(req, res, userData, next);
}
10 years ago
}
], function (err, data) {
10 years ago
if (err) {
return res.status(400).send(err.message);
}
10 years ago
if (req.body.userLang) {
user.setSetting(data.uid, 'userLang', req.body.userLang);
}
10 years ago
res.json(data);
});
};
9 years ago
function registerAndLoginUser(req, res, userData, callback) {
10 years ago
var uid;
async.waterfall([
function (next) {
plugins.fireHook('filter:register.interstitial', {
userData: userData,
interstitials: []
}, function (err, data) {
if (err) {
return next(err);
}
// If interstitials are found, save registration attempt into session and abort
var deferRegistration = data.interstitials.length;
if (!deferRegistration) {
return next();
} else {
userData.register = true;
req.session.registration = userData;
return res.json({ referrer: nconf.get('relative_path') + '/register/complete' });
}
});
},
function (next) {
10 years ago
user.create(userData, next);
10 years ago
},
function (_uid, next) {
10 years ago
uid = _uid;
9 years ago
if (res.locals.processLogin) {
authenticationController.doLogin(req, uid, next);
9 years ago
} else {
next();
}
10 years ago
},
function (next) {
user.deleteInvitationKey(userData.email);
10 years ago
plugins.fireHook('filter:register.complete', {uid: uid, referrer: req.body.referrer || nconf.get('relative_path') + '/'}, next);
10 years ago
}
10 years ago
], callback);
}
10 years ago
function addToApprovalQueue(req, userData, callback) {
10 years ago
async.waterfall([
function (next) {
10 years ago
userData.ip = req.ip;
user.addToApprovalQueue(userData, next);
},
function (next) {
10 years ago
next(null, {message: '[[register:registration-added-to-queue]]'});
}
], callback);
}
10 years ago
authenticationController.registerComplete = function (req, res, next) {
// For the interstitials that respond, execute the callback with the form body
plugins.fireHook('filter:register.interstitial', {
userData: req.session.registration,
interstitials: []
}, function (err, data) {
if (err) {
return next(err);
}
var callbacks = data.interstitials.reduce(function (memo, cur) {
if (cur.hasOwnProperty('callback') && typeof cur.callback === 'function') {
memo.push(async.apply(cur.callback, req.session.registration, req.body));
}
return memo;
}, []);
var done = function () {
delete req.session.registration;
if (req.session.returnTo) {
res.redirect(req.session.returnTo);
} else {
res.redirect(nconf.get('relative_path') + '/');
}
9 years ago
};
async.parallel(callbacks, function (err) {
if (err) {
req.flash('error', err.message);
return res.redirect(nconf.get('relative_path') + '/register/complete');
}
if (req.session.registration.register === true) {
res.locals.processLogin = true;
registerAndLoginUser(req, res, req.session.registration, done);
} else {
// Clear registration data in session
done();
}
});
});
};
authenticationController.registerAbort = function (req, res) {
// End the session and redirect to home
req.session.destroy(function () {
res.redirect(nconf.get('relative_path') + '/');
});
};
authenticationController.login = function (req, res, next) {
10 years ago
if (plugins.hasListeners('action:auth.overrideLogin')) {
return continueLogin(req, res, next);
}
var loginWith = meta.config.allowLoginWith || 'username-email';
if (req.body.username && utils.isEmailValid(req.body.username) && loginWith.indexOf('email') !== -1) {
user.getUsernameByEmail(req.body.username, function (err, username) {
10 years ago
if (err) {
return next(err);
}
req.body.username = username ? username : req.body.username;
continueLogin(req, res, next);
});
} else if (loginWith.indexOf('username') !== -1 && !validator.isEmail(req.body.username)) {
continueLogin(req, res, next);
} else {
res.status(500).send('[[error:wrong-login-type-' + loginWith + ']]');
}
};
function continueLogin(req, res, next) {
passport.authenticate('local', function (err, userData, info) {
10 years ago
if (err) {
return res.status(403).send(err.message);
}
if (!userData) {
if (typeof info === 'object') {
info = '[[error:invalid-username-or-password]]';
}
return res.status(403).send(info);
}
var passwordExpiry = userData.passwordExpiry !== undefined ? parseInt(userData.passwordExpiry, 10) : null;
// Alter user cookie depending on passed-in option
if (req.body.remember === 'on') {
9 years ago
var duration = 1000 * 60 * 60 * 24 * (parseInt(meta.config.loginDays, 10) || 14);
10 years ago
req.session.cookie.maxAge = duration;
req.session.cookie.expires = new Date(Date.now() + duration);
} else {
req.session.cookie.maxAge = false;
req.session.cookie.expires = false;
}
if (passwordExpiry && passwordExpiry < Date.now()) {
winston.verbose('[auth] Triggering password reset for uid ' + userData.uid + ' due to password policy');
req.session.passwordExpired = true;
user.reset.generate(userData.uid, function (err, code) {
if (err) {
return res.status(403).send(err.message);
}
10 years ago
res.status(200).send(nconf.get('relative_path') + '/reset/' + code);
});
} else {
authenticationController.doLogin(req, userData.uid, function (err) {
10 years ago
if (err) {
return res.status(403).send(err.message);
}
10 years ago
if (!req.session.returnTo) {
res.status(200).send(nconf.get('relative_path') + '/');
} else {
var next = req.session.returnTo;
delete req.session.returnTo;
res.status(200).send(next);
}
});
}
})(req, res, next);
}
authenticationController.doLogin = function (req, uid, callback) {
9 years ago
if (!uid) {
return callback();
}
req.login({uid: uid}, function (err) {
9 years ago
if (err) {
return callback(err);
}
9 years ago
authenticationController.onSuccessfulLogin(req, uid, callback);
});
};
9 years ago
authenticationController.onSuccessfulLogin = function (req, uid, callback) {
callback = callback || function () {};
9 years ago
var uuid = utils.generateUUID();
req.session.meta = {};
9 years ago
delete req.session.forceLogin;
9 years ago
// Associate IP used during login with user account
user.logIP(uid, req.ip);
req.session.meta.ip = req.ip;
// Associate metadata retrieved via user-agent
req.session.meta = _.extend(req.session.meta, {
uuid: uuid,
datetime: Date.now(),
platform: req.useragent.platform,
browser: req.useragent.browser,
version: req.useragent.version
});
// Associate login session with user
async.parallel([
function (next) {
user.auth.addSession(uid, req.sessionID, next);
},
function (next) {
db.setObjectField('uid:' + uid + 'sessionUUID:sessionId', uuid, req.sessionID, next);
},
function (next) {
user.updateLastOnlineTime(uid, next);
9 years ago
}
], function (err) {
9 years ago
if (err) {
return callback(err);
}
// Force session check for all connected socket.io clients with the same session id
sockets.in('sess_' + req.sessionID).emit('checkSession', uid);
9 years ago
plugins.fireHook('action:user.loggedIn', uid);
callback();
9 years ago
});
9 years ago
};
9 years ago
authenticationController.localLogin = function (req, username, password, next) {
9 years ago
if (!username) {
return next(new Error('[[error:invalid-username]]'));
10 years ago
}
var userslug = utils.slugify(username);
var uid, userData = {};
async.waterfall([
9 years ago
function (next) {
user.isPasswordValid(password, next);
},
function (next) {
10 years ago
user.getUidByUserslug(userslug, next);
},
9 years ago
function (_uid, next) {
10 years ago
if (!_uid) {
return next(new Error('[[error:no-user]]'));
}
uid = _uid;
user.auth.logAttempt(uid, req.ip, next);
},
9 years ago
function (next) {
10 years ago
async.parallel({
userData: function (next) {
db.getObjectFields('user:' + uid, ['password', 'passwordExpiry'], next);
10 years ago
},
isAdmin: function (next) {
10 years ago
user.isAdministrator(uid, next);
},
banned: function (next) {
user.isBanned(uid, next);
10 years ago
}
}, next);
},
9 years ago
function (result, next) {
10 years ago
userData = result.userData;
userData.uid = uid;
userData.isAdmin = result.isAdmin;
if (!result.isAdmin && parseInt(meta.config.allowLocalLogin, 10) === 0) {
return next(new Error('[[error:local-login-disabled]]'));
}
if (!userData || !userData.password) {
return next(new Error('[[error:invalid-user-data]]'));
}
if (result.banned) {
// Retrieve ban reason and show error
return user.getLatestBanInfo(uid, function (err, banInfo) {
if (err) {
next(err);
} else if (banInfo.reason) {
next(new Error('[[error:user-banned-reason, ' + banInfo.reason + ']]'));
} else {
next(new Error('[[error:user-banned]]'));
}
});
10 years ago
}
10 years ago
Password.compare(password, userData.password, next);
},
9 years ago
function (passwordMatch, next) {
10 years ago
if (!passwordMatch) {
return next(new Error('[[error:invalid-password]]'));
}
user.auth.clearLoginAttempts(uid);
next(null, userData, '[[success:authentication-successful]]');
}
], next);
};
authenticationController.logout = function (req, res, next) {
10 years ago
if (req.user && parseInt(req.user.uid, 10) > 0 && req.sessionID) {
10 years ago
var uid = parseInt(req.user.uid, 10);
var sessionID = req.sessionID;
user.auth.revokeSession(sessionID, uid, function (err) {
10 years ago
if (err) {
return next(err);
}
req.logout();
req.session.destroy();
9 years ago
user.setUserField(uid, 'lastonline', Date.now() - 300000);
plugins.fireHook('static:user.loggedOut', {req: req, res: res, uid: uid}, function () {
res.status(200).send('');
// Force session check for all connected socket.io clients with the same session id
sockets.in('sess_' + sessionID).emit('checkSession', 0);
});
10 years ago
});
} else {
res.status(200).send('');
}
};
module.exports = authenticationController;