-
Notifications
You must be signed in to change notification settings - Fork 1
/
v1.go
264 lines (230 loc) · 6.83 KB
/
v1.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package signer
import (
"context"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"hash"
"io"
"net/http"
"net/url"
"sort"
"strings"
"time"
)
var requiredSignedParametersMap = map[string]struct{}{
"acl": {},
"bucketInfo": {},
"location": {},
"stat": {},
"delete": {},
"append": {},
"tagging": {},
"objectMeta": {},
"uploads": {},
"uploadId": {},
"partNumber": {},
"security-token": {},
"position": {},
"response-content-type": {},
"response-content-language": {},
"response-expires": {},
"response-cache-control": {},
"response-content-disposition": {},
"response-content-encoding": {},
"restore": {},
"callback": {},
"callback-var": {},
"versions": {},
"versioning": {},
"versionId": {},
"sequential": {},
"continuation-token": {},
"regionList": {},
"cloudboxes": {},
"symlink": {},
}
const (
// headers
authorizationHeader = "Authorization"
securityTokenHeader = "x-oss-security-token"
dateHeader = "Date"
contentTypeHeader = "Content-Type"
contentMd5Header = "Content-MD5"
ossHeaderPreifx = "x-oss-"
ossDateHeader = "x-oss-date"
//Query
securityTokenQuery = "security-token"
expiresQuery = "Expires"
accessKeyIdQuery = "OSSAccessKeyId"
signatureQuery = "Signature"
defaultExpiresDuration = 15 * time.Minute
)
type SignerV1 struct {
}
func isSubResource(list []string, key string) bool {
for _, k := range list {
if key == k {
return true
}
}
return false
}
func (*SignerV1) calcStringToSign(date string, signingCtx *SigningContext) string {
/*
SignToString =
VERB + "\n"
+ Content-MD5 + "\n"
+ Content-Type + "\n"
+ Date + "\n"
+ CanonicalizedOSSHeaders
+ CanonicalizedResource
Signature = base64(hmac-sha1(AccessKeySecret, SignToString))
*/
request := signingCtx.Request
contentMd5 := request.Header.Get(contentMd5Header)
contentType := request.Header.Get(contentTypeHeader)
//CanonicalizedOSSHeaders
var headers []string
for k := range request.Header {
lowerCaseKey := strings.ToLower(k)
if strings.HasPrefix(lowerCaseKey, ossHeaderPreifx) {
headers = append(headers, lowerCaseKey)
}
}
sort.Strings(headers)
headerItems := make([]string, len(headers))
for i, k := range headers {
headerValues := make([]string, len(request.Header.Values(k)))
for i, v := range request.Header.Values(k) {
headerValues[i] = strings.TrimSpace(v)
}
headerItems[i] = k + ":" + strings.Join(headerValues, ",") + "\n"
}
canonicalizedOSSHeaders := strings.Join(headerItems, "")
//CanonicalizedResource
query := request.URL.Query()
var params []string
for k := range query {
if _, ok := requiredSignedParametersMap[k]; ok {
params = append(params, k)
} else if strings.HasPrefix(k, ossHeaderPreifx) {
params = append(params, k)
} else if isSubResource(signingCtx.SubResource, k) {
params = append(params, k)
}
}
sort.Strings(params)
paramItems := make([]string, len(params))
for i, k := range params {
v := query.Get(k)
if len(v) > 0 {
paramItems[i] = k + "=" + v
} else {
paramItems[i] = k
}
}
subResource := strings.Join(paramItems, "&")
canonicalizedResource := "/"
if signingCtx.Bucket != nil {
canonicalizedResource += *signingCtx.Bucket + "/"
}
if signingCtx.Key != nil {
canonicalizedResource += *signingCtx.Key
}
if subResource != "" {
canonicalizedResource += "?" + subResource
}
// string to Sign
stringToSign :=
request.Method + "\n" +
contentMd5 + "\n" +
contentType + "\n" +
date + "\n" +
canonicalizedOSSHeaders +
canonicalizedResource
//fmt.Printf("stringToSign:%s\n", stringToSign)
return stringToSign
}
func (s *SignerV1) authHeader(ctx context.Context, signingCtx *SigningContext) error {
request := signingCtx.Request
cred := signingCtx.Credentials
// Date
if signingCtx.Time.IsZero() {
signingCtx.Time = time.Now().Add(signingCtx.ClockOffset)
}
datetime := signingCtx.Time.UTC().Format(http.TimeFormat)
request.Header.Set(dateHeader, datetime)
// Credentials information
if cred.SecurityToken != "" {
request.Header.Set(securityTokenHeader, cred.SecurityToken)
}
// StringToSign
stringToSign := s.calcStringToSign(datetime, signingCtx)
signingCtx.StringToSign = stringToSign
// Signature
h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(cred.AccessKeySecret))
if _, err := io.WriteString(h, stringToSign); err != nil {
return err
}
signature := base64.StdEncoding.EncodeToString(h.Sum(nil))
// Authorization header
request.Header.Set(authorizationHeader, fmt.Sprintf("OSS %s:%s", cred.AccessKeyID, signature))
return nil
}
func (s *SignerV1) authQuery(ctx context.Context, signingCtx *SigningContext) error {
request := signingCtx.Request
cred := signingCtx.Credentials
// Date
if signingCtx.Time.IsZero() {
signingCtx.Time = time.Now().UTC().Add(defaultExpiresDuration)
}
datetime := fmt.Sprintf("%v", signingCtx.Time.UTC().Unix())
// Credentials information
query, _ := url.ParseQuery(request.URL.RawQuery)
if cred.SecurityToken != "" {
query.Add(securityTokenQuery, cred.SecurityToken)
request.URL.RawQuery = query.Encode()
}
// StringToSign
stringToSign := s.calcStringToSign(datetime, signingCtx)
signingCtx.StringToSign = stringToSign
// Signature
h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(cred.AccessKeySecret))
if _, err := io.WriteString(h, stringToSign); err != nil {
return err
}
signature := base64.StdEncoding.EncodeToString(h.Sum(nil))
// Authorization query
query.Add(expiresQuery, datetime)
query.Add(accessKeyIdQuery, cred.AccessKeyID)
query.Add(signatureQuery, signature)
request.URL.RawQuery = strings.Replace(query.Encode(), "+", "%20", -1)
return nil
}
func (s *SignerV1) Sign(ctx context.Context, signingCtx *SigningContext) error {
if signingCtx == nil {
return fmt.Errorf("SigningContext is null.")
}
if signingCtx.Credentials == nil || !signingCtx.Credentials.HasKeys() {
return fmt.Errorf("SigningContext.Credentials is null or empty.")
}
if signingCtx.Request == nil {
return fmt.Errorf("SigningContext.Request is null.")
}
if signingCtx.AuthMethodQuery {
return s.authQuery(ctx, signingCtx)
}
return s.authHeader(ctx, signingCtx)
}
func (*SignerV1) IsSignedHeader(h string) bool {
lowerCaseKey := strings.ToLower(h)
if strings.HasPrefix(lowerCaseKey, ossHeaderPreifx) ||
lowerCaseKey == "date" ||
lowerCaseKey == "content-type" ||
lowerCaseKey == "content-md5" {
return true
}
return false
}