2014-05-15 11:50:38 -04:00
|
|
|
PasswordResetHandler = require("./PasswordResetHandler")
|
2014-05-16 05:31:33 -04:00
|
|
|
RateLimiter = require("../../infrastructure/RateLimiter")
|
2015-12-11 06:30:06 -05:00
|
|
|
AuthenticationController = require("../Authentication/AuthenticationController")
|
|
|
|
UserGetter = require("../User/UserGetter")
|
2016-07-05 09:19:59 -04:00
|
|
|
UserSessionsManager = require("../User/UserSessionsManager")
|
2014-08-08 06:41:54 -04:00
|
|
|
logger = require "logger-sharelatex"
|
2014-05-15 11:20:23 -04:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
|
2014-05-15 11:50:38 -04:00
|
|
|
renderRequestResetForm: (req, res)->
|
2015-04-30 06:59:44 -04:00
|
|
|
logger.log "rendering request reset form"
|
2015-08-24 06:53:33 -04:00
|
|
|
res.render "user/passwordReset",
|
2014-08-01 08:47:14 -04:00
|
|
|
title:"reset_password"
|
2014-05-15 11:20:23 -04:00
|
|
|
|
2014-05-15 11:50:38 -04:00
|
|
|
requestReset: (req, res)->
|
2014-06-10 12:54:29 -04:00
|
|
|
email = req.body.email.trim().toLowerCase()
|
2015-08-24 06:53:33 -04:00
|
|
|
opts =
|
2014-06-25 05:46:58 -04:00
|
|
|
endpointName: "password_reset_rate_limit"
|
|
|
|
timeInterval: 60
|
|
|
|
subjectName: req.ip
|
|
|
|
throttle: 6
|
2014-10-30 04:33:18 -04:00
|
|
|
RateLimiter.addCount opts, (err, canContinue)->
|
|
|
|
if !canContinue
|
2014-08-01 09:03:38 -04:00
|
|
|
return res.send 500, { message: req.i18n.translate("rate_limit_hit_wait")}
|
2014-08-08 06:41:54 -04:00
|
|
|
PasswordResetHandler.generateAndEmailResetToken email, (err, exists)->
|
2014-05-16 05:31:33 -04:00
|
|
|
if err?
|
2014-05-16 06:04:48 -04:00
|
|
|
res.send 500, {message:err?.message}
|
2014-08-08 06:41:54 -04:00
|
|
|
else if exists
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 200
|
2014-08-08 06:41:54 -04:00
|
|
|
else
|
|
|
|
res.send 404, {message: req.i18n.translate("cant_find_email")}
|
2014-05-15 11:20:23 -04:00
|
|
|
|
2014-05-15 11:50:38 -04:00
|
|
|
renderSetPasswordForm: (req, res)->
|
2015-08-24 06:53:33 -04:00
|
|
|
if req.query.passwordResetToken?
|
|
|
|
req.session.resetToken = req.query.passwordResetToken
|
|
|
|
return res.redirect('/user/password/set')
|
|
|
|
if !req.session.resetToken?
|
|
|
|
return res.redirect('/user/password/reset')
|
|
|
|
res.render "user/setPassword",
|
2014-08-01 08:47:14 -04:00
|
|
|
title:"set_password"
|
2015-08-24 06:53:33 -04:00
|
|
|
passwordResetToken: req.session.resetToken
|
2014-05-15 11:20:23 -04:00
|
|
|
|
2015-12-11 06:30:06 -05:00
|
|
|
setNewUserPassword: (req, res, next)->
|
2014-05-15 12:16:20 -04:00
|
|
|
{passwordResetToken, password} = req.body
|
2014-05-16 06:04:48 -04:00
|
|
|
if !password? or password.length == 0 or !passwordResetToken? or passwordResetToken.length == 0
|
2015-07-08 11:56:38 -04:00
|
|
|
return res.sendStatus 400
|
2015-08-24 06:53:33 -04:00
|
|
|
delete req.session.resetToken
|
2015-12-11 06:30:06 -05:00
|
|
|
PasswordResetHandler.setNewUserPassword passwordResetToken?.trim(), password?.trim(), (err, found, user_id) ->
|
2014-10-08 12:18:24 -04:00
|
|
|
return next(err) if err?
|
|
|
|
if found
|
2016-07-05 09:19:59 -04:00
|
|
|
UserSessionsManager.revokeAllUserSessions {_id: user_id}, [], (err) ->
|
|
|
|
return next(err) if err?
|
|
|
|
if req.body.login_after
|
|
|
|
UserGetter.getUser user_id, {email: 1}, (err, user) ->
|
|
|
|
return next(err) if err?
|
|
|
|
AuthenticationController.doLogin {email:user.email, password: password}, req, res, next
|
|
|
|
else
|
|
|
|
res.sendStatus 200
|
2014-05-15 11:50:38 -04:00
|
|
|
else
|
2015-12-11 06:30:06 -05:00
|
|
|
res.sendStatus 404
|