2014-05-15 12:16:20 -04:00
|
|
|
Settings = require('settings-sharelatex')
|
2014-09-26 09:52:00 -04:00
|
|
|
redis = require("redis-sharelatex")
|
|
|
|
rclient = redis.createClient(Settings.redis.web)
|
2014-05-16 05:52:31 -04:00
|
|
|
crypto = require("crypto")
|
2014-05-15 13:08:21 -04:00
|
|
|
logger = require("logger-sharelatex")
|
2014-05-15 12:16:20 -04:00
|
|
|
|
2014-05-15 12:58:25 -04:00
|
|
|
ONE_HOUR_IN_S = 60 * 60
|
|
|
|
|
|
|
|
buildKey = (token)-> return "password_token:#{token}"
|
2014-05-15 12:16:20 -04:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
|
2015-05-26 10:24:09 -04:00
|
|
|
getNewToken: (value, options = {}, callback)->
|
2015-03-19 10:22:48 -04:00
|
|
|
# options is optional
|
|
|
|
if typeof options == "function"
|
|
|
|
callback = options
|
|
|
|
options = {}
|
|
|
|
expiresIn = options.expiresIn or ONE_HOUR_IN_S
|
2015-05-26 10:24:09 -04:00
|
|
|
logger.log value:value, "generating token for password reset"
|
2014-05-16 05:52:31 -04:00
|
|
|
token = crypto.randomBytes(32).toString("hex")
|
2014-05-15 12:16:20 -04:00
|
|
|
multi = rclient.multi()
|
2015-05-26 10:24:09 -04:00
|
|
|
multi.set buildKey(token), value
|
2015-03-19 10:22:48 -04:00
|
|
|
multi.expire buildKey(token), expiresIn
|
2014-05-15 12:16:20 -04:00
|
|
|
multi.exec (err)->
|
|
|
|
callback(err, token)
|
|
|
|
|
2015-05-26 10:24:09 -04:00
|
|
|
getValueFromTokenAndExpire: (token, callback)->
|
2014-05-15 13:08:21 -04:00
|
|
|
logger.log token:token, "getting user id from password token"
|
2014-05-15 12:20:42 -04:00
|
|
|
multi = rclient.multi()
|
2014-05-15 12:58:25 -04:00
|
|
|
multi.get buildKey(token)
|
|
|
|
multi.del buildKey(token)
|
2014-05-15 12:20:42 -04:00
|
|
|
multi.exec (err, results)->
|
|
|
|
callback err, results[0]
|
|
|
|
|