File size: 13,323 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import logging
try:  # Python 2
    from urlparse import urljoin
except:  # Python 3
    from urllib.parse import urljoin
import time

import requests

from msal.oauth2cli import Client, JwtSigner, AuthCodeReceiver
from msal.oauth2cli.authcode import obtain_auth_code
from tests import unittest, Oauth2TestCase
from tests.http_client import MinimalHttpClient, MinimalResponse


logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__file__)

CONFIG_FILENAME = "config.json"

def load_conf(filename):
    """
    Example of a configuration file:

    {
        "Note": "the OpenID Discovery will be updated by following optional content",
        "additional_openid_configuration": {
            "authorization_endpoint": "https://example.com/tenant/oauth2/authorize",
            "token_endpoint": "https://example.com/tenant/oauth2/token",
            "device_authorization_endpoint": "device_authorization"
        },

        "client_id": "289a413d-284b-4303-9c79-94380abe5d22",
        "client_secret": "your_secret",

        "scope": ["your_scope"],
        "resource": "Some IdP needs this",

        "oidp": "https://example.com/tenant/",
        "username": "you@example.com",
        "password": "I could tell you but then I would have to kill you",

        "placeholder": null
    }
    """
    conf = {}
    if os.path.exists(filename):
        with open(filename) as f:
            conf = json.load(f)
    else:
        # Do not raise unittest.SkipTest(...) here,
        # because it would still be considered as Test Error in Python 2
        logger.warning("Unable to locate JSON configuration %s" % filename)
    openid_configuration = {}
    if "oidp" in conf:
        try:
            # The following line may duplicate a '/' at the joining point,
            # but requests.get(...) would still work.
            # Besides, standard urljoin(...) is picky on insisting oidp ends with '/'
            discovery_uri = conf["oidp"] + '/.well-known/openid-configuration'
            openid_configuration.update(requests.get(discovery_uri).json())
        except:
            logger.warning(
                "openid configuration uri not accesible: %s", discovery_uri)
    openid_configuration.update(conf.get("additional_openid_configuration", {}))
    if openid_configuration.get("device_authorization_endpoint"):
        # The following urljoin(..., ...) trick allows a "path_name" shorthand
        openid_configuration["device_authorization_endpoint"] = urljoin(
            openid_configuration.get("token_endpoint", ""),
            openid_configuration.get("device_authorization_endpoint", ""))
    conf["openid_configuration"] = openid_configuration
    return conf

THIS_FOLDER = os.path.dirname(__file__)
CONFIG = load_conf(os.path.join(THIS_FOLDER, CONFIG_FILENAME)) or {}


# Since the OAuth2 specs uses snake_case, this test config also uses snake_case
@unittest.skipUnless("client_id" in CONFIG, "client_id missing")
@unittest.skipUnless(CONFIG.get("openid_configuration"), "openid_configuration missing")
class TestClient(Oauth2TestCase):

    @classmethod
    def setUpClass(cls):
        http_client = MinimalHttpClient()
        if "client_assertion" in CONFIG:
            cls.client = Client(
                CONFIG["openid_configuration"],
                CONFIG['client_id'],
                http_client=http_client,
                client_assertion=CONFIG["client_assertion"],
                client_assertion_type=Client.CLIENT_ASSERTION_TYPE_JWT,
                )
        elif "client_certificate" in CONFIG:
            private_key_path = CONFIG["client_certificate"]["private_key_path"]
            with open(os.path.join(THIS_FOLDER, private_key_path)) as f:
                private_key = f.read()  # Expecting PEM format
            cls.client = Client(
                CONFIG["openid_configuration"],
                CONFIG['client_id'],
                http_client=http_client,
                client_assertion=JwtSigner(
                        private_key,
                        algorithm="RS256",
                        sha1_thumbprint=CONFIG["client_certificate"]["thumbprint"]
                    ).sign_assertion(
                        audience=CONFIG["openid_configuration"]["token_endpoint"],
                        issuer=CONFIG["client_id"],
                    ),
                client_assertion_type=Client.CLIENT_ASSERTION_TYPE_JWT,
                )
        else:
            cls.client = Client(
                CONFIG["openid_configuration"], CONFIG['client_id'],
                http_client=http_client,
                client_secret=CONFIG.get('client_secret'))

    @unittest.skipIf(
        "token_endpoint" not in CONFIG.get("openid_configuration", {}),
        "token_endpoint missing")
    @unittest.skipIf("client_secret" not in CONFIG, "client_secret missing")
    def test_client_credentials(self):
        result = self.client.obtain_token_for_client(CONFIG.get('scope'))
        self.assertIn('access_token', result)

    @unittest.skipIf(
        "token_endpoint" not in CONFIG.get("openid_configuration", {}),
        "token_endpoint missing")
    @unittest.skipIf(
        not ("username" in CONFIG and "password" in CONFIG),
        "username/password missing")
    def test_username_password(self):
        result = self.client.obtain_token_by_username_password(
            CONFIG["username"], CONFIG["password"],
            data={"resource": CONFIG.get("resource")},  # MSFT AAD V1 only
            scope=CONFIG.get("scope"))
        self.assertLoosely(result)

    @unittest.skipUnless(
        "authorization_endpoint" in CONFIG.get("openid_configuration", {}),
        "authorization_endpoint missing")
    def test_auth_code(self):
        port = CONFIG.get("listen_port", 44331)
        redirect_uri = "http://localhost:%s" % port
        nonce = "nonce should contain sufficient entropy"
        auth_request_uri = self.client.build_auth_request_uri(
            "code",
            nonce=nonce,
            redirect_uri=redirect_uri, scope=CONFIG.get("scope"))
        ac = obtain_auth_code(port, auth_uri=auth_request_uri)
        self.assertNotEqual(ac, None)
        result = self.client.obtain_token_by_authorization_code(
            ac,
            data={
                "scope": CONFIG.get("scope"),
                "resource": CONFIG.get("resource"),
                },  # MSFT AAD only
            nonce=nonce,
            redirect_uri=redirect_uri)
        self.assertLoosely(result, lambda: self.assertIn('access_token', result))

    @unittest.skipUnless(
        "authorization_endpoint" in CONFIG.get("openid_configuration", {}),
        "authorization_endpoint missing")
    def test_auth_code_flow(self):
        with AuthCodeReceiver(port=CONFIG.get("listen_port")) as receiver:
            flow = self.client.initiate_auth_code_flow(
                redirect_uri="http://localhost:%d" % receiver.get_port(),
                scope=CONFIG.get("scope"),
                login_hint=CONFIG.get("username"),  # To skip the account selector
                )
            auth_response = receiver.get_auth_response(
                auth_uri=flow["auth_uri"],
                state=flow["state"],  # Optional but recommended
                timeout=120,
                welcome_template="""<html><body>
                    authorization_endpoint = {a}, client_id = {i}
                    <a href="$auth_uri">Sign In</a> or <a href="$abort_uri">Abort</a>
                    </body></html>""".format(
                        a=CONFIG["openid_configuration"]["authorization_endpoint"],
                        i=CONFIG.get("client_id")),
                )
            self.assertIsNotNone(
                auth_response.get("code"), "Error: {}, Detail: {}".format(
                    auth_response.get("error"), auth_response))
            result = self.client.obtain_token_by_auth_code_flow(flow, auth_response)
                #TBD: data={"resource": CONFIG.get("resource")},  # MSFT AAD v1 only
            self.assertLoosely(result, lambda: self.assertIn('access_token', result))

    def test_auth_code_flow_error_response(self):
        with self.assertRaisesRegexp(ValueError, "state missing"):
            self.client.obtain_token_by_auth_code_flow({}, {"code": "foo"})
        with self.assertRaisesRegexp(ValueError, "state mismatch"):
            self.client.obtain_token_by_auth_code_flow({"state": "1"}, {"state": "2"})
        with self.assertRaisesRegexp(ValueError, "scope"):
            self.client.obtain_token_by_auth_code_flow(
                {"state": "s", "scope": ["foo"]}, {"state": "s"}, scope=["bar"])
        self.assertEqual(
            {"error": "foo", "error_uri": "bar"},
            self.client.obtain_token_by_auth_code_flow(
                {"state": "s"},
                {"state": "s", "error": "foo", "error_uri": "bar", "access_token": "fake"}),
            "We should not leak malicious input into our output")

    @unittest.skipUnless(
        "authorization_endpoint" in CONFIG.get("openid_configuration", {}),
        "authorization_endpoint missing")
    def test_obtain_token_by_browser(self):
        result = self.client.obtain_token_by_browser(
            scope=CONFIG.get("scope"),
            redirect_uri=CONFIG.get("redirect_uri"),
            welcome_template="""<html><body>
                authorization_endpoint = {a}, client_id = {i}
                <a href="$auth_uri">Sign In</a> or <a href="$abort_uri">Abort</a>
                </body></html>""".format(
                    a=CONFIG["openid_configuration"]["authorization_endpoint"],
                    i=CONFIG.get("client_id")),
            success_template="<strong>Done. You can close this window now.</strong>",
            login_hint=CONFIG.get("username"),  # To skip the account selector
            timeout=60,
            )
        self.assertLoosely(result, lambda: self.assertIn('access_token', result))

    @unittest.skipUnless(
        CONFIG.get("openid_configuration", {}).get("device_authorization_endpoint"),
        "device_authorization_endpoint is missing")
    def test_device_flow(self):
        flow = self.client.initiate_device_flow(scope=CONFIG.get("scope"))
        try:
            msg = ("Use a web browser to open the page {verification_uri} and "
                "enter the code {user_code} to authenticate.".format(**flow))
        except KeyError:  # Some IdP might not be standard compliant
            msg = flow["message"]  # Not a standard parameter though
        logger.warning(msg)  # Avoid print(...) b/c its output would be buffered

        duration = 30
        logger.warning("We will wait up to %d seconds for you to sign in" % duration)
        flow["expires_at"] = time.time() + duration  # Shorten the time for quick test
        result = self.client.obtain_token_by_device_flow(flow)
        self.assertLoosely(
                result,
                assertion=lambda: self.assertIn('access_token', result),
                skippable_errors=self.client.DEVICE_FLOW_RETRIABLE_ERRORS)


class TestRefreshTokenCallbacks(unittest.TestCase):

    def _dummy(self, url, **kwargs):
        return MinimalResponse(status_code=200, text='{"refresh_token": "new"}')

    def test_rt_being_added(self):
        client = Client(
            {"token_endpoint": "http://example.com/token"},
            "client_id",
            http_client=MinimalHttpClient(),
            on_obtaining_tokens=lambda event:
                self.assertEqual("new", event["response"].get("refresh_token")),
            on_updating_rt=lambda rt_item, new_rt:
                self.fail("This should not be called here"),
            )
        client.obtain_token_by_authorization_code("code", post=self._dummy)

    def test_rt_being_updated(self):
        old_rt = {"refresh_token": "old"}
        client = Client(
            {"token_endpoint": "http://example.com/token"},
            "client_id",
            http_client=MinimalHttpClient(),
            on_obtaining_tokens=lambda event:
                self.assertNotIn("refresh_token", event["response"]),
            on_updating_rt=lambda old, new:  # TODO: ensure it being called
                (self.assertEqual(old_rt, old), self.assertEqual("new", new)),
            )
        client.obtain_token_by_refresh_token(
            {"refresh_token": "old"}, post=self._dummy)

    def test_rt_being_migrated(self):
        old_rt = {"refresh_token": "old"}
        client = Client(
            {"token_endpoint": "http://example.com/token"},
            "client_id",
            http_client=MinimalHttpClient(),
            on_obtaining_tokens=lambda event:
                self.assertEqual("new", event["response"].get("refresh_token")),
            on_updating_rt=lambda rt_item, new_rt:
                self.fail("This should not be called here"),
            )
        client.obtain_token_by_refresh_token(
            {"refresh_token": "old"}, on_updating_rt=False, post=self._dummy)


class TestSessionAccessibility(unittest.TestCase):
    def test_accessing_session_property_for_backward_compatibility(self):
        client = Client({"token_endpoint": "https://example.com"}, "client_id")
        client.session
        client.session.close()
        client.session = "something"