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

Support custom exception handler #424

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
6 changes: 6 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -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).
2 changes: 2 additions & 0 deletions rest_framework_jwt/settings.py
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion rest_framework_jwt/views.py
Original file line number Diff line number Diff line change
@@ -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)