From 23736549f90eb86afcca919e99e06abfe66c3105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Bratovi=C4=87?= Date: Tue, 2 Nov 2021 12:30:44 +0100 Subject: [PATCH] Implement HTTP basic auth feature --- server/model/monitor.js | 20 ++++++++++++++++++++ server/server.js | 4 ++++ src/pages/EditMonitor.vue | 10 ++++++++++ 3 files changed, 34 insertions(+) diff --git a/server/model/monitor.js b/server/model/monitor.js index fc3292317..c80e941e9 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -58,6 +58,8 @@ class Monitor extends BeanModel { method: this.method, body: this.body, headers: this.headers, + basicauth_user: this.basicauth_user, + basicauth_pass: this.basicauth_pass, hostname: this.hostname, port: this.port, maxretries: this.maxretries, @@ -80,6 +82,15 @@ class Monitor extends BeanModel { }; } + /** + * Encode user and password to Base64 encoding + * for HTTP "basic" auth, as per RFC-7617 + * @returns {string} + */ + encodeB64(user, pass) { + return btoa(user + ":" + pass); + } + /** * Parse to boolean * @returns {boolean} @@ -141,6 +152,14 @@ class Monitor extends BeanModel { // Do not do any queries/high loading things before the "bean.ping" let startTime = dayjs().valueOf(); + // HTTP basic auth + let basicauthHeader = {}; + if (this.basicauth_user) { + basicauthHeader = { + "Authorization": "Basic " + this.encodeB64(this.basicauth_user, this.basicauth_pass) + } + } + const options = { url: this.url, method: (this.method || "get").toLowerCase(), @@ -150,6 +169,7 @@ class Monitor extends BeanModel { "Accept": "*/*", "User-Agent": "Uptime-Kuma/" + version, ...(this.headers ? JSON.parse(this.headers) : {}), + ...(basicauthHeader) }, httpsAgent: new https.Agent({ maxCachedSessions: 0, // Use Custom agent to disable session reuse (https://github.com/nodejs/node/issues/3940) diff --git a/server/server.js b/server/server.js index d1fd7ff29..76d456c54 100644 --- a/server/server.js +++ b/server/server.js @@ -575,6 +575,8 @@ exports.entryPage = "dashboard"; bean.method = monitor.method; bean.body = monitor.body; bean.headers = monitor.headers; + bean.basicauth_user = monitor.basicauth_user; + bean.basicauth_pass = monitor.basicauth_pass; bean.interval = monitor.interval; bean.retryInterval = monitor.retryInterval; bean.hostname = monitor.hostname; @@ -1139,6 +1141,8 @@ exports.entryPage = "dashboard"; method: monitorListData[i].method || "GET", body: monitorListData[i].body, headers: monitorListData[i].headers, + basicauth_user: monitorListData[i].basicauth_user, + basicauth_pass: monitorListData[i].basicauth_pass, interval: monitorListData[i].interval, retryInterval: retryInterval, hostname: monitorListData[i].hostname, diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 65c3dad6e..18954968a 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -265,6 +265,15 @@ + + +

{{ $t("HTTP Basic Auth") }}

+
+ + + + +
@@ -487,6 +496,7 @@ export default { this.monitor.headers = JSON.stringify(JSON.parse(this.monitor.headers), null, 4); } + if (this.isAdd) { this.$root.add(this.monitor, async (res) => {