2013-12-12 20:48:23 +00:00
|
|
|
/**
|
|
|
|
* web/auth.js - Webserver functions for user authentication and registration
|
|
|
|
*
|
|
|
|
* @author Calvin Montgomery <cyzon@cyzon.us>
|
|
|
|
*/
|
|
|
|
|
2014-01-20 18:42:20 +00:00
|
|
|
var jade = require("jade");
|
|
|
|
var fs = require("fs");
|
|
|
|
var path = require("path");
|
|
|
|
var webserver = require("./webserver");
|
2014-02-25 00:25:49 +00:00
|
|
|
var cookieall = webserver.cookieall;
|
2014-01-20 18:42:20 +00:00
|
|
|
var sendJade = require("./jade").sendJade;
|
|
|
|
var Logger = require("../logger");
|
|
|
|
var $util = require("../utilities");
|
|
|
|
var db = require("../database");
|
2014-02-06 00:05:52 +00:00
|
|
|
var Config = require("../config");
|
2013-12-12 20:48:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes a login request. Sets a cookie upon successful authentication
|
|
|
|
*/
|
|
|
|
function handleLogin(req, res) {
|
|
|
|
var name = req.body.name;
|
|
|
|
var password = req.body.password;
|
|
|
|
|
2014-01-20 18:42:20 +00:00
|
|
|
if (typeof name !== "string" || typeof password !== "string") {
|
2013-12-12 20:48:23 +00:00
|
|
|
res.send(400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
password = password.substring(0, 100);
|
|
|
|
|
2013-12-12 23:09:49 +00:00
|
|
|
db.users.verifyLogin(name, password, function (err, user) {
|
2013-12-12 20:48:23 +00:00
|
|
|
if (err) {
|
2014-01-20 18:42:20 +00:00
|
|
|
if (err === "Invalid username/password combination") {
|
2014-01-28 00:37:48 +00:00
|
|
|
Logger.eventlog.log("[loginfail] Login failed (bad password): " + name
|
2014-01-20 18:42:20 +00:00
|
|
|
+ "@" + webserver.ipForRequest(req));
|
2013-12-12 20:48:23 +00:00
|
|
|
}
|
2014-01-20 18:42:20 +00:00
|
|
|
sendJade(res, "login", {
|
2013-12-12 20:48:23 +00:00
|
|
|
loggedIn: false,
|
|
|
|
loginError: err
|
|
|
|
});
|
|
|
|
} else {
|
2014-02-25 00:25:49 +00:00
|
|
|
cookieall(res, "auth", user.name + ":" + user.hash, {
|
2014-01-25 22:39:16 +00:00
|
|
|
expires: new Date(Date.now() + 7*24*60*60*1000),
|
2013-12-12 20:48:23 +00:00
|
|
|
httpOnly: true
|
|
|
|
});
|
2014-01-20 18:42:20 +00:00
|
|
|
|
2014-02-25 00:25:49 +00:00
|
|
|
cookieall(res, "rank", user.global_rank, {
|
2014-02-16 05:17:31 +00:00
|
|
|
expires: new Date(Date.now() + 7*24*60*60*1000),
|
|
|
|
httpOnly: true
|
|
|
|
});
|
|
|
|
|
2014-01-20 18:42:20 +00:00
|
|
|
// Try to find an appropriate redirect
|
|
|
|
var ref = req.header("referrer");
|
|
|
|
if (!ref) {
|
|
|
|
ref = req.body.redirect;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof ref !== "string") {
|
|
|
|
ref = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ref.match(/login|logout/)) {
|
|
|
|
ref = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ref) {
|
|
|
|
res.redirect(ref);
|
|
|
|
} else {
|
|
|
|
sendJade(res, "login", {
|
|
|
|
loggedIn: true,
|
|
|
|
loginName: user.name
|
|
|
|
});
|
|
|
|
}
|
2013-12-12 20:48:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a GET request for /login
|
|
|
|
*/
|
|
|
|
function handleLoginPage(req, res) {
|
2014-01-23 03:12:43 +00:00
|
|
|
if (webserver.redirectHttps(req, res)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-12 20:48:23 +00:00
|
|
|
if (req.cookies.auth) {
|
2014-01-20 18:42:20 +00:00
|
|
|
var split = req.cookies.auth.split(":");
|
2013-12-12 20:48:23 +00:00
|
|
|
if (split.length === 2) {
|
2014-01-20 18:42:20 +00:00
|
|
|
sendJade(res, "login", {
|
2013-12-12 20:48:23 +00:00
|
|
|
wasAlreadyLoggedIn: true,
|
|
|
|
loggedIn: true,
|
|
|
|
loginName: split[0]
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-01-20 18:42:20 +00:00
|
|
|
sendJade(res, "login", {
|
2013-12-27 16:08:03 +00:00
|
|
|
loggedIn: false,
|
2014-01-20 18:42:20 +00:00
|
|
|
redirect: req.header("Referrer")
|
2013-12-12 20:48:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a request for /logout. Clears auth cookie
|
|
|
|
*/
|
|
|
|
function handleLogout(req, res) {
|
2014-01-20 18:42:20 +00:00
|
|
|
res.clearCookie("auth");
|
|
|
|
// Try to find an appropriate redirect
|
|
|
|
var ref = req.header("referrer");
|
|
|
|
if (!ref) {
|
|
|
|
ref = req.body.redirect;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof ref !== "string") {
|
|
|
|
ref = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ref.match(/login|logout/)) {
|
|
|
|
ref = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ref) {
|
|
|
|
res.redirect(ref);
|
|
|
|
} else {
|
|
|
|
sendJade(res, "logout", {});
|
|
|
|
}
|
2013-12-12 20:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a GET request for /register
|
|
|
|
*/
|
|
|
|
function handleRegisterPage(req, res) {
|
2014-01-23 03:12:43 +00:00
|
|
|
if (webserver.redirectHttps(req, res)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-12 20:48:23 +00:00
|
|
|
if (req.cookies.auth) {
|
2014-01-20 18:42:20 +00:00
|
|
|
var split = req.cookies.auth.split(":");
|
2013-12-12 20:48:23 +00:00
|
|
|
if (split.length === 2) {
|
2014-01-20 18:42:20 +00:00
|
|
|
sendJade(res, "register", {
|
2013-12-12 20:48:23 +00:00
|
|
|
loggedIn: true,
|
|
|
|
loginName: split[0]
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:52:24 +00:00
|
|
|
|
2014-01-20 18:42:20 +00:00
|
|
|
sendJade(res, "register", {
|
2013-12-12 20:48:23 +00:00
|
|
|
registered: false,
|
|
|
|
registerError: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes a registration request.
|
|
|
|
*/
|
|
|
|
function handleRegister(req, res) {
|
|
|
|
var name = req.body.name;
|
|
|
|
var password = req.body.password;
|
|
|
|
var email = req.body.email;
|
2014-01-20 18:42:20 +00:00
|
|
|
if (typeof email !== "string") {
|
|
|
|
email = "";
|
2013-12-12 20:48:23 +00:00
|
|
|
}
|
|
|
|
var ip = webserver.ipForRequest(req);
|
|
|
|
|
2014-01-20 18:42:20 +00:00
|
|
|
if (typeof name !== "string" || typeof password !== "string") {
|
2013-12-12 20:48:23 +00:00
|
|
|
res.send(400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name.length === 0) {
|
2014-01-20 18:42:20 +00:00
|
|
|
sendJade(res, "register", {
|
|
|
|
registerError: "Username must not be empty"
|
2013-12-12 20:48:23 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-06 00:05:52 +00:00
|
|
|
if (name.match(Config.get("reserved-names.usernames"))) {
|
|
|
|
sendJade(res, "register", {
|
|
|
|
registerError: "That username is reserved"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-12 20:48:23 +00:00
|
|
|
if (password.length === 0) {
|
2014-01-20 18:42:20 +00:00
|
|
|
sendJade(res, "register", {
|
|
|
|
registerError: "Password must not be empty"
|
2013-12-12 20:48:23 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
password = password.substring(0, 100);
|
|
|
|
|
2013-12-27 03:15:54 +00:00
|
|
|
if (email.length > 0 && !$util.isValidEmail(email)) {
|
2014-01-20 18:42:20 +00:00
|
|
|
sendJade(res, "register", {
|
|
|
|
registerError: "Invalid email address"
|
2013-12-12 20:48:23 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-26 03:30:24 +00:00
|
|
|
db.users.register(name, password, email, ip, function (err) {
|
2013-12-12 20:48:23 +00:00
|
|
|
if (err) {
|
2014-01-20 18:42:20 +00:00
|
|
|
sendJade(res, "register", {
|
2013-12-12 20:48:23 +00:00
|
|
|
registerError: err
|
|
|
|
});
|
|
|
|
} else {
|
2014-01-28 00:37:48 +00:00
|
|
|
Logger.eventlog.log("[register] " + ip + " registered account: " + name +
|
2014-01-20 18:42:20 +00:00
|
|
|
(email.length > 0 ? " <" + email + ">" : ""));
|
|
|
|
sendJade(res, "register", {
|
2013-12-12 20:48:23 +00:00
|
|
|
registered: true,
|
|
|
|
registerName: name,
|
|
|
|
redirect: req.body.redirect
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
/**
|
|
|
|
* Initializes auth callbacks
|
|
|
|
*/
|
|
|
|
init: function (app) {
|
2014-01-20 18:42:20 +00:00
|
|
|
app.get("/login", handleLoginPage);
|
|
|
|
app.post("/login", handleLogin);
|
|
|
|
app.get("/logout", handleLogout);
|
|
|
|
app.get("/register", handleRegisterPage);
|
|
|
|
app.post("/register", handleRegister);
|
2013-12-12 20:48:23 +00:00
|
|
|
}
|
|
|
|
};
|