用户没有自定义身份验证后端的属性后端,但我调用了身份验证

时间:2016-10-31 08:33:11

标签: python django authentication login

我在django项目中创建了一个自定义授权后端。

我将AuthenticationForm子类化并覆盖了clean方法。 clean方法从我的auth后端调用authenticate方法,我的auth后端返回经过身份验证的用户。

设置:

AUTHENTICATION_BACKENDS = [
 'django.contrib.auth.backends.ModelBackend',
'accounts.authentication_backend.MyAuthBackend',
    ]

但是,我仍然收到以下错误:

AttributeError:'用户' object没有属性' backend'

我发现此错误的所有帖子都是指在调用登录之前需要进行身份验证的django文档,但我做到了!

AuthBackend:

class MyAuthBackend(object):
    supports_inactive_user = False

    def authenticate(self, username=None, password=None, v_code=None):
        print("****************\t\nin authenticate: tz:{}, phone:{}, code:{}\r\n********************".format(
            username, password, v_code))
        if not username or not password or not v_code:
            return None
        auth_response = MyUtilityClass.authenticateUser(password, username, v_code)

        if auth_response['status_code'] != 200:
            return None
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            user = User(username=username, password=phone)
            user.save()
        print ("about to return user:{}".format(user))
        return user

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

AuthenticationForm

class MyAuthForm(AuthenticationForm):

    username = forms.IntegerField(label=_("Teudat_Zehut"))
    password = forms.CharField(label=_("Mobile_Phone"))
    v_code = forms.CharField(label=_("Code"), required=True)

    def clean(self):
        user, created = User.objects.get_or_create(
            username=self.cleaned_data['username'], 
            password=self.cleaned_data['password']
        )

        backend = MyAuthBackend()
        self.user_cache = backend.authenticate(username=self.cleaned_data['username'],
            password=self.cleaned_data['password'], v_code=self.cleaned_data['v_code'])
        print("in clean wakeup auth, the user returned from authenticate:{}".format(self.user_cache))
        if self.user_cache is None or not self.user_cache.is_active:
            raise forms.ValidationError(_("Your username and password didn't match. Please try again"))
        return self.cleaned_data

堆栈跟踪:

****************
in authenticate: tz:327184271, phone:0548409573, code:dsfersfef
********************
about to return user:327184271
in clean wakeup auth, the user returned from authenticate:327184271
Internal Server Error: /report/login/
Traceback (most recent call last):
  File "C:\Users\user\Dropbox\virtualenvs\wakeup27\lib\site-    packages\django\core
\handlers\base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
  File "C:\Users\user\Dropbox\virtualenvs\wakeup27\lib\site-packages\django\core\handlers\base.py", line 147, in get_response
     response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\user\Dropbox\virtualenvs\wakeup27\lib\site-packages\django\contrib\auth\views.py", line 49, in inner
    return func(*args, **kwargs)
  File "C:\Users\user\Dropbox\virtualenvs\wakeup27\lib\site-packages\django\views\decorators\debug.py", line 76, in sensitive_post_parameters_wrapper
   return view(request, *args, **kwargs)
 File "C:\Users\user\Dropbox\virtualenvs\wakeup27\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
   response = view_func(request, *args, **kwargs)
 File "C:\Users\user\Dropbox\virtualenvs\wakeup27\lib\site-packages\django\views\decorators\cache.py", line 57, in _wrapped_view_func
  response = view_func(request, *args, **kwargs)
 File "C:\Users\user\Dropbox\virtualenvs\wakeup27\lib\site-packages\django\contrib\auth\views.py", line 76, in login auth_login(request, form.get_user())
  File "C:\Users\user\Dropbox\virtualenvs\wakeup27\lib\site-packages\django\contrib\auth\__init__.py", line 112, in login
    request.session[BACKEND_SESSION_KEY] = user.backend
AttributeError: 'User' object has no attribute 'backend'

1 个答案:

答案 0 :(得分:1)

您不应直接调用后端方法。相反,请使用django.contrib.auth中定义的函数:

from django.contrib.auth import authenticate


class MyAuthForm(AuthenticationForm):
    ...
    def clean(self):
        ...
        self.user_cache = authenticate(username=self.cleaned_data['username'],
            password=self.cleaned_data['password'], v_code=self.cleaned_data['v_code'])
        ...

这将尝试每个配置的后端,直到身份验证成功。