diff --git a/package-lock.json b/package-lock.json index e577390..8c38c4a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "basic-auth": "^2.0.1", "body-parser": "^1.20.3", "express": "^4.21.2", "fs": "^0.0.1-security" @@ -31,6 +32,22 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" }, + "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==", + "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==" + }, "node_modules/body-parser": { "version": "1.20.3", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.3.tgz", diff --git a/package.json b/package.json index ff2e6d0..04bb4fb 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "author": "", "license": "ISC", "dependencies": { + "basic-auth": "^2.0.1", "body-parser": "^1.20.3", "express": "^4.21.2", "fs": "^0.0.1-security" diff --git a/server.js b/server.js index f9331bd..ebd1b5e 100644 --- a/server.js +++ b/server.js @@ -2,6 +2,7 @@ const express = require('express'); const bodyParser = require('body-parser'); const fs = require('fs'); const path = require('path'); +const basicAuth = require('basic-auth'); const app = express(); const port = 3044; @@ -15,8 +16,22 @@ if (!fs.existsSync(dataDir)) { fs.mkdirSync(dataDir, { recursive: true }); } -// Endpoint to receive task data and append to file -app.post('/add-task', (req, res) => { +// Authentication middleware +const auth = (req, res, next) => { + const user = basicAuth(req); + const username = 'fredrik'; // Replace with your desired username + const password = 'apa'; // Replace with your desired password + + if (user && user.name === username && user.pass === password) { + return next(); + } else { + res.set('WWW-Authenticate', 'Basic realm="401"'); + return res.status(401).send('Authentication required.'); + } +}; + +// Protect the /add-task endpoint with authentication +app.post('/add-task', auth, (req, res) => { const { subject, description, scheduled } = req.body; const currentDateTime = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');