diff --git a/rest_framework_jwt/serializers.py b/rest_framework_jwt/serializers.py index 12b10a44..3208f6b6 100644 --- a/rest_framework_jwt/serializers.py +++ b/rest_framework_jwt/serializers.py @@ -6,6 +6,7 @@ from django.contrib.auth import authenticate, get_user_model from django.utils.translation import ugettext as _ from rest_framework import serializers +from rest_framework import exceptions from .compat import Serializer from rest_framework_jwt.settings import api_settings @@ -52,7 +53,7 @@ def validate(self, attrs): if user: if not user.is_active: msg = _('User account is disabled.') - raise serializers.ValidationError(msg) + raise exceptions.AuthenticationFailed(msg) payload = jwt_payload_handler(user) @@ -62,11 +63,11 @@ def validate(self, attrs): } else: msg = _('Unable to log in with provided credentials.') - raise serializers.ValidationError(msg) + raise exceptions.AuthenticationFailed(msg) else: msg = _('Must include "{username_field}" and "password".') msg = msg.format(username_field=self.username_field) - raise serializers.ValidationError(msg) + raise exceptions.AuthenticationFailed(msg) class VerificationBaseSerializer(Serializer): @@ -86,10 +87,10 @@ def _check_payload(self, token): payload = jwt_decode_handler(token) except jwt.ExpiredSignature: msg = _('Signature has expired.') - raise serializers.ValidationError(msg) + raise exceptions.AuthenticationFailed(msg) except jwt.DecodeError: msg = _('Error decoding signature.') - raise serializers.ValidationError(msg) + raise exceptions.AuthenticationFailed(msg) return payload @@ -98,18 +99,18 @@ def _check_user(self, payload): if not username: msg = _('Invalid payload.') - raise serializers.ValidationError(msg) + raise exceptions.AuthenticationFailed(msg) # Make sure user exists try: user = User.objects.get_by_natural_key(username) except User.DoesNotExist: msg = _("User doesn't exist.") - raise serializers.ValidationError(msg) + raise exceptions.AuthenticationFailed(msg) if not user.is_active: msg = _('User account is disabled.') - raise serializers.ValidationError(msg) + raise exceptions.AuthenticationFailed(msg) return user @@ -157,10 +158,10 @@ def validate(self, attrs): if now_timestamp > expiration_timestamp: msg = _('Refresh has expired.') - raise serializers.ValidationError(msg) + raise exceptions.AuthenticationFailed(msg) else: msg = _('orig_iat field is required.') - raise serializers.ValidationError(msg) + raise exceptions.AuthenticationFailed(msg) new_payload = jwt_payload_handler(user) new_payload['orig_iat'] = orig_iat @@ -168,4 +169,4 @@ def validate(self, attrs): return { 'token': jwt_encode_handler(new_payload), 'user': user - } + } \ No newline at end of file