overleaf/services/web/app/coffee/Features/Security/OneTimeTokenHandler.coffee

35 lines
1.1 KiB
CoffeeScript
Raw Normal View History

Settings = require('settings-sharelatex')
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:58:25 -04:00
ONE_HOUR_IN_S = 60 * 60
buildKey = (use, token)-> return "#{use}_token:#{token}"
module.exports =
getNewToken: (use, value, options = {}, callback)->
# 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")
logger.log {value, expiresIn, token_start: token.slice(0,8)}, "generating token for #{use}"
multi = rclient.multi()
multi.set buildKey(use, token), value
multi.expire buildKey(use, token), expiresIn
multi.exec (err)->
callback(err, token)
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()
multi.get buildKey(use, token)
multi.del buildKey(use, token)
2014-05-15 12:20:42 -04:00
multi.exec (err, results)->
callback err, results?[0]
2014-05-15 12:20:42 -04:00