简体   繁体   English

可以在Django中使用“ authenticate”吗?

[英]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 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)

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 ? 身份验证是否具有参数openid_keyprovider

Should i Rewrite authenticate my myself to handle it . 我是否应该重写自己的authenticate以进行处理。

thanks 谢谢

No, django does not have an authenticate function that expects openid_key , provider arguments. 不,django没有openid_key provider参数openid_keyauthenticate功能。

grep -r "openid" django returns nothing for version 1.2.3 grep -r "openid" django对于版本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 您正在查看的是一个自定义身份验证后端,例如在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
        #.... 

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. 您应该查看从哪里导入了authenticate函数,您会发现它不是Django项目代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM