-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
100 lines (78 loc) · 2.5 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
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
package auroradns
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"net/http"
"time"
)
// TokenTransport HTTP transport for API authentication.
type TokenTransport struct {
apiKey string
secret string
// Transport is the underlying HTTP transport to use when making requests.
// It will default to http.DefaultTransport if nil.
Transport http.RoundTripper
}
// NewTokenTransport Creates a new TokenTransport.
func NewTokenTransport(apiKey, secret string) (*TokenTransport, error) {
if apiKey == "" || secret == "" {
return nil, errors.New("credentials missing")
}
return &TokenTransport{apiKey: apiKey, secret: secret}, nil
}
// RoundTrip executes a single HTTP transaction.
func (t *TokenTransport) RoundTrip(req *http.Request) (*http.Response, error) {
enrichedReq := &http.Request{}
*enrichedReq = *req
enrichedReq.Header = make(http.Header, len(req.Header))
for k, s := range req.Header {
enrichedReq.Header[k] = append([]string(nil), s...)
}
if t.apiKey != "" && t.secret != "" {
timestamp := time.Now().UTC()
fmtTime := timestamp.Format("20060102T150405Z")
enrichedReq.Header.Set("X-AuroraDNS-Date", fmtTime)
token, err := newToken(t.apiKey, t.secret, req.Method, req.URL.Path, timestamp)
if err == nil {
enrichedReq.Header.Set("Authorization", fmt.Sprintf("AuroraDNSv1 %s", token))
}
}
return t.transport().RoundTrip(enrichedReq)
}
// Wrap Wraps an HTTP client Transport with the TokenTransport.
func (t *TokenTransport) Wrap(client *http.Client) *http.Client {
backup := client.Transport
t.Transport = backup
client.Transport = t
return client
}
// Client Creates a new HTTP client.
func (t *TokenTransport) Client() *http.Client {
return &http.Client{
Transport: t,
Timeout: 30 * time.Second,
}
}
func (t *TokenTransport) transport() http.RoundTripper {
if t.Transport != nil {
return t.Transport
}
return http.DefaultTransport
}
// newToken generates a token for accessing a specific method of the API.
func newToken(apiKey, secret, method, action string, timestamp time.Time) (string, error) {
fmtTime := timestamp.Format("20060102T150405Z")
message := method + action + fmtTime
signatureHmac := hmac.New(sha256.New, []byte(secret))
_, err := signatureHmac.Write([]byte(message))
if err != nil {
return "", err
}
signature := base64.StdEncoding.EncodeToString(signatureHmac.Sum(nil))
apiKeyAndSignature := fmt.Sprintf("%s:%s", apiKey, signature)
token := base64.StdEncoding.EncodeToString([]byte(apiKeyAndSignature))
return token, nil
}