Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bfc0bb6

Browse files
authored
Merge branch 'master' into add-connections-name-param
2 parents d7d6271 + 0bf0176 commit bfc0bb6

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

auth0/asyncify.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import aiohttp
22

3+
from auth0.authentication.base import AuthenticationBase
4+
from auth0.rest import RestClientOptions
35
from auth0.rest_async import AsyncRestClient
46

57

@@ -19,7 +21,7 @@ def asyncify(cls):
1921
if callable(getattr(cls, func)) and not func.startswith("_")
2022
]
2123

22-
class AsyncClient(cls):
24+
class AsyncManagementClient(cls):
2325
def __init__(
2426
self,
2527
domain,
@@ -29,40 +31,47 @@ def __init__(
2931
protocol="https",
3032
rest_options=None,
3133
):
32-
if token is None:
33-
# Wrap the auth client
34-
super().__init__(domain, telemetry, timeout, protocol)
35-
else:
36-
# Wrap the mngtmt client
37-
super().__init__(
38-
domain, token, telemetry, timeout, protocol, rest_options
39-
)
34+
super().__init__(domain, token, telemetry, timeout, protocol, rest_options)
4035
self.client = AsyncRestClient(
4136
jwt=token, telemetry=telemetry, timeout=timeout, options=rest_options
4237
)
4338

44-
class Wrapper(cls):
39+
class AsyncAuthenticationClient(cls):
4540
def __init__(
4641
self,
4742
domain,
48-
token=None,
43+
client_id,
44+
client_secret=None,
45+
client_assertion_signing_key=None,
46+
client_assertion_signing_alg=None,
4947
telemetry=True,
5048
timeout=5.0,
5149
protocol="https",
52-
rest_options=None,
5350
):
54-
if token is None:
55-
# Wrap the auth client
56-
super().__init__(domain, telemetry, timeout, protocol)
57-
else:
58-
# Wrap the mngtmt client
59-
super().__init__(
60-
domain, token, telemetry, timeout, protocol, rest_options
61-
)
62-
63-
self._async_client = AsyncClient(
64-
domain, token, telemetry, timeout, protocol, rest_options
51+
super().__init__(
52+
domain,
53+
client_id,
54+
client_secret,
55+
client_assertion_signing_key,
56+
client_assertion_signing_alg,
57+
telemetry,
58+
timeout,
59+
protocol,
60+
)
61+
self.client = AsyncRestClient(
62+
None,
63+
options=RestClientOptions(
64+
telemetry=telemetry, timeout=timeout, retries=0
65+
),
6566
)
67+
68+
class Wrapper(cls):
69+
def __init__(self, *args, **kwargs):
70+
super().__init__(*args, **kwargs)
71+
if AuthenticationBase in cls.__bases__:
72+
self._async_client = AsyncAuthenticationClient(*args, **kwargs)
73+
else:
74+
self._async_client = AsyncManagementClient(*args, **kwargs)
6675
for method in methods:
6776
setattr(
6877
self,

auth0/test_async/test_asyncify.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
from callee import Attrs
1313

1414
from auth0.asyncify import asyncify
15+
from auth0.authentication import GetToken
1516
from auth0.management import Clients, Guardian, Jobs
1617

1718
clients = re.compile(r"^https://example\.com/api/v2/clients.*")
19+
token = re.compile(r"^https://example\.com/oauth/token.*")
1820
factors = re.compile(r"^https://example\.com/api/v2/guardian/factors.*")
1921
users_imports = re.compile(r"^https://example\.com/api/v2/jobs/users-imports.*")
2022
payload = {"foo": "bar"}
@@ -84,6 +86,31 @@ async def test_post(self, mocked):
8486
timeout=ANY,
8587
)
8688

89+
@aioresponses()
90+
async def test_post_auth(self, mocked):
91+
callback, mock = get_callback()
92+
mocked.post(token, callback=callback)
93+
c = asyncify(GetToken)("example.com", "cid", client_secret="clsec")
94+
self.assertEqual(
95+
await c.login_async(username="usrnm", password="pswd"), payload
96+
)
97+
mock.assert_called_with(
98+
Attrs(path="/oauth/token"),
99+
allow_redirects=True,
100+
json={
101+
"client_id": "cid",
102+
"username": "usrnm",
103+
"password": "pswd",
104+
"realm": None,
105+
"scope": None,
106+
"audience": None,
107+
"grant_type": "http://auth0.com/oauth/grant-type/password-realm",
108+
"client_secret": "clsec",
109+
},
110+
headers={i: headers[i] for i in headers if i != "Authorization"},
111+
timeout=ANY,
112+
)
113+
87114
@aioresponses()
88115
async def test_file_post(self, mocked):
89116
callback, mock = get_callback()

0 commit comments

Comments
 (0)