async def callback(context: TurnContext):
            activity = Activity(
                type=ActivityTypes.invoke_response,
                value=InvokeResponse(
                    status=200,
                    body=TokenExchangeInvokeResponse(
                        id=context.activity.value.id,
                        connection_name=context.activity.value.connection_name,
                    ),
                ),
            )

            await context.send_activity(activity)
示例#2
0
 def _get_token_exchange_invoke_response(
     self, status: int, failure_detail: str, identifier: str = None
 ) -> Activity:
     return Activity(
         type=ActivityTypes.invoke_response,
         value=InvokeResponse(
             status=status,
             body=TokenExchangeInvokeResponse(
                 id=identifier,
                 connection_name=self._settings.connection_name,
                 failure_detail=failure_detail,
             ),
         ),
     )
    async def _exchanged_token(self, turn_context: TurnContext) -> bool:
        token_exchange_response: TokenResponse = None
        aux_dict = {}
        if turn_context.activity.value:
            for prop in ["id", "connection_name", "token", "properties"]:
                aux_dict[prop] = turn_context.activity.value.get(prop)
        token_exchange_request = TokenExchangeInvokeRequest(
            id=aux_dict["id"],
            connection_name=aux_dict["connection_name"],
            token=aux_dict["token"],
            properties=aux_dict["properties"],
        )
        try:
            adapter = turn_context.adapter
            if isinstance(turn_context.adapter, ExtendedUserTokenProvider):
                token_exchange_response = await adapter.exchange_token(
                    turn_context,
                    self._oauth_connection_name,
                    turn_context.activity.from_property.id,
                    TokenExchangeRequest(token=token_exchange_request.token),
                )
            else:
                raise Exception(
                    "Not supported: Token Exchange is not supported by the current adapter."
                )
        except:
            traceback.print_exc()
        if not token_exchange_response or not token_exchange_response.token:
            # The token could not be exchanged (which could be due to a consent requirement)
            # Notify the sender that PreconditionFailed so they can respond accordingly.

            invoke_response = TokenExchangeInvokeResponse(
                id=token_exchange_request.id,
                connection_name=self._oauth_connection_name,
                failure_detail="The bot is unable to exchange token. Proceed with regular login.",
            )

            await self._send_invoke_response(
                turn_context, invoke_response, HTTPStatus.PRECONDITION_FAILED
            )

            return False

        return True
        async def callback(context):
            result = await adapter.exchange_token_from_credentials(
                turn_context=context,
                oauth_app_credentials=None,
                connection_name=context.activity.value.connection_name,
                exchange_request=TokenExchangeRequest(
                    token=context.activity.value.token, uri=context.activity.service_url
                ),
                user_id="user_id",
            )

            activity = Activity(
                type=ActivityTypes.invoke_response,
                value=InvokeResponse(
                    status=200,
                    body=TokenExchangeInvokeResponse(
                        id=context.activity.value.id,
                        connection_name=result.connection_name,
                    ),
                ),
            )

            await context.send_activity(activity)