From 36226dbbaca3639b88cd17dbeccd84f484abc9c4 Mon Sep 17 00:00:00 2001 From: Kostas Drakontidis Date: Thu, 15 May 2025 10:15:44 +0300 Subject: [PATCH] Added basic auth --- app/samples/app.conf.sample | 2 ++ app/src/javascript/index.js | 4 +--- package-lock.json | 28 ++++++++++++++++++++++++++++ package.json | 1 + server.js | 8 +++++++- 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/app/samples/app.conf.sample b/app/samples/app.conf.sample index 08ae856..4cb0088 100644 --- a/app/samples/app.conf.sample +++ b/app/samples/app.conf.sample @@ -1,6 +1,8 @@ module.exports = { name: "CRTelemetry", port: 80, + username: "admin", + password: "password", logger: { level: "info", error_path: "/app/logs/error.log", diff --git a/app/src/javascript/index.js b/app/src/javascript/index.js index de41b2e..9b8e368 100644 --- a/app/src/javascript/index.js +++ b/app/src/javascript/index.js @@ -49,9 +49,7 @@ setEventListeners(download, () => { }); setEventListeners(logout, () => { - // fetch logout - // check response type - // redirect to auth + window.location.href = `${window.location.protocol}//log:out@${window.location.host}`; }); for (let i = 1; i < sql_rows.length; i++) { diff --git a/package-lock.json b/package-lock.json index 8dcebc2..92e679c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "better-sqlite3": "^11.10.0", "ejs": "^3.1.10", "express": "^5.1.0", + "express-basic-auth": "^1.2.1", "express-fileupload": "^1.5.1", "winston": "^3.17.0" } @@ -116,6 +117,24 @@ ], "license": "MIT" }, + "node_modules/basic-auth": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", + "integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==", + "license": "MIT", + "dependencies": { + "safe-buffer": "5.1.2" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/basic-auth/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "license": "MIT" + }, "node_modules/better-sqlite3": { "version": "11.10.0", "resolved": "https://registry.npmjs.org/better-sqlite3/-/better-sqlite3-11.10.0.tgz", @@ -576,6 +595,15 @@ "url": "https://opencollective.com/express" } }, + "node_modules/express-basic-auth": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/express-basic-auth/-/express-basic-auth-1.2.1.tgz", + "integrity": "sha512-L6YQ1wQ/mNjVLAmK3AG1RK6VkokA1BIY6wmiH304Xtt/cLTps40EusZsU1Uop+v9lTDPxdtzbFmdXfFO3KEnwA==", + "license": "MIT", + "dependencies": { + "basic-auth": "^2.0.1" + } + }, "node_modules/express-fileupload": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/express-fileupload/-/express-fileupload-1.5.1.tgz", diff --git a/package.json b/package.json index 8e5adbe..9029aed 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "better-sqlite3": "^11.10.0", "ejs": "^3.1.10", "express": "^5.1.0", + "express-basic-auth": "^1.2.1", "express-fileupload": "^1.5.1", "winston": "^3.17.0" } diff --git a/server.js b/server.js index 369f06b..222ab54 100644 --- a/server.js +++ b/server.js @@ -3,6 +3,7 @@ const Database = require("better-sqlite3"); const winston = require("winston"); const path = require("path"); const fileUpload = require("express-fileupload"); +const basicAuth = require("express-basic-auth"); const fs = require("fs"); const app_conf = require("./app/config/app.conf.js"); const db_conf = require("./app/config/db.conf.js"); @@ -19,10 +20,15 @@ app.use(app_conf.web_paths.js, express.static(path.join(__dirname, app_conf.web_ app.use(express.json()); app.use(fileUpload({ limits: { fileSize: 102400 }, - useTempFiles: true, tempFileDir: path.join(__dirname, app_conf.temp_directory) })); +app.use(basicAuth({ + users: { + [app_conf.username]: app_conf.password + }, + challenge: true +})); /* * Initialize console and file logger