-
Notifications
You must be signed in to change notification settings - Fork 11
/
auth.go
63 lines (51 loc) · 1.14 KB
/
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
package pulsar
import (
"crypto/md5"
"crypto/tls"
"encoding/hex"
"fmt"
"net/http"
)
type authProvider struct {
AccessID string
AccessKey string
rt http.RoundTripper
}
func NewAuthProvider(accessID, accessKey string) *authProvider {
return &authProvider{
AccessID: accessID,
AccessKey: accessKey,
}
}
func (a *authProvider) GetData() ([]byte, error) {
key := md5Hex(a.AccessID + md5Hex(a.AccessKey))
key = key[8:24]
return []byte(fmt.Sprintf(`{"username":"%s","password":"%s"}`, a.AccessID, key)), nil
}
func md5Hex(s string) string {
h := md5.New()
h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
}
func (a *authProvider) Init() error {
return nil
}
func (a *authProvider) Name() string {
return "auth1"
}
func (a *authProvider) GetTLSCertificate() (*tls.Certificate, error) {
return nil, nil
}
func (a *authProvider) Close() error {
return nil
}
func (a *authProvider) RoundTrip(req *http.Request) (*http.Response, error) {
return a.rt.RoundTrip(req)
}
func (a *authProvider) Transport() http.RoundTripper {
return a.rt
}
func (a *authProvider) WithTransport(tr http.RoundTripper) error {
a.rt = tr
return nil
}