繁体   English   中英

如何在views.py中调用django自定义编写的AuthenticationBackend的authenticate()方法?

[英]How to call the authenticate() method of Custom written AuthenticationBackend of django in views.py?

我正在处理一个 Django 项目,在该项目中,我定义了一个自定义用户模型,我需要为其编写自定义身份验证方法,按照我编写的文档如下但我在视图中调用它时遇到问题。 py请通过查看以下代码来帮助我
我已经定义了我的自定义后端如下
我的自定义身份验证后端

from django.contrib.auth.backends import BaseBackend
from .models import User
from IntellerMatrix.CommonUtilities.constants import Constants


class AuthenticationBackend(BaseBackend):
    """
    Authentication Backend
    :To manage the authentication process of user
    """

    def authenticate(self, email=None, password=None):
        user = User.objects.get(email=email)
        if user is not None and user.check_password(password):
            if user.is_active == Constants.YES:
                return user
            else:
                return "User is not activated"
        else:
            return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

设置.py

AUTHENTICATION_BACKENDS = ['Modules.users.authentication.AuthenticationBackend',
                           'django.contrib.auth.backends.ModelBackend', ]

视图.py

def login(request):
    email = 'ialihaider75@gmail.com'
    password = 'ali'
    user = # how to call here that custom authentication backend's authenticate method

    if user is None:
        return HttpResponse("<p>Not Valid</p>")
    else:
        return HttpResponse(user)

你可以调用authenticate(..)函数 [Django-doc]

使用authenticate()来验证一组凭据。 它将凭据作为关键字参数,默认情况下的usernamepassword根据每个身份验证后端检查它们,如果凭据对后端有效,则返回一个User对象。 所以:

from django.contrib.auth import authenticate

def login(request):
    email = 'ialihaider75@gmail.com'
    password = 'ali'
    user = authenticate(request, email=email, password=password)

    if user is None:
        return HttpResponse('<p>Not Valid</p>')
    else:
        return HttpResponse(user)

请注意,您实现的身份验证方法不能返回字符串。 正如编写身份验证后端文档所说:

(…)

无论哪种方式, authenticate()都应该检查它获取的凭据,如果凭据有效,则返回与这些凭据匹配的用户对象 如果它们无效,它应该返回None

class AuthenticationBackend(BaseBackend):
    """
    Authentication Backend
    :To manage the authentication process of user
    """

    def authenticate(self, request, email=None, password=None):
        try:
            user = User.objects.get(email=email)
        except User.DoesNotExist:
            return None
        if user is not None and user.check_password(password):
            if user.is_active == Constants.YES:
                return user
        return None

此外,这种登录您的使用,这只是检查证书是否有效。 所以如果你想登录用户,你仍然需要调用login(..)函数[Django-doc]

与其直接调用 Backend 类的authenticate()方法,不如使用authenticate()函数:

user = authenticate(email=email, password=password)

这更通用,并使您的代码灵活。 由于您可能有不同的身份验证后端,它们接受不同的参数,例如令牌。 因此,无需导入所有后端并尝试每个后端,您只需将所需参数传递给authenticate函数,这将自动调用每个身份验证后端。

暂无
暂无

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

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