Django AttributeError: объект 'tuple' не имеет атрибута 'backend'
Я пытаюсь реализовать аутентификацию с помощью Simple JWT Authentication. Я использую пользовательские модели пользователей, и я также хочу изменить метод аутентификации, потому что мой пароль зашифрован с помощью bcrypt, и я должен следовать этому. Мой проект называется backend, а приложение - custom_auth.
setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework_simplejwt',
'custom_auth',
]
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework_simplejwt.authentication.JWTAuthentication',
),
}
AUTH_USER_MODEL = 'custom_auth.User'
AUTHENTICATION_BACKENDS = ('custom_auth.custom_authenticate.CustomAuthentication',)
Я также включил SIMPLE_JWT в мои настройки, приведенные Здесь.
custom_authenticate.py
import bcrypt
from .models import User
from rest_framework import authentication
class CustomAuthentication(authentication.BaseAuthentication):
def authenticate(self,request,mail=None,password=None):
if mail is not None and password is not None:
print(mail,password)
user = User.objects.get(mail=mail)
hashed_password = user.password
is_check = bcrypt.checkpw(password.encode('utf8'),hashed_password.encode('utf8'))
print(is_check)
if is_check:
return (user,None)
else:
return None
else:
return None
def get_user(self,user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
Когда я предоставляю почту и пароль и печатаю is_check
, я обнаружил, что это True.
Полная ошибка
[17/Jun/2022 13:25:15] "GET /api/token/ HTTP/1.1" 405 7630
Internal Server Error: /api/token/
Traceback (most recent call last):
File "C:\myenv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner response = get_response(request)
File "C:\myenv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\myenv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\myenv\lib\site-packages\django\views\generic\base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "C:\myenv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\myenv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\myenv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\myenv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\myenv\lib\site-packages\rest_framework_simplejwt\views.py", line 27, in post
serializer.is_valid(raise_exception=True)
File "C:\myenv\lib\site-packages\rest_framework\serializers.py", line 220, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "C:\myenv\lib\site-packages\rest_framework\serializers.py", line 422, in run_validation
value = self.validate(value)
File "C:\myenv\lib\site-packages\rest_framework_simplejwt\serializers.py", line 68, in validate
data = super().validate(attrs)
File "C:\myenv\lib\site-packages\rest_framework_simplejwt\serializers.py", line 47, in validate
self.user = authenticate(**authenticate_kwargs)
File "C:\myenv\lib\site-packages\django\views\decorators\debug.py", line 42, in
sensitive_variables_wrapper
return func(*func_args, **func_kwargs)
File "C:\myenv\lib\site-packages\django\contrib\auth\__init__.py", line 83, in authenticate
user.backend = backend_path
AttributeError: 'tuple' object has no attribute 'backend'
urls.py
from django.contrib import admin
from django.urls import include, path
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('custom_auth.urls')),
# path('api-auth/',include('rest_framework.urls')),
path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]
Помогите, пожалуйста.