2014-05-15 12:16:20 -04:00
|
|
|
Settings = require('settings-sharelatex')
|
2017-05-04 10:22:54 -04:00
|
|
|
RedisWrapper = require("../../infrastructure/RedisWrapper")
|
|
|
|
rclient = RedisWrapper.client("one_time_token")
|
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
|
|
|
|
|
2018-06-19 08:55:34 -04:00
|
|
|
buildKey = (use, token)-> return "#{use}_token:#{token}"
|
2014-05-15 12:16:20 -04:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
|
2018-06-19 08:55:34 -04:00
|
|
|
getNewToken: (use, 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
|
2014-05-16 05:52:31 -04:00
|
|
|
token = crypto.randomBytes(32).toString("hex")
|
2018-06-19 08:55:34 -04:00
|
|
|
logger.log {value, expiresIn, token_start: token.slice(0,8)}, "generating token for #{use}"
|
2014-05-15 12:16:20 -04:00
|
|
|
multi = rclient.multi()
|
2018-06-19 08:55:34 -04:00
|
|
|
multi.set buildKey(use, token), value
|
|
|
|
multi.expire buildKey(use, token), expiresIn
|
2014-05-15 12:16:20 -04:00
|
|
|
multi.exec (err)->
|
|
|
|
callback(err, token)
|
|
|
|
|
2018-06-19 08:55:34 -04:00
|
|
|
getValueFromTokenAndExpire: (use, token, callback)->
|
|
|
|
logger.log token_start: token.slice(0,8), "getting value from #{use} token"
|
2014-05-15 12:20:42 -04:00
|
|
|
multi = rclient.multi()
|
2018-06-19 08:55:34 -04:00
|
|
|
multi.get buildKey(use, token)
|
|
|
|
multi.del buildKey(use, token)
|
2014-05-15 12:20:42 -04:00
|
|
|
multi.exec (err, results)->
|
2016-08-02 10:34:44 -04:00
|
|
|
callback err, results?[0]
|
2014-05-15 12:20:42 -04:00
|
|
|
|