Skip to content

Commit

Permalink
Added support for AAD auth
Browse files Browse the repository at this point in the history
  • Loading branch information
jorge-beauregard committed Jan 11, 2021
1 parent 0d70f57 commit 8c1e1c2
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ Caller must provide one of the following:
### Added

##### `IdentityClient`
- Added support for Azure Active Directory authentication for the Identity client
- Added support for Azure Active Directory authentication

#### `PhoneNumberAdministrationClient`
- Added support for Azure Active Directory authentication
## 1.0.0b3 (2020-11-16)

### Breaking Changes
Expand Down
13 changes: 11 additions & 2 deletions sdk/communication/azure-communication-administration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ endpoint = os.getenv('AZURE_COMMUNICATION_SERVICE_ENDPOINT')

# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have
# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.
identity_client_managed_identity = CommunicationIdentityClient.(endpoint, DefaultAzureCredential())
identity_client_managed_identity = CommunicationIdentityClient(endpoint, DefaultAzureCredential())

#You can also authenticate using your connection string
# You can also authenticate using your connection string
identity_client = CommunicationIdentityClient.from_connection_string(connection_str)

```
Expand All @@ -49,9 +49,18 @@ identity_client = CommunicationIdentityClient.from_connection_string(connection_
# You can find your endpoint and access token from your resource in the Azure Portal
import os
from azure.communication.administration import PhoneNumberAdministrationClient
from azure.identity import DefaultAzureCredential

connection_str = os.getenv('AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING')
endpoint = os.getenv('AZURE_COMMUNICATION_SERVICE_ENDPOINT')

# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have
# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.
phone_number_administration_client = PhoneNumberAdministrationClient(endpoint, DefaultAzureCredential())

# You can also authenticate using your connection string
phone_number_administration_client = PhoneNumberAdministrationClient.from_connection_string(connection_str)

```
### Phone plans overview

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
UpdatePhoneNumberCapabilitiesResponse
)

from ._shared.utils import parse_connection_str
from ._shared.policy import HMACCredentialsPolicy
from ._shared.utils import parse_connection_str, get_authentication_policy
from ._version import SDK_MONIKER

class PhoneNumberAdministrationClient(object):
Expand Down Expand Up @@ -64,7 +63,7 @@ def __init__(
self._endpoint = endpoint
self._phone_number_administration_client = PhoneNumberAdministrationClientGen(
self._endpoint,
authentication_policy=HMACCredentialsPolicy(endpoint, credential),
authentication_policy=get_authentication_policy(endpoint, credential),
sdk_moniker=SDK_MONIKER,
**kwargs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
UpdatePhoneNumberCapabilitiesResponse
)

from .._shared.utils import parse_connection_str
from .._shared.policy import HMACCredentialsPolicy
from .._shared.utils import parse_connection_str, get_authentication_policy

class PhoneNumberAdministrationClient(object):
"""Azure Communication Services Phone Number Management client.
Expand Down Expand Up @@ -70,7 +69,7 @@ def __init__(
self._endpoint = endpoint
self._phone_number_administration_client = PhoneNumberAdministrationClientGen(
self._endpoint,
authentication_policy=HMACCredentialsPolicy(endpoint, credential),
authentication_policy=get_authentication_policy(endpoint, credential),
sdk_moniker=SDK_MONIKER,
**kwargs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@
NumberUpdateCapabilities,
CreateSearchOptions
)
from azure.core.credentials import AccessToken
from phone_number_helper import PhoneNumberUriReplacer
from phone_number_testcase import PhoneNumberCommunicationTestCase
from _shared.testcase import BodyReplacerProcessor
from azure.identity import DefaultAzureCredential
from azure.communication.administration._shared.utils import parse_connection_str

SKIP_PHONE_NUMBER_TESTS = True
PHONE_NUMBER_TEST_SKIP_REASON= "Phone Number Administration live tests infra not ready yet"

class PhoneNumberAdministrationClientTest(PhoneNumberCommunicationTestCase):
class FakeTokenCredential(object):
def __init__(self):
self.token = AccessToken("Fake Token", 0)

def get_token(self, *args):
return self.token
class PhoneNumberAdministrationClientTest(PhoneNumberCommunicationTestCase):
def setUp(self):
super(PhoneNumberAdministrationClientTest, self).setUp()
self.recording_processors.extend([
Expand Down Expand Up @@ -125,6 +133,20 @@ def setUp(self):
self.capabilities_id = "capabilities_id"
self.release_id = "release_id"

@pytest.mark.live_test_only
@pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON)
def test_list_all_phone_numbers_from_managed_identity(self, connection_string):
endpoint, access_key = parse_connection_str(connection_string)
from devtools_testutils import is_live
if not is_live():
credential = FakeTokenCredential()
else:
credential = DefaultAzureCredential()
phone_number_client = PhoneNumberAdministrationClient(endpoint, credential)
user = identity_client.create_user()

assert user.identifier is not None

@pytest.mark.live_test_only
@pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON)
def test_list_all_phone_numbers(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
# license information.
# --------------------------------------------------------------------------
import pytest
from azure.core.credentials import AccessToken
from azure.communication.administration.aio import PhoneNumberAdministrationClient
from azure.communication.administration._shared.utils import parse_connection_str
from azure.communication.administration import (
PstnConfiguration,
NumberUpdateCapabilities,
Expand All @@ -14,11 +16,18 @@
from phone_number_helper import PhoneNumberUriReplacer
from phone_number_testcase_async import AsyncPhoneNumberCommunicationTestCase
from _shared.testcase import BodyReplacerProcessor, ResponseReplacerProcessor
from azure.identity import DefaultAzureCredential
import os

SKIP_PHONE_NUMBER_TESTS = True
PHONE_NUMBER_TEST_SKIP_REASON= "Phone Number Administration live tests infra not ready yet"

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

def get_token(self, *args):
return self.token
class PhoneNumberAdministrationClientTestAsync(AsyncPhoneNumberCommunicationTestCase):

def setUp(self):
Expand Down Expand Up @@ -126,6 +135,22 @@ def setUp(self):
self.capabilities_id = "capabilities_id"
self.release_id = "release_id"

@AsyncPhoneNumberCommunicationTestCase.await_prepared_test
@pytest.mark.live_test_only
@pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON)
async def test_create_user_from_managed_identity(self, connection_string):
endpoint, access_key = parse_connection_str(connection_string)
from devtools_testutils import is_live
if not is_live():
credential = FakeTokenCredential()
else:
credential = DefaultAzureCredential()
identity_client = CommunicationIdentityClient(endpoint, credential)
async with identity_client:
user = await identity_client.create_user()

assert user.identifier is not None

@AsyncPhoneNumberCommunicationTestCase.await_prepared_test
@pytest.mark.live_test_only
@pytest.mark.skipif(SKIP_PHONE_NUMBER_TESTS, reason=PHONE_NUMBER_TEST_SKIP_REASON)
Expand Down

0 comments on commit 8c1e1c2

Please sign in to comment.