【问题标题】:How should I continue a Python Social Auth Partial Pipeline我应该如何继续 Python Social Auth Partial Pipeline
【发布时间】:2018-06-10 03:04:51
【问题描述】:

我正在使用的应用程序有一个 Python Social Auth /complete/<backend>/ 端点的覆盖端点。

在我们的urls.py:

urlspatterns = [
    ...
    # Override of social_auth
    url(r'^api/v1/auth/oauth/complete/(?P<backend>[^/]+)/$', 
        social_auth_complete,
        name='social_complete'),
    ...
]

views.py内:

from social_django.views import complete


def social_auth_complete(request, backend, *args, **kwargs):
    """Overwritten social_auth_complete."""
    # some custom logic getting variables from session (Unrelated).

    response = complete(request, backend, *args, **kwargs)

    # Some custom logic adding args to the redirect (Unrelated).

我们正在尝试实现partial pipeline method。第一次调用端点时,一切都按预期工作。

@partial
def required_info(strategy, details, user=None, is_new=False, *args, **kwargs):
    """Verify the user has all the required information before proceeding."""
    if not is_new:
        return

   for field in settings.SOCIAL_USER_REQUIRED_DATA:
        if not details.get(field):
            data = strategy.request_data().get(field)
            if not data:
                current_partial = kwargs.get('current_partial')
                social_provider = kwargs.get('backend')
                return strategy.redirect(f'.../?partial_token={partial_token}&provider={social_provider}'
            else:
                details[field] = data

这会将用户重定向到前端,他们在其中填写一个表单,该表单调用 POST 请求到原始 API api/v1/auth/oauth/complete/(?P&lt;backend&gt;[^/]+)/,数据中包含以下内容: { 'required_fieldX': '数据', ... 'partial_token': '', }

关键问题

有两个问题;当我 pdb 进入required_info 时,strategy.request_data() 中永远不会有任何数据。 kwargs['request'].body 里面还有数据,我可以把数据拿出来。

然而

但我担心第二次我们永远不会从 social-core 进入这个block of code

partial = partial_pipeline_data(backend, user, *args, **kwargs)
if partial:
    user = backend.continue_pipeline(partial)
    # clean partial data after usage
    backend.strategy.clean_partial_pipeline(partial.token)
else:
    user = backend.complete(user=user, *args, **kwargs)

我知道这是真的,因为当我查询数据库时,原始 Partial 对象仍然存在,就好像从未调用过 backend.strategy.clean_partial_pipeline(partial.token)

最后的问题

为什么social_django.views.complete 没有按预期处理 POST 请求,并且在所有示例应用程序中似乎都如此。我们覆盖它有问题吗?我是否应该只创建一个单独的端点来处理 POST 请求,如果是这样,如何模仿@psa 中发生的所有事情,以便我可以调用backend.continue_pipeline(partial)

【问题讨论】:

标签: python python-social-auth


【解决方案1】:

我觉得这里只有一个问题,那就是django策略在加载请求数据的时候没有看request.body,可以看here的负责方法。在那里你可以看到它在寻找request.GET 和/或request.POST,而不是正文。

您可以通过定义从内置策略扩展的自定义策略轻松克服这一问题,并覆盖request_data 方法以查找request.body 中的值。然后定义SOCIAL_AUTH_STRATEGY 指向你的类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-17
    • 2016-07-03
    • 1970-01-01
    • 2016-06-01
    • 2015-10-25
    • 2015-12-05
    • 2018-04-18
    • 2014-01-21
    相关资源
    最近更新 更多