-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
145 lines (114 loc) · 3.17 KB
/
client.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package edgegrid
import (
"bytes"
"errors"
"net/http"
"net/url"
"strings"
"time"
"github.com/satori/go.uuid"
"gopkg.in/ini.v1"
)
const (
moniker string = "EG1-HMAC-SHA256"
)
// EdgeGrid Api interface to Akamai
type EdgeGrid struct {
host string
clientToken string
clientSecret string
accessToken string
maxBody string
}
// New returns a new instance of Edgecast
func New(host, clientToken, clientSecret, accessToken, maxBody string) *EdgeGrid {
e := new(EdgeGrid)
e.host = host
e.clientToken = clientToken
e.clientSecret = clientSecret
e.accessToken = accessToken
e.maxBody = maxBody
return e
}
// NewFromIni returns a new instance of Edgecast
func NewFromIni(iniFile string) *EdgeGrid {
cfg, err := ini.Load(iniFile)
if err != nil {
panic(err)
}
section, err := cfg.GetSection("default")
if err != nil {
panic(err)
}
host, err := section.GetKey("host")
if err != nil {
panic("Host name not found in config file")
}
clientToken, err := section.GetKey("client_token")
if err != nil {
panic("Client Token not found in config file")
}
clientSecret, err := section.GetKey("client_secret")
if err != nil {
panic("Client Secret not found in config file")
}
accessToken, err := section.GetKey("access_token")
if err != nil {
panic("Acces Token not found in config file")
}
maxBody, err := section.GetKey("max_body")
if err != nil {
panic("Max Body not found in config file")
}
return New(host.String(), clientToken.String(), clientSecret.String(), accessToken.String(), maxBody.String())
}
// Send a api request to Akamai
func (e *EdgeGrid) Send(method, path, body string) (*http.Response, error) {
client := &http.Client{}
method = strings.ToUpper(method)
if method == "POST" && (string(body) == "{}" || string(body) == "") {
return nil, errors.New("found empty data set")
}
r, _ := http.NewRequest(method, e.host+path, bytes.NewBufferString(body))
r.Header.Add("Content-Type", "application/json")
r.Header.Add("Authorization", e.signedRequest(method, path, r.Header, body))
return client.Do(r)
}
func (e *EdgeGrid) signedRequest(method, path string, headers http.Header, body string) string {
t := time.Now().UTC()
timestamp := t.Format("20060102T15:04:05-0700")
joinedPairs := []string{
"client_token=" + e.clientToken,
"access_token=" + e.accessToken,
"timestamp=" + timestamp,
"nonce=" + uuid.NewV4().String(),
}
authHeader := moniker + " " + strings.Join(joinedPairs, ";")
signingKey := ComputeHmac256(timestamp, e.clientSecret)
authHeader += ";signature=" + ComputeHmac256(dataToSign(method, e.host+path, authHeader, headers, body), signingKey)
return authHeader
}
func dataToSign(method, requestURL string, authHeader string, headers http.Header, body string) string {
parsedURL, err := url.Parse(requestURL)
if err != nil {
return ""
}
bodyHash := ""
if method == "POST" && len(body) > 0 {
bodyHash = Compute256(body)
}
dataToSign := []string{
method,
parsedURL.Scheme,
parsedURL.Host,
parsedURL.Path + parsedURL.RawQuery,
"",
bodyHash,
authHeader,
}
var returnString string
for _, k := range dataToSign {
returnString += k + "\t"
}
return strings.TrimSpace(returnString) + ";"
}