session start and resume
This commit is contained in:
parent
24484a8ddb
commit
6306a4457d
2 changed files with 50 additions and 58 deletions
|
@ -1,10 +1,11 @@
|
|||
# node libraries
|
||||
#(require 'debug').enable 'routes'
|
||||
logger = (require 'logging').default 'routes'
|
||||
path = (require 'path')
|
||||
|
||||
# my libraries
|
||||
FFTCGDB = (require './db')
|
||||
FFTCGSESSION = (require './session')
|
||||
logger = (require 'logging').default 'routes'
|
||||
|
||||
# open fftcg.db (persistent data)
|
||||
fftcgdb = new FFTCGDB path.resolve(__dirname, 'fftcg.db'), true
|
||||
|
@ -47,29 +48,35 @@ module.exports = [
|
|||
url: '/user/login'
|
||||
method: 'POST'
|
||||
handler: (request, reply) ->
|
||||
session_id = request.cookies.session
|
||||
logger.info session_id
|
||||
session.action session_id
|
||||
.then (user) ->
|
||||
logger.info user
|
||||
.catch (err) ->
|
||||
logger.error err
|
||||
session_id = request.cookies.session ? ''
|
||||
|
||||
new Promise (resolve, reject) ->
|
||||
session.check session_id
|
||||
.then (user) ->
|
||||
logger.debug "user '#{user.login}' resumed session '#{session_id}'"
|
||||
resolve user
|
||||
|
||||
.catch ->
|
||||
fftcgdb.login request.body.login, request.body.password
|
||||
.then (user) ->
|
||||
# login successful
|
||||
reply.setCookie 'user', JSON.stringify user
|
||||
|
||||
# return JSON status
|
||||
reply.send
|
||||
status: 'ok'
|
||||
user: user.user
|
||||
login: user.login
|
||||
# login successful: start new session
|
||||
session.start user
|
||||
.then (session_id) ->
|
||||
logger.debug "user '#{user.login}' logged into session '#{session_id}'"
|
||||
reply.setCookie 'session', session_id
|
||||
resolve user
|
||||
|
||||
.catch (err) ->
|
||||
logger.info "failed login for '#{request.body.login}'"
|
||||
# login failed
|
||||
reply.send
|
||||
status: 'fail'
|
||||
text: err
|
||||
msg: err
|
||||
|
||||
.then (user) ->
|
||||
# user is logged in
|
||||
reply.send
|
||||
status: 'ok'
|
||||
user: user
|
||||
,
|
||||
]
|
||||
|
|
|
@ -4,63 +4,48 @@ 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
|
||||
game: 1 * 60 * 60 * 24 * 7
|
||||
# logins expire 1 month after last action
|
||||
login: 1 * TIMES.month
|
||||
login: 1 * 60 * 60 * 24 * 7 * 4
|
||||
|
||||
|
||||
FFTCGSESSION = () ->
|
||||
@db = redis.createClient 6379, 'redis'
|
||||
@db = redis.createClient
|
||||
host: 'redis'
|
||||
port: 6379
|
||||
|
||||
@db.on 'error', (err) ->
|
||||
logger.error err.message
|
||||
|
||||
return
|
||||
|
||||
FFTCGSESSION::login = (login) ->
|
||||
FFTCGSESSION::start = (data) ->
|
||||
that = @
|
||||
|
||||
new Promise (resolve, reject) ->
|
||||
new Promise (resolve) ->
|
||||
# hash data
|
||||
hmac = crypto.createHmac 'sha256', Math.random().toString()
|
||||
hmac.update login
|
||||
digest = hmac.digest 'hex'
|
||||
hmac.update (JSON.stringify data)
|
||||
digest = hmac.digest 'base64'
|
||||
logger.debug 'digest', digest
|
||||
|
||||
that.db.setex digest, EXPIRY.login, login, (err) ->
|
||||
if err
|
||||
reject err
|
||||
|
||||
else
|
||||
# push (hash, data) into DB for the configured timespan
|
||||
that.db.setex digest, EXPIRY.login, (JSON.stringify data), (err) ->
|
||||
resolve digest
|
||||
|
||||
|
||||
FFTCGSESSION::action = (digest) ->
|
||||
FFTCGSESSION::check = (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
|
||||
# refresh expiry timer on digest
|
||||
that.db.expire digest, EXPIRY.login, (err, res) ->
|
||||
if err
|
||||
reject err
|
||||
if res == 0
|
||||
reject null
|
||||
|
||||
else
|
||||
resolve res
|
||||
that.db.get digest, (err, res) ->
|
||||
resolve JSON.parse res
|
||||
|
||||
module.exports = FFTCGSESSION
|
||||
|
|
Reference in a new issue