From d20bd8e82178514584f929d09a6fadb822426128 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 12 Jul 2015 11:32:51 +0300 Subject: [PATCH] Fixed #52 - Added Basic Auth support. --- zabbix/datasource.js | 4 +++- zabbix/zabbixAPIWrapper.js | 25 +++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/zabbix/datasource.js b/zabbix/datasource.js index f00173369..f05186632 100644 --- a/zabbix/datasource.js +++ b/zabbix/datasource.js @@ -23,6 +23,8 @@ function (angular, _, kbn) { function ZabbixAPIDatasource(datasource) { this.name = datasource.name; this.url = datasource.url; + this.basicAuth = datasource.basicAuth; + this.withCredentials = datasource.withCredentials; // TODO: fix passing username and password from config.html this.username = datasource.meta.username; @@ -36,7 +38,7 @@ function (angular, _, kbn) { this.limitmetrics = datasource.meta.limitmetrics || 100; // Initialize Zabbix API - this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password); + this.zabbixAPI = new ZabbixAPI(this.url, this.username, this.password, this.basicAuth, this.withCredentials); } /** diff --git a/zabbix/zabbixAPIWrapper.js b/zabbix/zabbixAPIWrapper.js index 626764a54..9e9fb15b6 100644 --- a/zabbix/zabbixAPIWrapper.js +++ b/zabbix/zabbixAPIWrapper.js @@ -9,11 +9,13 @@ function (angular, _) { module.factory('ZabbixAPI', function($q, backendSrv) { - function ZabbixAPI(api_url, username, password) { + function ZabbixAPI(api_url, username, password, basicAuth, withCredentials) { // Initialize API parameters. - this.url = api_url; - this.username = username; - this.password = password; + this.url = api_url; + this.username = username; + this.password = password; + this.basicAuth = basicAuth; + this.withCredentials = withCredentials; } var p = ZabbixAPI.prototype; @@ -45,6 +47,13 @@ function (angular, _) { } }; + if (this.basicAuth || this.withCredentials) { + options.withCredentials = true; + } + if (this.basicAuth) { + options.headers.Authorization = this.basicAuth; + } + var self = this; return backendSrv.datasourceRequest(options).then(function (response) { if (!response.data) { @@ -88,6 +97,14 @@ function (angular, _) { }, }; + if (this.basicAuth || this.withCredentials) { + options.withCredentials = true; + } + if (this.basicAuth) { + options.headers = options.headers || {}; + options.headers.Authorization = this.basicAuth; + } + return backendSrv.datasourceRequest(options).then(function (result) { if (!result.data) { return null;