Repository navigation
Expand file tree
/
Copy pathoauth.go
More file actions
195 lines (161 loc) · 4.61 KB
/
Copy pathoauth.go
File metadata and controls
195 lines (161 loc) · 4.61 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package oauth
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"sort"
"strconv"
"time"
)
type Config struct {
ConsumerKey string `json:"consumerKey"`
ConsumerSecret string `json:"consumerSecret"`
AccessToken string `json:"accessToken"`
AccessTokenSecret string `json:"accessTokenSecret"`
Version string `json:"version"`
}
func NewConfig(consumerKey, consumerSecret, accessToken, accessTokenSecret, version string) Config {
return Config{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
AccessToken: accessToken,
AccessTokenSecret: accessTokenSecret,
Version: version,
}
}
func (config Config) header() *oauthHeader {
return &oauthHeader{
ConsumerKey: config.ConsumerKey,
Nonce: oauthNonce(),
SignatureMethod: "HMAC-SHA1",
Timestamp: strconv.Itoa(int(time.Now().Unix())),
AccessToken: config.AccessToken,
Version: "1.0",
}
}
func (config Config) HashKey() string {
return config.ConsumerSecret + "&" + config.AccessTokenSecret
}
var headerFormat = `OAuth %s="%s", %s="%s", %s="%s", %s="%s", %s="%s", %s="%s", %s="%s"`
type oauthHeader struct {
ConsumerKey string
Nonce string
Signature string
SignatureMethod string
Timestamp string
AccessToken string
Version string
}
func (h *oauthHeader) Map() map[string]string {
return map[string]string{
"oauth_consumer_key": h.ConsumerKey,
"oauth_nonce": h.Nonce,
"oauth_signature_method": h.SignatureMethod,
"oauth_timestamp": h.Timestamp,
"oauth_token": h.AccessToken,
"oauth_version": h.Version,
}
}
func (h *oauthHeader) String() string {
return fmt.Sprintf(headerFormat,
"oauth_consumer_key",
percentEncode(h.ConsumerKey),
"oauth_nonce",
percentEncode(h.Nonce),
"oauth_signature",
percentEncode(h.Signature),
"oauth_signature_method",
percentEncode(h.SignatureMethod),
"oauth_timestamp",
percentEncode(h.Timestamp),
"oauth_token",
percentEncode(h.AccessToken),
"oauth_version",
percentEncode(h.Version))
}
func (config Config) Sign(req *http.Request) error {
header := config.header()
params, err := sortedOauthParameters(req, header)
if err != nil {
return err
}
s := oauthSignatureString(req, params)
header.Signature = oauthSign(s, config.HashKey())
req.Header.Add("Authorization", header.String())
return nil
}
func oauthSign(s string, key string) string {
sha1Hmac := hmac.New(sha1.New, []byte(key))
sha1Hmac.Write([]byte(s))
hash := sha1Hmac.Sum(nil)
return base64.StdEncoding.EncodeToString(hash)
}
func oauthNonce() string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
n := r.Int31()
var b bytes.Buffer
binary.Write(&b, binary.LittleEndian, &n)
return base64.StdEncoding.EncodeToString(b.Bytes())
}
func oauthSignatureString(req *http.Request, params []oauthParam) string {
var signatureString bytes.Buffer
// Method
signatureString.WriteString(req.Method)
signatureString.WriteByte('&')
// URL
signatureString.WriteString(percentEncode(req.URL.Scheme + "://" + req.URL.Host + req.URL.Path))
signatureString.WriteByte('&')
// Parm String
var buf bytes.Buffer
for i, p := range params {
buf.WriteString(p.key)
buf.WriteString("=")
buf.WriteString(p.value)
if i+1 < len(params) {
buf.WriteByte('&')
}
}
signatureString.WriteString(percentEncode(buf.String()))
return signatureString.String()
}
type oauthParam struct{ key, value string }
type paramList []oauthParam
func (p paramList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p paramList) Len() int { return len(p) }
func (p paramList) Less(i, j int) bool { return p[i].key < p[j].key }
func sortedOauthParameters(req *http.Request, h *oauthHeader) ([]oauthParam, error) {
// Gather the needed components for the signing
header := h.Map()
query := req.URL.Query()
var body url.Values
if req.Body != nil {
var err error
bodyBytes, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
body, err = url.ParseQuery(string(bodyBytes))
if err != nil {
return nil, err
}
}
params := make([]oauthParam, 0, len(header)+len(query)+len(body))
for k, v := range body {
params = append(params, oauthParam{key: percentEncode(k), value: percentEncode(v[0])})
}
for k, v := range query {
params = append(params, oauthParam{key: percentEncode(k), value: percentEncode(v[0])})
}
for k, v := range header {
params = append(params, oauthParam{key: percentEncode(k), value: percentEncode(v)})
}
sort.Sort(paramList(params))
return params, nil
}