From 7221dd31af5f9c476b7932dea0fd00a8e165bec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Sat, 16 Feb 2019 19:15:49 +0100 Subject: [PATCH] classify session --- backend/session.coffee | 57 +++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/backend/session.coffee b/backend/session.coffee index 2c4b8a4..afcc7ab 100644 --- a/backend/session.coffee +++ b/backend/session.coffee @@ -11,41 +11,36 @@ EXPIRY = login: 1 * 60 * 60 * 24 * 7 * 4 -FFTCGSESSION = () -> - @db = redis.createClient - host: 'redis' - port: 6379 +class FFTCGSESSION + constructor: -> + @db = redis.createClient + host: 'redis' + port: 6379 - @db.on 'error', (err) -> - logger.error err.message + @db.on 'error', (err) -> + logger.error err.message - return + start: (data) -> + new Promise (resolve) => + # hash data + hmac = crypto.createHmac 'sha256', Math.random().toString() + hmac.update (JSON.stringify data) + digest = hmac.digest 'base64' + logger.debug 'digest', digest -FFTCGSESSION::start = (data) -> - that = @ + # push (hash, data) into DB for the configured timespan + @db.setex digest, EXPIRY.login, (JSON.stringify data), (err) -> + resolve digest - new Promise (resolve) -> - # hash data - hmac = crypto.createHmac 'sha256', Math.random().toString() - hmac.update (JSON.stringify data) - digest = hmac.digest 'base64' - logger.debug 'digest', digest + check: (digest) -> + new Promise (resolve, reject) => + # refresh expiry timer on digest + @db.expire digest, EXPIRY.login, (err, res) => + if res == 0 + reject null - # push (hash, data) into DB for the configured timespan - that.db.setex digest, EXPIRY.login, (JSON.stringify data), (err) -> - resolve digest - -FFTCGSESSION::check = (digest) -> - that = @ - - new Promise (resolve, reject) -> - # refresh expiry timer on digest - that.db.expire digest, EXPIRY.login, (err, res) -> - if res == 0 - reject null - - else - that.db.get digest, (err, res) -> - resolve JSON.parse res + else + @db.get digest, (err, res) -> + resolve JSON.parse res module.exports = FFTCGSESSION