Skip to content

Commit

Permalink
Fixed #52 - Added Basic Auth support.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderzobnin committed Jul 12, 2015
1 parent d716c26 commit d20bd8e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
4 changes: 3 additions & 1 deletion zabbix/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand Down
25 changes: 21 additions & 4 deletions zabbix/zabbixAPIWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d20bd8e

Please sign in to comment.