forked from tgglv/wc-api-go
/
oauth.go
133 lines (109 loc) · 2.65 KB
/
oauth.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
package auth
import (
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"fmt"
"github.com/dp0h/wc-api-go/options"
"net/url"
"sort"
"strings"
)
const hashAlgorithm string = "SHA256"
// MicroTimerInterface should return string with microtime which will be used for hashing
type MicroTimerInterface interface {
Get() string
}
// OAuth authentication for doing non-SSL requests
type OAuth struct {
URL string
Key string
Secret string
Version string
Method string
Parameters url.Values
Timestamp string
MicroTimer MicroTimerInterface
}
// GetEnrichedQuery which appended OAuth specific ones
func (o *OAuth) GetEnrichedQuery() url.Values {
o.Parameters.Set("oauth_consumer_key", o.Key)
o.Parameters.Set("oauth_timestamp", o.Timestamp)
o.Parameters.Set("oauth_nonce", oAuthNonce(o.MicroTimer.Get()))
o.Parameters.Set("oauth_signature_method", "HMAC-"+hashAlgorithm)
generateSign(o)
return o.Parameters
}
func generateSign(o *OAuth) {
var urlEncoded string
urlEncoded = encode(o.URL)
o.Parameters = normalize(&o.Parameters)
var qs string
qs = queryString(&o.Parameters)
var stringToSign string
stringToSign = o.Method + "&" + urlEncoded + "&" + qs
mac := hmac.New(sha256.New, []byte(o.getSecret()))
mac.Write([]byte(stringToSign))
var sign string
sign = base64.StdEncoding.EncodeToString(mac.Sum(nil))
o.Parameters.Set("oauth_signature", sign)
}
func encode(s string) string {
return url.QueryEscape(s)
}
func normalize(p *url.Values) url.Values {
result := url.Values{}
for k := range *p {
value := encode(p.Get(k))
result.Del(k)
result.Set(encode(k), value)
}
return result
}
func queryString(p *url.Values) string {
var keys []string
for k := range *p {
keys = append(keys, k)
}
sort.Strings(keys)
var result []string
for _, k := range keys {
result = append(result, encode(k+"="+p.Get(k)))
}
return strings.Join(result, "%26")
}
// SetMicrotimer ...
func (o *OAuth) SetMicrotimer(m MicroTimerInterface) {
o.MicroTimer = m
}
// SetMethod ...
func (o *OAuth) SetMethod(method string) {
o.Method = method
}
func (o *OAuth) getSecret() string {
secret := o.Secret
if "v1" != o.Version && "v2" != o.Secret {
secret = secret + "&"
}
return secret
}
func oAuthNonce(mc string) string {
byteHash := sha1.Sum([]byte(mc))
return fmt.Sprintf("%x", byteHash)
}
// SetOptions ...
func (o *OAuth) SetOptions(opt options.Basic) {
o.Key = opt.Key
o.Secret = opt.Secret
o.Version = opt.Options.Version
o.Timestamp = opt.OAuthTimestamp()
}
// SetURL ...
func (o *OAuth) SetURL(url string) {
o.URL = url
}
// SetParameters ...
func (o *OAuth) SetParameters(params url.Values) {
o.Parameters = params
}