CyTube/auth.js

227 lines
6.6 KiB
JavaScript
Raw Normal View History

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
var mysql = require("mysql-libmysqlclient");
2013-05-04 18:01:04 +00:00
var Database = require("./database.js");
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) {
2013-05-26 01:01:32 +00:00
var db = Database.getConnection();
if(!db) {
2013-06-19 21:54:27 +00:00
throw "Database failure";
2013-02-16 05:02:42 +00:00
}
2013-05-26 01:01:32 +00:00
var query = Database.createQuery(
"SELECT * FROM `registrations` WHERE uname=?",
[name]
);
2013-02-16 05:02:42 +00:00
var results = db.querySync(query);
2013-05-26 01:01:32 +00:00
if(!results) {
return true;
}
2013-02-16 05:02:42 +00:00
var rows = results.fetchAllSync();
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
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;
2013-05-26 01:01:32 +00:00
var db = Database.getConnection();
if(!db) {
2013-02-16 17:23:51 +00:00
return false;
2013-02-16 05:02:42 +00:00
}
2013-05-26 01:01:32 +00:00
var hash = bcrypt.hashSync(pw, 10);
2013-05-26 01:01:32 +00:00
var query = Database.createQuery(
["INSERT INTO `registrations` VALUES ",
"(NULL, ?, ?, 1, '', 0, '', '', '')"].join(""),
2013-05-26 01:01:32 +00:00
[name, hash]
);
2013-02-16 05:02:42 +00:00
var results = db.querySync(query);
if(results) {
return exports.createSession(name);
}
return false;
2013-02-16 05:02:42 +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-05-26 01:01:32 +00:00
2013-02-16 05:02:42 +00:00
// Try to login
exports.loginPassword = function(name, pw) {
2013-05-26 01:01:32 +00:00
var db = Database.getConnection();
if(!db) {
2013-06-19 21:54:27 +00:00
throw "Database failure";
2013-02-16 05:02:42 +00:00
}
2013-05-26 01:01:32 +00:00
var query = Database.createQuery(
"SELECT * FROM `registrations` WHERE uname=?",
[name]
);
2013-02-16 05:02:42 +00:00
var results = db.querySync(query);
2013-05-26 01:01:32 +00:00
if(!results) {
return false;
}
2013-02-16 05:02:42 +00:00
var rows = results.fetchAllSync();
if(rows.length > 0) {
2013-03-27 20:02:43 +00:00
try {
if(bcrypt.compareSync(pw, rows[0].pw)) {
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);
2013-05-26 01:01:32 +00:00
var query = Database.createQuery(
["UPDATE `registrations` SET pw=?",
"WHERE uname=?"].join(""),
[newhash, name]
);
2013-03-27 20:02:43 +00:00
var results = db.querySync(query);
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-02-16 05:02:42 +00:00
}
return false;
}
exports.createSession = function(name) {
var salt = sessionSalt();
var hash = hashlib.sha256(salt + name);
2013-05-26 01:01:32 +00:00
var db = Database.getConnection();
if(!db) {
2013-06-19 21:54:27 +00:00
throw "Database failure";
}
2013-05-26 01:01:32 +00:00
var query = Database.createQuery(
["UPDATE `registrations` SET ",
"`session_hash`=?,",
"`expire`=? ",
"WHERE uname=?"].join(""),
[hash, Date.now() + 604800000, name]
);
var results = db.querySync(query);
return results ? hash : false;
}
exports.loginSession = function(name, hash) {
2013-05-26 01:01:32 +00:00
var db = Database.getConnection();
if(!db) {
2013-06-19 21:54:27 +00:00
throw "Database failure";
}
2013-05-26 01:01:32 +00:00
var query = Database.createQuery(
"SELECT * FROM `registrations` WHERE `uname`=?",
[name]
);
var results = db.querySync(query);
2013-05-26 01:01:32 +00:00
if(!results) {
return false;
}
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) {
2013-05-26 01:01:32 +00:00
var db = Database.getConnection();
if(!db) {
2013-05-04 18:01:04 +00:00
return false;
}
var hash = bcrypt.hashSync(pw, 10);
2013-05-26 01:01:32 +00:00
var query = Database.createQuery(
"UPDATE `registrations` SET `pw`=? WHERE `uname`=?",
[hash, name]
);
2013-05-04 18:01:04 +00:00
var result = db.querySync(query);
return result;
}
exports.getGlobalRank = function(name) {
2013-05-26 01:01:32 +00:00
var db = Database.getConnection();
if(!db) {
return false;
}
2013-05-26 01:01:32 +00:00
var query = Database.createQuery(
"SELECT * FROM `registrations` WHERE `uname`=?",
[name]
);
var results = db.querySync(query);
2013-05-26 01:01:32 +00:00
if(!results) {
return 0;
}
var rows = results.fetchAllSync();
if(rows.length > 0) {
return rows[0].global_rank;
}
return 0;
}