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

Commit

Permalink
Refactored views to inherit from a base JWT API view. Removed i18n fr…
Browse files Browse the repository at this point in the history
…om a dev-facing serializer error.
  • Loading branch information
Jwpe committed Feb 22, 2015
1 parent d47d4ea commit cff17f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
2 changes: 1 addition & 1 deletion rest_framework_jwt/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class VerificationBaseSerializer(Serializer):
token = serializers.CharField()

def validate(self, attrs):
msg = _('Please define a validate method.')
msg = 'Please define a validate method.'
raise NotImplementedError(msg)

def _check_payload(self, token):
Expand Down
37 changes: 13 additions & 24 deletions rest_framework_jwt/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@
jwt_response_payload_handler = api_settings.JWT_RESPONSE_PAYLOAD_HANDLER


class ObtainJSONWebToken(APIView):
class JWTAPIView(APIView):
"""
API View that receives a POST with a user's username and password.
Returns a JSON Web Token that can be used for authenticated requests.
Base API View that various JWT interactions inherit from.
"""
throttle_classes = ()
permission_classes = ()
authentication_classes = ()
parser_classes = (parsers.FormParser, parsers.JSONParser,)
renderer_classes = (renderers.JSONRenderer,)
serializer_class = JSONWebTokenSerializer

def post(self, request):
serializer = self.serializer_class(data=request.DATA)
Expand All @@ -40,32 +37,24 @@ def post(self, request):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class VerifyJSONWebToken(APIView):
class ObtainJSONWebToken(JWTAPIView):
"""
API View that receives a POST with a user's username and password.
Returns a JSON Web Token that can be used for authenticated requests.
"""
serializer_class = JSONWebTokenSerializer


class VerifyJSONWebToken(JWTAPIView):
"""
API View that checks the veracity of a token, returning the token if it
is valid.
"""
throttle_classes = ()
permission_classes = ()
authentication_classes = ()
parser_classes = (parsers.FormParser, parsers.JSONParser,)
renderer_classes = (renderers.JSONRenderer,)
serializer_class = VerifyJSONWebTokenSerializer

def post(self, request):
serializer = self.serializer_class(data=request.DATA)

if serializer.is_valid():
user = serializer.object.get('user') or request.user
token = serializer.object.get('token')
response_data = jwt_response_payload_handler(token, user)

return Response(response_data)

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class RefreshJSONWebToken(VerifyJSONWebToken):
class RefreshJSONWebToken(JWTAPIView):
"""
API View that returns a refreshed token (with new expiration) based on
existing token
Expand Down

0 comments on commit cff17f8

Please sign in to comment.