-
Notifications
You must be signed in to change notification settings - Fork 1
/
hmac.go
144 lines (135 loc) · 2.99 KB
/
hmac.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
package goutils
import (
"crypto/hmac"
"crypto/sha1"
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
const (
DATE = "auth[date]="
SIG = "&auth[signature]="
FORMAT = "Mon, _2 Jan 2006 15:04:05 GMT"
)
var HMAC Hmac
type Hmac struct {
}
func (h *Hmac) signRequest(urlp string, secret string, t time.Time) string {
u, _ := url.Parse(urlp)
q := u.Query()
parms := make(map[string]string)
parms["method"] = "GET"
parms["date"] = t.Format(FORMAT)
//parms["nonce"] = ""
parms["path"] = u.Path
r, get := h.canonicalRepresentation(parms, q)
mac := hmac.New(sha1.New, []byte(secret))
mac.Write([]byte(r))
expectedMAC := mac.Sum(nil)
date := strings.Replace(url.QueryEscape(parms["date"]), "+", "%20", -1)
x := ""
if len(get) > 0 {
x = "&"
}
f := ""
if len(u.Fragment) > 0 {
f = "#" + u.Fragment
}
start := ""
if u.Scheme != "" {
start = u.Scheme + "://" + u.Host
}
return fmt.Sprintf("%s%s?%s%s%s%x%s%s%s", start, u.Path, DATE, date, SIG, expectedMAC, x, get, f)
}
func (h *Hmac) Validate(urlp string, secret string) bool {
t, _ := h.ValidateTime(urlp, secret)
//fmt.Printf("Validate: " + strconv.Itoa(s) + "\n")
return t
}
func (h *Hmac) ValidateTime(urlp string, secret string) (bool, int) {
p := strings.LastIndex(urlp, DATE)
if p < 0 {
//fmt.Printf("<%s> does not contain <%s>\n", urlp, DATE)
return false, -1
}
s := strings.LastIndex(urlp, SIG)
if s < 0 {
return false, -2
}
times := urlp[p+len(DATE) : s]
tt, err := url.QueryUnescape(times)
if err != nil {
return false, -3
}
t, terr := time.Parse(FORMAT, tt)
if terr != nil {
return false, -4
}
if t.Unix() <= time.Now().UTC().Unix() {
return false, int(t.Unix() - time.Now().UTC().Unix())
}
newu := h.signRequest(urlp, secret, t)
if urlp == newu {
ts, _ := strconv.Atoi(t.Format(time.RFC850))
return true, ts
} else {
return false, -6
}
}
func (h *Hmac) SignUrl(url string, secret string, ttl int) string {
var t time.Time
// 1395756739
if ttl > 1000000000 {
t = time.Unix(int64(ttl), 0)
} else {
t = time.Now().UTC()
t = t.Add(time.Second * time.Duration(ttl))
}
return h.signRequest(url, secret, t)
}
func (h *Hmac) canonicalRepresentation(parms map[string]string, query map[string][]string) (string, string) {
var rep string
rep += strings.ToUpper(parms["method"]) + "\n"
delete(parms, "method")
rep += "date:" + parms["date"] + "\n"
rep += "nonce:" + parms["nonce"] + "\n"
rep += parms["path"]
get := ""
if query != nil && len(query) > 0 {
mk := make([]string, len(query))
var i uint32
i = 0
for key, _ := range query {
mk[i] = key
i++
}
sort.Strings(mk)
first := true
for _, key := range mk {
if key == "auth[date]" {
continue
}
if key == "auth[signature]" {
continue
}
if first {
first = false
rep += "?"
} else {
rep += "&"
get += "&"
}
rep += key
rep += "="
t, _ := url.QueryUnescape(query[key][0])
rep += t
get += key
get += "="
get += url.QueryEscape(query[key][0])
}
}
return rep, get
}