File log-warning.patch of Package python-djangorestframework-simplejwt
From fa4ec6dfec769dda40a4eb24c8d38494da4161ac Mon Sep 17 00:00:00 2001
From: Vjeran Grozdanic <vjeran.grozdanic@sentry.io>
Date: Wed, 26 Feb 2025 23:05:46 +0100
Subject: [PATCH 1/2] feat: log warning if token is being created for inactive
user
---
rest_framework_simplejwt/tokens.py | 7 +++++++
rest_framework_simplejwt/utils.py | 3 +++
2 files changed, 10 insertions(+)
Index: django-rest-framework-simplejwt-4.6.0/rest_framework_simplejwt/tokens.py
===================================================================
--- django-rest-framework-simplejwt-4.6.0.orig/rest_framework_simplejwt/tokens.py
+++ django-rest-framework-simplejwt-4.6.0/rest_framework_simplejwt/tokens.py
@@ -8,7 +8,7 @@ from .exceptions import TokenBackendErro
from .settings import api_settings
from .token_blacklist.models import BlacklistedToken, OutstandingToken
from .utils import (
- aware_utcnow, datetime_from_epoch, datetime_to_epoch, format_lazy,
+ aware_utcnow, datetime_from_epoch, datetime_to_epoch, format_lazy, logger
)
@@ -158,6 +158,12 @@ class Token:
Returns an authorization token for the given user that will be provided
after authenticating the user's credentials.
"""
+
+ if hasattr(user, "is_active") and not user.is_active:
+ logger.warning(
+ 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."
+ )
+
user_id = getattr(user, api_settings.USER_ID_FIELD)
if not isinstance(user_id, int):
user_id = str(user_id)
Index: django-rest-framework-simplejwt-4.6.0/rest_framework_simplejwt/utils.py
===================================================================
--- django-rest-framework-simplejwt-4.6.0.orig/rest_framework_simplejwt/utils.py
+++ django-rest-framework-simplejwt-4.6.0/rest_framework_simplejwt/utils.py
@@ -1,3 +1,4 @@
+import logging
from calendar import timegm
from datetime import datetime
@@ -30,3 +31,5 @@ def format_lazy(s, *args, **kwargs):
format_lazy = lazy(format_lazy, str)
+
+logger = logging.getLogger("rest_framework_simplejwt")