Skip to content

Commit a043949

Browse files
committed
Add a central point for checking tokens that are blocked.
I'm about to make the logic here more elaborate, and prefer it in one spot in any case.
1 parent 86b810a commit a043949

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

src/rest_framework_jwt/authentication.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def authenticate(self, request):
7070

7171
if apps.is_installed('rest_framework_jwt.blacklist'):
7272
from rest_framework_jwt.blacklist.models import BlacklistedToken
73-
if BlacklistedToken.objects.filter(token=force_str(token)).exists():
73+
if BlacklistedToken.is_blocked(token):
7474
msg = _('Token is blacklisted.')
7575
raise exceptions.PermissionDenied(msg)
7676

src/rest_framework_jwt/blacklist/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.db import models
66
from django.db.models import Q
77
from django.utils import timezone
8+
from django.utils.encoding import force_str
89

910

1011
class BlacklistedTokenManager(models.Manager):
@@ -34,3 +35,8 @@ class Meta:
3435

3536
def __str__(self):
3637
return 'Blacklisted token - {} - {}'.format(self.user, self.token)
38+
39+
40+
@staticmethod
41+
def is_blocked(token):
42+
return BlacklistedToken.objects.filter(token=force_str(token)).exists()

src/rest_framework_jwt/blacklist/permissions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ def has_permission(self, request, view):
1515
if token is None:
1616
return True
1717

18-
return not BlacklistedToken.objects.filter(token=token).exists()
18+
return not BlacklistedToken.is_blocked(token)

src/rest_framework_jwt/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
from django.apps import apps
1212
from django.contrib.auth import get_user_model
13-
from django.utils.encoding import force_str
1413

1514
from rest_framework import serializers
1615
from rest_framework.utils.encoders import JSONEncoder
@@ -206,7 +205,7 @@ def check_payload(token):
206205

207206
if apps.is_installed('rest_framework_jwt.blacklist'):
208207
from rest_framework_jwt.blacklist.models import BlacklistedToken
209-
if BlacklistedToken.objects.filter(token=force_str(token)).exists():
208+
if BlacklistedToken.is_blocked(token):
210209
msg = _('Token is blacklisted.')
211210
raise serializers.ValidationError(msg)
212211

0 commit comments

Comments
 (0)