From c6dbf0450566c40efc4a26f4f0717452b6ef95cd Mon Sep 17 00:00:00 2001 From: Minteck Date: Wed, 10 Aug 2022 10:38:44 +0200 Subject: Initial commit --- server/hornchat.serverlet.authentication.js | 80 +++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 server/hornchat.serverlet.authentication.js (limited to 'server/hornchat.serverlet.authentication.js') diff --git a/server/hornchat.serverlet.authentication.js b/server/hornchat.serverlet.authentication.js new file mode 100644 index 0000000..37fb58b --- /dev/null +++ b/server/hornchat.serverlet.authentication.js @@ -0,0 +1,80 @@ +const fs = require("fs"); +global.rateLimits = {}; + +module.exports = (socket, msg, updateConnectedDevices) => { + if (!updateConnectedDevices) updateConnectedDevices = false; + + let data; + try { + data = JSON.parse(msg); + } catch (e) { + socket.send(JSON.stringify({error:"INVALID_DATA", fatal: true})); + console.log("[" + socket.id + "] Unable to authenticate"); + rateLimits[socket.ip] = new Date(); + socket.close(); + return; + } + + try { + if (data.username && data.token) { + console.log("[" + socket.id + "] Username:", data.username, "Token:", ""); + let currentDevice = null; + + if (userCredentials.filter((i) => i.id === data.username).length > 0) { + if (userCredentials.filter((i) => i.id === data.username)[0]['devices'].length > 0) { + for (let device of userCredentials.filter((i) => i.id === data.username)[0]['devices']) { + if (data.token === device.token) { + currentDevice = device; + global.userCredentials = userCredentials.map((i) => { + if (i.id === data.username) { + i.devices = i.devices.map((j) => { + if (data.token === j.token) { + j.addresses = [...new Set([...j.addresses, socket.ip])]; + j.lastSeen = new Date(); + } + + return j; + }) + } + + return i; + }) + + fs.writeFileSync(dataPath + "/users.json", JSON.stringify(userCredentials, null, 2)); + socket.send(JSON.stringify({device: device.id})); + console.log("[" + socket.id + "] Authenticated successfully"); + + if (updateConnectedDevices) { + if (!connectedDevices[data.username]) connectedDevices[data.username] = []; + connectedDevices[data.username].push(device.id); + } + + socket.authenticated = { + device: device.id, + user: data.username + } + + break; + } + } + } + } else { + socket.send(JSON.stringify({error:"USER_NOT_FOUND", fatal: true})); + console.log("[" + socket.id + "] Unable to authenticate"); + rateLimits[socket.ip] = new Date(); + socket.close(); + } + } else { + socket.send(JSON.stringify({error:"MISSING_OPERAND", fatal: true})); + console.log("[" + socket.id + "] Unable to authenticate"); + rateLimits[socket.ip] = new Date(); + socket.close(); + } + } catch (e) { + console.error(e); + socket.send(JSON.stringify({error:"INTERNAL_ERROR", fatal: true})); + console.log("[" + socket.id + "] Unable to authenticate"); + rateLimits[socket.ip] = new Date(); + socket.close(); + } +} \ No newline at end of file -- cgit