azure.communication.identity package

class azure.communication.identity.CommunicationIdentifier(*args, **kwds)[source]

Communication Identifier.

Variables
kind: Optional[str] = None
properties: Mapping[str, Any] = {}
raw_id: Optional[str] = None
class azure.communication.identity.CommunicationIdentifierKind(value)[source]

Communication Identifier Kind.

COMMUNICATION_USER = 'communication_user'
MICROSOFT_TEAMS_USER = 'microsoft_teams_user'
PHONE_NUMBER = 'phone_number'
UNKNOWN = 'unknown'
class azure.communication.identity.CommunicationIdentityClient(endpoint: str, credential: TokenCredential, **kwargs: Any)[source]

Azure Communication Services Identity client.

Parameters
  • endpoint (str) – The endpoint url for Azure Communication Service resource.

  • credential (TokenCredential) – The TokenCredential we use to authenticate against the service.

Example:

: utf-8

-------------------------------------------------------------------
ght (c) Microsoft Corporation. All rights reserved.
ed under the MIT License. See License.txt in the project root for
e information.
--------------------------------------------------------------------


entity_sample.py
ION:
e samples demonstrate creating a user, issuing a token, revoking a token and deleting a user.


on identity_samples.py
the environment variables with your own values before running the sample:
OMMUNICATION_SAMPLES_CONNECTION_STRING - the connection string in your Communication Services resource
ZURE_CLIENT_ID - the client ID of your active directory application
ZURE_CLIENT_SECRET - the secret of your active directory application
ZURE_TENANT_ID - the tenant ID of your active directory application
OMMUNICATION_M365_APP_ID - the application id of M365
OMMUNICATION_M365_AAD_AUTHORITY - the AAD authority of M365  
OMMUNICATION_M365_AAD_TENANT - the tenant ID of M365 application
OMMUNICATION_M365_SCOPE - the scope of M365 application
OMMUNICATION_MSAL_USERNAME - the username for authenticating via MSAL library
COMMUNICATION_MSAL_PASSWORD - the password for authenticating via MSAL library 

s
re.communication.identity._shared.utils import parse_connection_str
l import PublicClientApplication
mmunicationIdentityClientSamples(object):

__init__(self):
self.connection_string = os.getenv('COMMUNICATION_SAMPLES_CONNECTION_STRING')
self.client_id = os.getenv('AZURE_CLIENT_ID')
self.client_secret = os.getenv('AZURE_CLIENT_SECRET')
self.tenant_id = os.getenv('AZURE_TENANT_ID')
self.m365_app_id = os.getenv('COMMUNICATION_M365_APP_ID') 
self.m365_aad_authority = os.getenv('COMMUNICATION_M365_AAD_AUTHORITY') 
self.m365_aad_tenant = os.getenv('COMMUNICATION_M365_AAD_TENANT')
self.m365_scope = os.getenv('COMMUNICATION_M365_SCOPE') 
self.msal_username = os.getenv('COMMUNICATION_MSAL_USERNAME') 
self.msal_password = os.getenv('COMMUNICATION_MSAL_PASSWORD') 

get_token(self):
from azure.communication.identity import (
    CommunicationIdentityClient,
    CommunicationTokenScope
)

if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
    from azure.identity import DefaultAzureCredential
    endpoint, _ = parse_connection_str(self.connection_string)
    identity_client = CommunicationIdentityClient(endpoint, DefaultAzureCredential())
else:
    identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
user = identity_client.create_user()
print("Getting token for: " + user.properties.get('id'))
tokenresponse = identity_client.get_token(user, scopes=[CommunicationTokenScope.CHAT])
print("Token issued with value: " + tokenresponse.token)

revoke_tokens(self):
from azure.communication.identity import (
    CommunicationIdentityClient,
    CommunicationTokenScope
)

if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
    from azure.identity import DefaultAzureCredential
    endpoint, _ = parse_connection_str(self.connection_string)
    identity_client = CommunicationIdentityClient(endpoint, DefaultAzureCredential())
else:
    identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
user = identity_client.create_user()
tokenresponse = identity_client.get_token(user, scopes=[CommunicationTokenScope.CHAT])
print("Revoking token: " + tokenresponse.token)
identity_client.revoke_tokens(user)
print(tokenresponse.token + " revoked successfully")

create_user(self):
from azure.communication.identity import CommunicationIdentityClient

if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
    from azure.identity import DefaultAzureCredential
    endpoint, _ = parse_connection_str(self.connection_string)
    identity_client = CommunicationIdentityClient(endpoint, DefaultAzureCredential())
else:
    identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
print("Creating new user")
user = identity_client.create_user()
print("User created with id:" + user.properties.get('id'))

create_user_and_token(self):
from azure.communication.identity import (
    CommunicationIdentityClient,
    CommunicationTokenScope
)
if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
    from azure.identity import DefaultAzureCredential
    endpoint, _ = parse_connection_str(self.connection_string)
    identity_client = CommunicationIdentityClient(endpoint, DefaultAzureCredential())
else:
    identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
print("Creating new user with token")
user, tokenresponse = identity_client.create_user_and_token(scopes=[CommunicationTokenScope.CHAT])
print("User created with id:" + user.properties.get('id'))
print("Token issued with value: " + tokenresponse.token)

delete_user(self):
from azure.communication.identity import CommunicationIdentityClient

if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
    from azure.identity import DefaultAzureCredential
    endpoint, _ = parse_connection_str(self.connection_string)
    identity_client = CommunicationIdentityClient(endpoint, DefaultAzureCredential())
else:
    identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
user = identity_client.create_user()
print("Deleting user: " + user.properties.get('id'))
identity_client.delete_user(user)
print(user.properties.get('id') + " deleted")

get_token_for_teams_user(self):
if (os.getenv("SKIP_INT_IDENTITY_EXCHANGE_TOKEN_TEST") == "true"):
    print("Skipping the Get Access Token for Teams User sample")
    return
from azure.communication.identity import CommunicationIdentityClient

if self.client_id is not None and self.client_secret is not None and self.tenant_id is not None:
    from azure.identity import DefaultAzureCredential
    endpoint, _ = parse_connection_str(self.connection_string)
    identity_client = CommunicationIdentityClient(endpoint, DefaultAzureCredential())
else:
    identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)

msal_app = PublicClientApplication(client_id=self.m365_app_id, authority="{}/{}".format(self.m365_aad_authority, self.m365_aad_tenant))
result = msal_app.acquire_token_by_username_password(
    username=self.msal_username,
    password=self.msal_password,
    scopes=[self.m365_scope])
add_token =  result["access_token"]
print("AAD access token of a Teams User: " + add_token)

tokenresponse = identity_client.get_token_for_teams_user(add_token)
print("Token issued with value: " + tokenresponse.token)


e__ == '__main__':
le = CommunicationIdentityClientSamples()
le.create_user()
le.create_user_and_token()
le.get_token()
le.revoke_tokens()
le.delete_user() 
le.get_token_for_teams_user()
create_user(**kwargs: Any)CommunicationUserIdentifier[source]

create a single Communication user

Returns

CommunicationUserIdentifier

Return type

CommunicationUserIdentifier

create_user_and_token(scopes: List[Union[str, CommunicationTokenScope]], **kwargs: Any)Tuple[CommunicationUserIdentifier, AccessToken][source]

Create a single Communication user with an identity token.

Parameters

scopes (list[str or CommunicationTokenScope]) – List of scopes to be added to the token.

Returns

A tuple of a CommunicationUserIdentifier and a AccessToken.

Return type

tuple of (CommunicationUserIdentifier, AccessToken)

delete_user(user: CommunicationUserIdentifier, **kwargs: Any)None[source]

Triggers revocation event for user and deletes all its data.

Parameters

user (CommunicationUserIdentifier) – Azure Communication User to delete

Returns

None

Return type

None

classmethod from_connection_string(conn_str: str, **kwargs: Any)azure.communication.identity._communication_identity_client.CommunicationIdentityClient[source]

Create CommunicationIdentityClient from a Connection String.

Parameters

conn_str (str) – A connection string to an Azure Communication Service resource.

Returns

Instance of CommunicationIdentityClient.

Return type

CommunicationIdentityClient

Example:

get_token(user: CommunicationUserIdentifier, scopes, **kwargs: Any)AccessToken[source]

Generates a new token for an identity.

Parameters
Returns

AccessToken

Return type

AccessToken

get_token_for_teams_user(add_token: str, **kwargs)AccessToken[source]

Exchanges an AAD access token of a Teams User for a new Communication Identity access token.

Parameters

add_token (str) – an AAD access token of a Teams User

Returns

AccessToken

Return type

AccessToken

revoke_tokens(user: CommunicationUserIdentifier, **kwargs: Any)None[source]

Schedule revocation of all tokens of an identity.

Parameters

user () – Azure Communication User.

Returns

None

Return type

None

class azure.communication.identity.CommunicationTokenScope(value)[source]

List of scopes for an access token.

CHAT = 'chat'
VOIP = 'voip'
class azure.communication.identity.CommunicationUserIdentifier(id: str, **kwargs: Any)[source]

Represents a user in Azure Communication Service.

Variables
  • raw_id (str) – Optional raw ID of the identifier.

  • kind (str or CommunicationIdentifierKind) – The type of identifier.

  • Any] properties (Mapping[str,) –

    The properties of the identifier. The keys in this mapping include:

    • `id`(str): ID of the Communication user as returned from Azure Communication Identity.

Parameters

id (str) – ID of the Communication user as returned from Azure Communication Identity.

kind = 'communication_user'
class azure.communication.identity.CommunicationUserProperties(**kwargs)
clear()None.  Remove all items from D.
copy()a shallow copy of D
fromkeys(value=None, /)

Returns a new dict with keys from iterable and values equal to value.

get(k[, d])D[k] if k in D, else d.  d defaults to None.
items()a set-like object providing a view on D’s items
keys()a set-like object providing a view on D’s keys
pop(k[, d])v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised

popitem()(k, v), remove and return some (key, value) pair as a

2-tuple; but raise KeyError if D is empty.

setdefault(k[, d])D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F)None.  Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values()an object providing a view on D’s values
id: str