|
27 | 27 | get_md5_hash_password, |
28 | 28 | logger, |
29 | 29 | ) |
| 30 | +from .cache import blacklist_cache |
30 | 31 |
|
31 | 32 | if TYPE_CHECKING: |
32 | 33 | from .backends import TokenBackend |
@@ -278,6 +279,12 @@ def check_blacklist(self) -> None: |
278 | 279 | """ |
279 | 280 | jti = self.payload[api_settings.JTI_CLAIM] |
280 | 281 |
|
| 282 | + if ( |
| 283 | + blacklist_cache.is_refresh_tokens_cache_enabled and |
| 284 | + blacklist_cache.is_refresh_token_blacklisted(jti) |
| 285 | + ): |
| 286 | + raise RefreshTokenBlacklistedError(_("Token is blacklisted")) |
| 287 | + |
281 | 288 | if BlacklistedToken.objects.filter(token__jti=jti).exists(): |
282 | 289 | raise RefreshTokenBlacklistedError(_("Token is blacklisted")) |
283 | 290 |
|
@@ -306,7 +313,12 @@ def blacklist(self) -> BlacklistedToken: |
306 | 313 | }, |
307 | 314 | ) |
308 | 315 |
|
309 | | - return BlacklistedToken.objects.get_or_create(token=token) |
| 316 | + blacklisted_token, created = BlacklistedToken.objects.get_or_create(token=token) |
| 317 | + |
| 318 | + if blacklist_cache.is_refresh_tokens_cache_enabled: |
| 319 | + blacklist_cache.add_refresh_token(jti) |
| 320 | + |
| 321 | + return blacklisted_token, created |
310 | 322 |
|
311 | 323 | def outstand(self) -> Optional[OutstandingToken]: |
312 | 324 | """ |
@@ -396,7 +408,12 @@ def blacklist_family(self) -> BlacklistedTokenFamily: |
396 | 408 | ) |
397 | 409 |
|
398 | 410 | # Blacklist the entire family |
399 | | - return BlacklistedTokenFamily.objects.get_or_create(family=family)[0] |
| 411 | + blacklisted_fam, created = BlacklistedTokenFamily.objects.get_or_create(family=family) |
| 412 | + |
| 413 | + if blacklist_cache.is_families_cache_enabled: |
| 414 | + blacklist_cache.add_token_family(family_id) |
| 415 | + |
| 416 | + return blacklisted_fam |
400 | 417 |
|
401 | 418 | def get_family_id(self) -> Optional[str]: |
402 | 419 | return self.payload.get(api_settings.TOKEN_FAMILY_CLAIM, None) |
@@ -442,6 +459,12 @@ def check_family_blacklist(token: T) -> None: |
442 | 459 | logger.warning(f"Token of user:{user_id} does not have a family_id. Skipping family blacklist check.") |
443 | 460 | return |
444 | 461 |
|
| 462 | + if ( |
| 463 | + blacklist_cache.is_families_cache_enabled and |
| 464 | + blacklist_cache.is_token_family_blacklisted(family_id) |
| 465 | + ): |
| 466 | + raise TokenError(_("Token family is blacklisted")) |
| 467 | + |
445 | 468 | if BlacklistedTokenFamily.objects.filter(family__family_id=family_id).exists(): |
446 | 469 | raise TokenError(_("Token family is blacklisted")) |
447 | 470 |
|
|
0 commit comments