| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-08 12:17:12 +02:00
										 |  |  | var async = require('async'); | 
					
						
							|  |  |  | var winston = require('winston'); | 
					
						
							|  |  |  | var passport = require('passport'); | 
					
						
							|  |  |  | var nconf = require('nconf'); | 
					
						
							|  |  |  | var validator = require('validator'); | 
					
						
							|  |  |  | var _ = require('underscore'); | 
					
						
							| 
									
										
										
										
											2016-08-22 16:24:28 -04:00
										 |  |  | var url = require('url'); | 
					
						
							| 
									
										
										
										
											2016-03-08 12:17:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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 authenticationController = {}; | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | authenticationController.register = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 	var registrationType = meta.config.registrationType || 'normal'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (registrationType === 'disabled') { | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 		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) { | 
					
						
							| 
									
										
										
										
											2016-04-19 21:03:00 -04:00
										 |  |  | 			if (registrationType === 'invite-only' || registrationType === 'admin-invite-only') { | 
					
						
							| 
									
										
										
										
											2015-06-28 21:54:21 -04:00
										 |  |  | 				user.verifyInvitation(userData, next); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				next(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function(next) { | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 			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')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-14 15:50:02 -04:00
										 |  |  | 			user.isPasswordValid(userData.password, next); | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function(next) { | 
					
						
							| 
									
										
										
										
											2016-02-10 15:26:14 -05:00
										 |  |  | 			res.locals.processLogin = true;	// set it to false in plugin if you wish to just register only
 | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 			plugins.fireHook('filter:register.check', {req: req, res: res, userData: userData}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function(data, next) { | 
					
						
							| 
									
										
										
										
											2016-04-19 21:03:00 -04:00
										 |  |  | 			if (registrationType === 'normal' || registrationType === 'invite-only' || registrationType === 'admin-invite-only') { | 
					
						
							| 
									
										
										
										
											2016-02-10 15:26:14 -05:00
										 |  |  | 				registerAndLoginUser(req, res, userData, next); | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 			} else if (registrationType === 'admin-approval') { | 
					
						
							| 
									
										
										
										
											2016-02-01 21:12:23 +02:00
										 |  |  | 				addToApprovalQueue(req, userData, next); | 
					
						
							| 
									
										
										
										
											2016-07-25 09:56:29 -05:00
										 |  |  | 			} else if (registrationType === 'admin-approval-ip') { | 
					
						
							|  |  |  | 				db.sortedSetCard('ip:' + req.ip + ':uid', function(err, count) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						next(err); | 
					
						
							|  |  |  | 					} else if (count) { | 
					
						
							|  |  |  | 						addToApprovalQueue(req, userData, next); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						registerAndLoginUser(req, res, userData, next); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	], function(err, data) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return res.status(400).send(err.message); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 06:13:25 -04:00
										 |  |  | 		if (req.body.userLang) { | 
					
						
							|  |  |  | 			user.setSetting(data.uid, 'userLang', req.body.userLang); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 		res.json(data); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-10 15:26:14 -05:00
										 |  |  | function registerAndLoginUser(req, res, userData, callback) { | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 	var uid; | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-06-08 16:05:40 -04:00
										 |  |  | 		function(next) { | 
					
						
							|  |  |  | 			plugins.fireHook('filter:register.interstitial', { | 
					
						
							|  |  |  | 				userData: userData, | 
					
						
							|  |  |  | 				interstitials: [] | 
					
						
							|  |  |  | 			}, function(err, data) { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return next(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:05:40 -04:00
										 |  |  | 				// If interstitials are found, save registration attempt into session and abort
 | 
					
						
							|  |  |  | 				var deferRegistration = data.interstitials.length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (!deferRegistration) { | 
					
						
							|  |  |  | 					return next(); | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2016-06-22 16:47:24 -04:00
										 |  |  | 					userData.register = true; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:05:40 -04:00
										 |  |  | 					req.session.registration = userData; | 
					
						
							| 
									
										
										
										
											2016-07-25 11:19:58 -04:00
										 |  |  | 					return res.json({ referrer: nconf.get('relative_path') + '/register/complete' }); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:05:40 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 		function(next) { | 
					
						
							|  |  |  | 			user.create(userData, next); | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function(_uid, next) { | 
					
						
							|  |  |  | 			uid = _uid; | 
					
						
							| 
									
										
										
										
											2016-03-08 12:17:12 +02:00
										 |  |  | 			if (res.locals.processLogin) { | 
					
						
							|  |  |  | 				authenticationController.doLogin(req, uid, next); | 
					
						
							| 
									
										
										
										
											2016-02-10 15:26:14 -05:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				next(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function(next) { | 
					
						
							| 
									
										
										
										
											2016-03-06 11:59:31 -06:00
										 |  |  | 			user.deleteInvitationKey(userData.email); | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 			plugins.fireHook('filter:register.complete', {uid: uid, referrer: req.body.referrer || nconf.get('relative_path') + '/'}, next); | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 21:12:23 +02:00
										 |  |  | function addToApprovalQueue(req, userData, callback) { | 
					
						
							| 
									
										
										
										
											2015-06-27 21:26:19 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function(next) { | 
					
						
							|  |  |  | 			userData.ip = req.ip; | 
					
						
							|  |  |  | 			user.addToApprovalQueue(userData, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function(next) { | 
					
						
							|  |  |  | 			next(null, {message: '[[register:registration-added-to-queue]]'}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-22 12:42:37 -04:00
										 |  |  | authenticationController.registerComplete = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2016-06-22 14:40:34 -04:00
										 |  |  | 	// 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) { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return next(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-22 14:40:34 -04:00
										 |  |  | 		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; | 
					
						
							|  |  |  | 		}, []); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-22 16:47:24 -04:00
										 |  |  | 		var done = function() { | 
					
						
							|  |  |  | 			delete req.session.registration; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (req.session.returnTo) { | 
					
						
							|  |  |  | 				res.redirect(req.session.returnTo); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				res.redirect(nconf.get('relative_path') + '/'); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-08-22 16:24:28 -04:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2016-06-22 16:47:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-22 14:40:34 -04:00
										 |  |  | 		async.parallel(callbacks, function(err) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				req.flash('error', err.message); | 
					
						
							|  |  |  | 				return res.redirect(nconf.get('relative_path') + '/register/complete'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-22 16:47:24 -04:00
										 |  |  | 			if (req.session.registration.register === true) { | 
					
						
							|  |  |  | 				res.locals.processLogin = true; | 
					
						
							|  |  |  | 				registerAndLoginUser(req, res, req.session.registration, done); | 
					
						
							| 
									
										
										
										
											2016-06-22 14:40:34 -04:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2016-06-22 16:47:24 -04:00
										 |  |  | 				// Clear registration data in session
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							| 
									
										
										
										
											2016-06-22 14:40:34 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-06-22 12:42:37 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-22 16:24:28 -04:00
										 |  |  | authenticationController.registerAbort = function(req, res) { | 
					
						
							| 
									
										
										
										
											2016-06-22 16:47:24 -04:00
										 |  |  | 	// End the session and redirect to home
 | 
					
						
							|  |  |  | 	req.session.destroy(function() { | 
					
						
							|  |  |  | 		res.redirect(nconf.get('relative_path') + '/'); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | authenticationController.login = function(req, res, next) { | 
					
						
							|  |  |  | 	// Handle returnTo data
 | 
					
						
							|  |  |  | 	if (req.body.hasOwnProperty('returnTo') && !req.session.returnTo) { | 
					
						
							| 
									
										
										
										
											2016-08-22 16:24:28 -04:00
										 |  |  | 		// As req.body is data obtained via userland, it is untrusted, restrict to internal links only
 | 
					
						
							|  |  |  | 		var parsed = url.parse(req.body.returnTo); | 
					
						
							|  |  |  | 		var isInternal = utils.isInternalURI(url.parse(req.body.returnTo), nconf.get('url_parsed'), nconf.get('relative_path')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		req.session.returnTo = isInternal ? req.body.returnTo : nconf.get('url'); | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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) { | 
					
						
							|  |  |  | 			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) { | 
					
						
							|  |  |  | 		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') { | 
					
						
							| 
									
										
										
										
											2015-11-23 11:59:00 -05:00
										 |  |  | 			var duration = 1000 * 60 * 60 * 24 * (parseInt(meta.config.loginDays, 10) || 14); | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 			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) { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return res.status(403).send(err.message); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 				res.status(200).send(nconf.get('relative_path') + '/reset/' + code); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2016-03-08 12:17:12 +02:00
										 |  |  | 			authenticationController.doLogin(req, userData.uid, function(err) { | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return res.status(403).send(err.message); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-12-23 10:48:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 				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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-08 12:17:12 +02:00
										 |  |  | authenticationController.doLogin = function(req, uid, callback) { | 
					
						
							|  |  |  | 	if (!uid) { | 
					
						
							|  |  |  | 		return callback(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-26 16:45:44 +02:00
										 |  |  | 	req.login({uid: uid}, function(err) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-08 12:29:19 +02:00
										 |  |  | 		authenticationController.onSuccessfulLogin(req, uid, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-02-26 16:45:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-08 12:29:19 +02:00
										 |  |  | authenticationController.onSuccessfulLogin = function(req, uid, callback) { | 
					
						
							|  |  |  | 	callback = callback || function() {}; | 
					
						
							|  |  |  | 	var uuid = utils.generateUUID(); | 
					
						
							|  |  |  | 	req.session.meta = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 19:13:10 +03:00
										 |  |  | 	delete req.session.forceLogin; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-08 12:29:19 +02:00
										 |  |  | 	// 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(err) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		plugins.fireHook('action:user.loggedIn', uid); | 
					
						
							|  |  |  | 		callback(); | 
					
						
							| 
									
										
										
										
											2016-02-26 16:45:44 +02:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-03-08 12:17:12 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-02-26 16:45:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | authenticationController.localLogin = function(req, username, password, next) { | 
					
						
							| 
									
										
										
										
											2016-01-10 10:26:47 +02:00
										 |  |  | 	if (!username) { | 
					
						
							|  |  |  | 		return next(new Error('[[error:invalid-username]]')); | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var userslug = utils.slugify(username); | 
					
						
							|  |  |  | 	var uid, userData = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-01-10 10:26:47 +02:00
										 |  |  | 		function (next) { | 
					
						
							|  |  |  | 			user.isPasswordValid(password, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 			user.getUidByUserslug(userslug, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-01-10 10:26:47 +02:00
										 |  |  | 		function (_uid, next) { | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 			if (!_uid) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:no-user]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			uid = _uid; | 
					
						
							|  |  |  | 			user.auth.logAttempt(uid, req.ip, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-01-10 10:26:47 +02:00
										 |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				userData: function(next) { | 
					
						
							| 
									
										
										
										
											2016-06-29 12:07:23 -04:00
										 |  |  | 					db.getObjectFields('user:' + uid, ['password', 'passwordExpiry'], next); | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				isAdmin: function(next) { | 
					
						
							|  |  |  | 					user.isAdministrator(uid, next); | 
					
						
							| 
									
										
										
										
											2016-06-29 12:07:23 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				banned: function(next) { | 
					
						
							|  |  |  | 					user.isBanned(uid, next); | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-01-10 10:26:47 +02:00
										 |  |  | 		function (result, next) { | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 			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]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-06-29 12:07:23 -04:00
										 |  |  | 			if (result.banned) { | 
					
						
							| 
									
										
										
										
											2016-09-01 09:20:39 -04:00
										 |  |  | 				// Retrieve ban reason and show error
 | 
					
						
							|  |  |  | 				return user.getLatestBanInfo(uid, function(err, banInfo) { | 
					
						
							|  |  |  | 					if (banInfo.reason) { | 
					
						
							|  |  |  | 						next(new Error('[[error:user-banned-reason, ' + banInfo.reason + ']]')); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						next(new Error('[[error:user-banned]]')); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-06-29 12:07:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 			Password.compare(password, userData.password, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-01-10 10:26:47 +02:00
										 |  |  | 		function (passwordMatch, next) { | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 			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) { | 
					
						
							|  |  |  | 	if (req.user && parseInt(req.user.uid, 10) > 0 && req.sessionID) { | 
					
						
							| 
									
										
										
										
											2015-07-20 15:53:03 -04:00
										 |  |  | 		var uid = parseInt(req.user.uid, 10); | 
					
						
							| 
									
										
										
										
											2015-12-23 10:48:45 -05:00
										 |  |  | 		user.auth.revokeSession(req.sessionID, uid, function(err) { | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return next(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			req.logout(); | 
					
						
							| 
									
										
										
										
											2016-06-21 21:02:07 -04:00
										 |  |  | 			req.session.destroy(); | 
					
						
							| 
									
										
										
										
											2015-07-18 14:06:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 11:13:08 +03:00
										 |  |  | 			user.setUserField(uid, 'lastonline', Date.now() - 300000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-07 15:41:51 -05:00
										 |  |  | 			plugins.fireHook('static:user.loggedOut', {req: req, res: res, uid: uid}, function() { | 
					
						
							|  |  |  | 				res.status(200).send(''); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-06-17 14:13:29 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		res.status(200).send(''); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = authenticationController; |