Merge pull request #1411 from thelounge/xpaw/refactor-auth
Refactor authentication flow
This commit is contained in:
commit
bd530552c2
@ -562,7 +562,7 @@ $(function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
sidebar.on("click", "#sign-out", function() {
|
sidebar.on("click", "#sign-out", function() {
|
||||||
socket.emit("sign-out", storage.get("token"));
|
socket.emit("sign-out");
|
||||||
storage.remove("token");
|
storage.remove("token");
|
||||||
|
|
||||||
if (!socket.connected) {
|
if (!socket.connected) {
|
||||||
|
@ -300,13 +300,13 @@ Client.prototype.updateSession = function(token, ip, request) {
|
|||||||
const agent = UAParser(request.headers["user-agent"] || "");
|
const agent = UAParser(request.headers["user-agent"] || "");
|
||||||
let friendlyAgent = "";
|
let friendlyAgent = "";
|
||||||
|
|
||||||
if (agent.browser.name.length) {
|
if (agent.browser.name) {
|
||||||
friendlyAgent = `${agent.browser.name} ${agent.browser.major}`;
|
friendlyAgent = `${agent.browser.name} ${agent.browser.major}`;
|
||||||
} else {
|
} else {
|
||||||
friendlyAgent = "Unknown browser";
|
friendlyAgent = "Unknown browser";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (agent.os.name.length) {
|
if (agent.os.name) {
|
||||||
friendlyAgent += ` on ${agent.os.name} ${agent.os.version}`;
|
friendlyAgent += ` on ${agent.os.name} ${agent.os.version}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,9 +100,10 @@ in ${config.public ? "public" : "private"} mode`);
|
|||||||
|
|
||||||
sockets.on("connect", function(socket) {
|
sockets.on("connect", function(socket) {
|
||||||
if (config.public) {
|
if (config.public) {
|
||||||
auth.call(socket, {});
|
performAuthentication.call(socket, {});
|
||||||
} else {
|
} else {
|
||||||
init(socket);
|
socket.emit("auth", {success: true});
|
||||||
|
socket.on("auth", performAuthentication);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -173,15 +174,9 @@ function index(req, res, next) {
|
|||||||
res.render("index", data);
|
res.render("index", data);
|
||||||
}
|
}
|
||||||
|
|
||||||
function init(socket, client, generateToken) {
|
function initializeClient(socket, client, generateToken, token) {
|
||||||
if (!client) {
|
|
||||||
socket.emit("auth", {success: true});
|
|
||||||
socket.on("auth", auth);
|
|
||||||
} else {
|
|
||||||
socket.emit("authorized");
|
socket.emit("authorized");
|
||||||
|
|
||||||
client.ip = getClientIp(socket.request);
|
|
||||||
|
|
||||||
socket.on("disconnect", function() {
|
socket.on("disconnect", function() {
|
||||||
client.clientDetach(socket.id);
|
client.clientDetach(socket.id);
|
||||||
});
|
});
|
||||||
@ -193,21 +188,25 @@ function init(socket, client, generateToken) {
|
|||||||
client.input(data);
|
client.input(data);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
socket.on(
|
socket.on(
|
||||||
"more",
|
"more",
|
||||||
function(data) {
|
function(data) {
|
||||||
client.more(data);
|
client.more(data);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
socket.on(
|
socket.on(
|
||||||
"conn",
|
"conn",
|
||||||
function(data) {
|
function(data) {
|
||||||
// prevent people from overriding webirc settings
|
// prevent people from overriding webirc settings
|
||||||
data.ip = null;
|
data.ip = null;
|
||||||
data.hostname = null;
|
data.hostname = null;
|
||||||
|
|
||||||
client.connect(data);
|
client.connect(data);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!Helper.config.public && !Helper.config.ldap.enable) {
|
if (!Helper.config.public && !Helper.config.ldap.enable) {
|
||||||
socket.on(
|
socket.on(
|
||||||
"change-password",
|
"change-password",
|
||||||
@ -256,18 +255,21 @@ function init(socket, client, generateToken) {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.on(
|
socket.on(
|
||||||
"open",
|
"open",
|
||||||
function(data) {
|
function(data) {
|
||||||
client.open(socket.id, data);
|
client.open(socket.id, data);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
socket.on(
|
socket.on(
|
||||||
"sort",
|
"sort",
|
||||||
function(data) {
|
function(data) {
|
||||||
client.sort(data);
|
client.sort(data);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
socket.on(
|
socket.on(
|
||||||
"names",
|
"names",
|
||||||
function(data) {
|
function(data) {
|
||||||
@ -294,7 +296,7 @@ function init(socket, client, generateToken) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("sign-out", (token) => {
|
socket.on("sign-out", () => {
|
||||||
delete client.config.sessions[token];
|
delete client.config.sessions[token];
|
||||||
|
|
||||||
client.manager.updateUser(client.name, {
|
client.manager.updateUser(client.name, {
|
||||||
@ -310,16 +312,18 @@ function init(socket, client, generateToken) {
|
|||||||
|
|
||||||
socket.join(client.id);
|
socket.join(client.id);
|
||||||
|
|
||||||
const sendInitEvent = (token) => {
|
const sendInitEvent = (tokenToSend) => {
|
||||||
socket.emit("init", {
|
socket.emit("init", {
|
||||||
active: client.lastActiveChannel,
|
active: client.lastActiveChannel,
|
||||||
networks: client.networks,
|
networks: client.networks,
|
||||||
token: token
|
token: tokenToSend
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
if (generateToken) {
|
if (generateToken) {
|
||||||
client.generateToken((token) => {
|
client.generateToken((newToken) => {
|
||||||
|
token = newToken;
|
||||||
|
|
||||||
client.updateSession(token, getClientIp(socket.request), socket.request);
|
client.updateSession(token, getClientIp(socket.request), socket.request);
|
||||||
|
|
||||||
client.manager.updateUser(client.name, {
|
client.manager.updateUser(client.name, {
|
||||||
@ -336,21 +340,6 @@ function init(socket, client, generateToken) {
|
|||||||
sendInitEvent(null);
|
sendInitEvent(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function reverseDnsLookup(socket, client) {
|
|
||||||
client.ip = getClientIp(socket.request);
|
|
||||||
|
|
||||||
dns.reverse(client.ip, function(err, host) {
|
|
||||||
if (!err && host.length) {
|
|
||||||
client.hostname = host[0];
|
|
||||||
} else {
|
|
||||||
client.hostname = client.ip;
|
|
||||||
}
|
|
||||||
|
|
||||||
init(socket, client);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function localAuth(client, user, password, callback) {
|
function localAuth(client, user, password, callback) {
|
||||||
// If no user is found, or if the client has not provided a password,
|
// If no user is found, or if the client has not provided a password,
|
||||||
@ -408,18 +397,25 @@ function ldapAuth(client, user, password, callback) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function auth(data) {
|
function performAuthentication(data) {
|
||||||
const socket = this;
|
const socket = this;
|
||||||
let client;
|
let client;
|
||||||
|
|
||||||
|
const finalInit = () => initializeClient(socket, client, !!data.remember, data.token || null);
|
||||||
|
|
||||||
const initClient = () => {
|
const initClient = () => {
|
||||||
// If webirc is enabled and we do not know this users IP address,
|
client.ip = getClientIp(socket.request);
|
||||||
// perform reverse dns lookup
|
|
||||||
if (Helper.config.webirc !== null && !client.config.ip) {
|
// If webirc is enabled perform reverse dns lookup
|
||||||
reverseDnsLookup(socket, client);
|
if (Helper.config.webirc === null) {
|
||||||
} else {
|
return finalInit();
|
||||||
init(socket, client, data.remember === "on");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reverseDnsLookup(client.ip, (hostname) => {
|
||||||
|
client.hostname = hostname;
|
||||||
|
|
||||||
|
finalInit();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
if (Helper.config.public) {
|
if (Helper.config.public) {
|
||||||
@ -470,3 +466,13 @@ function auth(data) {
|
|||||||
localAuth(client, data.user, data.password, authCallback);
|
localAuth(client, data.user, data.password, authCallback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function reverseDnsLookup(ip, callback) {
|
||||||
|
dns.reverse(ip, (err, hostnames) => {
|
||||||
|
if (!err && hostnames.length) {
|
||||||
|
return callback(hostnames[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(ip);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user