Skip to content

Commit 1b053bf

Browse files
add configuration for user authentication (#279)
* add configuration for user authentication. * Isort lint Co-authored-by: Andrew Chen Wang <[email protected]>
1 parent ba70084 commit 1b053bf

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

rest_framework_simplejwt/authentication.py

+11
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,14 @@ def get_user(self, validated_token):
129129
raise InvalidToken(_('Token contained no recognizable user identification'))
130130

131131
return api_settings.TOKEN_USER_CLASS(validated_token)
132+
133+
134+
def default_user_authentication_rule(user):
135+
# Prior to Django 1.10, inactive users could be authenticated with the
136+
# default `ModelBackend`. As of Django 1.10, the `ModelBackend`
137+
# prevents inactive users from authenticating. App designers can still
138+
# allow inactive users to authenticate by opting for the new
139+
# `AllowAllUsersModelBackend`. However, we explicitly prevent inactive
140+
# users from authenticating to enforce a reasonable policy and provide
141+
# sensible backwards compatibility with older Django versions.
142+
return True if user is not None and user.is_active else False

rest_framework_simplejwt/serializers.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import importlib
2+
13
from django.contrib.auth import authenticate
24
from django.utils.translation import gettext_lazy as _
35
from rest_framework import exceptions, serializers
@@ -6,6 +8,9 @@
68
from .state import User
79
from .tokens import RefreshToken, SlidingToken, UntypedToken
810

11+
rule_package, user_eligible_for_login = api_settings.USER_AUTHENTICATION_RULE.rsplit('.', 1)
12+
login_rule = importlib.import_module(rule_package)
13+
914

1015
class PasswordField(serializers.CharField):
1116
def __init__(self, *args, **kwargs):
@@ -42,14 +47,7 @@ def validate(self, attrs):
4247

4348
self.user = authenticate(**authenticate_kwargs)
4449

45-
# Prior to Django 1.10, inactive users could be authenticated with the
46-
# default `ModelBackend`. As of Django 1.10, the `ModelBackend`
47-
# prevents inactive users from authenticating. App designers can still
48-
# allow inactive users to authenticate by opting for the new
49-
# `AllowAllUsersModelBackend`. However, we explicitly prevent inactive
50-
# users from authenticating to enforce a reasonable policy and provide
51-
# sensible backwards compatibility with older Django versions.
52-
if self.user is None or not self.user.is_active:
50+
if not getattr(login_rule, user_eligible_for_login)(self.user):
5351
raise exceptions.AuthenticationFailed(
5452
self.error_messages['no_active_account'],
5553
'no_active_account',

rest_framework_simplejwt/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'AUTH_HEADER_TYPES': ('Bearer',),
2525
'USER_ID_FIELD': 'id',
2626
'USER_ID_CLAIM': 'user_id',
27+
'USER_AUTHENTICATION_RULE': 'rest_framework_simplejwt.authentication.default_user_authentication_rule',
2728

2829
'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),
2930
'TOKEN_TYPE_CLAIM': 'token_type',

0 commit comments

Comments
 (0)