diff options
author | Minteck <contact@minteck.org> | 2022-08-10 10:38:44 +0200 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-08-10 10:38:44 +0200 |
commit | c6dbf0450566c40efc4a26f4f0717452b6ef95cd (patch) | |
tree | b4be2d508223820d0a77d5a3e35e82684da3b6ec /server/hornchat.serverlet.authentication.js | |
download | hornchat-c6dbf0450566c40efc4a26f4f0717452b6ef95cd.tar.gz hornchat-c6dbf0450566c40efc4a26f4f0717452b6ef95cd.tar.bz2 hornchat-c6dbf0450566c40efc4a26f4f0717452b6ef95cd.zip |
Diffstat (limited to 'server/hornchat.serverlet.authentication.js')
-rw-r--r-- | server/hornchat.serverlet.authentication.js | 80 |
1 files changed, 80 insertions, 0 deletions
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:", "<redacted>"); + 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 |