2019-05-29 05:21:06 -04:00
|
|
|
const AuthenticationManager = require('./AuthenticationManager')
|
|
|
|
const LoginRateLimiter = require('../Security/LoginRateLimiter')
|
|
|
|
const UserUpdater = require('../User/UserUpdater')
|
|
|
|
const Metrics = require('metrics-sharelatex')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const querystring = require('querystring')
|
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const basicAuth = require('basic-auth-connect')
|
|
|
|
const UserHandler = require('../User/UserHandler')
|
|
|
|
const UserSessionsManager = require('../User/UserSessionsManager')
|
|
|
|
const Analytics = require('../Analytics/AnalyticsManager')
|
|
|
|
const passport = require('passport')
|
|
|
|
const NotificationsBuilder = require('../Notifications/NotificationsBuilder')
|
2019-09-25 10:29:10 -04:00
|
|
|
const UrlHelper = require('../Helpers/UrlHelper')
|
2019-05-29 05:21:06 -04:00
|
|
|
const SudoModeHandler = require('../SudoMode/SudoModeHandler')
|
2019-08-07 10:04:18 -04:00
|
|
|
const _ = require('lodash')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-06-06 08:43:17 -04:00
|
|
|
const AuthenticationController = (module.exports = {
|
2019-05-29 05:21:06 -04:00
|
|
|
serializeUser(user, callback) {
|
2019-08-07 10:04:18 -04:00
|
|
|
if (!user._id || !user.email) {
|
|
|
|
const err = new Error('serializeUser called with non-user object')
|
|
|
|
logger.warn({ user }, err.message)
|
|
|
|
return callback(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
const lightUser = {
|
|
|
|
_id: user._id,
|
|
|
|
first_name: user.first_name,
|
|
|
|
last_name: user.last_name,
|
|
|
|
isAdmin: user.isAdmin,
|
|
|
|
staffAccess: user.staffAccess,
|
|
|
|
email: user.email,
|
|
|
|
referal_id: user.referal_id,
|
|
|
|
session_created: new Date().toISOString(),
|
|
|
|
ip_address: user._login_req_ip,
|
|
|
|
must_reconfirm: user.must_reconfirm,
|
|
|
|
v1_id: user.overleaf != null ? user.overleaf.id : undefined
|
|
|
|
}
|
2019-08-07 10:04:18 -04:00
|
|
|
callback(null, lightUser)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
deserializeUser(user, cb) {
|
2019-08-07 10:04:18 -04:00
|
|
|
cb(null, user)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
afterLoginSessionSetup(req, user, callback) {
|
|
|
|
if (callback == null) {
|
2019-06-06 08:43:17 -04:00
|
|
|
callback = function() {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-07 10:04:18 -04:00
|
|
|
req.login(user, function(err) {
|
|
|
|
if (err) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn({ user_id: user._id, err }, 'error from req.login')
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
// Regenerate the session to get a new sessionID (cookie value) to
|
|
|
|
// protect against session fixation attacks
|
|
|
|
const oldSession = req.session
|
2019-08-07 10:04:18 -04:00
|
|
|
req.session.destroy(function(err) {
|
|
|
|
if (err) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ user_id: user._id, err },
|
|
|
|
'error when trying to destroy old session'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
req.sessionStore.generate(req)
|
|
|
|
for (let key in oldSession) {
|
|
|
|
const value = oldSession[key]
|
|
|
|
if (key !== '__tmp') {
|
|
|
|
req.session[key] = value
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 10:04:18 -04:00
|
|
|
req.session.save(function(err) {
|
|
|
|
if (err) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ user_id: user._id },
|
|
|
|
'error saving regenerated session after login'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
UserSessionsManager.trackSession(user, req.sessionID, function() {})
|
2019-08-07 10:04:18 -04:00
|
|
|
callback(null)
|
2019-05-29 05:21:06 -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
|
2019-08-07 10:04:18 -04:00
|
|
|
passport.authenticate('local', function(err, user, info) {
|
|
|
|
if (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
if (user) {
|
|
|
|
// `user` is either a user object or false
|
|
|
|
return AuthenticationController.finishLogin(user, req, res, next)
|
|
|
|
} else {
|
|
|
|
if (info.redir != null) {
|
|
|
|
return res.json({ redir: info.redir })
|
|
|
|
} else {
|
|
|
|
return res.json({ message: info })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})(req, res, next)
|
|
|
|
},
|
|
|
|
|
|
|
|
finishLogin(user, req, res, next) {
|
|
|
|
if (user === false) {
|
|
|
|
return res.redirect('/login')
|
|
|
|
} // OAuth2 'state' mismatch
|
|
|
|
if (user.must_reconfirm) {
|
|
|
|
return AuthenticationController._redirectToReconfirmPage(req, res, user)
|
|
|
|
}
|
2019-08-07 10:04:18 -04:00
|
|
|
const redir =
|
|
|
|
AuthenticationController._getRedirectFromSession(req) || '/project'
|
|
|
|
AuthenticationController._loginAsyncHandlers(req, user)
|
|
|
|
AuthenticationController.afterLoginSessionSetup(req, user, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
SudoModeHandler.activateSudoMode(user._id, function(err) {
|
|
|
|
if (err) {
|
|
|
|
logger.err(
|
|
|
|
{ err, user_id: user._id },
|
|
|
|
'Error activating Sudo Mode on login, continuing'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
AuthenticationController._clearRedirectFromSession(req)
|
|
|
|
if (
|
|
|
|
_.get(req, ['headers', 'accept'], '').match(/^application\/json.*$/)
|
|
|
|
) {
|
|
|
|
res.json({ redir })
|
|
|
|
} else {
|
|
|
|
res.redirect(redir)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
doPassportLogin(req, username, password, done) {
|
|
|
|
const email = username.toLowerCase()
|
|
|
|
const Modules = require('../../infrastructure/Modules')
|
2019-08-07 10:04:18 -04:00
|
|
|
Modules.hooks.fire('preDoPassportLogin', req, email, function(
|
2019-05-29 05:21:06 -04:00
|
|
|
err,
|
|
|
|
infoList
|
|
|
|
) {
|
2019-08-07 10:04:18 -04:00
|
|
|
if (err) {
|
2019-06-06 08:43:17 -04:00
|
|
|
return done(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const info = infoList.find(i => i != null)
|
|
|
|
if (info != null) {
|
|
|
|
return done(null, false, info)
|
|
|
|
}
|
2019-08-07 10:04:18 -04:00
|
|
|
LoginRateLimiter.processLoginRequest(email, function(err, isAllowed) {
|
|
|
|
if (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
if (!isAllowed) {
|
|
|
|
logger.log({ email }, 'too many login requests')
|
|
|
|
return done(null, null, {
|
|
|
|
text: req.i18n.translate('to_many_login_requests_2_mins'),
|
|
|
|
type: 'error'
|
|
|
|
})
|
|
|
|
}
|
2019-08-07 10:04:18 -04:00
|
|
|
AuthenticationManager.authenticate({ email }, password, function(
|
2019-05-29 05:21:06 -04:00
|
|
|
error,
|
|
|
|
user
|
|
|
|
) {
|
|
|
|
if (error != null) {
|
|
|
|
return done(error)
|
|
|
|
}
|
|
|
|
if (user != null) {
|
|
|
|
// async actions
|
2019-08-07 10:04:18 -04:00
|
|
|
done(null, user)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
|
|
|
AuthenticationController._recordFailedLogin()
|
|
|
|
logger.log({ email }, 'failed log in')
|
2019-08-07 10:04:18 -04:00
|
|
|
done(null, false, {
|
2019-05-29 05:21:06 -04:00
|
|
|
text: req.i18n.translate('email_or_password_wrong_try_again'),
|
|
|
|
type: 'error'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_loginAsyncHandlers(req, user) {
|
|
|
|
UserHandler.setupLoginData(user, function() {})
|
|
|
|
LoginRateLimiter.recordSuccessfulLogin(user.email)
|
|
|
|
AuthenticationController._recordSuccessfulLogin(user._id)
|
|
|
|
AuthenticationController.ipMatchCheck(req, user)
|
|
|
|
Analytics.recordEvent(user._id, 'user-logged-in', { ip: req.ip })
|
|
|
|
Analytics.identifyUser(user._id, req.sessionID)
|
|
|
|
logger.log(
|
|
|
|
{ email: user.email, user_id: user._id.toString() },
|
|
|
|
'successful log in'
|
|
|
|
)
|
|
|
|
req.session.justLoggedIn = true
|
|
|
|
// capture the request ip for use when creating the session
|
|
|
|
return (user._login_req_ip = req.ip)
|
|
|
|
},
|
|
|
|
|
|
|
|
ipMatchCheck(req, user) {
|
|
|
|
if (req.ip !== user.lastLoginIp) {
|
|
|
|
NotificationsBuilder.ipMatcherAffiliation(user._id).create(req.ip)
|
|
|
|
}
|
|
|
|
return UserUpdater.updateUser(user._id.toString(), {
|
|
|
|
$set: { lastLoginIp: req.ip }
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
setInSessionUser(req, props) {
|
2019-08-07 10:04:18 -04:00
|
|
|
const sessionUser = AuthenticationController.getSessionUser(req)
|
|
|
|
if (!sessionUser) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for (let key in props) {
|
|
|
|
const value = props[key]
|
|
|
|
sessionUser[key] = value
|
|
|
|
}
|
|
|
|
return null
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
isUserLoggedIn(req) {
|
2019-06-06 08:43:17 -04:00
|
|
|
const userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
return ![null, undefined, false].includes(userId)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// TODO: perhaps should produce an error if the current user is not present
|
|
|
|
getLoggedInUserId(req) {
|
|
|
|
const user = AuthenticationController.getSessionUser(req)
|
|
|
|
if (user) {
|
|
|
|
return user._id
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getLoggedInUserV1Id(req) {
|
|
|
|
const user = AuthenticationController.getSessionUser(req)
|
|
|
|
if ((user != null ? user.v1_id : undefined) != null) {
|
|
|
|
return user.v1_id
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getSessionUser(req) {
|
2019-08-07 10:04:18 -04:00
|
|
|
const sessionUser = _.get(req, ['session', 'user'])
|
|
|
|
const sessionPassportUser = _.get(req, ['session', 'passport', 'user'])
|
|
|
|
return sessionUser || sessionPassportUser || null
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
requireLogin() {
|
|
|
|
const doRequest = function(req, res, next) {
|
|
|
|
if (next == null) {
|
2019-06-06 08:43:17 -04:00
|
|
|
next = function() {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
if (!AuthenticationController.isUserLoggedIn(req)) {
|
|
|
|
return AuthenticationController._redirectToLoginOrRegisterPage(req, res)
|
|
|
|
} else {
|
|
|
|
req.user = AuthenticationController.getSessionUser(req)
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return doRequest
|
|
|
|
},
|
|
|
|
|
2019-06-06 17:14:16 -04:00
|
|
|
requireOauth() {
|
2019-05-29 05:21:06 -04:00
|
|
|
// require this here because module may not be included in some versions
|
|
|
|
const Oauth2Server = require('../../../../modules/oauth2-server/app/src/Oauth2Server')
|
|
|
|
return function(req, res, next) {
|
|
|
|
if (next == null) {
|
2019-06-06 08:43:17 -04:00
|
|
|
next = function() {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const request = new Oauth2Server.Request(req)
|
|
|
|
const response = new Oauth2Server.Response(res)
|
|
|
|
return Oauth2Server.server.authenticate(request, response, {}, function(
|
|
|
|
err,
|
|
|
|
token
|
|
|
|
) {
|
2019-08-07 10:04:18 -04:00
|
|
|
if (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
// use a 401 status code for malformed header for git-bridge
|
|
|
|
if (
|
|
|
|
err.code === 400 &&
|
|
|
|
err.message === 'Invalid request: malformed authorization header'
|
|
|
|
) {
|
|
|
|
err.code = 401
|
|
|
|
}
|
|
|
|
// send all other errors
|
|
|
|
return res
|
|
|
|
.status(err.code)
|
|
|
|
.json({ error: err.name, error_description: err.message })
|
|
|
|
}
|
|
|
|
req.oauth = { access_token: token.accessToken }
|
|
|
|
req.oauth_token = token
|
|
|
|
req.oauth_user = token.user
|
|
|
|
return next()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_globalLoginWhitelist: [],
|
|
|
|
addEndpointToLoginWhitelist(endpoint) {
|
|
|
|
return AuthenticationController._globalLoginWhitelist.push(endpoint)
|
|
|
|
},
|
|
|
|
|
|
|
|
requireGlobalLogin(req, res, next) {
|
|
|
|
if (
|
2019-08-07 10:04:18 -04:00
|
|
|
AuthenticationController._globalLoginWhitelist.includes(
|
2019-05-29 05:21:06 -04:00
|
|
|
req._parsedUrl.pathname
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.headers['authorization'] != null) {
|
2019-08-07 10:04:18 -04:00
|
|
|
AuthenticationController.httpAuth(req, res, next)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else if (AuthenticationController.isUserLoggedIn(req)) {
|
2019-08-07 10:04:18 -04:00
|
|
|
next()
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
|
|
|
logger.log(
|
|
|
|
{ url: req.url },
|
|
|
|
'user trying to access endpoint not in global whitelist'
|
|
|
|
)
|
|
|
|
AuthenticationController.setRedirectInSession(req)
|
2019-08-07 10:04:18 -04:00
|
|
|
res.redirect('/login')
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
httpAuth: basicAuth(function(user, pass) {
|
|
|
|
const isValid = Settings.httpAuthUsers[user] === pass
|
|
|
|
if (!isValid) {
|
|
|
|
logger.err({ user, pass }, 'invalid login details')
|
|
|
|
}
|
|
|
|
return isValid
|
|
|
|
}),
|
|
|
|
|
|
|
|
setRedirectInSession(req, value) {
|
|
|
|
if (value == null) {
|
|
|
|
value =
|
|
|
|
Object.keys(req.query).length > 0
|
|
|
|
? `${req.path}?${querystring.stringify(req.query)}`
|
|
|
|
: `${req.path}`
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
req.session != null &&
|
|
|
|
!/^\/(socket.io|js|stylesheets|img)\/.*$/.test(value) &&
|
|
|
|
!/^.*\.(png|jpeg|svg)$/.test(value)
|
|
|
|
) {
|
2019-09-25 10:29:10 -04:00
|
|
|
const safePath = UrlHelper.getSafeRedirectPath(value)
|
2019-05-29 05:21:06 -04:00
|
|
|
return (req.session.postLoginRedirect = safePath)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_redirectToLoginOrRegisterPage(req, res) {
|
|
|
|
if (
|
|
|
|
req.query.zipUrl != null ||
|
|
|
|
req.query.project_name != null ||
|
|
|
|
req.path === '/user/subscription/new'
|
|
|
|
) {
|
2019-08-07 10:04:18 -04:00
|
|
|
AuthenticationController._redirectToRegisterPage(req, res)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2019-08-07 10:04:18 -04:00
|
|
|
AuthenticationController._redirectToLoginPage(req, res)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_redirectToLoginPage(req, res) {
|
|
|
|
logger.log(
|
|
|
|
{ url: req.url },
|
|
|
|
'user not logged in so redirecting to login page'
|
|
|
|
)
|
|
|
|
AuthenticationController.setRedirectInSession(req)
|
|
|
|
const url = `/login?${querystring.stringify(req.query)}`
|
|
|
|
res.redirect(url)
|
2019-08-07 10:04:18 -04:00
|
|
|
Metrics.inc('security.login-redirect')
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_redirectToReconfirmPage(req, res, user) {
|
|
|
|
logger.log(
|
|
|
|
{ url: req.url },
|
|
|
|
'user needs to reconfirm so redirecting to reconfirm page'
|
|
|
|
)
|
|
|
|
req.session.reconfirm_email = user != null ? user.email : undefined
|
|
|
|
const redir = '/user/reconfirm'
|
2019-08-07 10:04:18 -04:00
|
|
|
if (_.get(req, ['headers', 'accept'], '').match(/^application\/json.*$/)) {
|
|
|
|
res.json({ redir })
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2019-08-07 10:04:18 -04:00
|
|
|
res.redirect(redir)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_redirectToRegisterPage(req, res) {
|
|
|
|
logger.log(
|
|
|
|
{ url: req.url },
|
|
|
|
'user not logged in so redirecting to register page'
|
|
|
|
)
|
|
|
|
AuthenticationController.setRedirectInSession(req)
|
|
|
|
const url = `/register?${querystring.stringify(req.query)}`
|
|
|
|
res.redirect(url)
|
2019-08-07 10:04:18 -04:00
|
|
|
Metrics.inc('security.login-redirect')
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2019-06-06 08:43:17 -04:00
|
|
|
_recordSuccessfulLogin(userId, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (callback == null) {
|
2019-06-06 08:43:17 -04:00
|
|
|
callback = function() {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-07 10:04:18 -04:00
|
|
|
UserUpdater.updateUser(
|
2019-06-06 08:43:17 -04:00
|
|
|
userId.toString(),
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
$set: { lastLoggedIn: new Date() },
|
|
|
|
$inc: { loginCount: 1 }
|
|
|
|
},
|
|
|
|
function(error) {
|
|
|
|
if (error != null) {
|
|
|
|
callback(error)
|
|
|
|
}
|
|
|
|
Metrics.inc('user.login.success')
|
2019-08-07 10:04:18 -04:00
|
|
|
callback()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
_recordFailedLogin(callback) {
|
|
|
|
Metrics.inc('user.login.failed')
|
2019-06-06 08:43:17 -04:00
|
|
|
if (callback) callback()
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_getRedirectFromSession(req) {
|
|
|
|
let safePath
|
2019-08-07 10:04:18 -04:00
|
|
|
const value = _.get(req, ['session', 'postLoginRedirect'])
|
2019-05-29 05:21:06 -04:00
|
|
|
if (value) {
|
2019-09-25 10:29:10 -04:00
|
|
|
safePath = UrlHelper.getSafeRedirectPath(value)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return safePath || null
|
|
|
|
},
|
|
|
|
|
|
|
|
_clearRedirectFromSession(req) {
|
|
|
|
if (req.session != null) {
|
2019-08-07 10:04:18 -04:00
|
|
|
delete req.session.postLoginRedirect
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 08:43:17 -04:00
|
|
|
})
|