This repository has been archived on 2024-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
node-fftcg/backend/session.coffee

46 lines
1.2 KiB
CoffeeScript

# node libraries
redis = (require 'redis')
crypto = (require 'crypto')
logger = (require 'logging').default 'session'
# expiry times in seconds
EXPIRY =
# games expire 1 week after creation
game: 1 * 60 * 60 * 24 * 7
# logins expire 1 month after last action
login: 1 * 60 * 60 * 24 * 7 * 4
class FFTCGSESSION
constructor: ->
@db = redis.createClient
host: 'redis'
port: 6379
@db.on 'error', (err) ->
logger.error err.message
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
# push (hash, data) into DB for the configured timespan
@db.setex digest, EXPIRY.login, (JSON.stringify data), (err) ->
resolve digest
check: (digest) ->
new Promise (resolve, reject) =>
# refresh expiry timer on digest
@db.expire digest, EXPIRY.login, (err, res) =>
if res == 0
reject null
else
@db.get digest, (err, res) ->
resolve JSON.parse res
module.exports = FFTCGSESSION