-
Notifications
You must be signed in to change notification settings - Fork 119
/
provider_test.go
136 lines (110 loc) · 4.69 KB
/
provider_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
package oauth
import (
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
)
func TestProviderIsAuthorizedGood(t *testing.T) {
p := NewProvider(func(s string, h map[string]string) (*Consumer, error) {
c := NewConsumer(s, "consumersecret", ServiceProvider{})
c.signer = &MockSigner{}
return c, nil
})
p.clock = &MockClock{Time: 1446226936}
fakeRequest, err := http.NewRequest("GET", "https://example.com/some/path?q=query&q1=another_query", nil)
if err != nil {
t.Fatal(err)
}
// Set header to good oauth1 header
fakeRequest.Header.Set(HTTP_AUTH_HEADER, "OAuth oauth_nonce=\"799507437267152061446226936\", oauth_timestamp=\"1446226936\", oauth_version=\"1.0\", oauth_signature_method=\"HMAC-SHA1\", oauth_consumer_key=\"consumerkey\", oauth_signature=\"MOCK_SIGNATURE\"")
authorized, err := p.IsAuthorized(fakeRequest)
assertEq(t, nil, err)
assertEq(t, "consumerkey", *authorized)
}
func TestProviderIsAuthorizedOauthParamsInBody(t *testing.T) {
p := NewProvider(func(s string, h map[string]string) (*Consumer, error) {
c := NewConsumer(s, "consumersecret", ServiceProvider{})
c.signer = &MockSigner{}
return c, nil
})
p.clock = &MockClock{Time: 1446226936}
fakeRequest, err := http.NewRequest("GET", "https://example.com/some/path?q=query&q1=another_query", nil)
if err != nil {
t.Fatal(err)
}
// Put good oauth params into the request body
form := url.Values{}
form.Set("oauth_consumer_key", "consumerkey")
form.Set("oauth_nonce", "799507437267152061446226936")
form.Set("oauth_signature", "MOCK_SIGNATURE")
form.Set("oauth_signature_method", "HMAC-SHA1")
form.Set("oauth_timestamp", "1446226936")
form.Set("oauth_version", "1.0")
encodedForm := form.Encode()
fakeRequest.Body = ioutil.NopCloser(strings.NewReader(encodedForm))
fakeRequest.ContentLength = int64(len(encodedForm))
fakeRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded")
authorized, err := p.IsAuthorized(fakeRequest)
assertEq(t, nil, err)
assertEq(t, "consumerkey", *authorized)
}
func TestProviderIsAuthorizedOauthParamsInQuery(t *testing.T) {
p := NewProvider(func(s string, h map[string]string) (*Consumer, error) {
c := NewConsumer(s, "consumersecret", ServiceProvider{})
c.signer = &MockSigner{}
return c, nil
})
p.clock = &MockClock{Time: 1446226936}
// Put good oauth params into the query string
oauthParams := url.Values{}
oauthParams.Set("oauth_consumer_key", "consumerkey")
oauthParams.Set("oauth_nonce", "799507437267152061446226936")
oauthParams.Set("oauth_signature", "MOCK_SIGNATURE")
oauthParams.Set("oauth_signature_method", "HMAC-SHA1")
oauthParams.Set("oauth_timestamp", "1446226936")
oauthParams.Set("oauth_version", "1.0")
encodedOauthParams := oauthParams.Encode()
url := "https://example.com/some/path?q=query&q1=another_query&" + encodedOauthParams
fakeRequest, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Fatal(err)
}
authorized, err := p.IsAuthorized(fakeRequest)
assertEq(t, nil, err)
assertEq(t, "consumerkey", *authorized)
}
func TestProviderIsAuthorizedWithBodyHash(t *testing.T) {
p := NewProvider(func(s string, h map[string]string) (*Consumer, error) {
c := NewConsumer(s, "consumersecret", ServiceProvider{BodyHash: true})
return c, nil
})
p.clock = &MockClock{Time: 1446226936}
fakeRequest, err := http.NewRequest("GET", "https://example.com/some/path?q=query&q1=another_query", nil)
if err != nil {
t.Fatal(err)
}
// Set header to good oauth1 header
fakeRequest.Header.Set(HTTP_AUTH_HEADER, "OAuth oauth_nonce=\"799507437267152061446226936\", oauth_timestamp=\"1446226936\", oauth_version=\"1.0\", oauth_signature_method=\"HMAC-SHA1\", oauth_consumer_key=\"consumerkey\", oauth_signature=\"RYUiwiUc5LHoipANhDxPbdFHgKc%3D\", oauth_body_hash=\"2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D\"")
authorized, err := p.IsAuthorized(fakeRequest)
assertEq(t, nil, err)
assertEq(t, "consumerkey", *authorized)
}
func TestConsumerKeyWithEqualsInIt(t *testing.T) {
p := NewProvider(func(s string, h map[string]string) (*Consumer, error) {
c := NewConsumer(s, "consumersecret", ServiceProvider{})
c.signer = &MockSigner{}
return c, nil
})
p.clock = &MockClock{Time: 1446226936}
fakeRequest, err := http.NewRequest("GET", "https://example.com/some/path?q=query&q1=another_query", nil)
if err != nil {
t.Fatal(err)
}
// Set header to good oauth1 header
fakeRequest.Header.Set(HTTP_AUTH_HEADER, "OAuth oauth_nonce=\"799507437267152061446226936\", oauth_timestamp=\"1446226936\", oauth_version=\"1.0\", oauth_signature_method=\"HMAC-SHA1\", oauth_consumer_key=\"consumerkeywithequals=\", oauth_signature=\"MOCK_SIGNATURE\"")
authorized, err := p.IsAuthorized(fakeRequest)
assertEq(t, nil, err)
assertEq(t, "consumerkeywithequals=", *authorized)
}