classify session
This commit is contained in:
parent
d0180f3b38
commit
7221dd31af
1 changed files with 26 additions and 31 deletions
|
@ -11,41 +11,36 @@ EXPIRY =
|
||||||
login: 1 * 60 * 60 * 24 * 7 * 4
|
login: 1 * 60 * 60 * 24 * 7 * 4
|
||||||
|
|
||||||
|
|
||||||
FFTCGSESSION = () ->
|
class FFTCGSESSION
|
||||||
@db = redis.createClient
|
constructor: ->
|
||||||
host: 'redis'
|
@db = redis.createClient
|
||||||
port: 6379
|
host: 'redis'
|
||||||
|
port: 6379
|
||||||
|
|
||||||
@db.on 'error', (err) ->
|
@db.on 'error', (err) ->
|
||||||
logger.error err.message
|
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) ->
|
# push (hash, data) into DB for the configured timespan
|
||||||
that = @
|
@db.setex digest, EXPIRY.login, (JSON.stringify data), (err) ->
|
||||||
|
resolve digest
|
||||||
|
|
||||||
new Promise (resolve) ->
|
check: (digest) ->
|
||||||
# hash data
|
new Promise (resolve, reject) =>
|
||||||
hmac = crypto.createHmac 'sha256', Math.random().toString()
|
# refresh expiry timer on digest
|
||||||
hmac.update (JSON.stringify data)
|
@db.expire digest, EXPIRY.login, (err, res) =>
|
||||||
digest = hmac.digest 'base64'
|
if res == 0
|
||||||
logger.debug 'digest', digest
|
reject null
|
||||||
|
|
||||||
# push (hash, data) into DB for the configured timespan
|
else
|
||||||
that.db.setex digest, EXPIRY.login, (JSON.stringify data), (err) ->
|
@db.get digest, (err, res) ->
|
||||||
resolve digest
|
resolve JSON.parse res
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
module.exports = FFTCGSESSION
|
module.exports = FFTCGSESSION
|
||||||
|
|
Reference in a new issue