Fixed updated auth handling

This commit is contained in:
2026-07-26 22:40:07 -04:00
parent ae0ce97b69
commit 350c22887d
2 changed files with 33 additions and 43 deletions
+8 -3
View File
@@ -24,17 +24,19 @@ app.all(/.*/, (req, res) => {
let hazePwHash = null; let hazePwHash = null;
let hazeUser = null; let hazeUser = null;
if (req.query.auth) { if (req.query.auth) {
request({ let api_data = {
...reqOpts, ...reqOpts,
url: reqOpts.url + req.baseUrl, url: reqOpts.url + req.baseUrl,
headers: { headers: {
Authorization: `Basic ${req.query.auth}` Authorization: `Basic ${req.query.auth}`
} }
}, (error, response, body) => { };
request(api_data, (error, response, body) => {
if (error) { if (error) {
res.status(500).send('Internal Server Error'); res.status(500).send('Internal Server Error');
return; return;
} else if (response.statusCode !== 200) { } else if (response.statusCode !== 200) {
console.dir(api_data);
res.status(response.statusCode).send(body); res.status(response.statusCode).send(body);
return; return;
} else { } else {
@@ -42,9 +44,12 @@ app.all(/.*/, (req, res) => {
res.send(body); res.send(body);
} }
}) })
} else {
res.status(401).send('Unauthorized');
return;
} }
}); });
app.listen(port, () => { app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`); console.log(`Server is running on http://localhost:${port}`);
}); });
+25 -40
View File
@@ -1,6 +1,5 @@
const express = require('express'); const express = require('express');
const WebSocket = require('ws'); const WebSocket = require('ws');
const auth = require('express-basic-auth');
const { createHash } = require('crypto'); const { createHash } = require('crypto');
@@ -95,42 +94,29 @@ const hazeTranslator = {
"Bats" "Bats"
] ]
}; };
app.use(auth({
authorizer: (username, password) => {
return true;
}
}));
// Assemble full Haze location data, can take a LONG time // Assemble full Haze location data, can take a LONG time
app.all('/locations_full.geojson', (req, res) => { app.all('/locations_full.geojson', (req, res) => {
// Verify Inputs // Verify Inputs
if ((!req.auth.user || !req.auth.password) && (!req.query.auth)) { if (!req.headers['authorization']) {
res.status(401).send('Unauthorized'); res.status(401).send('Unauthorized');
return; return;
} }
// Setup standard authentication variables
// Setup local variables for request
let hazePwHash = null; let hazePwHash = null;
let hazeUser = null; let hazeUser = null;
let hazeSocketCounter = 0; let authHeader = req.headers['authorization'];
let requestStartTime = new Date().getTime(); let paramAuth = Buffer.from(authHeader.replace('Basic ', ''), 'base64').toString('utf-8');
if (req.query.auth) { hazeUser = paramAuth.split(":")[0];
let paramAuth = Buffer.from(req.query.auth, 'base64').toString('utf-8'); hazePwHash = paramAuth.split(":")[1].replace(/\r?\n|\r/g, '');
hazeUser = paramAuth.split(":")[0]; if (!/\b[A-Fa-f0-9]{64}\b/.test(hazePwHash)) {
hazePwHash = paramAuth.split(":")[1]; hazePwHash = createHash('sha256').update(hazePwHash).digest('hex');
} else {
hazeUser = req.auth.user;
if (/\b[A-Fa-f0-9]{64}\b/.test(req.auth.password)) {
hazePwHash = req.auth.password;
} else {
hazePwHash = createHash('sha256').update(req.auth.password).digest('hex');
}
} }
const haze = new WebSocket('wss://do.ecven.com:8120/explr-mb'); const haze = new WebSocket('wss://do.ecven.com:8120/explr-mb');
let locations = []; let locations = [];
let failCounter = 0; let failCounter = 0;
let queryComplete = false; let queryComplete = false;
console.log(`Starting request at ${new Date().toISOString()} for ${hazeUser}`);
// Socket Message Handlers, defined here to use the local variables above // Socket Message Handlers, defined here to use the local variables above
const sendComplete = () => { const sendComplete = () => {
if (!queryComplete) { if (!queryComplete) {
@@ -234,7 +220,7 @@ app.all('/locations_full.geojson', (req, res) => {
} }
}); });
queryLocation(locations.findIndex(location => location && location.completed === 0)); queryLocation(locations.findIndex(location => location && location.completed === 0));
} else { } else {
console.dir(data); console.dir(data);
res.status(500).send('Failed to retrieve locations'); res.status(500).send('Failed to retrieve locations');
@@ -302,26 +288,25 @@ app.all('/locations_full.geojson', (req, res) => {
// Original API, for only listing locations with minimal data // Original API, for only listing locations with minimal data
app.all('/locations.geojson', (req, res) => { app.all('/locations.geojson', (req, res) => {
// Verify Inputs // Verify Inputs
if ((!req.auth.user || !req.auth.password) && (!req.query.auth)) { if (!req.headers['authorization']) {
res.status(401).send('Unauthorized'); res.status(401).send('Unauthorized');
return; return;
} }
// Query Haze API Websocket // Setup standard authentication variables
let hazePwHash = null; let hazePwHash = null;
let hazeUser = null; let hazeUser = null;
if (req.query.auth) { let authHeader = req.headers['authorization'];
let paramAuth = Buffer.from(req.query.auth, 'base64').toString('utf-8'); let paramAuth = Buffer.from(authHeader.replace('Basic ', ''), 'base64').toString('utf-8');
hawUser = paramAuth.split(":")[0]; hazeUser = paramAuth.split(":")[0];
hazePwHash = paramAuth.split(":")[1]; hazePwHash = paramAuth.split(":")[1].replace(/\r?\n|\r/g, '');
} else { if (!/\b[A-Fa-f0-9]{64}\b/.test(hazePwHash)) {
hazeUser = req.auth.user; hazePwHash = createHash('sha256').update(hazePwHash).digest('hex');
if (/\b[A-Fa-f0-9]{64}\b/.test(req.auth.password)) {
hazePwHash = req.auth.password;
} else {
hazePwHash = createHash('sha256').update(req.auth.password).digest('hex');
}
} }
const haze = new WebSocket('wss://do.ecven.com:8120/explr');
// At this point we need hazeUser as the email and hazePwHas as the sha256 hashed password
// Query Haze API Websocket
console.log(`Starting request at ${new Date().toISOString()} for ${hazeUser}`);
const haze = new WebSocket('wss://do.ecven.com:8120/explr-mb');
haze.addEventListener('message', (event) => { haze.addEventListener('message', (event) => {
const data = JSON.parse(event.data); const data = JSON.parse(event.data);
let haze_authToken = '', haze_username = ''; let haze_authToken = '', haze_username = '';
@@ -345,7 +330,7 @@ app.all('/locations.geojson', (req, res) => {
haze.send('{"event": "getMyLocationsRequest_request","body": [{"higlightImages": true}],"socketMessageId": 0}'); haze.send('{"event": "getMyLocationsRequest_request","body": [{"higlightImages": true}],"socketMessageId": 0}');
} else { } else {
console.dir(req.query); console.dir(req.query);
res.status(401).json({ ...data, password: req.auth.password }); res.status(401).json({ ...data });
haze.close(); haze.close();
res.end(); res.end();
} }
@@ -369,7 +354,7 @@ app.all('/locations.geojson', (req, res) => {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
haze.close(); haze.close();
res.end(JSON.stringify({ res.end(JSON.stringify({
...geonJsonTemplate, ...geoJsonTemplate,
features: hazeLocations features: hazeLocations
})); }));
} else { } else {