Implement HTTP basic auth feature

pull/864/head
Ivan Bratović 3 years ago
parent 665c263c03
commit 23736549f9

@ -58,6 +58,8 @@ class Monitor extends BeanModel {
method: this.method, method: this.method,
body: this.body, body: this.body,
headers: this.headers, headers: this.headers,
basicauth_user: this.basicauth_user,
basicauth_pass: this.basicauth_pass,
hostname: this.hostname, hostname: this.hostname,
port: this.port, port: this.port,
maxretries: this.maxretries, 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 * Parse to boolean
* @returns {boolean} * @returns {boolean}
@ -141,6 +152,14 @@ class Monitor extends BeanModel {
// Do not do any queries/high loading things before the "bean.ping" // Do not do any queries/high loading things before the "bean.ping"
let startTime = dayjs().valueOf(); 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 = { const options = {
url: this.url, url: this.url,
method: (this.method || "get").toLowerCase(), method: (this.method || "get").toLowerCase(),
@ -150,6 +169,7 @@ class Monitor extends BeanModel {
"Accept": "*/*", "Accept": "*/*",
"User-Agent": "Uptime-Kuma/" + version, "User-Agent": "Uptime-Kuma/" + version,
...(this.headers ? JSON.parse(this.headers) : {}), ...(this.headers ? JSON.parse(this.headers) : {}),
...(basicauthHeader)
}, },
httpsAgent: new https.Agent({ httpsAgent: new https.Agent({
maxCachedSessions: 0, // Use Custom agent to disable session reuse (https://github.com/nodejs/node/issues/3940) maxCachedSessions: 0, // Use Custom agent to disable session reuse (https://github.com/nodejs/node/issues/3940)

@ -575,6 +575,8 @@ exports.entryPage = "dashboard";
bean.method = monitor.method; bean.method = monitor.method;
bean.body = monitor.body; bean.body = monitor.body;
bean.headers = monitor.headers; bean.headers = monitor.headers;
bean.basicauth_user = monitor.basicauth_user;
bean.basicauth_pass = monitor.basicauth_pass;
bean.interval = monitor.interval; bean.interval = monitor.interval;
bean.retryInterval = monitor.retryInterval; bean.retryInterval = monitor.retryInterval;
bean.hostname = monitor.hostname; bean.hostname = monitor.hostname;
@ -1139,6 +1141,8 @@ exports.entryPage = "dashboard";
method: monitorListData[i].method || "GET", method: monitorListData[i].method || "GET",
body: monitorListData[i].body, body: monitorListData[i].body,
headers: monitorListData[i].headers, headers: monitorListData[i].headers,
basicauth_user: monitorListData[i].basicauth_user,
basicauth_pass: monitorListData[i].basicauth_pass,
interval: monitorListData[i].interval, interval: monitorListData[i].interval,
retryInterval: retryInterval, retryInterval: retryInterval,
hostname: monitorListData[i].hostname, hostname: monitorListData[i].hostname,

@ -265,6 +265,15 @@
<label for="headers" class="form-label">{{ $t("Headers") }}</label> <label for="headers" class="form-label">{{ $t("Headers") }}</label>
<textarea id="headers" v-model="monitor.headers" class="form-control" :placeholder="headersPlaceholder"></textarea> <textarea id="headers" v-model="monitor.headers" class="form-control" :placeholder="headersPlaceholder"></textarea>
</div> </div>
<!-- HTTP Basic Auth -->
<h4 class="mt-5 mb-2">{{ $t("HTTP Basic Auth") }}</h4>
<div class="my-3">
<label for="basicauth" class="form-label">{{ $t("Username") }}</label>
<input id="basicauth-user" v-model="monitor.basicauth_user" type="text" class="form-control" :placeholder="Username">
<label for="basicauth" class="form-label">{{ $t("Password") }}</label>
<input id="basicauth-pass" v-model="monitor.basicauth_pass" type="password" class="form-control" :placeholder="Password">
</div>
</template> </template>
</div> </div>
</div> </div>
@ -487,6 +496,7 @@ export default {
this.monitor.headers = JSON.stringify(JSON.parse(this.monitor.headers), null, 4); this.monitor.headers = JSON.stringify(JSON.parse(this.monitor.headers), null, 4);
} }
if (this.isAdd) { if (this.isAdd) {
this.$root.add(this.monitor, async (res) => { this.$root.add(this.monitor, async (res) => {

Loading…
Cancel
Save