forked from rlmcpherson/s3gof3r
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sign.go
139 lines (131 loc) · 3.98 KB
/
sign.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
package s3gof3r
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"io"
"net/http"
"net/url"
"sort"
"strings"
"time"
)
// See Amazon S3 Developer Guide for explanation
// http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html
var paramsToSign = map[string]bool{
"acl": true,
"location": true,
"logging": true,
"notification": true,
"partNumber": true,
"policy": true,
"requestPayment": true,
"torrent": true,
"uploadId": true,
"uploads": true,
"versionId": true,
"versioning": true,
"versions": true,
"response-content-type": true,
"response-content-language": true,
"response-expires": true,
"response-cache-control": true,
"response-content-disposition": true,
"response-content-encoding": true,
}
func (b *Bucket) Sign(req *http.Request) {
if req.Header == nil {
req.Header = http.Header{}
}
if dateHeader := req.Header.Get("Date"); dateHeader == "" {
req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
}
if b.S3.Keys.SecurityToken != "" {
req.Header.Set("X-Amz-Security-Token", b.S3.Keys.SecurityToken)
}
hm := hmac.New(sha1.New, []byte(b.S3.Keys.SecretKey))
b.writeSignature(hm, req)
signature := make([]byte, base64.StdEncoding.EncodedLen(hm.Size()))
base64.StdEncoding.Encode(signature, hm.Sum(nil))
req.Header.Set("Authorization", "AWS "+b.S3.Keys.AccessKey+":"+string(signature))
}
// From Amazon API documentation:
//
// Signature = Base64( HMAC-SHA1( YourSecretAccessKeyID, UTF-8-Encoding-Of( StringToSign ) ) );
//
// StringToSign = HTTP-Verb + "\n" +
// Content-MD5 + "\n" +
// Content-Type + "\n" +
// Date + "\n" +
// CanonicalizedAmzHeaders +
// CanonicalizedResource;
func (b *Bucket) writeSignature(w io.Writer, r *http.Request) {
w.Write([]byte(r.Method))
w.Write([]byte{'\n'})
w.Write([]byte(r.Header.Get("content-md5")))
w.Write([]byte{'\n'})
w.Write([]byte(r.Header.Get("content-type")))
w.Write([]byte{'\n'})
if _, ok := r.Header["X-Amz-Date"]; !ok {
w.Write([]byte(r.Header.Get("date")))
}
r.Header.Set("User-Agent", "S3Gof3r")
w.Write([]byte{'\n'})
b.writeCanonicalizedAmzHeaders(w, r)
b.writeCanonicializedResource(w, r)
}
// See Amazon S3 Developer Guide for explanation
// http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html
func (b *Bucket) writeCanonicalizedAmzHeaders(w io.Writer, r *http.Request) {
var amzHeaders []string
for h := range r.Header {
if strings.HasPrefix(strings.ToLower(h), "x-amz-") {
amzHeaders = append(amzHeaders, h)
}
}
sort.Strings(amzHeaders)
for _, h := range amzHeaders {
v := r.Header[h]
w.Write([]byte(strings.ToLower(h)))
w.Write([]byte(":"))
w.Write([]byte(strings.Join(v, ",")))
w.Write([]byte("\n"))
}
}
// From Amazon API documentation:
//
// CanonicalizedResource = [ "/" + Bucket ] +
// <HTTP-Request-URI, from the protocol name up to the query string> +
// [ subresource, if present. For example "?acl", "?location", "?logging", or "?torrent"];
func (b *Bucket) writeCanonicializedResource(w io.Writer, r *http.Request) {
if !strings.Contains(b.Name, ".") { // handling for bucket names containing periods
w.Write([]byte("/"))
w.Write([]byte(b.Name))
}
u := &url.URL{Path: r.URL.Path}
w.Write([]byte(u.String()))
b.writeSubResource(w, r)
}
// See Amazon S3 Developer Guide for explanation
// http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html
func (b *Bucket) writeSubResource(w io.Writer, r *http.Request) {
var sr []string
for k, vs := range r.URL.Query() {
if paramsToSign[k] {
for _, v := range vs {
if v == "" {
sr = append(sr, k)
} else {
sr = append(sr, k+"="+v)
}
}
}
}
sort.Strings(sr)
var q byte = '?'
for _, s := range sr {
w.Write([]byte{q})
w.Write([]byte(s))
q = '&'
}
}