Skip to content

Commit

Permalink
Support basic auth for backend requests, #1048
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderzobnin committed Oct 6, 2020
1 parent 03ceeb2 commit d2e9c2b
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpclient
import (
"crypto/tls"
"crypto/x509"
"encoding/base64"
"errors"
"fmt"
"net"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))
}

0 comments on commit d2e9c2b

Please sign in to comment.