From 05d9c6f83e9c3456e439c1f712ec386182a00a56 Mon Sep 17 00:00:00 2001 From: tadscottsmith <78445808+tadscottsmith@users.noreply.github.com> Date: Wed, 15 Nov 2023 20:23:27 -0600 Subject: [PATCH] Add system authorization check function. --- backend/controllers/systems.js | 34 ++++++++++++++++++++++++++++++++++ backend/index.js | 1 + 2 files changed, 35 insertions(+) diff --git a/backend/controllers/systems.js b/backend/controllers/systems.js index 515feb0..1d24e1d 100644 --- a/backend/controllers/systems.js +++ b/backend/controllers/systems.js @@ -144,3 +144,37 @@ exports.get_systems = async function (req, res) { })); } + exports.authorize_system = async function (req, res) { + + var shortName = req.params.shortName.toLowerCase(); + var apiKey = req.body.api_key; + let item = null; + + try { + item = await System.findOne({ 'shortName': shortName }, ["key"]); + } catch (err) { + console.warn("[" + req.params.shortName + "] Error /:shortName/authorize - Error: " + err); + res.status(500); + res.send("Invalid System Name\n"); + return; + } + + if (!item) { + console.info("[" + req.params.shortName + "] Error /:shortName/authorize ShortName does not exist"); + res.status(500); + res.send("Invalid System Name\n"); + return; + } + + if (apiKey != item.key) { + console.warn("[" + req.params.shortName + "] Error /:shortName/authorize API Key Mismatch - Provided key: " + apiKey); + res.status(403); + res.send("Invalid API Key\n"); + return; + } else { + // System shortName exists and the API Key is valid. + res.status(200).end(); + } + + } + diff --git a/backend/index.js b/backend/index.js index 188e359..b8ee349 100644 --- a/backend/index.js +++ b/backend/index.js @@ -118,6 +118,7 @@ app.post('/:shortName/upload', upload.single('call'), uploads.upload, async func /*------ SYSTEMS ----------*/ app.get('/systems', addSystemClients, systems.get_systems); app.post('/:shortName/contact', systems.contact_system); +app.post('/:shortName/authorize', systems.authorize_system); /*------ TALKGROUPS ----------*/ app.get('/:shortName/talkgroups', talkgroups.get_talkgroups);