Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

modified exception validation in serializer #392

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
23 changes: 12 additions & 11 deletions rest_framework_jwt/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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):
Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -157,15 +158,15 @@ 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

return {
'token': jwt_encode_handler(new_payload),
'user': user
}
}