feat: Update connectionHandler.js and routes.js to propmpt for basic credentials when accessing /ssh/host/<address> and pre-populate credentials and host info AND auto-connect to server.

This commit is contained in:
Bill Church 2024-07-18 15:59:08 +00:00
parent e39fb885fd
commit fe7248e056
No known key found for this signature in database
4 changed files with 40 additions and 15 deletions

View file

@ -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

View file

@ -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;

13
package-lock.json generated
View file

@ -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": {

View file

@ -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",