forked from dghubble/oauth1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auther_test.go
294 lines (275 loc) · 9.05 KB
/
auther_test.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package oauth1
import (
"net/http"
"net/url"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCommonOAuthParams(t *testing.T) {
cases := []struct {
auther *auther
expectedParams map[string]string
}{
{
&auther{
&Config{
ConsumerKey: "some_consumer_key",
Noncer: &fixedNoncer{"some_nonce"},
},
&fixedClock{time.Unix(50037133, 0)},
},
map[string]string{
"oauth_consumer_key": "some_consumer_key",
"oauth_signature_method": "HMAC-SHA1",
"oauth_timestamp": "50037133",
"oauth_nonce": "some_nonce",
"oauth_version": "1.0",
},
},
{
&auther{
&Config{
ConsumerKey: "some_consumer_key",
Realm: "photos",
Noncer: &fixedNoncer{"some_nonce"},
},
&fixedClock{time.Unix(50037133, 0)},
},
map[string]string{
"oauth_consumer_key": "some_consumer_key",
"oauth_signature_method": "HMAC-SHA1",
"oauth_timestamp": "50037133",
"oauth_nonce": "some_nonce",
"oauth_version": "1.0",
"realm": "photos",
},
},
}
for _, c := range cases {
assert.Equal(t, c.expectedParams, c.auther.commonOAuthParams())
}
}
func TestNonce(t *testing.T) {
auther := newAuther(nil)
nonce := auther.nonce()
// assert that 32 bytes (256 bites) become 44 bytes since a base64 byte
// zeros the 2 high bits. 3 bytes convert to 4 base64 bytes, 40 base64 bytes
// represent the first 30 of 32 bytes, = padding adds another 4 byte group.
// base64 bytes = 4 * floor(bytes/3) + 4
assert.Equal(t, 44, len([]byte(nonce)))
}
func TestEpoch(t *testing.T) {
a := newAuther(nil)
// assert that a real time is used by default
assert.InEpsilon(t, time.Now().Unix(), a.epoch(), 1)
// assert that the fixed clock can be used for testing
a = &auther{clock: &fixedClock{time.Unix(50037133, 0)}}
assert.Equal(t, int64(50037133), a.epoch())
}
func TestSigner_Default(t *testing.T) {
config := &Config{ConsumerSecret: "consumer_secret"}
a := newAuther(config)
// echo -n "hello world" | openssl dgst -sha1 -hmac "consumer_secret&token_secret" -binary | base64
expectedSignature := "BE0uILOruKfSXd4UzYlLJDfOq08="
// assert that the default signer produces the expected HMAC-SHA1 digest
method := a.signer().Name()
digest, err := a.signer().Sign("token_secret", "hello world")
assert.Nil(t, err)
assert.Equal(t, "HMAC-SHA1", method)
assert.Equal(t, expectedSignature, digest)
}
func TestSigner_SHA256(t *testing.T) {
config := &Config{
Signer: &HMAC256Signer{ConsumerSecret: "consumer_secret"},
}
a := newAuther(config)
// echo -n "hello world" | openssl dgst -sha256 -hmac "consumer_secret&token_secret" -binary | base64
expectedSignature := "pW9drXUyErU8DASWbsP2I3XZbju37AW+VzcGdYSeMo8="
// assert that the signer produces the expected HMAC-SHA256 digest
method := a.signer().Name()
digest, err := a.signer().Sign("token_secret", "hello world")
assert.Nil(t, err)
assert.Equal(t, "HMAC-SHA256", method)
assert.Equal(t, expectedSignature, digest)
}
type identitySigner struct{}
func (s *identitySigner) Name() string {
return "identity"
}
func (s *identitySigner) Sign(tokenSecret, message string) (string, error) {
return message, nil
}
func TestSigner_Custom(t *testing.T) {
config := &Config{
ConsumerSecret: "consumer_secret",
Signer: &identitySigner{},
}
a := newAuther(config)
// assert that the custom signer is used
method := a.signer().Name()
digest, err := a.signer().Sign("secret", "hello world")
assert.Nil(t, err)
assert.Equal(t, "identity", method)
assert.Equal(t, "hello world", digest)
}
func TestAuthHeaderValue(t *testing.T) {
cases := []struct {
params map[string]string
authHeader string
}{
{map[string]string{}, "OAuth "},
{map[string]string{"a": "b"}, `OAuth a="b"`},
{map[string]string{"a": "b", "c": "d", "e": "f", "1": "2"}, `OAuth 1="2", a="b", c="d", e="f"`},
{map[string]string{"/= +doencode": "/= +doencode"}, `OAuth %2F%3D%20%2Bdoencode="%2F%3D%20%2Bdoencode"`},
{map[string]string{"-._~dontencode": "-._~dontencode"}, `OAuth -._~dontencode="-._~dontencode"`},
}
for _, c := range cases {
assert.Equal(t, c.authHeader, authHeaderValue(c.params))
}
}
func TestEncodeParameters(t *testing.T) {
input := map[string]string{
"a": "Dogs, Cats & Mice",
"☃": "snowman",
"ル": "ル",
}
expected := map[string]string{
"a": "Dogs%2C%20Cats%20%26%20Mice",
"%E2%98%83": "snowman",
"%E3%83%AB": "%E3%83%AB",
}
assert.Equal(t, expected, encodeParameters(input))
}
func TestSortParameters(t *testing.T) {
input := map[string]string{
".": "ape",
"5.6": "bat",
"rsa": "cat",
"%20": "dog",
"%E3%83%AB": "eel",
"dup": "fox",
//"dup": "fix", // duplicate keys not supported
}
expected := []string{
"%20=dog",
"%E3%83%AB=eel",
".=ape",
"5.6=bat",
"dup=fox",
"rsa=cat",
}
assert.Equal(t, expected, sortParameters(input, "%s=%s"))
}
func TestCollectParameters(t *testing.T) {
// example from RFC 5849 3.4.1.3.1
oauthParams := map[string]string{
"oauth_token": "kkk9d7dh3k39sjv7",
"oauth_consumer_key": "9djdj82h48djs9d2",
"oauth_signature_method": "HMAC-SHA1",
"oauth_timestamp": "137131201",
"oauth_nonce": "7d8f3e4a",
"realm": "photos",
}
values := url.Values{}
values.Add("c2", "")
values.Add("plus", "2 q") // duplicate keys not supported, a3 -> plus
req, err := http.NewRequest("POST", "/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b", strings.NewReader(values.Encode()))
assert.Nil(t, err)
req.Header.Set(contentType, formContentType)
params, err := collectParameters(req, oauthParams)
// assert parameters were collected from oauthParams, the query, and form body
// excluding the realm parameter
expected := map[string]string{
"b5": "=%3D",
"a3": "a",
"c@": "",
"a2": "r b",
"oauth_token": "kkk9d7dh3k39sjv7",
"oauth_consumer_key": "9djdj82h48djs9d2",
"oauth_signature_method": "HMAC-SHA1",
"oauth_timestamp": "137131201",
"oauth_nonce": "7d8f3e4a",
"c2": "",
"plus": "2 q",
}
assert.Nil(t, err)
assert.Equal(t, expected, params)
// RFC 5849 3.4.1.3.1 requires a {"a3"="2 q"} be form encoded to "a3=2+q" in
// the application/x-www-form-urlencoded body. The parameter "2+q" should be
// read as "2 q" and percent encoded to "2%20q".
// In Go, data is form encoded by calling Encode on url.Values{} (URL
// encoding) and decoded with url.ParseQuery to url.Values. So the encoding
// of "2 q" to "2+q" and decoding back to "2 q" is handled and then params
// are percent encoded.
// http://golang.org/src/net/http/client.go#L496
// http://golang.org/src/net/http/request.go#L837
}
func TestSignatureBase(t *testing.T) {
reqA, err := http.NewRequest("get", "HTTPS://HELLO.IO?q=test", nil)
assert.Nil(t, err)
reqB, err := http.NewRequest("POST", "http://hello.io:8080", nil)
assert.Nil(t, err)
cases := []struct {
req *http.Request
params map[string]string
signatureBase string
}{
{reqA, map[string]string{"a": "b", "c": "d"}, "GET&https%3A%2F%2Fhello.io&a%3Db%26c%3Dd"},
{reqB, map[string]string{"a": "b"}, "POST&http%3A%2F%2Fhello.io%3A8080&a%3Db"},
}
// assert that method is uppercased, base uri rules applied, queries added, joined by &
for _, c := range cases {
base := signatureBase(c.req, c.params)
assert.Equal(t, c.signatureBase, base)
}
}
func TestBaseURI(t *testing.T) {
reqA, err := http.NewRequest("GET", "HTTP://EXAMPLE.COM:80/r%20v/X?id=123", nil)
assert.Nil(t, err)
reqB, err := http.NewRequest("POST", "https://www.example.net:8080/?q=1", nil)
assert.Nil(t, err)
reqC, err := http.NewRequest("POST", "https://example.com:443", nil)
cases := []struct {
req *http.Request
baseURI string
}{
{reqA, "http://example.com/r%20v/X"},
{reqB, "https://www.example.net:8080/"},
{reqC, "https://example.com"},
}
for _, c := range cases {
baseURI := baseURI(c.req)
assert.Equal(t, c.baseURI, baseURI)
}
}
func TestNormalizedParameterString(t *testing.T) {
simple := map[string]string{
"a": "b & c",
"☃": "snowman",
}
rfcExample := map[string]string{
"b5": "=%3D",
"a3": "a",
"c@": "",
"a2": "r b",
"oauth_token": "kkk9d7dh3k39sjv7",
"oauth_consumer_key": "9djdj82h48djs9d2",
"oauth_signature_method": "HMAC-SHA1",
"oauth_timestamp": "137131201",
"oauth_nonce": "7d8f3e4a",
"c2": "",
"plus": "2 q",
}
cases := []struct {
params map[string]string
parameterStr string
}{
{simple, "%E2%98%83=snowman&a=b%20%26%20c"},
{rfcExample, "a2=r%20b&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9djdj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1&oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7&plus=2%20q"},
}
for _, c := range cases {
assert.Equal(t, c.parameterStr, normalizedParameterString(c.params))
}
}