From d2e9c2bfd0da750c1f25552c4396b3939087a034 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Tue, 6 Oct 2020 17:22:34 +0300 Subject: [PATCH] Support basic auth for backend requests, #1048 --- pkg/httpclient/httpclient.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/httpclient/httpclient.go b/pkg/httpclient/httpclient.go index 6d7cbb9..991d0e7 100644 --- a/pkg/httpclient/httpclient.go +++ b/pkg/httpclient/httpclient.go @@ -3,6 +3,7 @@ package httpclient import ( "crypto/tls" "crypto/x509" + "encoding/base64" "errors" "fmt" "net" @@ -87,6 +88,13 @@ func getHttpTransport(ds *backend.DataSourceInstanceSettings) (*dataSourceTransp IdleConnTimeout: 90 * time.Second, } + if ds.BasicAuthEnabled { + user := ds.BasicAuthUser + password := ds.DecryptedSecureJSONData["basicAuthPassword"] + basicAuthHeader := getBasicAuthHeader(user, password) + customHeaders["Authorization"] = basicAuthHeader + } + dsTransport := &dataSourceTransport{ headers: customHeaders, transport: transport, @@ -169,3 +177,9 @@ func getCustomHeaders(ds *backend.DataSourceInstanceSettings) map[string]string return headers } + +// getBasicAuthHeader returns a base64 encoded string from user and password. +func getBasicAuthHeader(user string, password string) string { + var userAndPass = user + ":" + password + return "Basic " + base64.StdEncoding.EncodeToString([]byte(userAndPass)) +}