2013-12-12 14:48:23 -06:00
|
|
|
/**
|
|
|
|
|
* web/auth.js - Webserver functions for user authentication and registration
|
|
|
|
|
*
|
|
|
|
|
* @author Calvin Montgomery <cyzon@cyzon.us>
|
|
|
|
|
*/
|
|
|
|
|
|
2016-07-07 01:11:56 -07:00
|
|
|
var pug = require("pug");
|
2014-01-20 12:42:20 -06:00
|
|
|
var path = require("path");
|
|
|
|
|
var webserver = require("./webserver");
|
2016-07-07 01:11:56 -07:00
|
|
|
var sendPug = require("./pug").sendPug;
|
2014-01-20 12:42:20 -06:00
|
|
|
var Logger = require("../logger");
|
|
|
|
|
var $util = require("../utilities");
|
|
|
|
|
var db = require("../database");
|
2014-02-05 18:05:52 -06:00
|
|
|
var Config = require("../config");
|
2014-02-27 20:50:47 -06:00
|
|
|
var url = require("url");
|
2015-02-15 21:56:00 -06:00
|
|
|
var session = require("../session");
|
2015-02-22 18:15:22 -06:00
|
|
|
var csrf = require("./csrf");
|
2017-04-04 23:02:31 -07:00
|
|
|
|
2017-07-08 20:11:54 -07:00
|
|
|
const LOGGER = require('@calzoneman/jsli')('web/auth');
|
2013-12-12 14:48:23 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Processes a login request. Sets a cookie upon successful authentication
|
|
|
|
|
*/
|
|
|
|
|
function handleLogin(req, res) {
|
2015-02-22 18:15:22 -06:00
|
|
|
csrf.verify(req);
|
|
|
|
|
|
2013-12-12 14:48:23 -06:00
|
|
|
var name = req.body.name;
|
|
|
|
|
var password = req.body.password;
|
2015-02-07 15:13:28 -06:00
|
|
|
var rememberMe = req.body.remember;
|
2015-02-15 21:56:00 -06:00
|
|
|
var dest = req.body.dest || req.header("referer") || null;
|
2015-02-20 23:23:10 -06:00
|
|
|
dest = dest && dest.match(/login|logout/) ? null : dest;
|
2013-12-12 14:48:23 -06:00
|
|
|
|
2014-01-20 12:42:20 -06:00
|
|
|
if (typeof name !== "string" || typeof password !== "string") {
|
2015-02-19 20:30:35 -06:00
|
|
|
res.sendStatus(400);
|
2013-12-12 14:48:23 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-15 21:56:00 -06:00
|
|
|
var host = req.hostname;
|
|
|
|
|
if (host.indexOf(Config.get("http.root-domain")) === -1 &&
|
|
|
|
|
Config.get("http.alt-domains").indexOf(host) === -1) {
|
2017-04-04 23:02:31 -07:00
|
|
|
LOGGER.warn("Attempted login from non-approved domain " + host);
|
2015-02-19 20:30:35 -06:00
|
|
|
return res.sendStatus(403);
|
2015-02-15 21:56:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var expiration;
|
|
|
|
|
if (rememberMe) {
|
|
|
|
|
expiration = new Date("Fri, 31 Dec 9999 23:59:59 GMT");
|
|
|
|
|
} else {
|
|
|
|
|
expiration = new Date(Date.now() + 7*24*60*60*1000);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 14:48:23 -06:00
|
|
|
password = password.substring(0, 100);
|
|
|
|
|
|
2013-12-12 17:09:49 -06:00
|
|
|
db.users.verifyLogin(name, password, function (err, user) {
|
2013-12-12 14:48:23 -06:00
|
|
|
if (err) {
|
2014-01-20 12:42:20 -06:00
|
|
|
if (err === "Invalid username/password combination") {
|
2014-01-27 18:37:48 -06:00
|
|
|
Logger.eventlog.log("[loginfail] Login failed (bad password): " + name
|
2015-10-27 23:54:32 -07:00
|
|
|
+ "@" + req.realIP);
|
2013-12-12 14:48:23 -06:00
|
|
|
}
|
2016-07-07 01:11:56 -07:00
|
|
|
sendPug(res, "login", {
|
2013-12-12 14:48:23 -06:00
|
|
|
loggedIn: false,
|
|
|
|
|
loginError: err
|
|
|
|
|
});
|
2015-02-15 21:56:00 -06:00
|
|
|
return;
|
|
|
|
|
}
|
2014-02-27 20:50:47 -06:00
|
|
|
|
2015-02-15 21:56:00 -06:00
|
|
|
session.genSession(user, expiration, function (err, auth) {
|
|
|
|
|
if (err) {
|
2016-07-07 01:11:56 -07:00
|
|
|
sendPug(res, "login", {
|
2015-02-15 21:56:00 -06:00
|
|
|
loggedIn: false,
|
|
|
|
|
loginError: err
|
|
|
|
|
});
|
|
|
|
|
return;
|
2014-01-20 12:42:20 -06:00
|
|
|
}
|
|
|
|
|
|
Skip full user auth for most page renders
Previously, the user's session cookie was being checked against the
database for all non-static requests. However, this is not really
needed and wastes resources (and is slow).
For most page views (e.g. index, channel page), just parsing the value
of the cookie is sufficient:
* The cookies are already HMAC signed, so tampering with them ought to
be for all reasonable purposes, impossible.
* Assuming the worst case, all a nefarious user could manage to do is
change the text of the "Welcome, {user}" and cause a (non-functional)
ACP link to appear clientside, both of which are already possible by
using the Inspect Element tool.
For authenticated pages (currently, the ACP, and anything under
/account/), the full database check is still performed (for now).
2017-08-01 21:40:26 -07:00
|
|
|
webserver.setAuthCookie(req, res, expiration, auth);
|
2014-02-27 20:50:47 -06:00
|
|
|
|
2015-02-15 21:56:00 -06:00
|
|
|
if (dest) {
|
|
|
|
|
res.redirect(dest);
|
|
|
|
|
} else {
|
Skip full user auth for most page renders
Previously, the user's session cookie was being checked against the
database for all non-static requests. However, this is not really
needed and wastes resources (and is slow).
For most page views (e.g. index, channel page), just parsing the value
of the cookie is sufficient:
* The cookies are already HMAC signed, so tampering with them ought to
be for all reasonable purposes, impossible.
* Assuming the worst case, all a nefarious user could manage to do is
change the text of the "Welcome, {user}" and cause a (non-functional)
ACP link to appear clientside, both of which are already possible by
using the Inspect Element tool.
For authenticated pages (currently, the ACP, and anything under
/account/), the full database check is still performed (for now).
2017-08-01 21:40:26 -07:00
|
|
|
sendPug(res, "login", {
|
|
|
|
|
loggedIn: true,
|
|
|
|
|
loginName: user.name,
|
|
|
|
|
superadmin: user.global_rank >= 255
|
|
|
|
|
});
|
2015-02-15 21:56:00 -06:00
|
|
|
}
|
2014-02-27 20:50:47 -06:00
|
|
|
});
|
2015-02-15 21:56:00 -06:00
|
|
|
});
|
2014-02-27 20:50:47 -06:00
|
|
|
}
|
|
|
|
|
|
2013-12-12 14:48:23 -06:00
|
|
|
/**
|
|
|
|
|
* Handles a GET request for /login
|
|
|
|
|
*/
|
|
|
|
|
function handleLoginPage(req, res) {
|
2014-01-22 21:12:43 -06:00
|
|
|
if (webserver.redirectHttps(req, res)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
Skip full user auth for most page renders
Previously, the user's session cookie was being checked against the
database for all non-static requests. However, this is not really
needed and wastes resources (and is slow).
For most page views (e.g. index, channel page), just parsing the value
of the cookie is sufficient:
* The cookies are already HMAC signed, so tampering with them ought to
be for all reasonable purposes, impossible.
* Assuming the worst case, all a nefarious user could manage to do is
change the text of the "Welcome, {user}" and cause a (non-functional)
ACP link to appear clientside, both of which are already possible by
using the Inspect Element tool.
For authenticated pages (currently, the ACP, and anything under
/account/), the full database check is still performed (for now).
2017-08-01 21:40:26 -07:00
|
|
|
if (res.locals.loggedIn) {
|
2016-07-07 01:11:56 -07:00
|
|
|
return sendPug(res, "login", {
|
2015-02-15 21:56:00 -06:00
|
|
|
wasAlreadyLoggedIn: true
|
|
|
|
|
});
|
2013-12-12 14:48:23 -06:00
|
|
|
}
|
2015-02-15 21:56:00 -06:00
|
|
|
|
2016-10-01 21:31:04 -07:00
|
|
|
var redirect = req.query.dest || req.header("referer");
|
|
|
|
|
var locals = {};
|
|
|
|
|
if (!/\/register/.test(redirect)) {
|
|
|
|
|
locals.redirect = redirect;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendPug(res, "login", locals);
|
2013-12-12 14:48:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handles a request for /logout. Clears auth cookie
|
|
|
|
|
*/
|
|
|
|
|
function handleLogout(req, res) {
|
2015-02-22 18:15:22 -06:00
|
|
|
csrf.verify(req);
|
|
|
|
|
|
2014-01-20 12:42:20 -06:00
|
|
|
res.clearCookie("auth");
|
Skip full user auth for most page renders
Previously, the user's session cookie was being checked against the
database for all non-static requests. However, this is not really
needed and wastes resources (and is slow).
For most page views (e.g. index, channel page), just parsing the value
of the cookie is sufficient:
* The cookies are already HMAC signed, so tampering with them ought to
be for all reasonable purposes, impossible.
* Assuming the worst case, all a nefarious user could manage to do is
change the text of the "Welcome, {user}" and cause a (non-functional)
ACP link to appear clientside, both of which are already possible by
using the Inspect Element tool.
For authenticated pages (currently, the ACP, and anything under
/account/), the full database check is still performed (for now).
2017-08-01 21:40:26 -07:00
|
|
|
res.locals.loggedIn = res.locals.loginName = res.locals.superadmin = false;
|
2014-01-20 12:42:20 -06:00
|
|
|
// Try to find an appropriate redirect
|
2015-11-02 20:52:57 -08:00
|
|
|
var dest = req.body.dest || req.header("referer");
|
2015-02-20 23:23:10 -06:00
|
|
|
dest = dest && dest.match(/login|logout|account/) ? null : dest;
|
2014-01-20 12:42:20 -06:00
|
|
|
|
2014-08-19 00:25:36 -05:00
|
|
|
var host = req.hostname;
|
2014-02-27 20:50:47 -06:00
|
|
|
if (host.indexOf(Config.get("http.root-domain")) !== -1) {
|
2014-03-01 17:37:59 -06:00
|
|
|
res.clearCookie("auth", { domain: Config.get("http.root-domain-dotted") });
|
2014-01-20 12:42:20 -06:00
|
|
|
}
|
|
|
|
|
|
2015-02-15 21:56:00 -06:00
|
|
|
if (dest) {
|
|
|
|
|
res.redirect(dest);
|
2014-01-20 12:42:20 -06:00
|
|
|
} else {
|
2016-07-07 01:11:56 -07:00
|
|
|
sendPug(res, "logout", {});
|
2014-01-20 12:42:20 -06:00
|
|
|
}
|
2013-12-12 14:48:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handles a GET request for /register
|
|
|
|
|
*/
|
|
|
|
|
function handleRegisterPage(req, res) {
|
2014-01-22 21:12:43 -06:00
|
|
|
if (webserver.redirectHttps(req, res)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
Skip full user auth for most page renders
Previously, the user's session cookie was being checked against the
database for all non-static requests. However, this is not really
needed and wastes resources (and is slow).
For most page views (e.g. index, channel page), just parsing the value
of the cookie is sufficient:
* The cookies are already HMAC signed, so tampering with them ought to
be for all reasonable purposes, impossible.
* Assuming the worst case, all a nefarious user could manage to do is
change the text of the "Welcome, {user}" and cause a (non-functional)
ACP link to appear clientside, both of which are already possible by
using the Inspect Element tool.
For authenticated pages (currently, the ACP, and anything under
/account/), the full database check is still performed (for now).
2017-08-01 21:40:26 -07:00
|
|
|
if (res.locals.loggedIn) {
|
2016-07-07 01:11:56 -07:00
|
|
|
sendPug(res, "register", {});
|
2015-02-15 21:56:00 -06:00
|
|
|
return;
|
2013-12-12 14:48:23 -06:00
|
|
|
}
|
2014-02-09 19:52:24 -06:00
|
|
|
|
2016-07-07 01:11:56 -07:00
|
|
|
sendPug(res, "register", {
|
2013-12-12 14:48:23 -06:00
|
|
|
registered: false,
|
|
|
|
|
registerError: false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Processes a registration request.
|
|
|
|
|
*/
|
|
|
|
|
function handleRegister(req, res) {
|
2015-02-22 18:15:22 -06:00
|
|
|
csrf.verify(req);
|
|
|
|
|
|
2013-12-12 14:48:23 -06:00
|
|
|
var name = req.body.name;
|
|
|
|
|
var password = req.body.password;
|
|
|
|
|
var email = req.body.email;
|
2014-01-20 12:42:20 -06:00
|
|
|
if (typeof email !== "string") {
|
|
|
|
|
email = "";
|
2013-12-12 14:48:23 -06:00
|
|
|
}
|
2015-10-27 23:54:32 -07:00
|
|
|
var ip = req.realIP;
|
2013-12-12 14:48:23 -06:00
|
|
|
|
2014-01-20 12:42:20 -06:00
|
|
|
if (typeof name !== "string" || typeof password !== "string") {
|
2015-02-19 20:30:35 -06:00
|
|
|
res.sendStatus(400);
|
2013-12-12 14:48:23 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (name.length === 0) {
|
2016-07-07 01:11:56 -07:00
|
|
|
sendPug(res, "register", {
|
2014-01-20 12:42:20 -06:00
|
|
|
registerError: "Username must not be empty"
|
2013-12-12 14:48:23 -06:00
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-05 18:05:52 -06:00
|
|
|
if (name.match(Config.get("reserved-names.usernames"))) {
|
2016-07-07 01:11:56 -07:00
|
|
|
sendPug(res, "register", {
|
2014-02-05 18:05:52 -06:00
|
|
|
registerError: "That username is reserved"
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 14:48:23 -06:00
|
|
|
if (password.length === 0) {
|
2016-07-07 01:11:56 -07:00
|
|
|
sendPug(res, "register", {
|
2014-01-20 12:42:20 -06:00
|
|
|
registerError: "Password must not be empty"
|
2013-12-12 14:48:23 -06:00
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
password = password.substring(0, 100);
|
|
|
|
|
|
2013-12-26 22:15:54 -05:00
|
|
|
if (email.length > 0 && !$util.isValidEmail(email)) {
|
2016-07-07 01:11:56 -07:00
|
|
|
sendPug(res, "register", {
|
2014-01-20 12:42:20 -06:00
|
|
|
registerError: "Invalid email address"
|
2013-12-12 14:48:23 -06:00
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-25 22:30:24 -05:00
|
|
|
db.users.register(name, password, email, ip, function (err) {
|
2013-12-12 14:48:23 -06:00
|
|
|
if (err) {
|
2016-07-07 01:11:56 -07:00
|
|
|
sendPug(res, "register", {
|
2013-12-12 14:48:23 -06:00
|
|
|
registerError: err
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2014-01-27 18:37:48 -06:00
|
|
|
Logger.eventlog.log("[register] " + ip + " registered account: " + name +
|
2014-01-20 12:42:20 -06:00
|
|
|
(email.length > 0 ? " <" + email + ">" : ""));
|
2016-07-07 01:11:56 -07:00
|
|
|
sendPug(res, "register", {
|
2013-12-12 14:48:23 -06:00
|
|
|
registered: true,
|
|
|
|
|
registerName: name,
|
|
|
|
|
redirect: req.body.redirect
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
/**
|
|
|
|
|
* Initializes auth callbacks
|
|
|
|
|
*/
|
|
|
|
|
init: function (app) {
|
2014-01-20 12:42:20 -06:00
|
|
|
app.get("/login", handleLoginPage);
|
|
|
|
|
app.post("/login", handleLogin);
|
2015-10-26 23:21:09 -07:00
|
|
|
app.post("/logout", handleLogout);
|
2014-01-20 12:42:20 -06:00
|
|
|
app.get("/register", handleRegisterPage);
|
|
|
|
|
app.post("/register", handleRegister);
|
2013-12-12 14:48:23 -06:00
|
|
|
}
|
|
|
|
|
};
|