@@ -2,6 +2,7 @@ package provider
2
2
3
3
import (
4
4
"crypto/tls"
5
+ "encoding/base64"
5
6
"fmt"
6
7
"io"
7
8
"math"
@@ -25,6 +26,21 @@ type HTTP struct {
25
26
jq * gojq.Query
26
27
}
27
28
29
+ // Auth is the authorization config
30
+ type Auth struct {
31
+ Type , User , Password string
32
+ }
33
+
34
+ // NewAuth creates authorization headers from config
35
+ func NewAuth (log * util.Logger , auth Auth , headers map [string ]string ) {
36
+ if strings .ToLower (auth .Type ) != "basic" {
37
+ log .FATAL .Fatalf ("config: unsupported auth type: %s" , auth .Type )
38
+ }
39
+
40
+ basicAuth := auth .User + ":" + auth .Password
41
+ headers ["Authorization" ] = "Basic " + base64 .StdEncoding .EncodeToString ([]byte (basicAuth ))
42
+ }
43
+
28
44
// NewHTTPProviderFromConfig creates a HTTP provider
29
45
func NewHTTPProviderFromConfig (log * util.Logger , other map [string ]interface {}) * HTTP {
30
46
cc := struct {
@@ -34,6 +50,7 @@ func NewHTTPProviderFromConfig(log *util.Logger, other map[string]interface{}) *
34
50
Jq string
35
51
Scale float64
36
52
Insecure bool
53
+ Auth Auth
37
54
}{}
38
55
util .DecodeOther (log , other , & cc )
39
56
@@ -49,6 +66,14 @@ func NewHTTPProviderFromConfig(log *util.Logger, other map[string]interface{}) *
49
66
scale : cc .Scale ,
50
67
}
51
68
69
+ // handle basic auth
70
+ if cc .Auth .Type != "" {
71
+ if p .headers == nil {
72
+ p .headers = make (map [string ]string )
73
+ }
74
+ NewAuth (log , cc .Auth , p .headers )
75
+ }
76
+
52
77
// ignore the self signed certificate
53
78
if cc .Insecure {
54
79
customTransport := http .DefaultTransport .(* http.Transport ).Clone ()
0 commit comments