Beispiel #1
0
    async def _get_app_credentials(
        self, app_id: str, oauth_scope: str
    ) -> MicrosoftAppCredentials:
        if not app_id:
            return MicrosoftAppCredentials(None, None)

        cache_key = f"{app_id}{oauth_scope}"
        app_credentials = BotFrameworkHttpClient._APP_CREDENTIALS_CACHE.get(cache_key)

        if app_credentials:
            return app_credentials

        app_password = await self._credential_provider.get_app_password(app_id)
        app_credentials = MicrosoftAppCredentials(
            app_id, app_password, oauth_scope=oauth_scope
        )
        if self._channel_provider and self._channel_provider.is_government():
            app_credentials.oauth_endpoint = (
                GovernmentConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL
            )
            app_credentials.oauth_scope = (
                GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE
            )

        BotFrameworkHttpClient._APP_CREDENTIALS_CACHE[cache_key] = app_credentials
        return app_credentials
    async def create_connector_client(
        self, service_url: str, identity: ClaimsIdentity = None
    ) -> ConnectorClient:
        """Allows for mocking of the connector client in unit tests
        :param service_url: The service URL
        :param identity: The claims identity

        :return: An instance of the :class:`ConnectorClient` class
        """

        # Anonymous claims and non-skill claims should fall through without modifying the scope.
        credentials = self._credentials

        if identity:
            bot_app_id_claim = identity.claims.get(
                AuthenticationConstants.AUDIENCE_CLAIM
            ) or identity.claims.get(AuthenticationConstants.APP_ID_CLAIM)

            if bot_app_id_claim and SkillValidation.is_skill_claim(identity.claims):
                scope = JwtTokenValidation.get_app_id_from_claims(identity.claims)

                # Do nothing, if the current credentials and its scope are valid for the skill.
                # i.e. the adapter instance is pre-configured to talk with one skill.
                # Otherwise we will create a new instance of the AppCredentials
                # so self._credentials.oauth_scope isn't overridden.
                if self._credentials.oauth_scope != scope:
                    password = await self._credential_provider.get_app_password(
                        bot_app_id_claim
                    )
                    credentials = MicrosoftAppCredentials(
                        bot_app_id_claim, password, oauth_scope=scope
                    )
                    if (
                        self.settings.channel_provider
                        and self.settings.channel_provider.is_government()
                    ):
                        credentials.oauth_endpoint = (
                            GovernmentConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL
                        )
                        credentials.oauth_scope = (
                            GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE
                        )

        client_key = (
            f"{service_url}{credentials.microsoft_app_id if credentials else ''}"
        )
        client = self._connector_client_cache.get(client_key)

        if not client:
            client = ConnectorClient(credentials, base_url=service_url)
            client.config.add_user_agent(USER_AGENT)
            self._connector_client_cache[client_key] = client

        return client
Beispiel #3
0
    async def create_connector_client(
        self, service_url: str, identity: ClaimsIdentity = None
    ) -> ConnectorClient:
        """Allows for mocking of the connector client in unit tests
        :param service_url: The service URL
        :param identity: The claims identity

        :return: An instance of the :class:`ConnectorClient` class
        """
        if identity:
            bot_app_id_claim = identity.claims.get(
                AuthenticationConstants.AUDIENCE_CLAIM
            ) or identity.claims.get(AuthenticationConstants.APP_ID_CLAIM)

            credentials = None
            if bot_app_id_claim and SkillValidation.is_skill_claim(identity.claims):
                scope = JwtTokenValidation.get_app_id_from_claims(identity.claims)

                password = await self._credential_provider.get_app_password(
                    bot_app_id_claim
                )
                credentials = MicrosoftAppCredentials(
                    bot_app_id_claim, password, oauth_scope=scope
                )
                if (
                    self.settings.channel_provider
                    and self.settings.channel_provider.is_government()
                ):
                    credentials.oauth_endpoint = (
                        GovernmentConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL
                    )
                    credentials.oauth_scope = (
                        GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE
                    )
            else:
                credentials = self._credentials
        else:
            credentials = self._credentials

        client_key = (
            f"{service_url}{credentials.microsoft_app_id if credentials else ''}"
        )
        client = self._connector_client_cache.get(client_key)

        if not client:
            client = ConnectorClient(credentials, base_url=service_url)
            client.config.add_user_agent(USER_AGENT)
            self._connector_client_cache[client_key] = client

        return client