Skip to content

Commit c9bc33f

Browse files
committed
feat: validate token family settings on app startup in TokenFamilyConfig
1 parent 88ba93e commit c9bc33f

File tree

1 file changed

+53
-1
lines changed
  • rest_framework_simplejwt/token_family

1 file changed

+53
-1
lines changed
Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,60 @@
11
from django.apps import AppConfig
22
from django.utils.translation import gettext_lazy as _
3+
from django.core.exceptions import ImproperlyConfigured
4+
from django.utils.module_loading import import_string
5+
from datetime import timedelta
6+
from rest_framework_simplejwt.settings import api_settings
37

48

59
class TokenFamilyConfig(AppConfig):
610
name = "rest_framework_simplejwt.token_family"
711
verbose_name = _("Token Family")
8-
default_auto_field = "django.db.models.BigAutoField"
12+
default_auto_field = "django.db.models.BigAutoField"
13+
14+
def ready(self):
15+
"""Validate token family settings at startup."""
16+
try:
17+
self._validate_family_settings()
18+
except (ImproperlyConfigured, ImportError) as e:
19+
raise ImproperlyConfigured(f"Invalid Token Family settings: {e}") from e
20+
21+
@staticmethod
22+
def _validate_family_settings() -> None:
23+
"""
24+
Ensures that required token family settings are properly configured.
25+
This way we prevent undesired behavior.
26+
"""
27+
family_claim = api_settings.TOKEN_FAMILY_CLAIM
28+
if not isinstance(family_claim, str) or not family_claim.strip():
29+
raise ImproperlyConfigured("TOKEN_FAMILY_CLAIM must be a non-empty string")
30+
31+
family_exp_claim = api_settings.TOKEN_FAMILY_EXPIRATION_CLAIM
32+
if not isinstance(family_exp_claim, str) or not family_exp_claim.strip():
33+
raise ImproperlyConfigured("TOKEN_FAMILY_EXPIRATION_CLAIM must be a non-empty string")
34+
35+
family_lifetime = api_settings.TOKEN_FAMILY_LIFETIME
36+
if family_lifetime is not None and not isinstance(family_lifetime, timedelta):
37+
raise ImproperlyConfigured("TOKEN_FAMILY_LIFETIME must be of type timedelta or None")
38+
39+
family_enabled = api_settings.TOKEN_FAMILY_ENABLED
40+
if not isinstance(family_enabled, bool):
41+
raise ImproperlyConfigured("TOKEN_FAMILY_ENABLED must be of type bool")
42+
43+
check_on_access = api_settings.TOKEN_FAMILY_CHECK_ON_ACCESS
44+
if not isinstance(check_on_access, bool):
45+
raise ImproperlyConfigured("TOKEN_FAMILY_CHECK_ON_ACCESS must be of type bool")
46+
47+
blacklist_on_reuse = api_settings.TOKEN_FAMILY_BLACKLIST_ON_REUSE
48+
if not isinstance(blacklist_on_reuse, bool):
49+
raise ImproperlyConfigured("TOKEN_FAMILY_BLACKLIST_ON_REUSE must be of type bool")
50+
51+
52+
# Validate TOKEN_FAMILY_BLACKLIST_SERIALIZER
53+
blacklist_serializer_path = api_settings.TOKEN_FAMILY_BLACKLIST_SERIALIZER
54+
if not isinstance(blacklist_serializer_path, str) or not blacklist_serializer_path.strip():
55+
raise ImproperlyConfigured("TOKEN_FAMILY_BLACKLIST_SERIALIZER must be a non-empty string")
56+
57+
try:
58+
import_string(blacklist_serializer_path)
59+
except ImportError as e:
60+
raise ImportError(f"Could not import serializer '{blacklist_serializer_path}': {e}") from e

0 commit comments

Comments
 (0)