Skip to content

Commit 78b7a38

Browse files
authored
Added samples for AAD authentication for identity client (Azure#15972)
1 parent a209c20 commit 78b7a38

File tree

4 files changed

+78
-12
lines changed

4 files changed

+78
-12
lines changed

sdk/communication/azure-communication-administration/CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## 1.0.0b4 (Unreleased)
44

5+
### Breaking Changes
6+
57
##### `PhoneNumberAdministrationClient`
68
- `begin_reserve_phone_numbers` now takes `display_name`, `description`, `phone_plan_ids`,
79
`area_code`, `quantity`, `location_options`, or `continuation_token` keywords as input.
@@ -13,9 +15,14 @@ Caller must provide one of the following:
1315
(3) only keyword `continuation_token` to restart a poller from a saved state.
1416
- `list_all_orders` renamed to `list_all_reservations`.
1517

18+
### Added
19+
20+
##### `IdentityClient`
21+
- Added support for Azure Active Directory authentication for the Identity client
22+
1623
## 1.0.0b3 (2020-11-16)
1724

18-
**Breaking Changes**
25+
### Breaking Changes
1926

2027
##### `PhoneNumberSearch` renamed to `PhoneNumberReservation`.
2128

@@ -35,7 +42,7 @@ Caller must provide one of the following:
3542
- `begin_purchase_reservation` now returns `LROPoller[PurchaseReservationPolling]`.
3643
- `cancel_search` has been renamed to `cancel_reservation`.
3744

38-
**New Features**
45+
### Added
3946

4047
##### `PhoneNumberAdministrationClient`
4148
- Add long run operation polling method `ReservePhoneNumberPolling`,`PurchaseReservationPolling`,

sdk/communication/azure-communication-administration/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ pip install azure-communication-administration
2424

2525
- Create/revoke scoped user access tokens to access services such as chat, calling, sms. Tokens are issued for a valid Azure Communication identity and can be revoked at any time.
2626

27+
### Initializing Identity Client
28+
```python
29+
# You can find your endpoint and access token from your resource in the Azure Portal
30+
import os
31+
from azure.communication.administration import CommunicationIdentityClient
32+
from azure.identity import DefaultAzureCredential
33+
34+
connection_str = os.getenv('AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING')
35+
endpoint = os.getenv('AZURE_COMMUNICATION_SERVICE_ENDPOINT')
36+
37+
# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have
38+
# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.
39+
identity_client_managed_identity = CommunicationIdentityClient.(endpoint, DefaultAzureCredential())
40+
41+
#You can also authenticate using your connection string
42+
identity_client = CommunicationIdentityClient.from_connection_string(connection_str)
43+
44+
```
45+
2746
## CommunicationPhoneNumberClient
2847
### Initializing Phone Number Client
2948
```python

sdk/communication/azure-communication-administration/samples/identity_samples.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,55 @@ class CommunicationIdentityClientSamples(object):
2121

2222
def __init__(self):
2323
self.connection_string = os.getenv('AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING')
24+
self.endpoint = os.getenv('AZURE_COMMUNICATION_SERVICE_ENDPOINT')
25+
self.client_id = os.getenv('AZURE_CLIENT_ID')
26+
self.client_secret = os.getenv('AZURE_CLIENT_SECRET')
27+
self.tenant_id = os.getnenv('AZURE_TENANT_ID')
2428

2529
def issue_token(self):
2630
from azure.communication.administration import CommunicationIdentityClient
2731

28-
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
32+
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
33+
from azure.identity import DefaultAzureCredential
34+
identity_client = CommunicationIdentityClient(self.endpoint, DefaultAzureCredential())
35+
else:
36+
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
2937
user = identity_client.create_user()
3038
tokenresponse = identity_client.issue_token(user, scopes=["chat"])
3139
print(tokenresponse)
3240

3341
def revoke_tokens(self):
3442
from azure.communication.administration import CommunicationIdentityClient
3543

36-
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
44+
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
45+
from azure.identity import DefaultAzureCredential
46+
identity_client = CommunicationIdentityClient(self.endpoint, DefaultAzureCredential())
47+
else:
48+
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
3749
user = identity_client.create_user()
3850
tokenresponse = identity_client.issue_token(user, scopes=["chat"])
3951
identity_client.revoke_tokens(user)
4052
print(tokenresponse)
4153

4254
def create_user(self):
4355
from azure.communication.administration import CommunicationIdentityClient
44-
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
45-
user = identity_client.create_user()
4656

57+
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
58+
from azure.identity import DefaultAzureCredential
59+
identity_client = CommunicationIdentityClient(self.endpoint, DefaultAzureCredential())
60+
else:
61+
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
62+
user = identity_client.create_user()
4763
print(user.identifier)
4864

4965
def delete_user(self):
5066
from azure.communication.administration import CommunicationIdentityClient
51-
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
67+
68+
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
69+
from azure.identity import DefaultAzureCredential
70+
identity_client = CommunicationIdentityClient(self.endpoint, DefaultAzureCredential())
71+
else:
72+
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
5273
user = identity_client.create_user()
5374
identity_client.delete_user(user)
5475

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,18 @@ class CommunicationIdentityClientSamples(object):
2424

2525
def __init__(self):
2626
self.connection_string = os.getenv('AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING')
27+
self.endpoint = os.getenv('AZURE_COMMUNICATION_SERVICE_ENDPOINT')
28+
self.client_id = os.getenv('AZURE_CLIENT_ID')
29+
self.client_secret = os.getenv('AZURE_CLIENT_SECRET')
30+
self.tenant_id = os.getnenv('AZURE_TENANT_ID')
2731

2832
async def issue_token(self):
2933
from azure.communication.administration.aio import CommunicationIdentityClient
30-
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
34+
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
35+
from azure.identity import DefaultAzureCredential
36+
identity_client = CommunicationIdentityClient(self.endpoint, DefaultAzureCredential())
37+
else:
38+
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
3139

3240
async with identity_client:
3341
user = await identity_client.create_user()
@@ -37,7 +45,11 @@ async def issue_token(self):
3745

3846
async def revoke_tokens(self):
3947
from azure.communication.administration.aio import CommunicationIdentityClient
40-
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
48+
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
49+
from azure.identity import DefaultAzureCredential
50+
identity_client = CommunicationIdentityClient(self.endpoint, DefaultAzureCredential())
51+
else:
52+
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
4153

4254
async with identity_client:
4355
user = await identity_client.create_user()
@@ -47,19 +59,26 @@ async def revoke_tokens(self):
4759

4860
async def create_user(self):
4961
from azure.communication.administration.aio import CommunicationIdentityClient
50-
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
62+
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
63+
from azure.identity import DefaultAzureCredential
64+
identity_client = CommunicationIdentityClient(self.endpoint, DefaultAzureCredential())
65+
else:
66+
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
5167

5268
async with identity_client:
5369
user = await identity_client.create_user()
5470
print(user.identifier)
5571

5672
async def delete_user(self):
5773
from azure.communication.administration.aio import CommunicationIdentityClient
58-
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
74+
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
75+
from azure.identity import DefaultAzureCredential
76+
identity_client = CommunicationIdentityClient(self.endpoint, DefaultAzureCredential())
77+
else:
78+
identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
5979

6080
async with identity_client:
6181
user = await identity_client.create_user()
62-
6382
await identity_client.delete_user(user)
6483

6584
async def main():

0 commit comments

Comments
 (0)