Skip to content

Commit 14e8b2c

Browse files
authored
feat: log warning if token is being created for inactive user (#873)
As per [discussion](#779 (comment)), this PR adds a warning if a developer tries to create the token for the non-active user. Part of #779
1 parent 1ad763b commit 14e8b2c

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

rest_framework_simplejwt/tokens.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
datetime_to_epoch,
2424
format_lazy,
2525
get_md5_hash_password,
26+
logger,
2627
)
2728

2829
if TYPE_CHECKING:
@@ -235,6 +236,12 @@ def for_user(cls: type[T], user: AuthUser) -> T:
235236
Returns an authorization token for the given user that will be provided
236237
after authenticating the user's credentials.
237238
"""
239+
240+
if hasattr(user, "is_active") and not user.is_active:
241+
logger.warning(
242+
f"Creating token for inactive user: {user.id}. If this is not intentional, consider checking the user's status before calling the `for_user` method."
243+
)
244+
238245
user_id = getattr(user, api_settings.USER_ID_FIELD)
239246
if not isinstance(user_id, int):
240247
user_id = str(user_id)

rest_framework_simplejwt/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import hashlib
2+
import logging
23
from calendar import timegm
34
from datetime import datetime, timezone
45
from typing import Callable
@@ -46,3 +47,5 @@ def format_lazy(s: str, *args, **kwargs) -> str:
4647

4748

4849
format_lazy: Callable = lazy(format_lazy, str)
50+
51+
logger = logging.getLogger("rest_framework_simplejwt")

0 commit comments

Comments
 (0)