Skip to content

Commit

Permalink
🛂 Adds option for env vars for passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
Lissy93 committed Apr 28, 2024
1 parent fd421cd commit 85de40d
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
* */

/* Import built-in Node server modules */
const fs = require('fs');
const os = require('os');
const dns = require('dns');
const http = require('http');
const path = require('path');
const util = require('util');
const dns = require('dns');
const os = require('os');
const fs = require('fs');
const crypto = require('crypto');

/* Import NPM dependencies */
const yaml = require('js-yaml');

/* Import Express + middleware functions */
Expand Down Expand Up @@ -65,7 +67,7 @@ const printWelcomeMessage = () => {
console.log(printMessage(ip, port, isDocker)); // eslint-disable-line no-console
});
} catch (e) {
// Fetching info for welcome message failed, print simple msg instead
// No clue what could of gone wrong here, but print fallback message if above failed
console.log(`Dashy server has started (${port})`); // eslint-disable-line no-console
}
};
Expand All @@ -75,6 +77,7 @@ const printWarning = (msg, error) => {
console.warn(`\x1b[103m\x1b[34m${msg}\x1b[0m\n`, error || ''); // eslint-disable-line no-console
};

/* Load appConfig.auth.users from config (if present) for authorization purposes */
function loadUserConfig() {
try {
const filePath = path.join(__dirname, process.env.USER_DATA_DIR || 'user-data', 'conf.yml');
Expand All @@ -86,13 +89,20 @@ function loadUserConfig() {
}
}

/* If HTTP auth is enabled, and no username/password are pre-set, then check passed credentials */
function customAuthorizer(username, password) {
const sha256 = (input) => crypto.createHash('sha256').update(input).digest('hex').toUpperCase();
const generateUserToken = (user) => {
if (!user.user || (!user.hash && !user.password)) return '';
const strAndUpper = (input) => input.toString().toUpperCase();
const passwordHash = user.hash || sha256(process.env[user.password]);
const sha = sha256(strAndUpper(user.user) + strAndUpper(passwordHash));
return strAndUpper(sha);
};
if (password.startsWith('Bearer ')) {
const token = password.slice('Bearer '.length);
const tokenHash = sha256(token);
const users = loadUserConfig();
return users.some(user => user.hash.toUpperCase() === tokenHash);
return users.some(user => generateUserToken(user) === token);
} else {
const users = loadUserConfig();
const userHash = sha256(password);
Expand Down

0 comments on commit 85de40d

Please sign in to comment.