【问题标题】:Tornado - getting return value from await (multiple callbacks)Tornado - 从等待中获取返回值(多个回调)
【发布时间】:2019-05-09 08:55:13
【问题描述】:

我正在开发一个在 tornado 中使用 OAuth2 身份验证的应用程序。登录类如下所示:

class IDPLogin(tornado.web.RequestHandler, IDPLoginHandler):
    async def get(self):
        if self.get_argument('code', False):
            access = await self.get_authenticated_user(
                ...
            )
            print(type(access))
            #self.set_secure_cookie('user', self.get_argument('code'))
            #self.redirect('/')
        else:
            await self.authorize_redirect(
                ...
            )

get_authenticated_user 方法如下所示(两个额外的回调用于获取评估用户所需的所有详细信息):

class IDPHubLoginHandler(tornado.auth.OAuth2Mixin):
    def __init__(self):
        self._OAUTH_AUTHORIZE_URL = "..."
        self._OAUTH_ACCESS_TOKEN_URL = "..."
        self._USERINFO_ENDPOINT = "..."

    async def get_authenticated_user(self, redirect_uri, client_id, client_secret, code):
        http = self.get_auth_http_client()
        body = urllib_parse.urlencode({
            "redirect_uri": redirect_uri,
            "code": code,
            "client_id": client_id,
            "client_secret": client_secret,
            "grant_type": "authorization_code",
        })
        fut = http.fetch(self._OAUTH_ACCESS_TOKEN_URL,
                         method="POST",
                         headers={'Content-Type': 'application/x-www-form-urlencoded'},
                         body=body)
        fut.add_done_callback(wrap(functools.partial(self._on_access_token)))

    def _on_access_token(self, future):
        """Callback function for the exchange to the access token."""
        try:
            response = future.result()
        except Exception as e:
            future.set_exception(AuthError('IDP auth error: %s' % str(e)))
            return

        args = escape.json_decode(response.body)
        # Fetch userinfo
        http = self.get_auth_http_client()
        fut = http.fetch(self._USERINFO_ENDPOINT,
                         method="GET",
                         headers={
                             'Authorization': 'Bearer ' + args['access_token'],
                             'accept': 'application/json'
                         }
        )
        fut.add_done_callback(wrap(functools.partial(self._on_userinfo)))

    def _on_userinfo(self, future):
        response = future.result()
        r_body = escape.json_decode(response.body)
        return r_body

我希望能够访问 _on_userinfo 回调中返回的正文,但 Login 类中的 access 是“NoneType”,我想评估响应以拒绝访问或向用户提供一块饼干。

提供的代码成功地获取了所有必需的输入数据,但是我很难理解如何从回调中返回值并在主登录类 (IDPLogin) 中重用它们。我查看了 Tornado 文档,但找不到答案。 Oauth2/OpenID 示例充其量只是非常简短。

尝试在 asyncio.base_futures.InvalidStateError 中设置未来结果。

【问题讨论】:

    标签: python async-await tornado


    【解决方案1】:

    找到了另一种方法。不确定这是否是最规范的方法,但似乎可以完成工作。

    1. 如下实现Oauth2 Mixin:

      class IDPLoginHandler(tornado.auth.OAuth2Mixin):
      ...
          async def get_authenticated_user(self, redirect_uri, client_id, client_secret, code):
              #do fetching and return future result
          async def get_user_info(self, access_token):
              #do fetching and return future result
      
    2. 从主登录处理程序中使用 await 关键字顺序调用方法:

      class IDPLogin(tornado.web.RequestHandler, IDPLoginHandler):
          async def get(self):
              if self.get_argument('code', False):
                  response_token = await self.get_authenticated_user(
                      ...
                  )
                  token = escape.json_decode(response_token.body)['access_token']
                  response_userinfo = await self.get_user_info(token)
      

    【讨论】:

    • 是的,这是正确的做法。如果您使用async/await 语法,则不需要回调。
    猜你喜欢
    • 2013-12-26
    • 2018-06-30
    • 2019-07-25
    • 1970-01-01
    • 2012-05-04
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    相关资源
    最近更新 更多