Skip to content

Commit 1151698

Browse files
[Communication]: Updated ACS packages to use async bearer token credential policy (Azure#18936)
* Updated SMS package to use async bearer token credential policy * Updated Identity and Phone Numbers packages to use async bearer token credential policy * Add decode_url parameter to get_authentication_policy
1 parent 69b0093 commit 1151698

File tree

15 files changed

+69
-26
lines changed

15 files changed

+69
-26
lines changed

sdk/communication/azure-communication-chat/azure/communication/chat/_shared/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def create_access_token(token):
9494
def get_authentication_policy(
9595
endpoint, # type: str
9696
credential, # type: TokenCredential or str
97+
decode_url=False, # type: bool
9798
is_async=False, # type: bool
9899
):
99100
# type: (...) -> BearerTokenCredentialPolicy or HMACCredentialPolicy
@@ -112,12 +113,16 @@ def get_authentication_policy(
112113
if credential is None:
113114
raise ValueError("Parameter 'credential' must not be None.")
114115
if hasattr(credential, "get_token"):
116+
if is_async:
117+
from azure.core.pipeline.policies import AsyncBearerTokenCredentialPolicy
118+
return AsyncBearerTokenCredentialPolicy(
119+
credential, "https://communication.azure.com//.default")
115120
from azure.core.pipeline.policies import BearerTokenCredentialPolicy
116121
return BearerTokenCredentialPolicy(
117122
credential, "https://communication.azure.com//.default")
118123
if isinstance(credential, str):
119124
from .._shared.policy import HMACCredentialsPolicy
120-
return HMACCredentialsPolicy(endpoint, credential, decode_url=is_async)
125+
return HMACCredentialsPolicy(endpoint, credential, decode_url=decode_url)
121126

122127
raise TypeError("Unsupported credential: {}. Use an access token string to use HMACCredentialsPolicy"
123128
"or a token credential from azure.identity".format(type(credential)))

sdk/communication/azure-communication-identity/azure/communication/identity/_shared/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def _convert_expires_on_datetime_to_utc_int(expires_on):
9090
def get_authentication_policy(
9191
endpoint, # type: str
9292
credential, # type: TokenCredential or str
93+
decode_url=False, # type: bool
9394
is_async=False, # type: bool
9495
):
9596
# type: (...) -> BearerTokenCredentialPolicy or HMACCredentialPolicy
@@ -108,12 +109,16 @@ def get_authentication_policy(
108109
if credential is None:
109110
raise ValueError("Parameter 'credential' must not be None.")
110111
if hasattr(credential, "get_token"):
112+
if is_async:
113+
from azure.core.pipeline.policies import AsyncBearerTokenCredentialPolicy
114+
return AsyncBearerTokenCredentialPolicy(
115+
credential, "https://communication.azure.com//.default")
111116
from azure.core.pipeline.policies import BearerTokenCredentialPolicy
112117
return BearerTokenCredentialPolicy(
113118
credential, "https://communication.azure.com//.default")
114119
if isinstance(credential, str):
115120
from .._shared.policy import HMACCredentialsPolicy
116-
return HMACCredentialsPolicy(endpoint, credential, decode_url=is_async)
121+
return HMACCredentialsPolicy(endpoint, credential, decode_url=decode_url)
117122

118123
raise TypeError("Unsupported credential: {}. Use an access token string to use HMACCredentialsPolicy"
119124
"or a token credential from azure.identity".format(type(credential)))

sdk/communication/azure-communication-identity/azure/communication/identity/aio/_communication_identity_client_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(
5252
self._endpoint = endpoint
5353
self._identity_service_client = CommunicationIdentityClientGen(
5454
self._endpoint,
55-
authentication_policy=get_authentication_policy(endpoint, credential, is_async=True),
55+
authentication_policy=get_authentication_policy(endpoint, credential, decode_url=True, is_async=True),
5656
sdk_moniker=SDK_MONIKER,
5757
**kwargs)
5858

sdk/communication/azure-communication-identity/samples/identity_samples_async.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async def get_token(self):
3838
from azure.communication.identity.aio import CommunicationIdentityClient
3939
from azure.communication.identity import CommunicationTokenScope
4040
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
41-
from azure.identity import DefaultAzureCredential
41+
from azure.identity.aio import DefaultAzureCredential
4242
endpoint, _ = parse_connection_str(self.connection_string)
4343
identity_client = CommunicationIdentityClient(endpoint, DefaultAzureCredential())
4444
else:
@@ -54,7 +54,7 @@ async def revoke_tokens(self):
5454
from azure.communication.identity.aio import CommunicationIdentityClient
5555
from azure.communication.identity import CommunicationTokenScope
5656
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
57-
from azure.identity import DefaultAzureCredential
57+
from azure.identity.aio import DefaultAzureCredential
5858
endpoint, _ = parse_connection_str(self.connection_string)
5959
identity_client = CommunicationIdentityClient(endpoint, DefaultAzureCredential())
6060
else:
@@ -70,7 +70,7 @@ async def revoke_tokens(self):
7070
async def create_user(self):
7171
from azure.communication.identity.aio import CommunicationIdentityClient
7272
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
73-
from azure.identity import DefaultAzureCredential
73+
from azure.identity.aio import DefaultAzureCredential
7474
endpoint, _ = parse_connection_str(self.connection_string)
7575
identity_client = CommunicationIdentityClient(endpoint, DefaultAzureCredential())
7676
else:
@@ -85,7 +85,7 @@ async def create_user_and_token(self):
8585
from azure.communication.identity.aio import CommunicationIdentityClient
8686
from azure.communication.identity import CommunicationTokenScope
8787
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
88-
from azure.identity import DefaultAzureCredential
88+
from azure.identity.aio import DefaultAzureCredential
8989
endpoint, _ = parse_connection_str(self.connection_string)
9090
identity_client = CommunicationIdentityClient(endpoint, DefaultAzureCredential())
9191
else:
@@ -100,7 +100,7 @@ async def create_user_and_token(self):
100100
async def delete_user(self):
101101
from azure.communication.identity.aio import CommunicationIdentityClient
102102
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
103-
from azure.identity import DefaultAzureCredential
103+
from azure.identity.aio import DefaultAzureCredential
104104
endpoint, _ = parse_connection_str(self.connection_string)
105105
identity_client = CommunicationIdentityClient(endpoint, DefaultAzureCredential())
106106
else:

sdk/communication/azure-communication-identity/tests/test_communication_identity_client_async.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
from _shared.testcase import BodyReplacerProcessor
1818
from _shared.communication_service_preparer import CommunicationPreparer
1919
from _shared.utils import get_http_logging_policy
20-
from azure.identity import DefaultAzureCredential
20+
from azure.identity.aio import DefaultAzureCredential
2121

2222
class FakeTokenCredential(object):
2323
def __init__(self):
2424
self.token = AccessToken("Fake Token", 0)
2525

26-
def get_token(self, *args):
26+
async def get_token(self, *args):
2727
return self.token
2828
class CommunicationIdentityClientTestAsync(AsyncCommunicationTestCase):
2929
def setUp(self):

sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/_shared/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def create_access_token(token):
8787
def get_authentication_policy(
8888
endpoint, # type: str
8989
credential, # type: TokenCredential or str
90+
decode_url=False, # type: bool
9091
is_async=False, # type: bool
9192
):
9293
# type: (...) -> BearerTokenCredentialPolicy or HMACCredentialPolicy
@@ -105,12 +106,16 @@ def get_authentication_policy(
105106
if credential is None:
106107
raise ValueError("Parameter 'credential' must not be None.")
107108
if hasattr(credential, "get_token"):
109+
if is_async:
110+
from azure.core.pipeline.policies import AsyncBearerTokenCredentialPolicy
111+
return AsyncBearerTokenCredentialPolicy(
112+
credential, "https://communication.azure.com//.default")
108113
from azure.core.pipeline.policies import BearerTokenCredentialPolicy
109114
return BearerTokenCredentialPolicy(
110115
credential, "https://communication.azure.com//.default")
111116
if isinstance(credential, str):
112117
from .._shared.policy import HMACCredentialsPolicy
113-
return HMACCredentialsPolicy(endpoint, credential, decode_url=is_async)
118+
return HMACCredentialsPolicy(endpoint, credential, decode_url=decode_url)
114119

115120
raise TypeError("Unsupported credential: {}. Use an access token string to use HMACCredentialsPolicy"
116121
"or a token credential from azure.identity".format(type(credential)))

sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/aio/_phone_numbers_client_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(
5050
self._endpoint = endpoint
5151
self._phone_number_client = PhoneNumbersClientGen(
5252
self._endpoint,
53-
authentication_policy=get_authentication_policy(endpoint, credential),
53+
authentication_policy=get_authentication_policy(endpoint, credential, is_async=True),
5454
sdk_moniker=SDK_MONIKER,
5555
**kwargs)
5656

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
# -------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See License.txt in the project root for
5+
# license information.
6+
# --------------------------------------------------------------------------
7+
from azure.core.credentials import AccessToken
8+
9+
class AsyncFakeTokenCredential(object):
10+
def __init__(self):
11+
self.token = AccessToken("Fake Token", 0)
12+
13+
async def get_token(self, *args):
14+
return self.token

sdk/communication/azure-communication-phonenumbers/test/_shared/utils.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@
44
# license information.
55
# -------------------------------------------------------------------------
66

7-
from .fake_token_credential import FakeTokenCredential
8-
from azure.identity import DefaultAzureCredential
97
from azure.core.pipeline.policies import HttpLoggingPolicy
108

119
def create_token_credential():
1210
# type: () -> FakeTokenCredential or DefaultAzureCredential
1311
from devtools_testutils import is_live
1412
if not is_live():
13+
from .fake_token_credential import FakeTokenCredential
1514
return FakeTokenCredential()
15+
from azure.identity import DefaultAzureCredential
16+
return DefaultAzureCredential()
17+
18+
def async_create_token_credential():
19+
# type: () -> AsyncFakeTokenCredential or DefaultAzureCredential
20+
from devtools_testutils import is_live
21+
if not is_live():
22+
from .async_fake_token_credential import AsyncFakeTokenCredential
23+
return AsyncFakeTokenCredential()
24+
from azure.identity.aio import DefaultAzureCredential
1625
return DefaultAzureCredential()
1726

1827
def get_http_logging_policy(**kwargs):

sdk/communication/azure-communication-phonenumbers/test/test_phone_number_administration_client_async.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from _shared.asynctestcase import AsyncCommunicationTestCase
55
from _shared.testcase import ResponseReplacerProcessor, BodyReplacerProcessor
66
from _shared.utils import (
7-
create_token_credential,
7+
async_create_token_credential,
88
get_http_logging_policy
99
)
1010
from azure.communication.phonenumbers import (
@@ -46,7 +46,7 @@ def setUp(self):
4646
@AsyncCommunicationTestCase.await_prepared_test
4747
async def test_list_purchased_phone_numbers_from_managed_identity(self):
4848
endpoint, access_key = parse_connection_str(self.connection_str)
49-
credential = create_token_credential()
49+
credential = async_create_token_credential()
5050
phone_number_client = PhoneNumbersClient(
5151
endpoint,
5252
credential,
@@ -71,7 +71,7 @@ async def test_list_purchased_phone_numbers(self):
7171
@AsyncCommunicationTestCase.await_prepared_test
7272
async def test_get_purchased_phone_number_from_managed_identity(self):
7373
endpoint, access_key = parse_connection_str(self.connection_str)
74-
credential = create_token_credential()
74+
credential = async_create_token_credential()
7575
phone_number_client = PhoneNumbersClient(
7676
endpoint,
7777
credential,
@@ -91,7 +91,7 @@ async def test_get_purchased_phone_number(self):
9191
@AsyncCommunicationTestCase.await_prepared_test
9292
async def test_search_available_phone_numbers_from_managed_identity(self):
9393
endpoint, access_key = parse_connection_str(self.connection_str)
94-
credential = create_token_credential()
94+
credential = async_create_token_credential()
9595
phone_number_client = PhoneNumbersClient(
9696
endpoint,
9797
credential,
@@ -148,7 +148,7 @@ async def test_update_phone_number_capabilities(self):
148148
@AsyncCommunicationTestCase.await_prepared_test
149149
async def test_update_phone_number_capabilities_from_managed_identity(self):
150150
endpoint, access_key = parse_connection_str(self.connection_str)
151-
credential = create_token_credential()
151+
credential = async_create_token_credential()
152152
phone_number_client = PhoneNumbersClient(
153153
endpoint,
154154
credential,
@@ -171,7 +171,7 @@ async def test_update_phone_number_capabilities_from_managed_identity(self):
171171
@AsyncCommunicationTestCase.await_prepared_test
172172
async def test_purchase_phone_numbers_from_managed_identity(self):
173173
endpoint, access_key = parse_connection_str(self.connection_str)
174-
credential = create_token_credential()
174+
credential = async_create_token_credential()
175175
phone_number_client = PhoneNumbersClient(
176176
endpoint,
177177
credential,

0 commit comments

Comments
 (0)