2016-10-31 24 views
0

私はdjangoプロジェクトでカスタム認証バックエンドを作成します。ユーザにカスタム認証バックエンドを持つバックエンドがありませんが、認証と呼ばれています

私はAuthenticationFormをサブクラス化し、クリーンメソッドをオーバーライドしました。クリーンメソッドは私の認証バックエンドからauthenticateメソッドを呼び出し、私の認証バックエンドは認証されたユーザーを返します。

セッティング:私はきた 'ユーザ' オブジェクトが属性 'バックエンド'

を持っていないすべての記事:

はAttributeError:しかし

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

、私はまだ、次のエラーを取得していますこのエラーで見つかったのは、loginを呼び出す前に認証が必要な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

完全なスタックトレースを追加します。 –

答えて

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

関連する問題