2016-04-19 11:48:51 -04:00
|
|
|
Settings = require "settings-sharelatex"
|
|
|
|
request = require('request')
|
2017-05-04 10:22:54 -04:00
|
|
|
RedisWrapper = require("../../infrastructure/RedisWrapper")
|
|
|
|
rclient = RedisWrapper.client("clsi_cookie")
|
2017-05-15 10:35:01 -04:00
|
|
|
if Settings.redis.clsi_cookie_secondary?
|
|
|
|
rclient_secondary = RedisWrapper.client("clsi_cookie_secondary")
|
2016-04-20 10:06:39 -04:00
|
|
|
Cookie = require('cookie')
|
|
|
|
logger = require "logger-sharelatex"
|
2016-04-19 11:48:51 -04:00
|
|
|
|
|
|
|
buildKey = (project_id)->
|
|
|
|
return "clsiserver:#{project_id}"
|
|
|
|
|
2016-05-19 11:55:58 -04:00
|
|
|
clsiCookiesEnabled = Settings.clsiCookie?.key? and Settings.clsiCookie.key.length != 0
|
2016-04-27 11:56:21 -04:00
|
|
|
|
2016-04-19 11:48:51 -04:00
|
|
|
|
2016-04-20 10:06:39 -04:00
|
|
|
module.exports = ClsiCookieManager =
|
2016-04-19 11:48:51 -04:00
|
|
|
|
|
|
|
_getServerId : (project_id, callback = (err, serverId)->)->
|
2016-04-27 11:56:21 -04:00
|
|
|
rclient.get buildKey(project_id), (err, serverId)->
|
2016-04-19 11:48:51 -04:00
|
|
|
if err?
|
|
|
|
return callback(err)
|
2017-05-18 06:09:33 -04:00
|
|
|
if !serverId? or serverId == ""
|
2016-04-20 10:06:39 -04:00
|
|
|
return ClsiCookieManager._populateServerIdViaRequest project_id, callback
|
2017-05-18 06:09:33 -04:00
|
|
|
else
|
|
|
|
return callback(null, serverId)
|
2016-04-19 11:48:51 -04:00
|
|
|
|
|
|
|
|
2016-04-20 10:06:39 -04:00
|
|
|
_populateServerIdViaRequest :(project_id, callback = (err, serverId)->)->
|
2016-04-19 11:48:51 -04:00
|
|
|
url = "#{Settings.apis.clsi.url}/project/#{project_id}/status"
|
|
|
|
request.get url, (err, res, body)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, "error getting initial server id for project"
|
|
|
|
return callback(err)
|
2016-04-27 11:20:10 -04:00
|
|
|
ClsiCookieManager.setServerId project_id, res, (err, serverId)->
|
2016-04-20 10:06:39 -04:00
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, "error setting server id via populate request"
|
2016-04-27 11:20:10 -04:00
|
|
|
callback(err, serverId)
|
2016-04-19 11:48:51 -04:00
|
|
|
|
|
|
|
_parseServerIdFromResponse : (response)->
|
2016-04-20 10:06:39 -04:00
|
|
|
cookies = Cookie.parse(response.headers["set-cookie"]?[0] or "")
|
2016-05-19 11:55:58 -04:00
|
|
|
return cookies?[Settings.clsiCookie.key]
|
2016-04-19 11:48:51 -04:00
|
|
|
|
2016-04-27 11:20:10 -04:00
|
|
|
setServerId: (project_id, response, callback = (err, serverId)->)->
|
2016-04-27 12:05:12 -04:00
|
|
|
if !clsiCookiesEnabled
|
|
|
|
return callback()
|
2016-04-20 10:06:39 -04:00
|
|
|
serverId = ClsiCookieManager._parseServerIdFromResponse(response)
|
2017-05-19 05:23:01 -04:00
|
|
|
if !serverId? # We don't get a cookie back if it hasn't changed
|
|
|
|
return callback()
|
2017-05-15 10:35:01 -04:00
|
|
|
if rclient_secondary?
|
|
|
|
@_setServerIdInRedis rclient_secondary, project_id, serverId
|
|
|
|
@_setServerIdInRedis rclient, project_id, serverId, (err) ->
|
|
|
|
callback(err, serverId)
|
|
|
|
|
|
|
|
_setServerIdInRedis: (rclient, project_id, serverId, callback = (err) ->) ->
|
2016-04-19 11:48:51 -04:00
|
|
|
multi = rclient.multi()
|
|
|
|
multi.set buildKey(project_id), serverId
|
2016-05-19 11:55:58 -04:00
|
|
|
multi.expire buildKey(project_id), Settings.clsiCookie.ttl
|
2017-05-15 10:35:01 -04:00
|
|
|
multi.exec callback
|
2016-04-19 11:48:51 -04:00
|
|
|
|
2016-08-31 11:10:24 -04:00
|
|
|
clearServerId: (project_id, callback = (err)->)->
|
|
|
|
if !clsiCookiesEnabled
|
|
|
|
return callback()
|
|
|
|
rclient.del buildKey(project_id), callback
|
2016-04-19 11:48:51 -04:00
|
|
|
|
2016-04-20 10:06:39 -04:00
|
|
|
getCookieJar: (project_id, callback = (err, jar)->)->
|
2016-04-27 12:05:12 -04:00
|
|
|
if !clsiCookiesEnabled
|
|
|
|
return callback(null, request.jar())
|
2016-04-20 10:06:39 -04:00
|
|
|
ClsiCookieManager._getServerId project_id, (err, serverId)=>
|
2016-04-19 11:48:51 -04:00
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, "error getting server id"
|
|
|
|
return callback(err)
|
2016-05-19 11:55:58 -04:00
|
|
|
serverCookie = request.cookie("#{Settings.clsiCookie.key}=#{serverId}")
|
2016-04-19 11:48:51 -04:00
|
|
|
jar = request.jar()
|
2016-04-20 10:06:39 -04:00
|
|
|
jar.setCookie serverCookie, Settings.apis.clsi.url
|
2016-04-19 11:48:51 -04:00
|
|
|
callback(null, jar)
|
|
|
|
|
|
|
|
|