Skip to content

Commit 0c9eb31

Browse files
committed
feat: add cache.py for handling caching logic for blacklisted tokens and families
1 parent 2f9de9d commit 0c9eb31

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

rest_framework_simplejwt/cache.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from django.core.cache import caches
2+
from .settings import api_settings
3+
4+
class BlacklistCache:
5+
"""
6+
Cache implementation for Simple JWT blacklist functionalities.
7+
Provides caching for both blacklisted refresh tokens and token families.
8+
"""
9+
10+
@property
11+
def cache(self):
12+
"""Get the configured cache backend for Simple JWT."""
13+
return caches[api_settings.SJWT_CACHE_NAME]
14+
15+
@property
16+
def is_refresh_tokens_cache_enabled(self):
17+
"""Check if refresh token caching is enabled."""
18+
return api_settings.CACHE_BLACKLISTED_REFRESH_TOKENS
19+
20+
@property
21+
def is_families_cache_enabled(self):
22+
"""Check if token family caching is enabled."""
23+
return api_settings.CACHE_BLACKLISTED_FAMILIES
24+
25+
def _get_refresh_token_key(self, jti: str) -> str:
26+
"""Generate cache key for a refresh token JTI."""
27+
return f"{api_settings.CACHE_KEY_PREFIX_BLACKLISTED_REFRESH_TOKENS}:{jti}"
28+
29+
def _get_family_key(self, family_id: str) -> str:
30+
"""Generate cache key for a token family ID."""
31+
return f"{api_settings.CACHE_KEY_PREFIX_BLACKLISTED_FAMILIES}:{family_id}"
32+
33+
def add_refresh_token(self, jti: str) -> None:
34+
"""Stores refresh token JTI in the cache."""
35+
key = self._get_refresh_token_key(jti)
36+
self.cache.set(
37+
key,
38+
True,
39+
timeout=api_settings.CACHE_TTL_BLACKLISTED_REFRESH_TOKENS
40+
)
41+
42+
def is_refresh_token_blacklisted(self, jti: str) -> bool:
43+
"""Checks if a refresh token JTI is blacklisted in cache."""
44+
return self.cache.get(self._get_refresh_token_key(jti), False)
45+
46+
def add_token_family(self, family_id: str) -> None:
47+
"""Stores a token family ID in the cache."""
48+
key = self._get_family_key(family_id)
49+
self.cache.set(
50+
key,
51+
True,
52+
timeout=api_settings.CACHE_TTL_BLACKLISTED_FAMILIES
53+
)
54+
55+
def is_token_family_blacklisted(self, family_id: str) -> bool:
56+
"""Checks if a token family is blacklisted in cache."""
57+
return self.cache.get(self._get_family_key(family_id), False)
58+
59+
def delete_refresh_token_from_cache(self, jti: str) -> bool:
60+
"""Returns True if the token jti was successfully deleted, False otherwise."""
61+
return self.cache.delete(self._get_refresh_token_key(jti))
62+
63+
def delete_family_from_cache(self, family_id: str) -> bool:
64+
"""Returns True if the family ID was successfully deleted, False otherwise."""
65+
return self.cache.delete(self._get_family_key(family_id))
66+
67+
68+
# Singleton instance for centralized cache management
69+
blacklist_cache = BlacklistCache()

0 commit comments

Comments
 (0)