Skip to content

Commit 18fc99c

Browse files
authored
refactored check user authentication rule (#27)
1 parent 005df24 commit 18fc99c

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

ninja_jwt/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Django Ninja JWT - JSON Web Token for Django-Ninja"""
22

3-
__version__ = "5.2.4"
3+
__version__ = "5.2.5"

ninja_jwt/controller.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class TokenObtainPairController(ControllerBase):
6666
url_name="token_obtain_pair",
6767
)
6868
def obtain_token(self, user_token: schema.obtain_pair_schema):
69+
user_token.check_user_authentication_rule()
6970
return user_token.to_response_schema()
7071

7172
@http_post(
@@ -86,6 +87,7 @@ class TokenObtainSlidingController(TokenObtainPairController):
8687
url_name="token_obtain_sliding",
8788
)
8889
def obtain_token(self, user_token: schema.obtain_sliding_schema):
90+
user_token.check_user_authentication_rule()
8991
return user_token.to_response_schema()
9092

9193
@http_post(
@@ -146,6 +148,7 @@ class AsyncTokenObtainPairController(TokenObtainPairController):
146148
url_name="token_obtain_pair",
147149
)
148150
async def obtain_token(self, user_token: schema.obtain_pair_schema):
151+
await sync_to_async(user_token.check_user_authentication_rule)()
149152
return user_token.to_response_schema()
150153

151154
@http_post(
@@ -164,6 +167,7 @@ class AsyncTokenObtainSlidingController(TokenObtainSlidingController):
164167
url_name="token_obtain_sliding",
165168
)
166169
async def obtain_token(self, user_token: schema.obtain_sliding_schema):
170+
await sync_to_async(user_token.check_user_authentication_rule)()
167171
return user_token.to_response_schema()
168172

169173
@http_post(

ninja_jwt/schema.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import warnings
2-
from typing import Any, Callable, Dict, Optional, Type, Union, cast
2+
from typing import Any, Callable, Dict, Optional, Type, cast
33

44
from django.conf import settings
55
from django.contrib.auth import authenticate, get_user_model
@@ -46,11 +46,11 @@ class TokenInputSchemaMixin(InputSchemaMixin):
4646
"no_active_account": _("No active account found with the given credentials")
4747
}
4848

49-
@classmethod
50-
def check_user_authentication_rule(
51-
cls, user: Optional[Union[AbstractUser, Any]], values: Dict
52-
) -> bool:
53-
return api_settings.USER_AUTHENTICATION_RULE(user)
49+
def check_user_authentication_rule(self) -> None:
50+
if not api_settings.USER_AUTHENTICATION_RULE(self._user):
51+
raise exceptions.AuthenticationFailed(
52+
self._default_error_messages["no_active_account"]
53+
)
5454

5555
@classmethod
5656
def validate_values(cls, values: Dict) -> dict:
@@ -71,14 +71,13 @@ def validate_values(cls, values: Dict) -> dict:
7171
raise exceptions.ValidationError({"password": "password is required"})
7272

7373
_user = authenticate(**values)
74+
cls._user = _user
7475

75-
if not cls.check_user_authentication_rule(_user, values):
76+
if _user is None:
7677
raise exceptions.AuthenticationFailed(
7778
cls._default_error_messages["no_active_account"]
7879
)
7980

80-
cls._user = _user
81-
8281
return values
8382

8483
def output_schema(self) -> Schema:
@@ -117,6 +116,7 @@ def post_validate_schema(cls, values: Dict) -> dict:
117116
:return:
118117
"""
119118
# get_token can return values that wants to apply to `OutputSchema`
119+
120120
data = cls.get_token(cls._user)
121121

122122
if not isinstance(data, dict):

tests/test_custom_schema.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ def get_token(cls, user) -> Dict:
6161
class MyNewObtainTokenSlidingSchemaInput(TokenObtainSlidingInputSchema):
6262
my_extra_field: str
6363

64+
def check_user_authentication_rule(self) -> bool:
65+
my_extra_field = self.my_extra_field
66+
return api_settings.USER_AUTHENTICATION_RULE(self._user)
67+
6468
@classmethod
6569
def get_response_schema(cls):
6670
return MyNewObtainTokenSlidingSchemaOutput

tests/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import contextlib
21
import json
32

43
from django.db import connection
54
from django.db.migrations.executor import MigrationExecutor
6-
from django.test import Client, TestCase
5+
from django.test import Client
76
from django.test.utils import override_settings
87

98
from ninja_jwt.compat import reverse

0 commit comments

Comments
 (0)