可以`验证`在django中这样使用吗?

时间:2010-12-18 00:57:40

标签: python django arguments authentication

这是代码:

def openid_done(request, provider=None):
    """
    When the request reaches here, the user has completed the Openid
    authentication flow. He has authorised us to login via Openid, so
    request.openid is populated.
    After coming here, we want to check if we are seeing this openid first time.
    If we are, we will create a new Django user for this Openid, else login the
    existing openid.
    """

    if not provider:
        provider = request.session.get('openid_provider', '')
    if hasattr(request,'openid') and request.openid:
        #check for already existing associations
        openid_key = str(request.openid)

        #authenticate and login
        try:
            user = authenticate(openid_key=openid_key, request=request, provider = provider)
        except:
            user = None

        if user:
            login(request, user)
            if 'openid_next' in request.session :
                openid_next = request.session.get('openid_next')
                if len(openid_next.strip()) >  0 :
                    return HttpResponseRedirect(openid_next)
            return HttpResponseRedirect(LOGIN_REDIRECT_URL)
            # redirect_url = reverse('socialauth_editprofile')
            # return HttpResponseRedirect(redirect_url)
        else:
            return HttpResponseRedirect(LOGIN_URL)
    else:
        return HttpResponseRedirect(LOGIN_URL)

并且代码使用如下:

authenticate(openid_key=openid_key, request=request, provider = provider)

是不是?

我认为代码必须是这样的:

user = authenticate(username='john', password='secret')

身份验证是否包含参数openid_keyprovider

我应该重写authenticate我自己来处理它。

感谢

1 个答案:

答案 0 :(得分:0)

不,django没有authenticate函数,需要openid_keyprovider个参数。

grep -r "openid" django不返回版本1.2.3

您正在查看的是自定义身份验证后端,就像在github上找到的那样:https://github.com/agiliq/Django-Socialauth/blob/master/socialauth/auth_backends.py

 class OpenIdBackend:
    def authenticate(self, openid_key, request, provider, user=None):
        try:
            assoc = UserAssociation.objects.get(openid_key=openid_key)
            return assoc.user
        #.... 

下次你注意到某个功能没有正常使用时,你应该开始想知道它是否与你正在考虑的相同:)

您应该查看从哪里导入authenticate函数,您会发现它不是django项目代码。