-
Notifications
You must be signed in to change notification settings - Fork 126
/
basic_auth.go
125 lines (98 loc) · 3.43 KB
/
basic_auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package service
import (
"context"
b64 "encoding/base64"
"net/http"
)
type BasicAuthConfig struct {
UserName string
Password string
}
func (a *BasicAuthConfig) AddOption(h HTTP) HTTP {
return &BasicAuthProvider{
userName: a.UserName,
password: a.Password,
HTTP: h,
}
}
type BasicAuthProvider struct {
userName string
password string
HTTP
}
func (ba *BasicAuthProvider) addAuthorizationHeader(headers map[string]string) error {
decodedPassword, err := b64.StdEncoding.DecodeString(ba.password)
if err != nil {
return err
}
encodedAuth := b64.StdEncoding.EncodeToString([]byte(ba.userName + ":" + string(decodedPassword)))
headers["Authorization"] = "basic " + encodedAuth
return nil
}
func (ba *BasicAuthProvider) Get(ctx context.Context, path string, queryParams map[string]interface{}) (*http.Response, error) {
return ba.GetWithHeaders(ctx, path, queryParams, nil)
}
func (ba *BasicAuthProvider) GetWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
headers map[string]string) (*http.Response, error) {
err := ba.populateHeaders(headers)
if err != nil {
return nil, err
}
return ba.HTTP.GetWithHeaders(ctx, path, queryParams, headers)
}
func (ba *BasicAuthProvider) Post(ctx context.Context, path string, queryParams map[string]interface{},
body []byte) (*http.Response, error) {
return ba.PostWithHeaders(ctx, path, queryParams, body, nil)
}
func (ba *BasicAuthProvider) PostWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
body []byte, headers map[string]string) (*http.Response, error) {
err := ba.populateHeaders(headers)
if err != nil {
return nil, err
}
return ba.HTTP.PostWithHeaders(ctx, path, queryParams, body, headers)
}
func (ba *BasicAuthProvider) Put(ctx context.Context, api string, queryParams map[string]interface{}, body []byte) (*http.Response, error) {
return ba.PutWithHeaders(ctx, api, queryParams, body, nil)
}
func (ba *BasicAuthProvider) PutWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
body []byte, headers map[string]string) (*http.Response, error) {
err := ba.populateHeaders(headers)
if err != nil {
return nil, err
}
return ba.HTTP.PutWithHeaders(ctx, path, queryParams, body, headers)
}
func (ba *BasicAuthProvider) Patch(ctx context.Context, path string, queryParams map[string]interface{},
body []byte) (*http.Response, error) {
return ba.PatchWithHeaders(ctx, path, queryParams, body, nil)
}
func (ba *BasicAuthProvider) PatchWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
body []byte, headers map[string]string) (*http.Response, error) {
err := ba.populateHeaders(headers)
if err != nil {
return nil, err
}
return ba.HTTP.PatchWithHeaders(ctx, path, queryParams, body, headers)
}
func (ba *BasicAuthProvider) Delete(ctx context.Context, path string, body []byte) (*http.Response, error) {
return ba.DeleteWithHeaders(ctx, path, body, nil)
}
func (ba *BasicAuthProvider) DeleteWithHeaders(ctx context.Context, path string, body []byte,
headers map[string]string) (*http.Response, error) {
err := ba.populateHeaders(headers)
if err != nil {
return nil, err
}
return ba.HTTP.DeleteWithHeaders(ctx, path, body, headers)
}
func (ba *BasicAuthProvider) populateHeaders(headers map[string]string) error {
if headers == nil {
headers = make(map[string]string)
}
err := ba.addAuthorizationHeader(headers)
if err != nil {
return err
}
return nil
}