108 lines
2.7 KiB
Go
108 lines
2.7 KiB
Go
package signature
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha1"
|
|
"encoding/base64"
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
var b64 = base64.StdEncoding
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// S3 signing (http://goo.gl/G1LrK)
|
|
|
|
var s3ParamsToSign = 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 makeSign(accessKey, secretKey string, req *http.Request) (string, error) {
|
|
var md5, ctype, date, xamz string
|
|
var xamzDate bool
|
|
var sarray []string
|
|
headers := req.Header
|
|
req.ParseForm()
|
|
params := req.Form
|
|
method := req.Method
|
|
canonicalPath := req.URL.EscapedPath()
|
|
for k, v := range headers {
|
|
k = strings.ToLower(k)
|
|
switch k {
|
|
case "content-md5":
|
|
md5 = v[0]
|
|
case "content-type":
|
|
ctype = v[0]
|
|
case "date":
|
|
if !xamzDate {
|
|
date = v[0]
|
|
}
|
|
default:
|
|
if strings.HasPrefix(k, "x-amz-") {
|
|
vall := strings.Join(v, ",")
|
|
sarray = append(sarray, k+":"+vall)
|
|
if k == "x-amz-date" {
|
|
xamzDate = true
|
|
date = ""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if len(sarray) > 0 {
|
|
sort.StringSlice(sarray).Sort()
|
|
xamz = strings.Join(sarray, "\n") + "\n"
|
|
}
|
|
|
|
// expires := false
|
|
if v, ok := params["Expires"]; ok {
|
|
// Query string request authentication alternative.
|
|
// expires = true
|
|
date = v[0]
|
|
params["HSCAccessKeyId"] = []string{accessKey}
|
|
}
|
|
|
|
sarray = sarray[0:0]
|
|
for k, v := range params {
|
|
if s3ParamsToSign[k] {
|
|
for _, vi := range v {
|
|
if vi == "" {
|
|
sarray = append(sarray, k)
|
|
} else {
|
|
// "When signing you do not encode these values."
|
|
sarray = append(sarray, k+"="+vi)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if len(sarray) > 0 {
|
|
sort.StringSlice(sarray).Sort()
|
|
canonicalPath = canonicalPath + "?" + strings.Join(sarray, "&")
|
|
}
|
|
|
|
payload := method + "\n" + md5 + "\n" + ctype + "\n" + date + "\n" + xamz + canonicalPath
|
|
hash := hmac.New(sha1.New, []byte(secretKey))
|
|
hash.Write([]byte(payload))
|
|
signature := make([]byte, b64.EncodedLen(hash.Size()))
|
|
b64.Encode(signature, hash.Sum(nil))
|
|
return string(signature), nil
|
|
}
|