This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
signature.go
120 lines (101 loc) · 2.63 KB
/
signature.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
package aftership
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"net/url"
"sort"
"strings"
)
type AuthenticationType int64
const (
APIKey AuthenticationType = iota
AES
)
const (
HeaderAsSignatureHMAC = "as-signature-hmac-sha256"
)
func GetSignature(authenticationType AuthenticationType, secretKey []byte, asHeaders map[string]string, contentType, uri, method, date, body string) (string, string, error) {
canonicalizedAmHeaders := GetCanonicalizedHeaders(asHeaders)
canonicalizedResource, err := GetCanonicalizedResource(uri)
if err != nil {
return "", "", err
}
signString, err := GetSignString(method, body, contentType, date, canonicalizedAmHeaders, canonicalizedResource)
if err != nil {
return "", "", err
}
if authenticationType == AES {
return HeaderAsSignatureHMAC, GetHMACSignature(signString, secretKey), nil
}
return "", "", errors.New("authenticationType incorrect")
}
func GetHMACSignature(signString string, secret []byte) string {
h := hmac.New(sha256.New, secret)
h.Write([]byte(signString))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func GetSignString(method, body, contentType, date, canonicalizedAmHeaders, canonicalizedResource string) (signature string, err error) {
result := ""
result += method + "\n"
if body != "" {
body, err = Md5Encode(body)
if err != nil {
return "", err
}
} else {
contentType = ""
}
result += body + "\n"
result += contentType + "\n"
result += date + "\n"
result += canonicalizedAmHeaders + "\n"
result += canonicalizedResource
return result, nil
}
func GetCanonicalizedHeaders(headers map[string]string) string {
if headers == nil {
return ""
}
keys := make([]string, 0, len(headers))
newHeaders := make(map[string]string, len(headers))
for key, value := range headers {
newKey := strings.ToLower(key)
if !strings.HasPrefix(newKey, "as-") {
continue
}
keys = append(keys, newKey)
newHeaders[newKey] = strings.TrimLeft(value, " ")
}
sort.Strings(keys)
result := make([]string, 0, len(keys))
for _, key := range keys {
result = append(result, key+":"+newHeaders[key])
}
return strings.Join(result, "\n")
}
func GetCanonicalizedResource(rawUrl string) (result string, err error) {
url, err := url.Parse(rawUrl)
if err != nil {
return "", err
}
result += url.Path
params := url.Query().Encode()
if params != "" {
result += "?" + params
}
return result, nil
}
func Md5Encode(source string) (hashed string, err error) {
h := md5.New()
_, err = h.Write([]byte(source))
if err != nil {
return
}
sum := h.Sum(nil)
hashed = strings.ToUpper(hex.EncodeToString(sum))
return
}