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