2013-03-24 02:28:20 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
Copyright (c) 2013 Calvin Montgomery
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2013-02-16 17:19:59 +00:00
|
|
|
|
2013-03-24 17:23:48 +00:00
|
|
|
var mysql = require("mysql-libmysqlclient");
|
2013-05-04 18:01:04 +00:00
|
|
|
var Database = require("./database.js");
|
2013-03-24 17:23:48 +00:00
|
|
|
var Config = require("./config.js");
|
2013-03-26 19:12:02 +00:00
|
|
|
var bcrypt = require("bcrypt");
|
|
|
|
var hashlib = require("node_hash");
|
2013-03-27 19:28:51 +00:00
|
|
|
var Logger = require("./logger.js");
|
2013-02-16 05:02:42 +00:00
|
|
|
|
|
|
|
// Check if a name is taken
|
|
|
|
exports.isRegistered = function(name) {
|
|
|
|
var db = mysql.createConnectionSync();
|
|
|
|
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
|
|
|
|
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
|
|
|
|
if(!db.connectedSync()) {
|
2013-03-27 19:28:51 +00:00
|
|
|
Logger.errlog.log("Auth.isRegistered: DB connection failed");
|
2013-02-16 17:23:51 +00:00
|
|
|
return true;
|
2013-02-16 05:02:42 +00:00
|
|
|
}
|
2013-03-24 17:23:48 +00:00
|
|
|
var query = "SELECT * FROM registrations WHERE uname='{}'"
|
2013-02-16 05:02:42 +00:00
|
|
|
.replace(/\{\}/, name);
|
|
|
|
var results = db.querySync(query);
|
|
|
|
var rows = results.fetchAllSync();
|
|
|
|
db.closeSync();
|
|
|
|
return rows.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a name is valid
|
|
|
|
// Valid names are 1-20 characters, alphanumeric and underscores
|
|
|
|
exports.validateName = function(name) {
|
|
|
|
if(name.length > 20)
|
|
|
|
return false;
|
|
|
|
const VALID_REGEX = /^[a-zA-Z0-9_]+$/;
|
|
|
|
return name.match(VALID_REGEX) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to register a new account
|
2013-03-26 19:12:02 +00:00
|
|
|
exports.register = function(name, pw) {
|
2013-02-16 05:02:42 +00:00
|
|
|
if(!exports.validateName(name))
|
|
|
|
return false;
|
|
|
|
if(exports.isRegistered(name))
|
|
|
|
return false;
|
|
|
|
var db = mysql.createConnectionSync();
|
|
|
|
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
|
|
|
|
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
|
|
|
|
if(!db.connectedSync()) {
|
2013-03-27 19:28:51 +00:00
|
|
|
Logger.errlog.log("Auth.register: DB connection failed");
|
2013-02-16 17:23:51 +00:00
|
|
|
return false;
|
2013-02-16 05:02:42 +00:00
|
|
|
}
|
2013-03-26 19:12:02 +00:00
|
|
|
var hash = bcrypt.hashSync(pw, 10);
|
2013-04-26 03:50:12 +00:00
|
|
|
var query = "INSERT INTO registrations VALUES (NULL, '{1}', '{2}', 1, '', 0)"
|
2013-02-16 05:02:42 +00:00
|
|
|
.replace(/\{1\}/, name)
|
2013-03-26 19:12:02 +00:00
|
|
|
.replace(/\{2\}/, hash);
|
2013-02-16 05:02:42 +00:00
|
|
|
var results = db.querySync(query);
|
|
|
|
db.closeSync();
|
2013-04-26 03:50:12 +00:00
|
|
|
if(results) {
|
|
|
|
return exports.createSession(name);
|
|
|
|
}
|
|
|
|
return false;
|
2013-02-16 05:02:42 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 03:50:12 +00:00
|
|
|
exports.login = function(name, pw, session) {
|
|
|
|
if(session) {
|
|
|
|
var res = exports.loginSession(name, session);
|
|
|
|
if(res) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else if(!pw) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var row = exports.loginPassword(name, pw);
|
|
|
|
if(row) {
|
|
|
|
var hash = exports.createSession(name);
|
|
|
|
row.session_hash = hash;
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:02:42 +00:00
|
|
|
// Try to login
|
2013-04-26 03:50:12 +00:00
|
|
|
exports.loginPassword = function(name, pw) {
|
2013-02-16 05:02:42 +00:00
|
|
|
var db = mysql.createConnectionSync();
|
|
|
|
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
|
|
|
|
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
|
|
|
|
if(!db.connectedSync()) {
|
2013-03-27 19:28:51 +00:00
|
|
|
Logger.errlog.log("Auth.login: DB connection failed");
|
2013-02-16 17:23:51 +00:00
|
|
|
return false;
|
2013-02-16 05:02:42 +00:00
|
|
|
}
|
2013-03-26 19:12:02 +00:00
|
|
|
var query = "SELECT * FROM registrations WHERE uname='{1}'"
|
2013-02-16 05:02:42 +00:00
|
|
|
.replace(/\{1\}/, name)
|
|
|
|
var results = db.querySync(query);
|
|
|
|
var rows = results.fetchAllSync();
|
|
|
|
if(rows.length > 0) {
|
2013-03-27 20:02:43 +00:00
|
|
|
try {
|
|
|
|
if(bcrypt.compareSync(pw, rows[0].pw)) {
|
2013-03-26 19:12:02 +00:00
|
|
|
db.closeSync();
|
|
|
|
return rows[0];
|
|
|
|
}
|
2013-03-27 20:02:43 +00:00
|
|
|
else {
|
|
|
|
// Check if the sha256 is in the database
|
|
|
|
// If so, migrate to bcrypt
|
|
|
|
var sha256 = hashlib.sha256(pw);
|
|
|
|
if(sha256 == rows[0].pw) {
|
|
|
|
var newhash = bcrypt.hashSync(pw, 10);
|
|
|
|
var query = "UPDATE registrations SET pw='{1}' WHERE uname='{2}'"
|
|
|
|
.replace(/\{1\}/, newhash)
|
|
|
|
.replace(/\{2\}/, name);
|
|
|
|
var results = db.querySync(query);
|
|
|
|
db.closeSync();
|
|
|
|
if(!results) {
|
|
|
|
Logger.errlog.log("Failed to migrate password! user=" + name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return rows[0];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
Logger.errlog.log("Auth.login fail");
|
|
|
|
Logger.errlog.log(e);
|
2013-03-26 19:12:02 +00:00
|
|
|
}
|
2013-02-16 05:02:42 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-22 18:28:40 +00:00
|
|
|
|
2013-04-26 03:50:12 +00:00
|
|
|
exports.createSession = function(name) {
|
|
|
|
var salt = sessionSalt();
|
|
|
|
var hash = hashlib.sha256(salt + name);
|
|
|
|
var db = mysql.createConnectionSync();
|
|
|
|
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
|
|
|
|
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
|
|
|
|
if(!db.connectedSync()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var query = ["UPDATE registrations SET ",
|
|
|
|
"session_hash='{1}',",
|
|
|
|
"expire={2} ",
|
|
|
|
"WHERE uname='{3}'"].join("")
|
|
|
|
.replace(/\{1\}/, hash)
|
|
|
|
.replace(/\{2\}/, new Date().getTime() + 604800000)
|
|
|
|
.replace(/\{3\}/, name)
|
|
|
|
var results = db.querySync(query);
|
|
|
|
return results ? hash : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.loginSession = function(name, hash) {
|
|
|
|
var db = mysql.createConnectionSync();
|
|
|
|
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
|
|
|
|
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
|
|
|
|
if(!db.connectedSync()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var query = "SELECT * FROM registrations WHERE uname='{1}'"
|
|
|
|
.replace(/\{1\}/, name)
|
|
|
|
var results = db.querySync(query);
|
|
|
|
var rows = results.fetchAllSync();
|
|
|
|
if(rows.length != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var dbhash = rows[0].session_hash;
|
|
|
|
if(hash != dbhash) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var timeout = rows[0].expire;
|
|
|
|
if(timeout < new Date().getTime()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return rows[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
function sessionSalt() {
|
|
|
|
var chars = "abcdefgihjklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
+ "0123456789!@#$%^&*_+=~";
|
|
|
|
var salt = [];
|
|
|
|
for(var i = 0; i < 32; i++) {
|
|
|
|
salt.push(chars[parseInt(Math.random()*chars.length)]);
|
|
|
|
}
|
|
|
|
return salt.join('');
|
|
|
|
}
|
|
|
|
|
2013-05-04 18:01:04 +00:00
|
|
|
exports.setUserPassword = function(name, pw) {
|
|
|
|
var db = mysql.createConnectionSync();
|
|
|
|
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
|
|
|
|
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
|
|
|
|
if(!db.connectedSync()) {
|
|
|
|
Logger.errlog.log("Auth.setUserPassword: DB connection failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var hash = bcrypt.hashSync(pw, 10);
|
|
|
|
var query = "UPDATE registrations SET pw='{1}' WHERE uname='{2}'"
|
|
|
|
.replace("{1}", Database.sqlEscape(hash))
|
|
|
|
.replace("{2}", Database.sqlEscape(name));
|
|
|
|
var result = db.querySync(query);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-04-22 18:28:40 +00:00
|
|
|
exports.getGlobalRank = function(name) {
|
|
|
|
var db = mysql.createConnectionSync();
|
|
|
|
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
|
|
|
|
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
|
|
|
|
if(!db.connectedSync()) {
|
|
|
|
Logger.errlog.log("Auth.getGlobalRank: DB connection failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var query = "SELECT * FROM registrations WHERE uname='{1}'"
|
|
|
|
.replace(/\{1\}/, name)
|
|
|
|
var results = db.querySync(query);
|
|
|
|
var rows = results.fetchAllSync();
|
|
|
|
if(rows.length > 0) {
|
|
|
|
return rows[0].global_rank;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|