From fe7248e0564e78538ee327b66ca4e95259b4de63 Mon Sep 17 00:00:00 2001 From: Bill Church Date: Thu, 18 Jul 2024 15:59:08 +0000 Subject: [PATCH] feat: Update connectionHandler.js and routes.js to propmpt for basic credentials when accessing `/ssh/host/
` and pre-populate credentials and host info AND auto-connect to server. --- app/connectionHandler.js | 7 +++++-- app/routes.js | 34 ++++++++++++++++++++++++---------- package-lock.json | 13 ++++++++++--- package.json | 1 + 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/app/connectionHandler.js b/app/connectionHandler.js index 5c522c5..849d9c9 100644 --- a/app/connectionHandler.js +++ b/app/connectionHandler.js @@ -23,8 +23,11 @@ function handleConnection(req, res, urlParams) { }, ssh: { host: connectionParams.host || '', - port: connectionParams.port || 22 - } + port: connectionParams.port || 22, + username: connectionParams.username || '', + password: connectionParams.password || '' + }, + autoConnect: !!(connectionParams.host && connectionParams.username && connectionParams.password) }; // Read the client.htm file diff --git a/app/routes.js b/app/routes.js index 7406883..717079e 100644 --- a/app/routes.js +++ b/app/routes.js @@ -1,18 +1,32 @@ // server // /app/routes.js -const express = require('express'); -const path = require('path'); -const router = express.Router(); -const handleConnection = require('./connectionHandler'); +var express = require('express'); +var router = express.Router(); +var handleConnection = require('./connectionHandler'); +var basicAuth = require('basic-auth'); -// Route for host in URL -router.get('/host/:host', (req, res) => { - handleConnection(req, res, { host: req.params.host }); -}); +function auth(req, res, next) { + var credentials = basicAuth(req); + if (!credentials) { + res.setHeader('WWW-Authenticate', 'Basic realm="WebSSH2"'); + return res.status(401).send('Authentication required.'); + } + req.sshCredentials = credentials; + next(); +} -// Default route -router.get('/', (req, res) => { +// Scenario 1: No auth required +router.get('/', function(req, res) { handleConnection(req, res); }); +// Scenario 2: Auth required +router.get('/host/:host', auth, function(req, res) { + handleConnection(req, res, { + host: req.params.host, + username: req.sshCredentials.name, + password: req.sshCredentials.pass + }); +}); + module.exports = router; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 69056e8..145c5fb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -330,6 +330,14 @@ "resolved": "https://registry.npmjs.org/base64id/-/base64id-1.0.0.tgz", "integrity": "sha512-rz8L+d/xByiB/vLVftPkyY215fqNrmasrcJsYkVcm4TgJNz+YXKrFaFAWibSaHkiKoSgMDCb+lipOIRQNGYesw==" }, + "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==", + "requires": { + "safe-buffer": "5.1.2" + } + }, "bcrypt-pbkdf": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", @@ -3675,8 +3683,7 @@ "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==", - "dev": true + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" }, "safe-regex": { "version": "1.1.0", @@ -4722,7 +4729,7 @@ "integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==" }, "webssh2_client": { - "version": "git+ssh://git@github.com/billchurch/webssh2_client.git#86a1131e7ce3a31803c9e6c1c416f88d68e53a01", + "version": "git+ssh://git@github.com/billchurch/webssh2_client.git#ef2d4753306ec3c43247fc76484b5a5b9d928e50", "from": "git+ssh://git@github.com/billchurch/webssh2_client.git" }, "which": { diff --git a/package.json b/package.json index 7e9ef06..3eb0c88 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ }, "dependencies": { "ajv": "^4.11.8", + "basic-auth": "^2.0.1", "body-parser": "^1.15.2", "debug": "~4.1.0", "express": "^4.14.1",