开发者

Can `authenticate` use like this in django?

this is the code :

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 asso开发者_Python百科ciations
        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)

and the code use like this :

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

Is it right ?

I think the code must be like this :

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

Does authenticate have the argument openid_key,provider ?

Should i Rewrite authenticate my myself to handle it .

thanks


No, django does not have an authenticate function that expects openid_key, provider arguments.

grep -r "openid" django returns nothing for version 1.2.3

What you are looking at is a custom authentication backend, like the one found on github here: 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
        #.... 

Next time you notice that a function isn't being used normally, you should start wondering if it's even the same one you are thinking about : )

You should look at where the authenticate function was imported from, and you'd see it's not django project code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜