Commit d79dcb95 by Torkel Ödegaard

Refactoring pull request for basic auth improvement

parent bb64e812
......@@ -34,15 +34,19 @@ function (_, crypto) {
settings[key] = typeof options[key] !== 'undefined' ? options[key] : defaults[key];
});
var basicAuth = function(url) {
var passwordAt = url.indexOf('@');
if (passwordAt > 0) {
var userStart = url.indexOf('//') + 2;
var userAndPassword = url.substring(userStart, passwordAt);
var parseBasicAuth = function(datasource) {
var passwordEnd = datasource.url.indexOf('@');
if (passwordEnd > 0) {
var userStart = datasource.url.indexOf('//') + 2;
var userAndPassword = datasource.url.substring(userStart, passwordEnd);
var bytes = crypto.charenc.Binary.stringToBytes(userAndPassword);
var base64 = crypto.util.bytesToBase64(bytes);
return base64;
datasource.basicAuth = crypto.util.bytesToBase64(bytes);
var urlHead = datasource.url.substring(0, userStart);
datasource.url = urlHead + datasource.url.substring(passwordEnd + 1);
}
return datasource;
};
if (options.graphiteUrl) {
......@@ -50,18 +54,17 @@ function (_, crypto) {
graphite: {
name: 'default',
url: options.graphiteUrl,
default: true,
basicAuth: basicAuth(options.graphiteUrl)
default: true
}
};
}
else {
_.each(_.values(settings.datasources), function(source) {
source.basicAuth = basicAuth(source.url);
});
}
settings.elasticsearchBasicAuth = basicAuth(settings.elasticsearch);
_.map(settings.datasources, parseBasicAuth);
var elasticParsed = parseBasicAuth({ url: settings.elasticsearch });
settings.elasticsearchBasicAuth = elasticParsed.basicAuth;
settings.elasticsearch = elasticParsed.url;
return settings;
};
});
......@@ -14,17 +14,9 @@ function (angular, _, $, config, kbn, moment) {
module.factory('GraphiteDatasource', function(dashboard, $q, filterSrv, $http) {
function GraphiteDatasource(datasource) {
var passwordEnd = datasource.url.indexOf('@');
if(passwordEnd > 0) {
var userStart = datasource.url.indexOf('//') + 2;
var urlHead = datasource.url.substring(0,userStart);
this.url = urlHead + datasource.url.substring(passwordEnd);
}
else {
this.url = datasource.url;
}
this.type = 'graphite';
this.basicAuth = datasource.basicAuth;
this.url = datasource.url;
}
GraphiteDatasource.prototype.query = function(options) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment