overleaf/services/web/app/coffee/Features/PasswordReset/PasswordResetTokenHandler.coffee

31 lines
915 B
CoffeeScript
Raw Normal View History

Settings = require('settings-sharelatex')
redis = require('redis')
rclient = redis.createClient(Settings.redis.web.port, Settings.redis.web.host)
rclient.auth(Settings.redis.web.password)
2014-05-16 09:52:31 +00:00
crypto = require("crypto")
2014-05-15 17:08:21 +00:00
logger = require("logger-sharelatex")
2014-05-15 16:58:25 +00:00
ONE_HOUR_IN_S = 60 * 60
buildKey = (token)-> return "password_token:#{token}"
module.exports =
getNewToken: (user_id, callback)->
2014-05-15 17:08:21 +00:00
logger.log user_id:user_id, "generating token for password reset"
2014-05-16 09:52:31 +00:00
token = crypto.randomBytes(32).toString("hex")
multi = rclient.multi()
2014-05-15 16:58:25 +00:00
multi.set buildKey(token), user_id
multi.expire buildKey(token), ONE_HOUR_IN_S
multi.exec (err)->
callback(err, token)
getUserIdFromTokenAndExpire: (token, callback)->
2014-05-15 17:08:21 +00:00
logger.log token:token, "getting user id from password token"
2014-05-15 16:20:42 +00:00
multi = rclient.multi()
2014-05-15 16:58:25 +00:00
multi.get buildKey(token)
multi.del buildKey(token)
2014-05-15 16:20:42 +00:00
multi.exec (err, results)->
callback err, results[0]