Debian Codegraph @

tests/authentication/test_get_token.py

raw symbols SCIP
1 import unittest
2 import requests
3 from fnmatch import fnmatch
4 from unittest import mock
5 from unittest.mock import ANY
6
7 from cryptography.hazmat.primitives import asymmetric, serialization
8
9 from auth0.authentication.exceptions import RateLimitError
10 from auth0.authentication.get_token import GetToken
11
12
13 def get_private_key():
14 private_key = asymmetric.rsa.generate_private_key(
15 public_exponent=65537,
16 key_size=2048,
17 )
18 return private_key.private_bytes(
19 encoding=serialization.Encoding.PEM,
20 format=serialization.PrivateFormat.PKCS8,
21 encryption_algorithm=serialization.NoEncryption(),
22 )
23
24
25 class TestGetToken(unittest.TestCase):
26 @mock.patch("auth0.authentication.rest.RestClient.post")
27 def test_authorization_code(self, mock_post):
28 g = GetToken("my.domain.com", "cid", client_secret="clsec")
29
30 g.authorization_code(
31 code="cd",
32 grant_type="gt",
33 redirect_uri="idt",
34 )
35
36 args, kwargs = mock_post.call_args
37
38 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
39 self.assertEqual(
40 kwargs["data"],
41 {
42 "client_id": "cid",
43 "client_secret": "clsec",
44 "code": "cd",
45 "grant_type": "gt",
46 "redirect_uri": "idt",
47 },
48 )
49
50 @mock.patch("auth0.authentication.rest.RestClient.post")
51 def test_authorization_code_with_client_assertion(self, mock_post):
52 g = GetToken(
53 "my.domain.com", "cid", client_assertion_signing_key=get_private_key()
54 )
55
56 g.authorization_code(code="cd", grant_type="gt", redirect_uri="idt")
57
58 args, kwargs = mock_post.call_args
59
60 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
61 self.assertEqual(
62 kwargs["data"],
63 {
64 "client_id": "cid",
65 "client_assertion": ANY,
66 "client_assertion_type": "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
67 "code": "cd",
68 "grant_type": "gt",
69 "redirect_uri": "idt",
70 },
71 )
72
73 self.assertTrue(fnmatch(kwargs["data"]["client_assertion"], "*.*.*"))
74
75 @mock.patch("auth0.authentication.rest.RestClient.post")
76 def test_authorization_code_pkce(self, mock_post):
77 g = GetToken("my.domain.com", "cid")
78
79 g.authorization_code_pkce(
80 code_verifier="cdver",
81 code="cd",
82 grant_type="gt",
83 redirect_uri="idt",
84 )
85
86 args, kwargs = mock_post.call_args
87
88 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
89 self.assertEqual(
90 kwargs["data"],
91 {
92 "client_id": "cid",
93 "code_verifier": "cdver",
94 "code": "cd",
95 "grant_type": "gt",
96 "redirect_uri": "idt",
97 },
98 )
99
100 @mock.patch("auth0.authentication.rest.RestClient.post")
101 def test_client_credentials(self, mock_post):
102 g = GetToken("my.domain.com", "cid", client_secret="clsec")
103
104 g.client_credentials(audience="aud", grant_type="gt")
105
106 args, kwargs = mock_post.call_args
107
108 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
109 self.assertEqual(
110 kwargs["data"],
111 {
112 "client_id": "cid",
113 "client_secret": "clsec",
114 "audience": "aud",
115 "grant_type": "gt",
116 "organization": None,
117 },
118 )
119
120 @mock.patch("auth0.authentication.rest.RestClient.post")
121 def test_client_credentials_with_client_assertion(self, mock_post):
122 g = GetToken(
123 "my.domain.com", "cid", client_assertion_signing_key=get_private_key()
124 )
125
126 g.client_credentials("aud", grant_type="gt")
127
128 args, kwargs = mock_post.call_args
129
130 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
131 self.assertEqual(
132 kwargs["data"],
133 {
134 "client_id": "cid",
135 "client_assertion": ANY,
136 "client_assertion_type": "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
137 "audience": "aud",
138 "grant_type": "gt",
139 "organization": None,
140 },
141 )
142
143 self.assertTrue(fnmatch(kwargs["data"]["client_assertion"], "*.*.*"))
144
145 @mock.patch("auth0.authentication.rest.RestClient.post")
146 def test_client_credentials_with_organization(self, mock_post):
147 g = GetToken("my.domain.com", "cid", client_secret="clsec")
148
149 g.client_credentials("aud", organization="my-org")
150
151 args, kwargs = mock_post.call_args
152
153 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
154 self.assertEqual(
155 kwargs["data"],
156 {
157 "client_id": "cid",
158 "grant_type": "client_credentials",
159 "client_secret": "clsec",
160 "audience": "aud",
161 "organization": "my-org",
162 },
163 )
164
165 @mock.patch("auth0.authentication.rest.RestClient.post")
166 def test_login(self, mock_post):
167 g = GetToken("my.domain.com", "cid", client_secret="clsec")
168
169 g.login(
170 username="usrnm",
171 password="pswd",
172 scope="http://test.com/api",
173 realm="rlm",
174 audience="aud",
175 grant_type="gt",
176 )
177
178 args, kwargs = mock_post.call_args
179
180 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
181 self.assertEqual(
182 kwargs["data"],
183 {
184 "client_id": "cid",
185 "client_secret": "clsec",
186 "username": "usrnm",
187 "password": "pswd",
188 "scope": "http://test.com/api",
189 "realm": "rlm",
190 "audience": "aud",
191 "grant_type": "gt",
192 },
193 )
194
195 @mock.patch("auth0.authentication.rest.RestClient.post")
196 def test_login_simple(self, mock_post):
197 g = GetToken("my.domain.com", "cid", client_secret="clsec")
198
199 g.login(
200 username="usrnm",
201 password="pswd",
202 )
203
204 args, kwargs = mock_post.call_args
205
206 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
207 self.assertEqual(
208 kwargs["data"],
209 {
210 "client_id": "cid",
211 "client_secret": "clsec",
212 "username": "usrnm",
213 "password": "pswd",
214 "realm": None,
215 "scope": None,
216 "audience": None,
217 "grant_type": "http://auth0.com/oauth/grant-type/password-realm",
218 },
219 )
220
221 @mock.patch("auth0.authentication.rest.RestClient.post")
222 def test_login_with_forwarded_for(self, mock_post):
223 g = GetToken("my.domain.com", "cid", client_secret="clsec")
224
225 g.login(username="usrnm", password="pswd", forwarded_for="192.168.0.1")
226
227 args, kwargs = mock_post.call_args
228
229 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
230 self.assertEqual(
231 kwargs["headers"],
232 {
233 "auth0-forwarded-for": "192.168.0.1",
234 },
235 )
236
237 @mock.patch("auth0.authentication.rest.RestClient.post")
238 def test_refresh_token(self, mock_post):
239 g = GetToken("my.domain.com", "cid", client_secret="clsec")
240
241 g.refresh_token(
242 refresh_token="rt",
243 grant_type="gt",
244 scope="s",
245 )
246
247 args, kwargs = mock_post.call_args
248
249 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
250 self.assertEqual(
251 kwargs["data"],
252 {
253 "client_id": "cid",
254 "client_secret": "clsec",
255 "refresh_token": "rt",
256 "grant_type": "gt",
257 "scope": "s",
258 },
259 )
260
261 @mock.patch("auth0.authentication.rest.RestClient.post")
262 def test_passwordless_login_with_sms(self, mock_post):
263 g = GetToken("my.domain.com", "cid", client_secret="csec")
264
265 g.passwordless_login(
266 username="123456",
267 otp="abcd",
268 realm="sms",
269 audience="aud",
270 scope="openid",
271 )
272
273 args, kwargs = mock_post.call_args
274
275 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
276 self.assertEqual(
277 kwargs["data"],
278 {
279 "client_id": "cid",
280 "client_secret": "csec",
281 "realm": "sms",
282 "grant_type": "http://auth0.com/oauth/grant-type/passwordless/otp",
283 "username": "123456",
284 "otp": "abcd",
285 "audience": "aud",
286 "scope": "openid",
287 },
288 )
289
290 @mock.patch("auth0.authentication.rest.RestClient.post")
291 def test_passwordless_login_with_email(self, mock_post):
292 g = GetToken("my.domain.com", "cid", client_secret="csec")
293
294 g.passwordless_login(
295 username="[email protected]",
296 otp="abcd",
297 realm="email",
298 audience="aud",
299 scope="openid",
300 )
301
302 args, kwargs = mock_post.call_args
303
304 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
305 self.assertEqual(
306 kwargs["data"],
307 {
308 "client_id": "cid",
309 "client_secret": "csec",
310 "realm": "email",
311 "grant_type": "http://auth0.com/oauth/grant-type/passwordless/otp",
312 "username": "[email protected]",
313 "otp": "abcd",
314 "audience": "aud",
315 "scope": "openid",
316 },
317 )
318
319 @mock.patch("auth0.authentication.rest.RestClient.post")
320 def test_backchannel_login(self, mock_post):
321 g = GetToken("my.domain.com", "cid", client_secret="csec")
322
323 g.backchannel_login(
324 auth_req_id="reqid",
325 grant_type="urn:openid:params:grant-type:ciba",
326 )
327
328 args, kwargs = mock_post.call_args
329
330 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
331 self.assertEqual(
332 kwargs["data"],
333 {
334 "client_id": "cid",
335 "client_secret": "csec",
336 "auth_req_id": "reqid",
337 "grant_type": "urn:openid:params:grant-type:ciba",
338 },
339 )
340
341 @mock.patch("requests.request")
342 def test_backchannel_login_headers_on_slow_down(self, mock_requests_request):
343 response = requests.Response()
344 response.status_code = 429
345 response.headers = {"Retry-After": "100"}
346 response._content = b'{"error":"slow_down"}'
347 mock_requests_request.return_value = response
348
349 g = GetToken("my.domain.com", "cid", client_secret="csec")
350
351 with self.assertRaises(RateLimitError) as context:
352 g.backchannel_login(
353 auth_req_id="reqid",
354 grant_type="urn:openid:params:grant-type:ciba",
355 )
356 self.assertEqual(context.exception.headers["Retry-After"], "100")
357 self.assertEqual(context.exception.status_code, 429)
358
359 @mock.patch("auth0.authentication.rest.RestClient.post")
360 def test_connection_login(self, mock_post):
361 g = GetToken("my.domain.com", "cid", client_secret="csec")
362
363 g.access_token_for_connection(
364 grant_type="urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token",
365 subject_token_type="urn:ietf:params:oauth:token-type:refresh_token",
366 subject_token="refid",
367 requested_token_type="http://auth0.com/oauth/token-type/federated-connection-access-token",
368 connection="google-oauth2"
369 )
370
371 args, kwargs = mock_post.call_args
372
373 print(kwargs["data"])
374
375 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
376 self.assertEqual(
377 kwargs["data"],
378 {
379 "grant_type": "urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token",
380 "client_id": "cid",
381 "client_secret": "csec",
382 "subject_token_type": "urn:ietf:params:oauth:token-type:refresh_token",
383 "subject_token": "refid",
384 "requested_token_type": "http://auth0.com/oauth/token-type/federated-connection-access-token",
385 "connection": "google-oauth2"
386 },
387 )
388
389 @mock.patch("auth0.authentication.rest.RestClient.post")
390 def test_connection_login_with_login_hint(self, mock_post):
391 g = GetToken("my.domain.com", "cid", client_secret="csec")
392
393 g.access_token_for_connection(
394 subject_token_type="urn:ietf:params:oauth:token-type:refresh_token",
395 subject_token="refid",
396 requested_token_type="http://auth0.com/oauth/token-type/federated-connection-access-token",
397 connection="google-oauth2",
398 login_hint="[email protected]"
399 )
400
401 args, kwargs = mock_post.call_args
402
403 self.assertEqual(args[0], "https://my.domain.com/oauth/token")
404 self.assertEqual(
405 kwargs["data"],
406 {
407 "grant_type": "urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token",
408 "client_id": "cid",
409 "client_secret": "csec",
410 "subject_token_type": "urn:ietf:params:oauth:token-type:refresh_token",
411 "subject_token": "refid",
412 "requested_token_type": "http://auth0.com/oauth/token-type/federated-connection-access-token",
413 "connection": "google-oauth2",
414 "login_hint": "[email protected]"
415 },
416 )