From 7d02d4632a5d6a4bb250eee3ee3010052939e76f Mon Sep 17 00:00:00 2001 From: An4ik Date: Sat, 24 Feb 2018 17:14:01 +0600 Subject: [PATCH] added availabilty to consider configured custom exception hanlding in DRF --- docs/index.md | 6 ++++++ rest_framework_jwt/settings.py | 2 ++ rest_framework_jwt/views.py | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index 0a476bb1..5c82cb56 100644 --- a/docs/index.md +++ b/docs/index.md @@ -179,6 +179,7 @@ JWT_AUTH = { 'JWT_AUTH_HEADER_PREFIX': 'JWT', 'JWT_AUTH_COOKIE': None, + 'JWT_SERIALIZER_RAISE_EXCEPTION': False, } ``` @@ -292,6 +293,11 @@ procedure will also look into this cookie, if set. The 'Authorization' header ta Default is `None` and no cookie is set when creating tokens nor accepted when validating them. +### JWT_SERIALIZER_RAISE_EXCEPTION +If you have implemented custom exception handling as described in [django_rest_framework documentation](http://www.django-rest-framework.org/api-guide/exceptions/#custom-exception-handling) and want to have that function which converts exceptions raised in your API views into response objects. + +Default is 'False' + ## Extending `JSONWebTokenAuthentication` Right now `JSONWebTokenAuthentication` assumes that the JWT will come in the header, or a cookie if configured (see [JWT_AUTH_COOKIE](#JWT_AUTH_COOKIE)). The JWT spec does not require this (see: [Making a service Call](https://developer.atlassian.com/static/connect/docs/concepts/authentication.html)). For example, the JWT may come in the querystring. The ability to send the JWT in the querystring is needed in cases where the user cannot set the header (for example the src element in HTML). diff --git a/rest_framework_jwt/settings.py b/rest_framework_jwt/settings.py index e47320bb..e6f00790 100644 --- a/rest_framework_jwt/settings.py +++ b/rest_framework_jwt/settings.py @@ -46,6 +46,8 @@ 'JWT_AUTH_HEADER_PREFIX': 'JWT', 'JWT_AUTH_COOKIE': None, + + 'JWT_SERIALIZER_RAISE_EXCEPTION': False, } # List of settings that may be in string import notation. diff --git a/rest_framework_jwt/views.py b/rest_framework_jwt/views.py index 30cd4646..e5b1959c 100644 --- a/rest_framework_jwt/views.py +++ b/rest_framework_jwt/views.py @@ -54,7 +54,7 @@ def get_serializer(self, *args, **kwargs): def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) - if serializer.is_valid(): + if serializer.is_valid(raise_exception=api_settings.JWT_SERIALIZER_RAISE_EXCEPTION): user = serializer.object.get('user') or request.user token = serializer.object.get('token') response_data = jwt_response_payload_handler(token, user, request)