Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

πŸ” Updates auth credential priority

Alicia Sykesf77c0f7e6eda
diff --git a/.env b/.env
index 7200bc09..87202c32 100644
--- a/.env
+++ b/.env
@@ -27,16 +27,18 @@
# The path to the user data directory
# USER_DATA_DIR=user-data
-# Enable HTTP basic auth to protect your *.yml config files
+# Enable HTTP auth on all server-side endpoints, using the users defined in conf.yml
+# This is the recommended approach when using Dashy's built-in auth (via appConfig.auth.users)
# ENABLE_HTTP_AUTH=true
-# Enable basic HTTP auth to protect your *.yml config files
-# BASIC_AUTH_USERNAME
-# BASIC_AUTH_PASSWORD
+# Alternatively, protect server-side endpoints with a single static username/password
+# BASIC_AUTH_USERNAME=
+# BASIC_AUTH_PASSWORD=
-# If you'd like frontend to automatically authenticate when basic auth enabled, set credentials here too
-# VUE_APP_BASIC_AUTH_USERNAME
-# VUE_APP_BASIC_AUTH_PASSWORD
+# If using BASIC_AUTH above, set these to the same values to skip the browser auth prompt
+# Requires a rebuild to take effect - only use on trusted networks
+# VUE_APP_BASIC_AUTH_USERNAME=
+# VUE_APP_BASIC_AUTH_PASSWORD=
# Override where the path to the configuration file is, can be a remote URL
# VUE_APP_CONFIG_PATH=/conf.yml
diff --git a/server.js b/server.js
index c183022e..79a87087 100644
--- a/server.js
+++ b/server.js
@@ -89,7 +89,7 @@ function loadUserConfig() {
}
}
-/* If HTTP auth is enabled, and no username/password are pre-set, then check passed credentials */
+/* Authorizer for ENABLE_HTTP_AUTH: validates credentials against conf.yml users */
function customAuthorizer(username, password) {
const sha256 = (input) => crypto.createHash('sha256').update(input).digest('hex').toUpperCase();
const generateUserToken = (user) => {
@@ -102,7 +102,9 @@ function customAuthorizer(username, password) {
if (password.startsWith('Bearer ')) {
const token = password.slice('Bearer '.length);
const users = loadUserConfig();
- return users.some(user => generateUserToken(user) === token);
+ return users.some(user => (
+ user.user.toLowerCase() === username.toLowerCase() && generateUserToken(user) === token
+ ));
} else {
const users = loadUserConfig();
const userHash = sha256(password);
@@ -112,21 +114,33 @@ function customAuthorizer(username, password) {
}
}
-/* If a username and password are set, setup auth for config access, otherwise skip */
+/* If auth is enabled, setup auth for config access, otherwise skip */
function getBasicAuthMiddleware() {
- const configUsers = process.env.ENABLE_HTTP_AUTH ? loadUserConfig() : null;
+ const confUsers = loadUserConfig();
+ const hasConfUsers = confUsers && confUsers.length > 0;
+ const useConfAuth = process.env.ENABLE_HTTP_AUTH && hasConfUsers;
const { BASIC_AUTH_USERNAME, BASIC_AUTH_PASSWORD } = process.env;
- if (BASIC_AUTH_USERNAME && BASIC_AUTH_PASSWORD) {
+ const hasStaticCreds = BASIC_AUTH_USERNAME && BASIC_AUTH_PASSWORD;
+
+ // Warn if both auth methods are configured - they don't work together
+ if (hasStaticCreds && hasConfUsers) {
+ printWarning(useConfAuth
+ ? 'BASIC_AUTH env vars are ignored because ENABLE_HTTP_AUTH is active with conf.yml users.'
+ : 'BASIC_AUTH env vars and appConfig.auth.users are both set but use different credentials.'
+ + ' This will cause auth failures. Set ENABLE_HTTP_AUTH=true, or remove users from conf.yml.');
+ }
+
+ if (useConfAuth) {
return basicAuth({
- users: { [BASIC_AUTH_USERNAME]: BASIC_AUTH_PASSWORD },
+ authorizer: customAuthorizer,
challenge: true,
- unauthorizedResponse: () => 'Unauthorized - Incorrect username or password',
+ unauthorizedResponse: () => 'Unauthorized - Incorrect token',
});
- } else if ((configUsers && configUsers.length > 0)) {
+ } else if (hasStaticCreds) {
return basicAuth({
- authorizer: customAuthorizer,
+ users: { [BASIC_AUTH_USERNAME]: BASIC_AUTH_PASSWORD },
challenge: true,
- unauthorizedResponse: () => 'Unauthorized - Incorrect token',
+ unauthorizedResponse: () => 'Unauthorized - Incorrect username or password',
});
} else {
return (req, res, next) => next();