2016-10-31 33 views
0

我在django項目中創建一個自定義授權後端。用戶沒有屬性後端自定義身份驗證後端,但我稱爲驗證

我分類了AuthenticationForm並覆蓋了乾淨的方法。 clean方法從我的auth後端調用authenticate方法,我的auth後端返回經過身份驗證的用戶。

設置:

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

不過,我還是發現了以下錯誤:

AttributeError的: '用戶' 對象有沒有屬性 '後臺'

所有的職位我已經發現這個錯誤是指在調用登錄之前需要進行身份驗證的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' 
+0

添加完整的Stacktrace。 –

回答

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']) 
     ... 

這將嘗試每個配置的後端,直到驗證成功。

+0

就是這樣,非常感謝! – hgolov

相關問題