Wagtail手动登录用户

问题描述 投票:1回答:1

我有一个Wagtail应用程序,需要使用OAuth登录并验证/admin部分的用户。与OAuth提供程序的交互工作正常,仅在authorize视图的末尾,我收到Too many redirects错误。这是我的要旨:

SESSION_KEY = 'access_token'

oauth = OAuth()
oauth_client = oauth.remote_app(#oauth client setup here)

视图

def login(request):
    """
    This view is redirecting to my oauth provider, log the user
    and redirect back to /authorize with the data in the request.
    """
    return HttpResponseRedirect(oauth_client.authorize(
        callback='http://localhost/admin/authorize'))


def authorize(request):    
    resp = oauth_client.authorized_response(request.GET)    
    request.session[SESSION_KEY] = (resp['access_token'], '')
    data = baton.get('/oauth/me')
    username = json.loads(data.data).get('username')

    user, _ = User.objects.get_or_create(
        username=username,
        defaults={'password': username, 'is_superuser': True}
    )
    auth_user = authenticate(username=username, password=username)
    login(auth_user)

    return HttpResponseRedirect('/admin')

网址

urlpatterns = [
    url(r'^admin/login', login, name='login'),
    url(r'^admin/authorize', authorize, name='authorize'),

    url(r'^admin/', include(wagtailadmin_urls)),
]

我在浏览器中看到的Too many redirects是在authorize视图验证了所有逻辑并尝试将其重定向到/admin时发生的,但我认为发生的事是Wagtail然后再次重定向到/login,但是我虽然验证并且login一个用户应该足以使用Wagtail,显然不是。

有关如何解决此问题的任何想法,或以编程方式将用户登录到Wagtail管理员。

django oauth wagtail django-authentication
1个回答
2
投票

login(auth_user)行正在调用您在上面定义的login视图,但是您正在传递用户对象而不是请求。是否应该从login调用django.contrib.auth函数?如果是这样,您需要给它一个与视图不冲突的独特名称,也许使用:

from django.contrib.auth import login as auth_login

((在这种情况下,您仍然需要修复login / auth_login调用的参数,因为它需要一个请求和一个用户-请参阅https://docs.djangoproject.com/en/3.0/topics/auth/default/#django.contrib.auth.login处的文档]

© www.soinside.com 2019 - 2024. All rights reserved.