async def get_relay_config(self): from azure.communication.networktraversal.aio import CommunicationRelayClient from azure.communication.identity.aio 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.aio import DefaultAzureCredential endpoint, _ = parse_connection_str(self.connection_string) identity_client = CommunicationIdentityClient( endpoint, DefaultAzureCredential()) relay_client = CommunicationRelayClient(endpoint, DefaultAzureCredential()) else: identity_client = CommunicationIdentityClient.from_connection_string( self.connection_string) relay_client = CommunicationRelayClient.from_connection_string( self.connection_string) async with identity_client: print("Creating new user") user = await identity_client.create_user() print("User created with id:" + user.properties.get('id')) async with relay_client: print("Getting relay configuration") relay_configuration = await relay_client.get_relay_configuration( user) for iceServer in relay_configuration.ice_servers: print("Icer server:") print(iceServer)
async def 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.aio 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.aio import DefaultAzureCredential endpoint, _ = parse_connection_str(self.connection_string) identity_client = CommunicationIdentityClient( endpoint, DefaultAzureCredential()) else: identity_client = CommunicationIdentityClient.from_connection_string( self.connection_string) async with identity_client: 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: "******"AAD access token of a Teams User: "******"Token issued with value: " + tokenresponse.token)
async def delete_user(self): from azure.communication.identity.aio 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) async with identity_client: user = await identity_client.create_user() await identity_client.delete_user(user)
async def create_user(self): from azure.communication.identity.aio 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) async with identity_client: print("Creating new user") user = await identity_client.create_user() print("User created with id:" + user.properties['id'])
async def revoke_tokens(self): from azure.communication.identity.aio 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) async with identity_client: user = await identity_client.create_user() tokenresponse = await identity_client.issue_token(user, scopes=["chat"]) await identity_client.revoke_tokens(user) print(tokenresponse)
async def get_token(self): from azure.communication.identity.aio import CommunicationIdentityClient from azure.communication.identity import 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) async with identity_client: user = await identity_client.create_user() print("Issuing token for: " + user.properties['id']) tokenresponse = await identity_client.get_token(user, scopes=[CommunicationTokenScope.CHAT]) print("Token issued with value: " + tokenresponse.token)
async def delete_user(self): from azure.communication.identity.aio 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.aio import DefaultAzureCredential endpoint, _ = parse_connection_str(self.connection_string) identity_client = CommunicationIdentityClient(endpoint, DefaultAzureCredential()) else: identity_client = CommunicationIdentityClient.from_connection_string(self.connection_string) async with identity_client: user = await identity_client.create_user() print("Deleting user: "******" deleted")
async def create_user_with_token(self): from azure.communication.identity.aio import CommunicationIdentityClient from azure.communication.identity import 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) async with identity_client: print("Creating new user with token") user, tokenresponse = await identity_client.create_user_with_token(scopes=[CommunicationTokenScope.CHAT]) print("User created with id:" + user.identifier) print("Token issued with value: " + tokenresponse.token)
async def revoke_tokens(self): from azure.communication.identity.aio import CommunicationIdentityClient from azure.communication.identity import 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) async with identity_client: user = await identity_client.create_user() tokenresponse = await identity_client.issue_token(user, scopes=[CommunicationTokenScope.CHAT]) print("Revoking token: " + tokenresponse.token) await identity_client.revoke_tokens(user) print(tokenresponse.token + " revoked successfully")
async def test_create_user(self, connection_string): identity_client = CommunicationIdentityClient.from_connection_string( connection_string) async with identity_client: user = await identity_client.create_user() assert user.identifier is not None
async def test_get_relay_configuration( self, communication_livetest_dynamic_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, api_version=ApiVersion.V2021_03_07, http_logging_policy=get_http_logging_policy()) async with identity_client: user = await identity_client.create_user() networkTraversalClient = CommunicationRelayClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy()) async with networkTraversalClient: print('Getting relay config:\n') config = await networkTraversalClient.get_relay_configuration(user) print('Ice Servers Async:\n') for iceServer in config.ice_servers: assert iceServer.username is not None print('Username: '******'Credential: ' + iceServer.credential) assert iceServer.urls is not None for url in iceServer.urls: print('Url:' + url) assert config is not None
async def test_delete_user(self, communication_connection_string): identity_client = CommunicationIdentityClient.from_connection_string(communication_connection_string) async with identity_client: user = await identity_client.create_user() await identity_client.delete_user(user) assert user.properties.get('id') is not None
async def test_get_relay_configuration_with_route_type_nearest( self, communication_livetest_dynamic_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy()) async with identity_client: user = await identity_client.create_user() networkTraversalClient = CommunicationRelayClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy()) async with networkTraversalClient: print('Getting relay config with nearest type:\n') config = await networkTraversalClient.get_relay_configuration( user=user, route_type=RouteType.NEAREST) print('Ice Servers Async:\n') for iceServer in config.ice_servers: assert iceServer.username is not None print('Username: '******'Credential: ' + iceServer.credential) assert iceServer.urls is not None for url in iceServer.urls: print('Url:' + url) assert iceServer.route_type == RouteType.NEAREST assert config is not None
async def test_create_user_and_token(self, communication_connection_string): identity_client = CommunicationIdentityClient.from_connection_string(communication_connection_string) async with identity_client: user, token_response = await identity_client.create_user_and_token(scopes=[CommunicationTokenScope.CHAT]) assert user.properties.get('id') is not None assert token_response.token is not None
async def test_create_user(self, communication_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_connection_string, http_logging_policy=get_http_logging_policy()) async with identity_client: user = await identity_client.create_user() assert user.properties.get('id') is not None
async def test_revoke_tokens_with_no_user(self, communication_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_connection_string) async with identity_client: with pytest.raises(Exception) as ex: await identity_client.revoke_tokens(user=None)
async def test_create_user_and_token_with_no_scopes(self, communication_livetest_dynamic_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy() ) async with identity_client: with pytest.raises(Exception) as ex: user, token_response = await identity_client.create_user_and_token(scopes=None)
async def test_create_user_and_token_with_no_scopes( self, communication_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_connection_string) async with identity_client: with pytest.raises(Exception) as ex: user, token_response = await identity_client.create_user_and_token( scopes=None)
async def test_get_token_with_no_user(self, communication_livetest_dynamic_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy() ) async with identity_client: with pytest.raises(Exception) as ex: token_response = await identity_client.get_token(user=None, scopes=[CommunicationTokenScope.CHAT])
async def test_revoke_tokens_with_no_user( self, communication_livetest_dynamic_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy()) async with identity_client: with pytest.raises(Exception) as ex: await identity_client.revoke_tokens(user=None)
async def test_get_token(self, connection_string): identity_client = CommunicationIdentityClient.from_connection_string( connection_string) async with identity_client: user = await identity_client.create_user() token_response = await identity_client.get_token( user, scopes=[CommunicationTokenScope.CHAT]) assert user.identifier is not None assert token_response.token is not None
async def test_revoke_tokens(self, connection_string): identity_client = CommunicationIdentityClient.from_connection_string( connection_string) async with identity_client: user = await identity_client.create_user() token_response = await identity_client.issue_token(user, scopes=["chat"]) await identity_client.revoke_tokens(user) assert user.identifier is not None assert token_response.token is not None
async def test_get_token(self, communication_livetest_dynamic_connection_string): identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy() ) async with identity_client: user = await identity_client.create_user() token_response = await identity_client.get_token(user, scopes=[CommunicationTokenScope.CHAT]) assert user.properties.get('id') is not None assert token_response.token is not None
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
async def test_get_token_for_teams_user_with_valid_token( self, communication_livetest_dynamic_connection_string): if (self.skip_get_token_for_teams_user_test()): return identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy()) async with identity_client: add_token = self.generate_teams_user_aad_token() token_response = await identity_client.get_token_for_teams_user( add_token) assert token_response.token is not None
async 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) async with identity_client: user = await identity_client.create_user() token_response = await identity_client.get_token(user, scopes=[CommunicationTokenScope.CHAT]) assert user.properties.get('id') is not None assert token_response.token is not None
async def test_get_token_for_teams_user_with_expired_token( self, communication_livetest_dynamic_connection_string): if (self.skip_get_token_for_teams_user_test()): return identity_client = CommunicationIdentityClient.from_connection_string( communication_livetest_dynamic_connection_string, http_logging_policy=get_http_logging_policy()) async with identity_client: with pytest.raises(Exception) as ex: token_response = await identity_client.get_token_for_teams_user( self.expired_teams_token) assert str(ex.value.status_code) == "401" assert ex.value.message is not None
async def test_create_user_from_managed_identity(self, communication_livetest_dynamic_connection_string): endpoint, access_key = parse_connection_str(communication_livetest_dynamic_connection_string) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() identity_client = CommunicationIdentityClient( endpoint, credential, http_logging_policy=get_http_logging_policy() ) async with identity_client: user = await identity_client.create_user() assert user.properties.get('id') is not None
async def test_revoke_tokens_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() token_response = await identity_client.issue_token(user, scopes=["chat"]) await identity_client.revoke_tokens(user) assert user.identifier is not None assert token_response.token is not None
async def test_get_token_for_teams_user_from_managed_identity( self, communication_livetest_dynamic_connection_string): if (self.skip_get_token_for_teams_user_test()): return endpoint, access_key = parse_connection_str( communication_livetest_dynamic_connection_string) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() identity_client = CommunicationIdentityClient( endpoint, credential, http_logging_policy=get_http_logging_policy()) async with identity_client: add_token = self.generate_teams_user_aad_token() token_response = await identity_client.get_token_for_teams_user( add_token) assert token_response.token is not None