async def delete_user(self): from azure.communication.administration.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 revoke_tokens(self): from azure.communication.administration.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 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 create_user(self): from azure.communication.administration.aio import CommunicationIdentityClient identity_client = CommunicationIdentityClient.from_connection_string( self.connection_string) async with identity_client: user = await identity_client.create_user() print(user.identifier)
async def test_issue_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.issue_token(user, scopes=["chat"]) assert user.identifier is not None assert token_response.token is not None
def setUp(self): super(CommunicationIdentityClientTestAsync, self).setUp() self.recording_processors.extend([ BodyReplacerProcessor(keys=["id", "token"]), URIIdentityReplacer(), ResponseReplacerProcessor(keys=[self._resource_name]) ]) self.identity_client = CommunicationIdentityClient.from_connection_string( self.connection_str)
async def revoke_tokens(self): from azure.communication.administration.aio import CommunicationIdentityClient 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 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_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 main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') try: req_body = req.get_json() except ValueError as e: return func.HttpResponse( 'This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.', status_code=400) # Initialize Client conn = os.environ.get('COMMUNICATION_SERVICES_CONNECTION_STRING') user = CommunicationUser(req_body['acs_id']) async with CommunicationIdentityClient.from_connection_string( conn) as client: token_result = await client.issue_token(user, ['voip']) response = {'access_token': token_result.token} return func.HttpResponse(body=json.dumps(response), status_code=200, headers={'Content-Type': 'application/json'})
async def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') req_body = req.get_json() connection_string = os.environ.get( 'COMMUNICATION_SERVICES_CONNECTION_STRING') async with CommunicationIdentityClient.from_connection_string( connection_string) as client: identity = await client.create_user() logging.info(f'Function Created User with ID: {identity.identifier}') user_id = str(uuid.uuid4()) result = { 'id': user_id, 'name': req_body['name'], 'acs_id': identity.identifier } return func.HttpResponse(body=json.dumps(result), status_code=200, headers={'Content-Type': 'application/json'})