Exemple #1
0
 def authorize() -> AuthorizeResult:
     return AuthorizeResult(enterprise_id="E111", team_id="T111")
Exemple #2
0
    async def __call__(
        self,
        *,
        context: AsyncBoltContext,
        enterprise_id: Optional[str],
        team_id: str,
        user_id: Optional[str],
    ) -> Optional[AuthorizeResult]:

        if self.find_installation_available is None:
            self.find_installation_available = hasattr(
                self.installation_store, "async_find_installation")

        bot_token: Optional[str] = None
        user_token: Optional[str] = None

        if not self.bot_only and self.find_installation_available:
            # since v1.1, this is the default way
            try:
                installation: Optional[
                    Installation] = await self.installation_store.async_find_installation(
                        enterprise_id=enterprise_id,
                        team_id=team_id,
                        is_enterprise_install=context.is_enterprise_install,
                    )
                if installation is None:
                    self._debug_log_for_not_found(enterprise_id, team_id)
                    return None

                if installation.user_id != user_id:
                    # try to fetch the request user's installation
                    # to reflect the user's access token if exists
                    user_installation = (
                        await self.installation_store.async_find_installation(
                            enterprise_id=enterprise_id,
                            team_id=team_id,
                            user_id=user_id,
                            is_enterprise_install=context.
                            is_enterprise_install,
                        ))
                    if user_installation is not None:
                        installation = user_installation

                bot_token, user_token = installation.bot_token, installation.user_token
            except NotImplementedError as _:
                self.find_installation_available = False

        if self.bot_only or not self.find_installation_available:
            # Use find_bot to get bot value (legacy)
            bot: Optional[Bot] = await self.installation_store.async_find_bot(
                enterprise_id=enterprise_id,
                team_id=team_id,
                is_enterprise_install=context.is_enterprise_install,
            )
            if bot is None:
                self._debug_log_for_not_found(enterprise_id, team_id)
                return None
            bot_token, user_token = bot.bot_token, None

        token: Optional[str] = bot_token or user_token
        if token is None:
            return None

        # Check cache to see if the bot object already exists
        if self.cache_enabled and token in self.authorize_result_cache:
            return self.authorize_result_cache[token]

        try:
            auth_test_api_response = await context.client.auth_test(token=token
                                                                    )
            authorize_result = AuthorizeResult.from_auth_test_response(
                auth_test_response=auth_test_api_response,
                bot_token=bot_token,
                user_token=user_token,
            )
            if self.cache_enabled:
                self.authorize_result_cache[token] = authorize_result
            return authorize_result
        except SlackApiError as err:
            self.logger.debug(
                f"The stored bot token for enterprise_id: {enterprise_id} team_id: {team_id} "
                f"is no longer valid. (response: {err.response})")
            return None