2016-03-12 13:50:43 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var gTokens = {};
|
|
|
|
|
|
|
|
var form = require('form');
|
2016-04-03 15:31:03 -04:00
|
|
|
var http = require('http');
|
2016-03-12 13:50:43 -05:00
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
var gDatabase = new Database("auth");
|
2016-03-12 13:50:43 -05:00
|
|
|
|
|
|
|
function readSession(session) {
|
2021-01-02 13:10:00 -05:00
|
|
|
var result = session ? gDatabase.get("session:" + session) : null;
|
2016-03-12 13:50:43 -05:00
|
|
|
|
|
|
|
if (result) {
|
|
|
|
result = JSON.parse(result);
|
|
|
|
|
|
|
|
let kRefreshInterval = 1 * 60 * 60 * 1000;
|
|
|
|
let now = Date.now();
|
|
|
|
if (!result.lastAccess || result.lastAccess < now - kRefreshInterval) {
|
|
|
|
result.lastAccess = now;
|
|
|
|
writeSession(session, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeSession(session, value) {
|
2021-01-02 13:10:00 -05:00
|
|
|
gDatabase.set("session:" + session, JSON.stringify(value));
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeSession(session, value) {
|
2021-01-02 13:10:00 -05:00
|
|
|
gDatabase.remove("session:" + session);
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function newSession() {
|
|
|
|
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
|
var result = "";
|
|
|
|
for (var i = 0; i < 32; i++) {
|
|
|
|
result += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function verifyPassword(password, hash) {
|
|
|
|
return bCrypt.hashpw(password, hash) == hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
function hashPassword(password) {
|
|
|
|
var salt = bCrypt.gensalt(12);
|
|
|
|
return bCrypt.hashpw(password, salt);
|
|
|
|
}
|
|
|
|
|
|
|
|
function noAdministrator() {
|
|
|
|
return !gGlobalSettings || !gGlobalSettings.permissions || !Object.keys(gGlobalSettings.permissions).some(function(name) {
|
|
|
|
return gGlobalSettings.permissions[name].indexOf("administration") != -1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeAdministrator(name) {
|
|
|
|
if (!gGlobalSettings.permissions) {
|
|
|
|
gGlobalSettings.permissions = {};
|
|
|
|
}
|
|
|
|
if (!gGlobalSettings.permissions[name]) {
|
|
|
|
gGlobalSettings.permissions[name] = [];
|
|
|
|
}
|
|
|
|
if (gGlobalSettings.permissions[name].indexOf("administration") == -1) {
|
|
|
|
gGlobalSettings.permissions[name].push("administration");
|
|
|
|
}
|
|
|
|
setGlobalSettings(gGlobalSettings);
|
|
|
|
}
|
|
|
|
|
|
|
|
function authHandler(request, response) {
|
|
|
|
var session = getCookies(request.headers).session;
|
|
|
|
if (request.uri == "/login") {
|
|
|
|
var sessionIsNew = false;
|
|
|
|
var loginError;
|
|
|
|
|
|
|
|
var formData = form.decodeForm(request.query);
|
|
|
|
|
|
|
|
if (request.method == "POST" || formData.submit) {
|
|
|
|
session = newSession();
|
|
|
|
sessionIsNew = true;
|
2022-01-29 14:05:57 -05:00
|
|
|
formData = form.decodeForm(utf8Decode(request.body), formData);
|
2016-03-12 13:50:43 -05:00
|
|
|
if (formData.submit == "Login") {
|
2021-01-02 13:10:00 -05:00
|
|
|
var account = gDatabase.get("user:" + formData.name);
|
|
|
|
account = account ? JSON.parse(account) : account;
|
2016-03-12 13:50:43 -05:00
|
|
|
if (formData.register == "1") {
|
2021-01-02 13:10:00 -05:00
|
|
|
if (!account &&
|
2016-03-12 13:50:43 -05:00
|
|
|
formData.password == formData.confirm) {
|
|
|
|
writeSession(session, {name: formData.name});
|
2021-01-02 13:10:00 -05:00
|
|
|
account = {password: hashPassword(formData.password)};
|
|
|
|
gDatabase.set("user:" + formData.name, JSON.stringify(account));
|
2016-03-12 13:50:43 -05:00
|
|
|
if (noAdministrator()) {
|
|
|
|
makeAdministrator(formData.name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
loginError = "Error registering account.";
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-02 13:10:00 -05:00
|
|
|
if (account &&
|
|
|
|
account.password &&
|
|
|
|
verifyPassword(formData.password, account.password)) {
|
2016-03-12 13:50:43 -05:00
|
|
|
writeSession(session, {name: formData.name});
|
|
|
|
if (noAdministrator()) {
|
|
|
|
makeAdministrator(formData.name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
loginError = "Invalid username or password.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Proceed as Guest
|
|
|
|
writeSession(session, {name: "guest"});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var cookie = "session=" + session + "; path=/; Max-Age=604800";
|
|
|
|
var entry = readSession(session);
|
|
|
|
if (entry && formData.return) {
|
|
|
|
response.writeHead(303, {"Location": formData.return, "Set-Cookie": cookie});
|
|
|
|
response.end();
|
|
|
|
} else {
|
2021-10-05 21:25:33 -04:00
|
|
|
File.readFile("core/auth.html").then(function(data) {
|
2022-01-26 20:15:54 -05:00
|
|
|
var html = utf8Decode(data);
|
2021-10-05 21:25:33 -04:00
|
|
|
var contents = "";
|
2016-03-12 13:50:43 -05:00
|
|
|
|
2021-10-05 21:25:33 -04:00
|
|
|
if (entry) {
|
|
|
|
if (sessionIsNew) {
|
|
|
|
contents += '<div>Welcome back, ' + entry.name + '.</div>\n';
|
|
|
|
} else {
|
|
|
|
contents += '<div>You are already logged in, ' + entry.name + '.</div>\n';
|
|
|
|
}
|
|
|
|
contents += '<div><a href="/login/logout">Logout</a></div>\n';
|
2016-03-12 13:50:43 -05:00
|
|
|
} else {
|
2021-10-05 21:25:33 -04:00
|
|
|
contents += '<form method="POST">\n';
|
|
|
|
if (loginError) {
|
|
|
|
contents += "<p>" + loginError + "</p>\n";
|
|
|
|
}
|
|
|
|
contents += '<div id="auth_greeting"><b>Halt. Who goes there?</b></div>\n'
|
|
|
|
contents += '<div id="auth">\n';
|
|
|
|
contents += '<div id="auth_login">\n'
|
|
|
|
if (noAdministrator()) {
|
|
|
|
contents += '<div class="notice">There is currently no administrator. You will be made administrator.</div>\n';
|
|
|
|
}
|
|
|
|
contents += '<div><label for="name">Name:</label> <input type="text" id="name" name="name" value=""></div>\n';
|
|
|
|
contents += '<div><label for="password">Password:</label> <input type="password" id="password" name="password" value=""></div>\n';
|
|
|
|
contents += '<div id="confirmPassword" style="display: none"><label for="confirm">Confirm:</label> <input type="password" id="confirm" name="confirm" value=""></div>\n';
|
|
|
|
contents += '<div><input type="checkbox" id="register" name="register" value="1" onchange="showHideConfirm()"> <label for="register">Register a new account</label></div>\n';
|
|
|
|
contents += '<div><input id="loginButton" type="submit" name="submit" value="Login"></div>\n';
|
|
|
|
contents += '</div>';
|
|
|
|
contents += '<div class="auth_or"> - or - </div>';
|
|
|
|
contents += '<div id="auth_guest">\n';
|
|
|
|
contents += '<input id="guestButton" type="submit" name="submit" value="Proceeed as Guest">\n';
|
|
|
|
contents += '</div>\n';
|
|
|
|
contents += '</div>\n';
|
|
|
|
contents += '</form>';
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
2021-10-05 21:25:33 -04:00
|
|
|
var text = html.replace("<!--SESSION-->", contents);
|
|
|
|
response.writeHead(200, {"Content-Type": "text/html; charset=utf-8", "Set-Cookie": cookie, "Content-Length": text.length});
|
|
|
|
response.end(text);
|
|
|
|
}).catch(function(error) {
|
|
|
|
response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Connection": "close"});
|
|
|
|
response.end("404 File not found");
|
|
|
|
});
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
} else if (request.uri == "/login/logout") {
|
|
|
|
removeSession(session);
|
|
|
|
response.writeHead(303, {"Set-Cookie": "session=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT", "Location": "/login" + (request.query ? "?" + request.query : "")});
|
|
|
|
response.end();
|
|
|
|
} else {
|
|
|
|
response.writeHead(200, {"Content-Type": "text/plain; charset=utf-8", "Connection": "close"});
|
|
|
|
response.end("Hello, " + request.client.peerName + ".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPermissions(session) {
|
|
|
|
var permissions;
|
|
|
|
var entry = readSession(session);
|
|
|
|
if (entry) {
|
|
|
|
permissions = getPermissionsForUser(entry.name);
|
|
|
|
permissions.authenticated = entry.name !== "guest";
|
|
|
|
}
|
|
|
|
return permissions || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPermissionsForUser(userName) {
|
|
|
|
var permissions = {};
|
|
|
|
if (gGlobalSettings && gGlobalSettings.permissions && gGlobalSettings.permissions[userName]) {
|
|
|
|
for (var i in gGlobalSettings.permissions[userName]) {
|
|
|
|
permissions[gGlobalSettings.permissions[userName][i]] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return permissions;
|
|
|
|
}
|
|
|
|
|
|
|
|
function query(headers) {
|
|
|
|
var session = getCookies(headers).session;
|
|
|
|
var entry;
|
2021-12-01 18:29:53 -05:00
|
|
|
var autologin = tildefriends.args.autologin;
|
|
|
|
if (entry = autologin ? {name: autologin} : readSession(session)) {
|
|
|
|
return {session: entry, permissions: autologin ? getPermissionsForUser(autologin) : getPermissions(session)};
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.handler = authHandler;
|
|
|
|
exports.query = query;
|