azure.communication.identity package

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.

uthenticating a client via a connection string

on identity_samples.py
the environment variables with your own values before running the sample:
ZURE_COMMUNICATION_SERVICE_ENDPOINT - Communication Service endpoint url

s

mmunicationIdentityClientSamples(object):

__init__(self):
self.connection_string = os.getenv('AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING')
self.endpoint = os.getenv('AZURE_COMMUNICATION_SERVICE_ENDPOINT')
self.client_id = os.getenv('AZURE_CLIENT_ID')
self.client_secret = os.getenv('AZURE_CLIENT_SECRET')
self.tenant_id = os.getenv('AZURE_TENANT_ID')

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
    identity_client = CommunicationIdentityClient(self.endpoint, DefaultAzureCredential())
else:
    identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
user = identity_client.create_user()
print("Getting token for: " + user.identifier)
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
    identity_client = CommunicationIdentityClient(self.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
    identity_client = CommunicationIdentityClient(self.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.identifier)

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
    identity_client = CommunicationIdentityClient(self.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.identifier)
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
    identity_client = CommunicationIdentityClient(self.endpoint, DefaultAzureCredential())
else:
    identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string)
user = identity_client.create_user()
print("Deleting user: " + user.identifier)
identity_client.delete_user(user)
print(user.identifier + " deleted")

e__ == '__main__':
le = CommunicationIdentityClientSamples()
le.create_user()
le.create_user_and_token()
le.get_token()
le.revoke_tokens()
le.delete_user()
create_user(**kwargs) → azure.communication.identity._shared.models.CommunicationUserIdentifier[source]

create a single Communication user

Returns

CommunicationUserIdentifier

Return type

CommunicationUserIdentifier

create_user_and_token(scopes: List[Union[str, ‘_model.CommunicationTokenScope’]], **kwargs: Any) → Tuple[CommunicationUserIdentifier, AccessToken][source]

create a single Communication user with an identity token. :param scopes:

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(communication_user: CommunicationUserIdentifier, **kwargs: Any)None[source]

Triggers revocation event for user and deletes all its data.

Parameters

communication_user (CommunicationUserIdentifier) – Azure Communication User to delete

Returns

None

Return type

None

classmethod from_connection_string(conn_str: str, **kwargs: Any) → 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

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[source]

List of scopes for an access token.

CHAT = 'chat'
VOIP = 'voip'
class azure.communication.identity.CommunicationUserIdentifier(identifier)[source]

Represents a user in Azure Communication Service. :ivar identifier: Communication user identifier. :vartype identifier: str :param identifier: Identifier to initialize CommunicationUserIdentifier. :type identifier: str