refactor: cleanup basic auth code
This commit is contained in:
parent
78dafebb2b
commit
9dfa8031c0
2 changed files with 15 additions and 23 deletions
|
@ -1,6 +1,6 @@
|
||||||
/* jshint esversion: 6, asi: true, node: true */
|
/* jshint esversion: 6, asi: true, node: true */
|
||||||
/* eslint no-unused-expressions: ["error", { "allowShortCircuit": true, "allowTernary": true }],
|
/* eslint no-unused-expressions: ["error", { "allowShortCircuit": true, "allowTernary": true }],
|
||||||
no-console: ["error", { allow: ["warn", "error"] }] */
|
no-console: ["error", { allow: ["warn", "error", "info"] }] */
|
||||||
// app.js
|
// app.js
|
||||||
|
|
||||||
// eslint-disable-next-line import/order
|
// eslint-disable-next-line import/order
|
||||||
|
@ -21,12 +21,7 @@ const session = require('express-session')(config.express);
|
||||||
const appSocket = require('./socket');
|
const appSocket = require('./socket');
|
||||||
const myutil = require('./util');
|
const myutil = require('./util');
|
||||||
|
|
||||||
myutil.setDefaultCredentials(
|
myutil.setDefaultCredentials(config);
|
||||||
config.user.name,
|
|
||||||
config.user.password,
|
|
||||||
config.user.privatekey,
|
|
||||||
config.user.overridebasic
|
|
||||||
);
|
|
||||||
|
|
||||||
// safe shutdown
|
// safe shutdown
|
||||||
let shutdownMode = false;
|
let shutdownMode = false;
|
||||||
|
@ -43,8 +38,7 @@ function safeShutdownGuard(req, res, next) {
|
||||||
// clean stop
|
// clean stop
|
||||||
function stopApp(reason) {
|
function stopApp(reason) {
|
||||||
shutdownMode = false;
|
shutdownMode = false;
|
||||||
// eslint-disable-next-line no-console
|
if (reason) console.info(`Stopping: ${reason}`);
|
||||||
if (reason) console.log(`Stopping: ${reason}`);
|
|
||||||
if (shutdownInterval) clearInterval(shutdownInterval);
|
if (shutdownInterval) clearInterval(shutdownInterval);
|
||||||
io.close();
|
io.close();
|
||||||
server.close();
|
server.close();
|
||||||
|
|
|
@ -5,33 +5,31 @@
|
||||||
const debug = require('debug')('WebSSH2');
|
const debug = require('debug')('WebSSH2');
|
||||||
const Auth = require('basic-auth');
|
const Auth = require('basic-auth');
|
||||||
|
|
||||||
const defaultCredentials = { username: null, password: null, privatekey: null };
|
let defaultCredentials = { username: null, password: null, privatekey: null };
|
||||||
|
|
||||||
exports.setDefaultCredentials = function setDefaultCredentials(
|
exports.setDefaultCredentials = function setDefaultCredentials({
|
||||||
username,
|
name: username,
|
||||||
password,
|
password,
|
||||||
privatekey,
|
privatekey,
|
||||||
overridebasic
|
overridebasic,
|
||||||
) {
|
}) {
|
||||||
defaultCredentials.username = username;
|
defaultCredentials = { username, password, privatekey, overridebasic };
|
||||||
defaultCredentials.password = password;
|
|
||||||
defaultCredentials.privatekey = privatekey;
|
|
||||||
defaultCredentials.overridebasic = overridebasic;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.basicAuth = function basicAuth(req, res, next) {
|
exports.basicAuth = function basicAuth(req, res, next) {
|
||||||
const myAuth = Auth(req);
|
const myAuth = Auth(req);
|
||||||
// If Authorize: Basic header exists and the password isn't blank
|
// If Authorize: Basic header exists and the password isn't blank
|
||||||
// AND config.user.overridebasic is false, extract basic credentials
|
// AND config.user.overridebasic is false, extract basic credentials
|
||||||
// from client
|
// from client]
|
||||||
if (myAuth && myAuth.pass !== '' && !defaultCredentials.overridebasic) {
|
const { username, password, privatekey, overridebasic } = defaultCredentials;
|
||||||
|
if (myAuth && myAuth.pass !== '' && !overridebasic) {
|
||||||
req.session.username = myAuth.name;
|
req.session.username = myAuth.name;
|
||||||
req.session.userpassword = myAuth.pass;
|
req.session.userpassword = myAuth.pass;
|
||||||
debug(`myAuth.name: ${myAuth.name} and password ${myAuth.pass ? 'exists' : 'is blank'}`);
|
debug(`myAuth.name: ${myAuth.name} and password ${myAuth.pass ? 'exists' : 'is blank'}`);
|
||||||
} else {
|
} else {
|
||||||
req.session.username = defaultCredentials.username;
|
req.session.username = username;
|
||||||
req.session.userpassword = defaultCredentials.password;
|
req.session.userpassword = password;
|
||||||
req.session.privatekey = defaultCredentials.privatekey;
|
req.session.privatekey = privatekey;
|
||||||
}
|
}
|
||||||
if (!req.session.userpassword && !req.session.privatekey) {
|
if (!req.session.userpassword && !req.session.privatekey) {
|
||||||
res.statusCode = 401;
|
res.statusCode = 401;
|
||||||
|
|
Loading…
Reference in a new issue