2018-12-07 09:38:46 +00:00
|
|
|
# libraries
|
|
|
|
bcrypt = (require 'bcrypt')
|
|
|
|
sqlite3 = (require 'sqlite3').verbose()
|
|
|
|
|
2018-12-16 01:37:00 +00:00
|
|
|
# bruteforce countermeasure
|
|
|
|
saltRounds = 13
|
|
|
|
|
2018-12-14 06:03:03 +00:00
|
|
|
FFTCGDB = (filename) ->
|
|
|
|
@filename = filename
|
|
|
|
|
|
|
|
@db = new sqlite3.Database @filename, (err) ->
|
|
|
|
if err
|
|
|
|
console.error err.message
|
|
|
|
|
|
|
|
@db.run """
|
|
|
|
CREATE TABLE IF NOT EXISTS users (
|
2018-12-14 12:31:07 +00:00
|
|
|
login text NOT NULL COLLATE NOCASE,
|
|
|
|
pwdhash text NOT NULL,
|
2018-12-14 06:03:03 +00:00
|
|
|
UNIQUE(login)
|
|
|
|
);
|
|
|
|
"""
|
2018-12-16 21:51:08 +00:00
|
|
|
|
2018-12-14 12:31:07 +00:00
|
|
|
console.log "[FFTCGDB] Connected to '#{@filename}'"
|
2018-12-14 06:03:03 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
FFTCGDB::close = ->
|
2018-12-14 12:31:07 +00:00
|
|
|
new Promise (resolve, reject) ->
|
|
|
|
@db.close (err) ->
|
|
|
|
if err
|
|
|
|
resolve "[FFTCGDB] Error closing: '#{err.message}'"
|
|
|
|
else
|
|
|
|
reject "[FFTCGDB] Closed '#{@filename}'"
|
2018-12-14 06:03:03 +00:00
|
|
|
|
|
|
|
FFTCGDB::register = (login, password) ->
|
|
|
|
that = @
|
|
|
|
|
|
|
|
new Promise (resolve, reject) ->
|
2018-12-16 21:51:08 +00:00
|
|
|
# validate user input
|
|
|
|
if login == '' or password == ''
|
|
|
|
# no user name or password given
|
|
|
|
console.log "[FFTCGDB] reg: user name '#{login}' or password empty"
|
|
|
|
reject 'invalid'
|
2018-12-14 12:31:07 +00:00
|
|
|
|
2018-12-14 06:03:03 +00:00
|
|
|
# hash password
|
2018-12-07 09:38:46 +00:00
|
|
|
bcrypt.hash password, saltRounds, (err, hash) ->
|
2018-12-16 21:51:08 +00:00
|
|
|
if err
|
|
|
|
console.log "[FFTCGDB] reg: hash fail for name '#{login}'"
|
|
|
|
reject 'hash'
|
|
|
|
|
2018-12-14 06:03:03 +00:00
|
|
|
# try creating row in users table
|
2018-12-16 21:51:08 +00:00
|
|
|
stmt = that.db.prepare 'INSERT INTO users (login, pwdhash) VALUES (?, ?)'
|
|
|
|
stmt.run [login, hash], (err) ->
|
2018-12-14 12:31:07 +00:00
|
|
|
if err
|
2018-12-16 21:51:08 +00:00
|
|
|
console.log "[FFTCGDB] reg: DB fail '#{err.code}' for name '#{login}'"
|
|
|
|
stmt.finalize()
|
|
|
|
# reduce attack surface, don't disclose user names
|
|
|
|
reject 'db' # user already exists
|
2018-12-14 12:31:07 +00:00
|
|
|
|
|
|
|
else
|
2018-12-16 21:51:08 +00:00
|
|
|
console.log "[FFTCGDB] reg: OK '#{login}'"
|
|
|
|
stmt.finalize()
|
2018-12-14 12:31:07 +00:00
|
|
|
# registration successful
|
2018-12-16 21:51:08 +00:00
|
|
|
resolve
|
|
|
|
uid: @lastID
|
|
|
|
login: login
|
2018-12-14 06:03:03 +00:00
|
|
|
|
|
|
|
FFTCGDB::login = (login, password) ->
|
|
|
|
that = @
|
|
|
|
|
|
|
|
new Promise (resolve, reject) ->
|
2018-12-14 12:31:07 +00:00
|
|
|
# get users table row
|
2018-12-16 21:51:08 +00:00
|
|
|
stmt = that.db.prepare 'SELECT rowid, login, pwdhash FROM users WHERE login = ?'
|
|
|
|
stmt.get [login], (err, row) ->
|
2018-12-14 12:31:07 +00:00
|
|
|
if err
|
2018-12-16 21:51:08 +00:00
|
|
|
console.log "[FFTCGDB] login: DB fail '#{err.code}' for name '#{login}'"
|
|
|
|
stmt.finalize()
|
2018-12-14 12:31:07 +00:00
|
|
|
reject 'db'
|
2018-12-07 09:38:46 +00:00
|
|
|
|
2018-12-16 21:51:08 +00:00
|
|
|
else if not row
|
|
|
|
# hash the password for timing attack reasons
|
2018-12-16 01:37:00 +00:00
|
|
|
bcrypt.hash password, saltRounds, (err, hash) ->
|
2018-12-16 21:51:08 +00:00
|
|
|
console.log "[FFTCGDB] login: nonexistent '#{login}'"
|
|
|
|
stmt.finalize()
|
|
|
|
# reduce attack surface, don't disclose user names
|
|
|
|
reject 'login' # user doesnt exist
|
2018-12-07 09:38:46 +00:00
|
|
|
|
2018-12-14 12:31:07 +00:00
|
|
|
else
|
|
|
|
bcrypt.compare password, row.pwdhash, (err, res) ->
|
2018-12-16 21:51:08 +00:00
|
|
|
if err
|
|
|
|
console.log "[FFTCGDB] login: hash fail for name '#{login}'"
|
|
|
|
reject 'hash'
|
|
|
|
|
2018-12-14 12:31:07 +00:00
|
|
|
if res == true
|
2018-12-16 21:51:08 +00:00
|
|
|
console.log "[FFTCGDB] login: OK '#{row.login}'"
|
|
|
|
stmt.finalize()
|
|
|
|
# login successful
|
|
|
|
resolve
|
|
|
|
uid: row.rowid
|
|
|
|
login: row.login
|
|
|
|
|
2018-12-14 12:31:07 +00:00
|
|
|
else
|
2018-12-16 21:51:08 +00:00
|
|
|
console.log "[FFTCGDB] login: wrong password for '#{login}'"
|
|
|
|
stmt.finalize()
|
|
|
|
# login failed
|
2018-12-14 12:31:07 +00:00
|
|
|
reject 'login'
|
|
|
|
|
2018-12-16 21:51:08 +00:00
|
|
|
|
2018-12-14 12:31:07 +00:00
|
|
|
module.exports = FFTCGDB
|