File: test_oauth1_session.py

package info (click to toggle)
python-authlib 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,016 kB
  • sloc: python: 26,998; makefile: 53; sh: 14
file content (267 lines) | stat: -rw-r--r-- 10,772 bytes parent folder | download | duplicates (2)
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
from io import StringIO
from unittest import TestCase
from unittest import mock

import pytest
import requests

from authlib.common.encoding import to_unicode
from authlib.integrations.requests_client import OAuth1Session
from authlib.integrations.requests_client import OAuthError
from authlib.oauth1 import SIGNATURE_PLAINTEXT
from authlib.oauth1 import SIGNATURE_RSA_SHA1
from authlib.oauth1 import SIGNATURE_TYPE_BODY
from authlib.oauth1 import SIGNATURE_TYPE_QUERY
from authlib.oauth1.rfc5849.util import escape

from ..util import mock_text_response
from ..util import read_key_file

TEST_RSA_OAUTH_SIGNATURE = (
    "j8WF8PGjojT82aUDd2EL%2Bz7HCoHInFzWUpiEKMCy%2BJ2cYHWcBS7mXlmFDLgAKV0"
    "P%2FyX4TrpXODYnJ6dRWdfghqwDpi%2FlQmB2jxCiGMdJoYxh3c5zDf26gEbGdP6D7O"
    "Ssp5HUnzH6sNkmVjuE%2FxoJcHJdc23H6GhOs7VJ2LWNdbhKWP%2FMMlTrcoQDn8lz"
    "%2Fb24WsJ6ae1txkUzpFOOlLM8aTdNtGL4OtsubOlRhNqnAFq93FyhXg0KjzUyIZzmMX"
    "9Vx90jTks5QeBGYcLE0Op2iHb2u%2FO%2BEgdwFchgEwE5LgMUyHUI4F3Wglp28yHOAM"
    "jPkI%2FkWMvpxtMrU3Z3KN31WQ%3D%3D"
)


class OAuth1SessionTest(TestCase):
    def test_no_client_id(self):
        with pytest.raises(ValueError):
            OAuth1Session(None)

    def test_signature_types(self):
        def verify_signature(getter):
            def fake_send(r, **kwargs):
                signature = to_unicode(getter(r))
                assert "oauth_signature" in 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("authlib.oauth1.rfc5849.client_auth.generate_timestamp")
    @mock.patch("authlib.oauth1.rfc5849.client_auth.generate_nonce")
    def test_signature_methods(self, generate_nonce, generate_timestamp):
        generate_nonce.return_value = "abc"
        generate_timestamp.return_value = "123"

        signature = ", ".join(
            [
                '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", '
            f'oauth_signature="{TEST_RSA_OAUTH_SIGNATURE}"'
        )

        rsa_key = read_key_file("rsa_private.pem")
        auth = OAuth1Session(
            "foo", signature_method=SIGNATURE_RSA_SHA1, rsa_key=rsa_key
        )
        auth.send = self.verify_signature(signature)
        auth.post("https://i.b")

    @mock.patch("authlib.oauth1.rfc5849.client_auth.generate_timestamp")
    @mock.patch("authlib.oauth1.rfc5849.client_auth.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"}

        def fake_send(r, **kwargs):
            auth_header = r.headers["Authorization"]
            assert "oauth_body_hash" in auth_header

        auth = OAuth1Session("foo", force_include_body=True)
        auth.send = fake_send
        auth.post("https://i.b", headers=headers, files=[("fake", fake_xml)])

    @mock.patch("authlib.oauth1.rfc5849.client_auth.generate_timestamp")
    @mock.patch("authlib.oauth1.rfc5849.client_auth.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_redirect_uri(self):
        sess = OAuth1Session("foo")
        assert sess.redirect_uri is None
        url = "https://i.b"
        sess.redirect_uri = url
        assert sess.redirect_uri == url

    def test_set_token(self):
        sess = OAuth1Session("foo")
        try:
            sess.token = {}
        except OAuthError as exc:
            assert exc.error == "missing_token"

        sess.token = {"oauth_token": "a", "oauth_token_secret": "b"}
        assert sess.token["oauth_verifier"] is None
        sess.token = {"oauth_token": "a", "oauth_verifier": "c"}
        assert sess.token["oauth_token_secret"] == "b"
        assert sess.token["oauth_verifier"] == "c"

        sess.token = None
        assert sess.token["oauth_token"] is None
        assert sess.token["oauth_token_secret"] is None
        assert sess.token["oauth_verifier"] is None

    def test_create_authorization_url(self):
        auth = OAuth1Session("foo")
        url = "https://example.comm/authorize"
        token = "asluif023sf"
        auth_url = auth.create_authorization_url(url, request_token=token)
        assert auth_url == url + "?oauth_token=" + token
        redirect_uri = "https://c.b"
        auth = OAuth1Session("foo", redirect_uri=redirect_uri)
        auth_url = auth.create_authorization_url(url, request_token=token)
        assert escape(redirect_uri) in auth_url

    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)
        assert resp["oauth_token"] == "foo"
        assert resp["oauth_verifier"] == "bar"
        for k, v in resp.items():
            assert isinstance(k, str)
            assert isinstance(v, str)

    def test_fetch_request_token(self):
        auth = OAuth1Session("foo", realm="A")
        auth.send = mock_text_response("oauth_token=foo")
        resp = auth.fetch_request_token("https://example.com/token")
        assert resp["oauth_token"] == "foo"
        for k, v in resp.items():
            assert isinstance(k, str)
            assert isinstance(v, str)

        resp = auth.fetch_request_token("https://example.com/token")
        assert resp["oauth_token"] == "foo"

    def test_fetch_request_token_with_optional_arguments(self):
        auth = OAuth1Session("foo")
        auth.send = mock_text_response("oauth_token=foo")
        resp = auth.fetch_request_token(
            "https://example.com/token", verify=False, stream=True
        )
        assert resp["oauth_token"] == "foo"
        for k, v in resp.items():
            assert isinstance(k, str)
            assert isinstance(v, str)

    def test_fetch_access_token(self):
        auth = OAuth1Session("foo", verifier="bar")
        auth.send = mock_text_response("oauth_token=foo")
        resp = auth.fetch_access_token("https://example.com/token")
        assert resp["oauth_token"] == "foo"
        for k, v in resp.items():
            assert isinstance(k, str)
            assert isinstance(v, str)

        auth = OAuth1Session("foo", verifier="bar")
        auth.send = mock_text_response('{"oauth_token":"foo"}')
        resp = auth.fetch_access_token("https://example.com/token")
        assert resp["oauth_token"] == "foo"

        auth = OAuth1Session("foo")
        auth.send = mock_text_response("oauth_token=foo")
        resp = auth.fetch_access_token("https://example.com/token", verifier="bar")
        assert resp["oauth_token"] == "foo"

    def test_fetch_access_token_with_optional_arguments(self):
        auth = OAuth1Session("foo", verifier="bar")
        auth.send = mock_text_response("oauth_token=foo")
        resp = auth.fetch_access_token(
            "https://example.com/token", verify=False, stream=True
        )
        assert resp["oauth_token"] == "foo"
        for k, v in resp.items():
            assert isinstance(k, str)
            assert isinstance(v, str)

    def _test_fetch_access_token_raises_error(self, session):
        """Assert that an error is being raised whenever there's no verifier
        passed in to the client.
        """
        session.send = mock_text_response("oauth_token=foo")
        with pytest.raises(OAuthError, match="missing_verifier"):
            session.fetch_access_token("https://example.com/token")

    def test_fetch_token_invalid_response(self):
        auth = OAuth1Session("foo")
        auth.send = mock_text_response("not valid urlencoded response!")
        with pytest.raises(ValueError):
            auth.fetch_request_token("https://example.com/token")

        for code in (400, 401, 403):
            auth.send = mock_text_response("valid=response", code)
            with pytest.raises(OAuthError, match="fetch_token_denied"):
                auth.fetch_request_token("https://example.com/token")

    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):
        session = OAuth1Session("foo")
        session.auth.verifier = None
        self._test_fetch_access_token_raises_error(session)

    def verify_signature(self, signature):
        def fake_send(r, **kwargs):
            auth_header = to_unicode(r.headers["Authorization"])
            assert auth_header == signature
            resp = mock.MagicMock(spec=requests.Response)
            resp.cookies = []
            return resp

        return fake_send