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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
from __future__ import unicode_literals, print_function
import unittest
import sys
import requests
from io import StringIO
from oauthlib.oauth1 import SIGNATURE_TYPE_QUERY, SIGNATURE_TYPE_BODY
from oauthlib.oauth1 import SIGNATURE_RSA, SIGNATURE_PLAINTEXT
from requests_oauthlib import OAuth1Session
try:
import mock
except ImportError:
from unittest import mock
try:
import cryptography
except ImportError:
cryptography = None
try:
import jwt
except ImportError:
jwt = None
if sys.version[0] == "3":
unicode_type = str
else:
unicode_type = unicode
TEST_RSA_KEY = (
"-----BEGIN RSA PRIVATE KEY-----\n"
"MIIEogIBAAKCAQEApF1JaMSN8TEsh4N4O/5SpEAVL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pcgfkT6Dm706GFFttLL8CgYBl/HeXk1H47xAiHO4dJKnb\n"
"v0B+X8To/RXamF01r+8BpUoOubOQetdyX7ic+d6deuHu8i6LD/GSCeYJZYFR/KVg\n"
"AtiW757QYalnq3ZogkhFrVCZP8IRfTPOFBxp752TlyAcrSI7T9pQ47IBe4094KXM\n"
"CJWSfPgAJkOxd0iU0XJpmwKBgGfQxuMTgSlwYRKFlD1zKap5TdID8fbUbVnth0Q5\n"
"GbH7vwlp/qrxCdS/aj0n0irOpbOaW9ccnlrHiqY25VpVMLYIkt3DrDOEiNNx+KNR\n"
"TItdTwbcSiTYrS4L0/56ydM/H6bsfsXxRjI18hSJqMZiqXqS84OZz2aOn+h7HCzc\n"
"LEiZAoGASk20wFvilpRKHq79xxFWiDUPHi0x0pp82dYIEntGQkKUWkbSlhgf3MAi\n"
"5NEQTDmXdnB+rVeWIvEi+BXfdnNgdn8eC4zSdtF4sIAhYr5VWZo0WVWDhT7u2ccv\n"
"ZBFymiz8lo3gN57wGUCi9pbZqzV1+ZppX6YTNDdDCE0q+KO3Cec=\n"
"-----END RSA PRIVATE KEY-----"
)
TEST_RSA_OAUTH_SIGNATURE = (
"j8WF8PGjojT82aUDd2EL%2Bz7HCoHInFzWUpiEKMCy%2BJ2cYHWcBS7mXlmFDLgAKV0"
"P%2FyX4TrpXODYnJ6dRWdfghqwDpi%2FlQmB2jxCiGMdJoYxh3c5zDf26gEbGdP6D7O"
"Ssp5HUnzH6sNkmVjuE%2FxoJcHJdc23H6GhOs7VJ2LWNdbhKWP%2FMMlTrcoQDn8lz"
"%2Fb24WsJ6ae1txkUzpFOOlLM8aTdNtGL4OtsubOlRhNqnAFq93FyhXg0KjzUyIZzmMX"
"9Vx90jTks5QeBGYcLE0Op2iHb2u%2FO%2BEgdwFchgEwE5LgMUyHUI4F3Wglp28yHOAM"
"jPkI%2FkWMvpxtMrU3Z3KN31WQ%3D%3D"
)
class OAuth1SessionTest(unittest.TestCase):
def test_signature_types(self):
def verify_signature(getter):
def fake_send(r, **kwargs):
signature = getter(r)
if isinstance(signature, bytes):
signature = signature.decode("utf-8")
self.assertIn("oauth_signature", signature)
resp = mock.MagicMock(spec=requests.Response)
resp.cookies = []
return resp
return fake_send
header = OAuth1Session("foo")
header.send = verify_signature(lambda r: r.headers["Authorization"])
header.post("https://i.b")
query = OAuth1Session("foo", signature_type=SIGNATURE_TYPE_QUERY)
query.send = verify_signature(lambda r: r.url)
query.post("https://i.b")
body = OAuth1Session("foo", signature_type=SIGNATURE_TYPE_BODY)
headers = {"Content-Type": "application/x-www-form-urlencoded"}
body.send = verify_signature(lambda r: r.body)
body.post("https://i.b", headers=headers, data="")
@mock.patch("oauthlib.oauth1.rfc5849.generate_timestamp")
@mock.patch("oauthlib.oauth1.rfc5849.generate_nonce")
def test_signature_methods(self, generate_nonce, generate_timestamp):
if not cryptography:
raise unittest.SkipTest("cryptography module is required")
if not jwt:
raise unittest.SkipTest("pyjwt module is required")
generate_nonce.return_value = "abc"
generate_timestamp.return_value = "123"
signature = 'OAuth oauth_nonce="abc", oauth_timestamp="123", oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="foo", oauth_signature="h2sRqLArjhlc5p3FTkuNogVHlKE%3D"'
auth = OAuth1Session("foo")
auth.send = self.verify_signature(signature)
auth.post("https://i.b")
signature = 'OAuth oauth_nonce="abc", oauth_timestamp="123", oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="foo", oauth_signature="%26"'
auth = OAuth1Session("foo", signature_method=SIGNATURE_PLAINTEXT)
auth.send = self.verify_signature(signature)
auth.post("https://i.b")
signature = (
"OAuth "
'oauth_nonce="abc", oauth_timestamp="123", oauth_version="1.0", '
'oauth_signature_method="RSA-SHA1", oauth_consumer_key="foo", '
'oauth_signature="{sig}"'
).format(sig=TEST_RSA_OAUTH_SIGNATURE)
auth = OAuth1Session(
"foo", signature_method=SIGNATURE_RSA, rsa_key=TEST_RSA_KEY
)
auth.send = self.verify_signature(signature)
auth.post("https://i.b")
@mock.patch("oauthlib.oauth1.rfc5849.generate_timestamp")
@mock.patch("oauthlib.oauth1.rfc5849.generate_nonce")
def test_binary_upload(self, generate_nonce, generate_timestamp):
generate_nonce.return_value = "abc"
generate_timestamp.return_value = "123"
fake_xml = StringIO("hello world")
headers = {"Content-Type": "application/xml"}
signature = 'OAuth oauth_nonce="abc", oauth_timestamp="123", oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="foo", oauth_signature="h2sRqLArjhlc5p3FTkuNogVHlKE%3D"'
auth = OAuth1Session("foo")
auth.send = self.verify_signature(signature)
auth.post("https://i.b", headers=headers, files=[("fake", fake_xml)])
@mock.patch("oauthlib.oauth1.rfc5849.generate_timestamp")
@mock.patch("oauthlib.oauth1.rfc5849.generate_nonce")
def test_nonascii(self, generate_nonce, generate_timestamp):
generate_nonce.return_value = "abc"
generate_timestamp.return_value = "123"
signature = 'OAuth oauth_nonce="abc", oauth_timestamp="123", oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="foo", oauth_signature="W0haoue5IZAZoaJiYCtfqwMf8x8%3D"'
auth = OAuth1Session("foo")
auth.send = self.verify_signature(signature)
auth.post("https://i.b?cjk=%E5%95%A6%E5%95%A6")
def test_authorization_url(self):
auth = OAuth1Session("foo")
url = "https://example.comm/authorize"
token = "asluif023sf"
auth_url = auth.authorization_url(url, request_token=token)
self.assertEqual(auth_url, url + "?oauth_token=" + token)
def test_parse_response_url(self):
url = "https://i.b/callback?oauth_token=foo&oauth_verifier=bar"
auth = OAuth1Session("foo")
resp = auth.parse_authorization_response(url)
self.assertEqual(resp["oauth_token"], "foo")
self.assertEqual(resp["oauth_verifier"], "bar")
for k, v in resp.items():
self.assertIsInstance(k, unicode_type)
self.assertIsInstance(v, unicode_type)
def test_fetch_request_token(self):
auth = OAuth1Session("foo")
auth.send = self.fake_body("oauth_token=foo")
resp = auth.fetch_request_token("https://example.com/token")
self.assertEqual(resp["oauth_token"], "foo")
for k, v in resp.items():
self.assertIsInstance(k, unicode_type)
self.assertIsInstance(v, unicode_type)
def test_fetch_request_token_with_optional_arguments(self):
auth = OAuth1Session("foo")
auth.send = self.fake_body("oauth_token=foo")
resp = auth.fetch_request_token(
"https://example.com/token", verify=False, stream=True
)
self.assertEqual(resp["oauth_token"], "foo")
for k, v in resp.items():
self.assertIsInstance(k, unicode_type)
self.assertIsInstance(v, unicode_type)
def test_fetch_access_token(self):
auth = OAuth1Session("foo", verifier="bar")
auth.send = self.fake_body("oauth_token=foo")
resp = auth.fetch_access_token("https://example.com/token")
self.assertEqual(resp["oauth_token"], "foo")
for k, v in resp.items():
self.assertIsInstance(k, unicode_type)
self.assertIsInstance(v, unicode_type)
def test_fetch_access_token_with_optional_arguments(self):
auth = OAuth1Session("foo", verifier="bar")
auth.send = self.fake_body("oauth_token=foo")
resp = auth.fetch_access_token(
"https://example.com/token", verify=False, stream=True
)
self.assertEqual(resp["oauth_token"], "foo")
for k, v in resp.items():
self.assertIsInstance(k, unicode_type)
self.assertIsInstance(v, unicode_type)
def _test_fetch_access_token_raises_error(self, auth):
"""Assert that an error is being raised whenever there's no verifier
passed in to the client.
"""
auth.send = self.fake_body("oauth_token=foo")
with self.assertRaises(ValueError) as cm:
auth.fetch_access_token("https://example.com/token")
self.assertEqual("No client verifier has been set.", str(cm.exception))
def test_fetch_token_invalid_response(self):
auth = OAuth1Session("foo")
auth.send = self.fake_body("not valid urlencoded response!")
self.assertRaises(
ValueError, auth.fetch_request_token, "https://example.com/token"
)
for code in (400, 401, 403):
auth.send = self.fake_body("valid=response", code)
with self.assertRaises(ValueError) as cm:
auth.fetch_request_token("https://example.com/token")
self.assertEqual(cm.exception.status_code, code)
self.assertIsInstance(cm.exception.response, requests.Response)
def test_fetch_access_token_missing_verifier(self):
self._test_fetch_access_token_raises_error(OAuth1Session("foo"))
def test_fetch_access_token_has_verifier_is_none(self):
auth = OAuth1Session("foo")
del auth._client.client.verifier
self._test_fetch_access_token_raises_error(auth)
def test_token_proxy_set(self):
token = {
"oauth_token": "fake-key",
"oauth_token_secret": "fake-secret",
"oauth_verifier": "fake-verifier",
}
sess = OAuth1Session("foo")
self.assertIsNone(sess._client.client.resource_owner_key)
self.assertIsNone(sess._client.client.resource_owner_secret)
self.assertIsNone(sess._client.client.verifier)
self.assertEqual(sess.token, {})
sess.token = token
self.assertEqual(sess._client.client.resource_owner_key, "fake-key")
self.assertEqual(sess._client.client.resource_owner_secret, "fake-secret")
self.assertEqual(sess._client.client.verifier, "fake-verifier")
def test_token_proxy_get(self):
token = {
"oauth_token": "fake-key",
"oauth_token_secret": "fake-secret",
"oauth_verifier": "fake-verifier",
}
sess = OAuth1Session(
"foo",
resource_owner_key=token["oauth_token"],
resource_owner_secret=token["oauth_token_secret"],
verifier=token["oauth_verifier"],
)
self.assertEqual(sess.token, token)
sess._client.client.resource_owner_key = "different-key"
token["oauth_token"] = "different-key"
self.assertEqual(sess.token, token)
def test_authorized_false(self):
sess = OAuth1Session("foo")
self.assertIs(sess.authorized, False)
def test_authorized_false_rsa(self):
signature = (
"OAuth "
'oauth_nonce="abc", oauth_timestamp="123", oauth_version="1.0", '
'oauth_signature_method="RSA-SHA1", oauth_consumer_key="foo", '
'oauth_signature="{sig}"'
).format(sig=TEST_RSA_OAUTH_SIGNATURE)
sess = OAuth1Session(
"foo", signature_method=SIGNATURE_RSA, rsa_key=TEST_RSA_KEY
)
sess.send = self.verify_signature(signature)
self.assertIs(sess.authorized, False)
def test_authorized_true(self):
sess = OAuth1Session("key", "secret", verifier="bar")
sess.send = self.fake_body("oauth_token=foo&oauth_token_secret=bar")
sess.fetch_access_token("https://example.com/token")
self.assertIs(sess.authorized, True)
@mock.patch("oauthlib.oauth1.rfc5849.generate_timestamp")
@mock.patch("oauthlib.oauth1.rfc5849.generate_nonce")
def test_authorized_true_rsa(self, generate_nonce, generate_timestamp):
if not cryptography:
raise unittest.SkipTest("cryptography module is required")
if not jwt:
raise unittest.SkipTest("pyjwt module is required")
generate_nonce.return_value = "abc"
generate_timestamp.return_value = "123"
signature = (
"OAuth "
'oauth_nonce="abc", oauth_timestamp="123", oauth_version="1.0", '
'oauth_signature_method="RSA-SHA1", oauth_consumer_key="foo", '
'oauth_verifier="bar", oauth_signature="{sig}"'
).format(sig=TEST_RSA_OAUTH_SIGNATURE)
sess = OAuth1Session(
"key",
"secret",
signature_method=SIGNATURE_RSA,
rsa_key=TEST_RSA_KEY,
verifier="bar",
)
sess.send = self.fake_body("oauth_token=foo&oauth_token_secret=bar")
sess.fetch_access_token("https://example.com/token")
self.assertIs(sess.authorized, True)
def verify_signature(self, signature):
def fake_send(r, **kwargs):
auth_header = r.headers["Authorization"]
if isinstance(auth_header, bytes):
auth_header = auth_header.decode("utf-8")
self.assertEqual(auth_header, signature)
resp = mock.MagicMock(spec=requests.Response)
resp.cookies = []
return resp
return fake_send
def fake_body(self, body, status_code=200):
def fake_send(r, **kwargs):
resp = mock.MagicMock(spec=requests.Response)
resp.cookies = []
resp.text = body
resp.status_code = status_code
return resp
return fake_send
|