def 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)
def 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")
def test_get_token_from_managed_identity(self, communication_connection_string): endpoint, access_key = parse_connection_str( communication_connection_string) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() identity_client = CommunicationIdentityClient(endpoint, credential) user = identity_client.create_user() token_response = identity_client.get_token( user, scopes=[CommunicationTokenScope.CHAT]) assert user.properties.get('id') is not None assert token_response.token is not None