2014-02-12 05:23:40 -05:00
|
|
|
AuthenticationManager = require ("./AuthenticationManager")
|
|
|
|
LoginRateLimiter = require("../Security/LoginRateLimiter")
|
|
|
|
UserGetter = require "../User/UserGetter"
|
|
|
|
UserUpdater = require "../User/UserUpdater"
|
|
|
|
Metrics = require('../../infrastructure/Metrics')
|
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
querystring = require('querystring')
|
|
|
|
Url = require("url")
|
2015-04-15 06:14:38 -04:00
|
|
|
Settings = require "settings-sharelatex"
|
2015-06-30 07:04:41 -04:00
|
|
|
basicAuth = require('basic-auth-connect')
|
2016-02-17 11:24:09 -05:00
|
|
|
UserHandler = require("../User/UserHandler")
|
2016-06-29 06:35:25 -04:00
|
|
|
UserSessionsManager = require("../User/UserSessionsManager")
|
2016-08-11 09:09:45 -04:00
|
|
|
Analytics = require "../Analytics/AnalyticsManager"
|
2016-09-15 09:36:11 -04:00
|
|
|
passport = require 'passport'
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = AuthenticationController =
|
2016-09-05 05:28:47 -04:00
|
|
|
|
2016-09-02 11:17:37 -04:00
|
|
|
serializeUser: (user, callback) ->
|
|
|
|
lightUser =
|
|
|
|
_id: user._id
|
|
|
|
first_name: user.first_name
|
|
|
|
last_name: user.last_name
|
|
|
|
isAdmin: user.isAdmin
|
|
|
|
email: user.email
|
|
|
|
referal_id: user.referal_id
|
|
|
|
session_created: (new Date()).toISOString()
|
|
|
|
ip_address: user._login_req_ip
|
|
|
|
callback(null, lightUser)
|
|
|
|
|
|
|
|
deserializeUser: (user, cb) ->
|
|
|
|
cb(null, user)
|
|
|
|
|
2016-09-15 09:36:11 -04:00
|
|
|
passportLogin: (req, res, next) ->
|
|
|
|
# This function is middleware which wraps the passport.authenticate middleware,
|
|
|
|
# so we can send back our custom `{message: {text: "", type: ""}}` responses on failure,
|
|
|
|
# and send a `{redir: ""}` response on success
|
|
|
|
passport.authenticate('local', (err, user, info) ->
|
|
|
|
if err?
|
|
|
|
return next(err)
|
2016-09-20 09:50:26 -04:00
|
|
|
if user # `user` is either a user object or false
|
2016-09-15 09:36:11 -04:00
|
|
|
req.login user, (err) ->
|
2016-09-21 08:03:37 -04:00
|
|
|
# Regenerate the session to get a new sessionID (cookie value) to
|
|
|
|
# protect against session fixation attacks
|
|
|
|
oldSession = req.session
|
|
|
|
req.session.destroy()
|
|
|
|
req.sessionStore.generate(req)
|
|
|
|
for key, value of oldSession
|
|
|
|
req.session[key] = value
|
|
|
|
req.session.save (err) ->
|
|
|
|
if err?
|
|
|
|
logger.err {user_id: user._id}, "error saving regenerated session after login"
|
|
|
|
return next(err)
|
|
|
|
UserSessionsManager.trackSession(user, req.sessionID, () ->)
|
|
|
|
res.json {redir: req._redir}
|
2016-09-15 09:36:11 -04:00
|
|
|
else
|
|
|
|
res.json message: info
|
|
|
|
)(req, res, next)
|
|
|
|
|
2016-09-02 11:17:37 -04:00
|
|
|
doPassportLogin: (req, username, password, done) ->
|
|
|
|
email = username.toLowerCase()
|
|
|
|
redir = Url.parse(req?.body?.redir or "/project").path
|
|
|
|
LoginRateLimiter.processLoginRequest email, (err, isAllowed)->
|
|
|
|
return done(err) if err?
|
|
|
|
if !isAllowed
|
|
|
|
logger.log email:email, "too many login requests"
|
2016-09-15 09:36:11 -04:00
|
|
|
return done(null, null, {text: req.i18n.translate("to_many_login_requests_2_mins"), type: 'error'})
|
2016-09-02 11:17:37 -04:00
|
|
|
AuthenticationManager.authenticate email: email, password, (error, user) ->
|
|
|
|
return done(error) if error?
|
|
|
|
if user?
|
|
|
|
# async actions
|
|
|
|
UserHandler.setupLoginData(user, ()->)
|
|
|
|
LoginRateLimiter.recordSuccessfulLogin(email)
|
|
|
|
AuthenticationController._recordSuccessfulLogin(user._id)
|
|
|
|
Analytics.recordEvent(user._id, "user-logged-in")
|
|
|
|
logger.log email: email, user_id: user._id.toString(), "successful log in"
|
2016-09-21 08:03:37 -04:00
|
|
|
req.session.justLoggedIn = true
|
2016-09-02 11:17:37 -04:00
|
|
|
# capture the request ip for use when creating the session
|
|
|
|
user._login_req_ip = req.ip
|
|
|
|
req._redir = redir
|
|
|
|
return done(null, user)
|
|
|
|
else
|
|
|
|
AuthenticationController._recordFailedLogin()
|
|
|
|
logger.log email: email, "failed log in"
|
2016-09-15 09:36:11 -04:00
|
|
|
return done(null, false, {text: req.i18n.translate("email_or_password_wrong_try_again"), type: 'error'})
|
2016-09-02 11:17:37 -04:00
|
|
|
|
2016-09-05 10:58:31 -04:00
|
|
|
isUserLoggedIn: (req) ->
|
|
|
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
2016-09-07 09:05:51 -04:00
|
|
|
return user_id != null
|
2016-09-02 11:17:37 -04:00
|
|
|
|
2016-09-05 10:58:31 -04:00
|
|
|
# TODO: perhaps should produce an error if the current user is not present
|
|
|
|
getLoggedInUserId: (req) ->
|
2016-09-06 10:22:13 -04:00
|
|
|
user = AuthenticationController.getSessionUser(req)
|
2016-09-07 09:05:51 -04:00
|
|
|
if user
|
2016-09-06 10:22:13 -04:00
|
|
|
return user._id
|
|
|
|
else
|
|
|
|
return null
|
|
|
|
|
|
|
|
getSessionUser: (req) ->
|
2016-09-07 09:05:51 -04:00
|
|
|
if req?.session?.user?
|
2016-09-06 10:22:13 -04:00
|
|
|
return req.session.user
|
2016-09-07 09:05:51 -04:00
|
|
|
else if req?.session?.passport?.user
|
2016-09-06 10:22:13 -04:00
|
|
|
return req.session.passport.user
|
2014-04-02 10:56:54 -04:00
|
|
|
else
|
2016-09-05 10:58:31 -04:00
|
|
|
return null
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-03-10 12:15:14 -05:00
|
|
|
requireLogin: () ->
|
2014-02-12 05:23:40 -05:00
|
|
|
doRequest = (req, res, next = (error) ->) ->
|
2016-09-06 08:21:22 -04:00
|
|
|
if !AuthenticationController.isUserLoggedIn(req)
|
2016-07-01 10:33:59 -04:00
|
|
|
AuthenticationController._redirectToLoginOrRegisterPage(req, res)
|
2014-02-12 05:23:40 -05:00
|
|
|
else
|
2016-09-06 08:21:22 -04:00
|
|
|
next()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
return doRequest
|
|
|
|
|
2015-04-15 06:14:38 -04:00
|
|
|
_globalLoginWhitelist: []
|
|
|
|
addEndpointToLoginWhitelist: (endpoint) ->
|
|
|
|
AuthenticationController._globalLoginWhitelist.push endpoint
|
|
|
|
|
|
|
|
requireGlobalLogin: (req, res, next) ->
|
2015-04-30 06:57:40 -04:00
|
|
|
if req._parsedUrl.pathname in AuthenticationController._globalLoginWhitelist
|
2015-04-15 06:14:38 -04:00
|
|
|
return next()
|
|
|
|
|
|
|
|
if req.headers['authorization']?
|
|
|
|
return AuthenticationController.httpAuth(req, res, next)
|
2016-09-07 09:05:51 -04:00
|
|
|
else if AuthenticationController.isUserLoggedIn(req)
|
2015-04-15 06:14:38 -04:00
|
|
|
return next()
|
|
|
|
else
|
2015-04-30 06:57:40 -04:00
|
|
|
logger.log url:req.url, "user trying to access endpoint not in global whitelist"
|
2015-04-15 06:14:38 -04:00
|
|
|
return res.redirect "/login"
|
|
|
|
|
2015-06-30 07:04:41 -04:00
|
|
|
httpAuth: basicAuth (user, pass)->
|
2015-04-15 06:14:38 -04:00
|
|
|
isValid = Settings.httpAuthUsers[user] == pass
|
|
|
|
if !isValid
|
|
|
|
logger.err user:user, pass:pass, "invalid login details"
|
|
|
|
return isValid
|
2014-11-13 12:12:39 -05:00
|
|
|
|
|
|
|
_redirectToLoginOrRegisterPage: (req, res)->
|
|
|
|
if req.query.zipUrl? or req.query.project_name?
|
2016-07-01 10:33:59 -04:00
|
|
|
return AuthenticationController._redirectToRegisterPage(req, res)
|
2014-11-13 12:12:39 -05:00
|
|
|
else
|
2016-07-01 10:33:59 -04:00
|
|
|
AuthenticationController._redirectToLoginPage(req, res)
|
2014-11-13 12:12:39 -05:00
|
|
|
|
|
|
|
_redirectToLoginPage: (req, res) ->
|
|
|
|
logger.log url: req.url, "user not logged in so redirecting to login page"
|
|
|
|
req.query.redir = req.path
|
|
|
|
url = "/login?#{querystring.stringify(req.query)}"
|
|
|
|
res.redirect url
|
|
|
|
Metrics.inc "security.login-redirect"
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
_redirectToRegisterPage: (req, res) ->
|
|
|
|
logger.log url: req.url, "user not logged in so redirecting to register page"
|
|
|
|
req.query.redir = req.path
|
|
|
|
url = "/register?#{querystring.stringify(req.query)}"
|
|
|
|
res.redirect url
|
|
|
|
Metrics.inc "security.login-redirect"
|
|
|
|
|
|
|
|
_recordSuccessfulLogin: (user_id, callback = (error) ->) ->
|
|
|
|
UserUpdater.updateUser user_id.toString(), {
|
|
|
|
$set: { "lastLoggedIn": new Date() },
|
|
|
|
$inc: { "loginCount": 1 }
|
|
|
|
}, (error) ->
|
|
|
|
callback(error) if error?
|
|
|
|
Metrics.inc "user.login.success"
|
|
|
|
callback()
|
|
|
|
|
|
|
|
_recordFailedLogin: (callback = (error) ->) ->
|
|
|
|
Metrics.inc "user.login.failed"
|
|
|
|
callback()
|