CyTube/auth.js

79 lines
2.4 KiB
JavaScript
Raw Normal View History

2013-02-16 17:19:59 +00:00
/**
* Copyright 2013 Calvin 'calzoneman' Montgomery
*
* Licensed under Creative Commons Attribution-NonCommercial 3.0
* See http://creativecommons.org/licenses/by-nc/3.0/
*
*/
2013-02-16 05:02:42 +00:00
var mysql = require('mysql-libmysqlclient');
var Config = require('./config.js');
// 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-02-16 17:23:51 +00:00
console.log("MySQL Connection Failed");
return true;
2013-02-16 05:02:42 +00:00
}
var query = 'SELECT * FROM registrations WHERE uname="{}"'
.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
exports.register = function(name, sha256) {
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-02-16 17:23:51 +00:00
console.log("MySQL Connection Failed");
return false;
2013-02-16 05:02:42 +00:00
}
var query = 'INSERT INTO registrations VALUES (NULL, "{1}", "{2}", 0)'
.replace(/\{1\}/, name)
.replace(/\{2\}/, sha256);
var results = db.querySync(query);
db.closeSync();
return results;
}
// Try to login
exports.login = function(name, sha256) {
var db = mysql.createConnectionSync();
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER,
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) {
2013-02-16 17:23:51 +00:00
console.log("MySQL Connection Failed");
return false;
2013-02-16 05:02:42 +00:00
}
var query = 'SELECT * FROM registrations WHERE uname="{1}" AND pw="{2}"'
.replace(/\{1\}/, name)
.replace(/\{2\}/, sha256);
var results = db.querySync(query);
var rows = results.fetchAllSync();
db.closeSync();
if(rows.length > 0) {
return rows[0];
}
return false;
}