66 lines
1.3 KiB
CoffeeScript
66 lines
1.3 KiB
CoffeeScript
# node libraries
|
|
redis = (require 'redis')
|
|
crypto = (require 'crypto')
|
|
logger = (require 'logging').default 'session'
|
|
|
|
# expiry times in seconds
|
|
TIMES =
|
|
minute: 60
|
|
hour: 60 * 60
|
|
day: 60 * 60 * 24
|
|
week: 60 * 60 * 24 * 7
|
|
month: 60 * 60 * 24 * 7 * 4
|
|
|
|
EXPIRY =
|
|
# games expire 1 week after last action
|
|
game: 1 * TIMES.week
|
|
# logins expire 1 month after last action
|
|
login: 1 * TIMES.month
|
|
|
|
|
|
FFTCGSESSION = () ->
|
|
@db = redis.createClient 6379, 'redis'
|
|
|
|
@db.on 'error', (err) ->
|
|
logger.error err.message
|
|
|
|
return
|
|
|
|
FFTCGSESSION::login = (login) ->
|
|
that = @
|
|
|
|
new Promise (resolve, reject) ->
|
|
hmac = crypto.createHmac 'sha256', Math.random().toString()
|
|
hmac.update login
|
|
digest = hmac.digest 'hex'
|
|
|
|
that.db.setex digest, EXPIRY.login, login, (err) ->
|
|
if err
|
|
reject err
|
|
|
|
else
|
|
resolve digest
|
|
|
|
|
|
FFTCGSESSION::action = (digest) ->
|
|
that = @
|
|
|
|
new Promise (resolve, reject) ->
|
|
that.db.get digest (err, res) ->
|
|
logger.info 'err', err, 'res', res
|
|
|
|
if err
|
|
reject err
|
|
|
|
else if res == 0
|
|
resolve null
|
|
|
|
else
|
|
that.db.expire digest, EXPIRY.login, (err, res) ->
|
|
if err
|
|
reject err
|
|
|
|
else
|
|
resolve res
|
|
|
|
module.exports = FFTCGSESSION
|