2014-05-15 11:20:23 -04:00
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
async = require("async")
|
|
|
|
UserGetter = require("../User/UserGetter")
|
2015-05-27 10:06:36 -04:00
|
|
|
OneTimeTokenHandler = require("../Security/OneTimeTokenHandler")
|
2014-05-15 11:20:23 -04:00
|
|
|
EmailHandler = require("../Email/EmailHandler")
|
|
|
|
AuthenticationManager = require("../Authentication/AuthenticationManager")
|
2014-05-15 13:08:21 -04:00
|
|
|
logger = require("logger-sharelatex")
|
2018-12-18 06:14:41 -05:00
|
|
|
V1Api = require("../V1/V1Api")
|
2014-05-15 11:20:23 -04:00
|
|
|
|
2018-12-18 06:14:41 -05:00
|
|
|
module.exports = PasswordResetHandler =
|
2014-05-15 11:20:23 -04:00
|
|
|
|
2014-08-08 06:41:54 -04:00
|
|
|
generateAndEmailResetToken:(email, callback = (error, exists) ->)->
|
2018-12-18 06:14:41 -05:00
|
|
|
PasswordResetHandler._getPasswordResetData email, (error, exists, data) ->
|
|
|
|
if error? or !exists
|
|
|
|
return callback(error, exists)
|
|
|
|
OneTimeTokenHandler.getNewToken 'password', data, (err, token)->
|
2014-05-15 11:50:38 -04:00
|
|
|
if err then return callback(err)
|
|
|
|
emailOptions =
|
|
|
|
to : email
|
2015-04-30 06:59:44 -04:00
|
|
|
setNewPasswordUrl : "#{settings.siteUrl}/user/password/set?passwordResetToken=#{token}&email=#{encodeURIComponent(email)}"
|
2014-08-08 06:41:54 -04:00
|
|
|
EmailHandler.sendEmail "passwordResetRequested", emailOptions, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback null, true
|
2014-05-15 11:20:23 -04:00
|
|
|
|
2015-12-11 06:30:06 -05:00
|
|
|
setNewUserPassword: (token, password, callback = (error, found, user_id) ->)->
|
2018-12-18 06:14:41 -05:00
|
|
|
OneTimeTokenHandler.getValueFromTokenAndExpire 'password', token, (err, data)->
|
2014-05-15 11:20:23 -04:00
|
|
|
if err then return callback(err)
|
2018-12-18 06:14:41 -05:00
|
|
|
if !data?
|
2015-12-11 06:30:06 -05:00
|
|
|
return callback null, false, null
|
2018-12-18 06:14:41 -05:00
|
|
|
if typeof data == "string"
|
|
|
|
# Backwards compatible with old format.
|
|
|
|
# Tokens expire after 1h, so this can be removed soon after deploy.
|
|
|
|
# Possibly we should keep this until we do an onsite release too.
|
|
|
|
data = { user_id: data }
|
|
|
|
if data.user_id?
|
|
|
|
AuthenticationManager.setUserPassword data.user_id, password, (err, reset) ->
|
|
|
|
if err then return callback(err)
|
|
|
|
callback null, reset, data.user_id
|
|
|
|
else if data.v1_user_id?
|
|
|
|
AuthenticationManager.setUserPasswordInV1 data.v1_user_id, password, (error, reset) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
UserGetter.getUser { 'overleaf.id': data.v1_user_id }, {_id:1}, (error, user) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback null, reset, user?._id
|
|
|
|
|
|
|
|
_getPasswordResetData: (email, callback = (error, exists, data) ->) ->
|
|
|
|
if settings.overleaf?
|
|
|
|
# Overleaf v2
|
|
|
|
V1Api.request {
|
|
|
|
url: "/api/v1/sharelatex/user_emails"
|
|
|
|
qs:
|
|
|
|
email: email
|
|
|
|
expectedStatusCodes: [404]
|
|
|
|
}, (error, response, body) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if response.statusCode == 404
|
|
|
|
return callback null, false
|
|
|
|
else
|
|
|
|
return callback null, true, { v1_user_id: body.user_id }
|
|
|
|
else
|
|
|
|
# ShareLaTeX
|
|
|
|
UserGetter.getUserByMainEmail email, (err, user)->
|
2014-10-08 12:18:24 -04:00
|
|
|
if err then return callback(err)
|
2018-12-18 06:14:41 -05:00
|
|
|
if !user? or user.holdingAccount or user.overleaf?
|
|
|
|
logger.err email:email, "user could not be found for password reset"
|
|
|
|
return callback(null, false)
|
|
|
|
return callback null, true, { user_id: user._id }
|