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

52 lines
1.2 KiB
CoffeeScript
Raw Normal View History

# node libraries
redis = (require 'redis')
crypto = (require 'crypto')
logger = (require 'logging').default 'session'
# expiry times in seconds
EXPIRY =
2019-02-16 18:03:21 +00:00
# games expire 1 week after creation
2019-02-15 02:50:11 +00:00
game: 1 * 60 * 60 * 24 * 7
# logins expire 1 month after last action
2019-02-15 02:50:11 +00:00
login: 1 * 60 * 60 * 24 * 7 * 4
FFTCGSESSION = () ->
2019-02-15 02:50:11 +00:00
@db = redis.createClient
host: 'redis'
port: 6379
@db.on 'error', (err) ->
logger.error err.message
return
2019-02-15 02:50:11 +00:00
FFTCGSESSION::start = (data) ->
that = @
2019-02-15 02:50:11 +00:00
new Promise (resolve) ->
# hash data
hmac = crypto.createHmac 'sha256', Math.random().toString()
2019-02-15 02:50:11 +00:00
hmac.update (JSON.stringify data)
digest = hmac.digest 'base64'
logger.debug 'digest', digest
2019-02-15 02:50:11 +00:00
# push (hash, data) into DB for the configured timespan
that.db.setex digest, EXPIRY.login, (JSON.stringify data), (err) ->
resolve digest
2019-02-15 02:50:11 +00:00
FFTCGSESSION::check = (digest) ->
that = @
new Promise (resolve, reject) ->
2019-02-15 02:50:11 +00:00
# refresh expiry timer on digest
that.db.expire digest, EXPIRY.login, (err, res) ->
if res == 0
reject null
else
2019-02-15 02:50:11 +00:00
that.db.get digest, (err, res) ->
resolve JSON.parse res
module.exports = FFTCGSESSION