Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify Serializer Should Honour Blacklist #239

Merged
merged 7 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion rest_framework_simplejwt/serializers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from django.contrib.auth import authenticate
from django.utils.translation import gettext_lazy as _
from rest_framework import exceptions, serializers
from rest_framework.exceptions import ValidationError

from .exceptions import TokenError
from .settings import api_settings
from .state import User
from .tokens import RefreshToken, SlidingToken, UntypedToken

if api_settings.BLACKLIST_AFTER_ROTATION:
from .token_blacklist.models import BlacklistedToken

class PasswordField(serializers.CharField):
def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -139,6 +143,11 @@ class TokenVerifySerializer(serializers.Serializer):
token = serializers.CharField()

def validate(self, attrs):
UntypedToken(attrs['token'])
token = UntypedToken(attrs['token'])

if api_settings.BLACKLIST_AFTER_ROTATION:
jti = token.get(api_settings.JTI_CLAIM)
if BlacklistedToken.objects.filter(token__jti=jti).exists():
raise ValidationError("Token is blacklisted")

return {}
32 changes: 31 additions & 1 deletion tests/test_token_blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.test import TestCase

from rest_framework_simplejwt.exceptions import TokenError
from rest_framework_simplejwt.serializers import TokenVerifySerializer
from rest_framework_simplejwt.settings import api_settings
from rest_framework_simplejwt.token_blacklist.models import (
BlacklistedToken, OutstandingToken,
Expand All @@ -14,7 +15,7 @@
)
from rest_framework_simplejwt.utils import aware_utcnow, datetime_from_epoch

from .utils import MigrationTestCase
from .utils import MigrationTestCase, override_api_settings


class TestTokenBlacklist(TestCase):
Expand Down Expand Up @@ -190,3 +191,32 @@ def test_jti_field_should_contain_uuid_hex_strings(self):
actual_hexes = [i.jti_hex for i in OutstandingToken.objects.all()]

self.assertEqual(actual_hexes, self.expected_hexes)


class TokenVerifySerializerShouldHonourBlacklist(MigrationTestCase):
migrate_from = ('token_blacklist', '0002_outstandingtoken_jti_hex')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is testing migration but the PR is not showing any migration changes, or it was already done but not tested?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

migrate_to = ('token_blacklist', '0003_auto_20171017_2007')

def setUp(self):
self.user = User.objects.create(
username='test_user',
password='test_password',
)

super().setUp()

def test_token_verify_serializer_should_honour_blacklist_if_blacklisting_enabled(self):
with override_api_settings(BLACKLIST_AFTER_ROTATION=True):
refresh_token = RefreshToken.for_user(self.user)
refresh_token.blacklist()

serializer = TokenVerifySerializer(data={"token": str(refresh_token)})
self.assertFalse(serializer.is_valid())

def test_token_verify_serializer_should_not_honour_blacklist_if_blacklisting_not_enabled(self):
with override_api_settings(BLACKLIST_AFTER_ROTATION=False):
refresh_token = RefreshToken.for_user(self.user)
refresh_token.blacklist()

serializer = TokenVerifySerializer(data={"token": str(refresh_token)})
self.assertTrue(serializer.is_valid())