Open pddg opened 6 years ago
Problem is: AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend']
the default "ModelBackend" authentication backend does not allow users with is_active = False to log in.
so if you want to login with is_active= False, try AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.AllowAllUsersModelBackend']
checkout : https://docs.djangoproject.com/en/2.0/ref/contrib/auth/#django.contrib.auth.models.User.is_active
for more detail.
Sorry, I'm confused.
I don't want to allow user with is_active = False to login. I want to know the real reason of refusing login.
This issue is a problem of django-rest-framework-jwt, I think. When user which is not activated try to login, Django refuse by default('django.contrib.auth.backends.ModelBackend'
). This is because of 'The user is not activated.', not 'Unable to log in with provided credentials.'.
Current implemention of django-rest-framework-jw try to handle this, but it contains a bug , so it return 'Unable to log in with provided credentials.'.
if all(credentials.values()): user = authenticate(**credentials)
if user:
if not user.is_active:
msg = _('User account is disabled.')
raise serializers.ValidationError(msg)
..............
in this portion ,authenticate(credentials) will return none when is_active = false** because of 'django.contrib.auth.backends.ModelBackend'
due to user = none if user: is False so it returns 'Unable to log in with provided credentials.'. i don't know if django-rest-framework-jwt is aware of it or not ...or i may also be wrong
My environment as follows:
What is expected instead of "Unable to log in with provided credentials."
'User account is disabled.'
is expected, I think. InJSONWebTokenSerializer.validate
, it seems to be implemented but not working.How to reproduce
Install django and DRF and DRF-jwt with pip, and just start new app.
Add
rest_framework
andrest_framework_jwt
intoINSTALLED_APPS
insettings.py
. Then,myapp.urls
edit as follows.Execute migration, create superuser and login to admin page.
Then, create new user (for example,
username
is'user'
,password
is'hogefuga'
) and turn offActivate
from admin page. Finally, post credentials to/jwt/create
.In my opinion
In
JSONWebTokenSerializer.validate()
, User is authenticated usingsettings.AUTHENTICATION_BACKENDS
('django.contrib.auth.backends.ModelBackend'
is used by default). Then, Error handling is executed based on the value which is returned by AUTHENTICATION_BACKENDS.django.contrib.auth.backends.ModelBackend
is return user instance, whenand
is_active
== True)However, if the user is not activated, it returns
None
instead, now. So, current implementation ofJSONWebTokenSerializer
cannot handle'User account is disabled.'
error.Are there any workaround for this issue? I want to distinguish these two errors.