forked from dghubble/oauth1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
verifier_test.go
238 lines (212 loc) · 13 KB
/
verifier_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
package oauth1
import (
"crypto"
"crypto/x509"
"encoding/base64"
"fmt"
"net/http"
"net/url"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
_ Verifier = &RSAVerifier{}
_ Verifier = &HMACVerifier{}
)
const (
rsaKey = `MIIEowIBAAKCAQEAy45DIEXPGTM/h3DC4GJKN+7k1wyZo5VpMGjESRkmq2RJOW+CJ8dlqcir4COX9wQlvmIZKSD/UDuai9zTXs3yHm1CizrOmF4PE0xxC8kNUvQccffBImoWLLzFs0sJHk/r0GNByTh+glQZksXhOhIaFETDPG/04MdwppvKDIC8him4dhQSNtf+5B62tvR1vmSaiUIq16twOYqDRAxZNEJ+SQ6d4uubDfQzIvQDGxgSi50WFcuNGdlzZgQpKYofC5lci06QoHV145yDMhl429Oj6urgcSX9kmypRa2PAWSRo7hmPp/siFHDk9OS3pLlOuWkVfNpMTJ8pbN+1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wPrFgQDpXzQT1kgdK9/Z+ou5m2laozMqzCzoMtMZwTK25BPXOu1c93EeFBP/V7JP1glWfHulBXNWXRm0qkNDQeTzRwDkB4Nqu9chbaDRSSwtWa+k93nHNiWqUfn8/XhU+b5mM6ObonlhM7QrffwH3o8Obpw34n`
)
func TestMakeURLAbs_NoHeader(t *testing.T) {
req := http.Request{}
req.Host = "www.example.com"
req.URL = &url.URL{}
NewVerifierManager(nil, "default-scheme", -1).makeURLAbs(&req)
assert.Equal(t, req.Host, req.URL.Host)
assert.Equal(t, "default-scheme", req.URL.Scheme)
}
func TestMakeURLAbs_WithForwardedHeader(t *testing.T) {
req := http.Request{}
req.Host = "www.example.com"
req.URL = &url.URL{}
req.Header = make(http.Header)
req.Header.Set("Forwarded", `for="127.0.0.1";proto=https`)
NewVerifierManager(nil, "default-scheme", -1).makeURLAbs(&req)
assert.Equal(t, req.Host, req.URL.Host)
assert.Equal(t, "https", req.URL.Scheme)
}
func TestMakeURLAbs_WithXForwardedProtoHeader(t *testing.T) {
req := http.Request{}
req.Host = "www.example.com"
req.URL = &url.URL{}
req.Header = make(http.Header)
req.Header.Set("X-Forwarded-Proto", `https`)
NewVerifierManager(nil, "default-scheme", -1).makeURLAbs(&req)
assert.Equal(t, req.Host, req.URL.Host)
assert.Equal(t, "https", req.URL.Scheme)
}
func TestCollectRequestParameters(t *testing.T) {
// example from TestCollectParameters
values := url.Values{}
values.Add("c2", "")
values.Add("plus", "2 q")
req, err := http.NewRequest("POST", "/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b", strings.NewReader(values.Encode()))
assert.NoError(t, err)
req.Header.Set(contentType, formContentType)
req.Header.Set(authorizationHeaderParam, `OAuth realm="Example", oauth_consumer_key="9djdj82h48djs9d2", oauth_token="kkk9d7dh3k39sjv7", oauth_signature_method="HMAC-SHA1", oauth_timestamp="137131201", oauth_nonce="7d8f3e4a", oauth_signature="djosJKDKJSD8743243%2Fjdk33klY%3D"`)
params, signature, err := collectRequestParameters(req)
require.NoError(t, err)
assert.Equal(t, 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",
}, params)
assert.Equal(t, `djosJKDKJSD8743243/jdk33klY=`, signature)
}
func TestVerifier_RSA(t *testing.T) {
// example from TestCollectParameters
values := url.Values{}
values.Add("c2", "")
values.Add("plus", "2 q") // duplicate keys not supported, a3 -> plus
req, err := http.NewRequest("POST", "http://127.0.0.1:50428/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b", strings.NewReader(values.Encode()))
assert.NoError(t, err)
req.Header.Set(contentType, formContentType)
req.Header.Set(authorizationHeaderParam, `OAuth oauth_consumer_key="consumer_key", oauth_nonce="6hrVr5eVPa5cWUtnW3sRIMlti2uB0zM43pk9mYIggFY%3D", oauth_signature="Nu%2B7FzqMw%2B18w6%2BzcT45SjWBXWjvf%2FW8adgIpgNahfZGzSExrIA6YRugfngCD97t4ms%2B4Vo2ozPOYHhxq%2BIF3EqoSdno5v53rA9mBvOmNU9XKr7gb92F0MVw%2F6M8MQUhputsUW4L7JixEXHymQUEub82ZC58xHJHklPUNIUtmyuxpzeII7E2K09KLMDp9%2F4ne%2FIm%2FufSoWDCBWn9497SIYZKNGyDAHav9zuXFy8x%2FItwknSpvSGG5zr1j2OyaZz7P5AIHVYPryi1N0Mwu35QHES4pafc0z1Z%2Fgm8PMvcI2BofqdEHbs65okhrE%2BSCxPRqJtc1k4A5LkmWbyp91WqHw%3D%3D", oauth_signature_method="RSA-SHA1", oauth_timestamp="1659497715", oauth_token="", oauth_version="1.0"`)
err = NewVerifierManager(func(consumerKey, method string, params map[string]string) (Verifier, error) {
assert.Equal(t, "consumer_key", consumerKey)
assert.Equal(t, "RSA-SHA1", method)
b, err := base64.StdEncoding.DecodeString(rsaKey)
require.NoError(t, err)
k, err := x509.ParsePKCS1PrivateKey(b)
require.NoError(t, err)
return NewRSAVerifier(&k.PublicKey, crypto.SHA1), nil
}, "", -1).Verify(req)
require.NoError(t, err)
}
func TestVerifier_InvalidRSA(t *testing.T) {
// example from TestCollectParameters
cases := []string{
// invalid signature
// first N -> n
"nu%2B7FzqMw%2B18w6%2BzcT45SjWBXWjvf%2FW8adgIpgNahfZGzSExrIA6YRugfngCD97t4ms%2B4Vo2ozPOYHhxq%2BIF3EqoSdno5v53rA9mBvOmNU9XKr7gb92F0MVw%2F6M8MQUhputsUW4L7JixEXHymQUEub82ZC58xHJHklPUNIUtmyuxpzeII7E2K09KLMDp9%2F4ne%2FIm%2FufSoWDCBWn9497SIYZKNGyDAHav9zuXFy8x%2FItwknSpvSGG5zr1j2OyaZz7P5AIHVYPryi1N0Mwu35QHES4pafc0z1Z%2Fgm8PMvcI2BofqdEHbs65okhrE%2BSCxPRqJtc1k4A5LkmWbyp91WqHw%3D%3D",
// not percent encoded
"Nu%%B7FzqMw%2B18w6%2BzcT45SjWBXWjvf%2FW8adgIpgNahfZGzSExrIA6YRugfngCD97t4ms%2B4Vo2ozPOYHhxq%2BIF3EqoSdno5v53rA9mBvOmNU9XKr7gb92F0MVw%2F6M8MQUhputsUW4L7JixEXHymQUEub82ZC58xHJHklPUNIUtmyuxpzeII7E2K09KLMDp9%2F4ne%2FIm%2FufSoWDCBWn9497SIYZKNGyDAHav9zuXFy8x%2FItwknSpvSGG5zr1j2OyaZz7P5AIHVYPryi1N0Mwu35QHES4pafc0z1Z%2Fgm8PMvcI2BofqdEHbs65okhrE%2BSCxPRqJtc1k4A5LkmWbyp91WqHw%3D%3D",
// not base64 encoded
// last = is replaced
"Nu%2B7FzqMw%2B18w6%2BzcT45SjWBXWjvf%2FW8adgIpgNahfZGzSExrIA6YRugfngCD97t4ms%2B4Vo2ozPOYHhxq%2BIF3EqoSdno5v53rA9mBvOmNU9XKr7gb92F0MVw%2F6M8MQUhputsUW4L7JixEXHymQUEub82ZC58xHJHklPUNIUtmyuxpzeII7E2K09KLMDp9%2F4ne%2FIm%2FufSoWDCBWn9497SIYZKNGyDAHav9zuXFy8x%2FItwknSpvSGG5zr1j2OyaZz7P5AIHVYPryi1N0Mwu35QHES4pafc0z1Z%2Fgm8PMvcI2BofqdEHbs65okhrE%2BSCxPRqJtc1k4A5LkmWbyp91WqHw%3D%3A",
}
for _, signature := range cases {
values := url.Values{}
values.Add("c2", "")
values.Add("plus", "2 q") // duplicate keys not supported, a3 -> plus
req, err := http.NewRequest("POST", "http://127.0.0.1:50428/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b", strings.NewReader(values.Encode()))
assert.NoError(t, err)
req.Header.Set(contentType, formContentType)
header := `OAuth oauth_consumer_key="consumer_key", oauth_nonce="6hrVr5eVPa5cWUtnW3sRIMlti2uB0zM43pk9mYIggFY%3D", oauth_signature="` + signature + `", oauth_signature_method="RSA-SHA1", oauth_timestamp="1659497715", oauth_token="", oauth_version="1.0"`
req.Header.Set(authorizationHeaderParam, header)
err = NewVerifierManager(func(consumerKey, method string, params map[string]string) (Verifier, error) {
assert.Equal(t, "consumer_key", consumerKey)
assert.Equal(t, "RSA-SHA1", method)
b, err := base64.StdEncoding.DecodeString(rsaKey)
require.NoError(t, err)
k, err := x509.ParsePKCS1PrivateKey(b)
require.NoError(t, err)
return NewRSAVerifier(&k.PublicKey, crypto.SHA1), nil
}, "", -1).Verify(req)
require.Error(t, err)
}
}
func TestVerifier_HMACSHA1_WithoutOAuthToken(t *testing.T) {
// example from TestCollectParameters
values := url.Values{}
values.Add("c2", "")
values.Add("plus", "2 q")
req, err := http.NewRequest("POST", "http://127.0.0.1:51060/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b", strings.NewReader(values.Encode()))
assert.NoError(t, err)
req.Header.Set(contentType, formContentType)
req.Header.Set(authorizationHeaderParam, `OAuth oauth_consumer_key="9djdj82h48djs9d2", oauth_nonce="UrmFlgNMjd2UF8sodAzDPqN5AylKo33kxF9gqnd1j7E%3D", oauth_signature="4ZYe7rg2We2jgfv20ZNqlVbCibY%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1659507521", oauth_token="", oauth_version="1.0"`)
err = NewVerifierManager(func(consumerKey, method string, params map[string]string) (Verifier, error) {
assert.Equal(t, "9djdj82h48djs9d2", consumerKey)
assert.Equal(t, "HMAC-SHA1", method)
return NewHMACVerifier(NewConfig("9djdj82h48djs9d2", "j49sk3j29djd"), ""), nil
}, "", -1).Verify(req)
require.NoError(t, err)
}
func TestVerifier_HMACSHA1_WithOAuthToken(t *testing.T) {
// example from TestCollectParameters
values := url.Values{}
values.Add("c2", "")
values.Add("plus", "2 q")
req, err := http.NewRequest("POST", "/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b", strings.NewReader(values.Encode()))
assert.NoError(t, err)
req.Header.Set(contentType, formContentType)
req.Header.Set(authorizationHeaderParam, `OAuth oauth_consumer_key="9djdj82h48djs9d2", oauth_nonce="FnwtgC3exdLhc2Kspuc9GYPhGzgyQEB1T5tRcfM2FtM%3D", oauth_signature="6F8L5pN4iERKpwRqfuMGA9WesYU%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1659507394", oauth_token="kkk9d7dh3k39sjv7", oauth_version="1.0"`)
req.Header.Set("Forwarded", `for="127.0.0.1:51043";proto=http`)
req.Host = "127.0.0.1:51043"
err = NewVerifierManager(func(consumerKey, method string, params map[string]string) (Verifier, error) {
assert.Equal(t, "9djdj82h48djs9d2", consumerKey)
assert.Equal(t, "HMAC-SHA1", method)
return NewHMACVerifier(NewConfig("9djdj82h48djs9d2", "j49sk3j29djd"), "dh893hdasih9"), nil
}, "", -1).Verify(req)
require.NoError(t, err)
}
func TestVerifier_InvalidHMACSHA1(t *testing.T) {
// example from TestCollectParameters
values := url.Values{}
values.Add("c2", "")
values.Add("plus", "2 q")
req, err := http.NewRequest("POST", "http://127.0.0.1:51060/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b", strings.NewReader(values.Encode()))
assert.NoError(t, err)
req.Header.Set(contentType, formContentType)
req.Header.Set(authorizationHeaderParam, `OAuth oauth_consumer_key="9djdj82h48djs9d2", oauth_nonce="UrmFlgNMjd2UF8sodAzDPqN5AylKo33kxF9gqnd1j7E%3D", oauth_signature="3ZYe7rg2We2jgfv20ZNqlVbCibY%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1659507521", oauth_token="", oauth_version="1.0"`)
err = NewVerifierManager(func(consumerKey, method string, params map[string]string) (Verifier, error) {
assert.Equal(t, "9djdj82h48djs9d2", consumerKey)
assert.Equal(t, "HMAC-SHA1", method)
return NewHMACVerifier(NewConfig("9djdj82h48djs9d2", "j49sk3j29djd"), ""), nil
}, "", -1).Verify(req)
require.Error(t, err)
}
func TestVerifier_TooOld(t *testing.T) {
// example from TestCollectParameters
values := url.Values{}
values.Add("c2", "")
values.Add("plus", "2 q")
req, err := http.NewRequest("POST", "/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b", strings.NewReader(values.Encode()))
assert.NoError(t, err)
req.Header.Set(contentType, formContentType)
req.Header.Set(authorizationHeaderParam, `OAuth oauth_consumer_key="9djdj82h48djs9d2", oauth_nonce="FnwtgC3exdLhc2Kspuc9GYPhGzgyQEB1T5tRcfM2FtM%3D", oauth_signature="6F8L5pN4iERKpwRqfuMGA9WesYU%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1659507394", oauth_token="kkk9d7dh3k39sjv7", oauth_version="1.0"`)
req.Header.Set("Forwarded", `for="127.0.0.1:51043";proto=http`)
req.Host = "127.0.0.1:51043"
err = NewVerifierManager(func(consumerKey, method string, params map[string]string) (Verifier, error) {
assert.Equal(t, "9djdj82h48djs9d2", consumerKey)
assert.Equal(t, "HMAC-SHA1", method)
return NewHMACVerifier(NewConfig("9djdj82h48djs9d2", "j49sk3j29djd"), "dh893hdasih9"), nil
}, "", time.Hour).Verify(req)
require.Error(t, err)
}
func TestVerifier_RejectedByGetVerifier(t *testing.T) {
// example from TestCollectParameters
values := url.Values{}
values.Add("c2", "")
values.Add("plus", "2 q")
req, err := http.NewRequest("POST", "/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b", strings.NewReader(values.Encode()))
assert.NoError(t, err)
req.Header.Set(contentType, formContentType)
req.Header.Set(authorizationHeaderParam, `OAuth oauth_consumer_key="9djdj82h48djs9d2", oauth_nonce="FnwtgC3exdLhc2Kspuc9GYPhGzgyQEB1T5tRcfM2FtM%3D", oauth_signature="6F8L5pN4iERKpwRqfuMGA9WesYU%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1659507394", oauth_token="kkk9d7dh3k39sjv7", oauth_version="1.0"`)
req.Header.Set("Forwarded", `for="127.0.0.1:51043";proto=http`)
req.Host = "127.0.0.1:51043"
err = NewVerifierManager(func(consumerKey, method string, params map[string]string) (Verifier, error) {
return nil, fmt.Errorf("fail to get verifier")
}, "", -1).Verify(req)
require.Error(t, err)
}