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

Commit 9e5f572

Browse files
committed
Implement setting for Auth header prefix #32
1 parent 345f571 commit 9e5f572

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ JWT_AUTH = {
103103

104104
'JWT_ALLOW_REFRESH': False,
105105
'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),
106+
107+
'JWT_AUTH_HEADER_PREFIX': 'JWT',
106108
}
107109
```
108110
This packages uses the JSON Web Token Python implementation, [PyJWT](https://github.com/progrium/pyjwt) and allows to modify some of it's available options.

rest_framework_jwt/authentication.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import jwt
22
from rest_framework import exceptions
3+
from rest_framework_jwt.settings import api_settings
34
from rest_framework.authentication import (BaseAuthentication,
45
get_authorization_header)
5-
from rest_framework_jwt.settings import api_settings
66

77
try:
88
from django.contrib.auth import get_user_model
@@ -33,15 +33,16 @@ def authenticate(self, request):
3333
supplied using JWT-based authentication. Otherwise returns `None`.
3434
"""
3535
auth = get_authorization_header(request).split()
36+
jwt_auth_header_prefix = api_settings.JWT_AUTH_HEADER_PREFIX
3637

37-
if not auth or auth[0].lower() != b'jwt':
38+
if not auth or auth[0].lower() != jwt_auth_header_prefix.lower():
3839
return None
3940

4041
if len(auth) == 1:
41-
msg = 'Invalid JWT header. No credentials provided.'
42+
msg = 'Invalid Authorization header. No credentials provided.'
4243
raise exceptions.AuthenticationFailed(msg)
4344
elif len(auth) > 2:
44-
msg = ('Invalid JWT header. Credentials string '
45+
msg = ('Invalid Authorization header. Credentials string '
4546
'should not contain spaces.')
4647
raise exceptions.AuthenticationFailed(msg)
4748

rest_framework_jwt/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
'JWT_ALLOW_REFRESH': False,
3030
'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),
3131

32-
'JWT_AUTH_HEADER_PREFIX': 'JWT'
32+
'JWT_AUTH_HEADER_PREFIX': 'JWT',
3333
}
3434

3535
# List of settings that may be in string import notation.

tests/test_authentication.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from rest_framework.views import APIView
1212

1313
from rest_framework_jwt import utils
14+
from rest_framework_jwt.settings import api_settings, DEFAULTS
1415
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
1516

1617

@@ -56,7 +57,7 @@ def setUp(self):
5657

5758
def test_post_form_passing_jwt_auth(self):
5859
"""
59-
Ensure POSTing json over JWT auth with correct credentials
60+
Ensure POSTing form over JWT auth with correct credentials
6061
passes and does not require CSRF
6162
"""
6263
payload = utils.jwt_payload_handler(self.user)
@@ -70,7 +71,7 @@ def test_post_form_passing_jwt_auth(self):
7071

7172
def test_post_json_passing_jwt_auth(self):
7273
"""
73-
Ensure POSTing form over JWT auth with correct credentials
74+
Ensure POSTing JSON over JWT auth with correct credentials
7475
passes and does not require CSRF
7576
"""
7677
payload = utils.jwt_payload_handler(self.user)
@@ -108,7 +109,7 @@ def test_post_no_jwt_header_failing_jwt_auth(self):
108109
'/jwt/', {'example': 'example'},
109110
HTTP_AUTHORIZATION=auth, format='json')
110111

111-
msg = 'Invalid JWT header. No credentials provided.'
112+
msg = 'Invalid Authorization header. No credentials provided.'
112113

113114
self.assertEqual(response.data['detail'], msg)
114115
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
@@ -123,7 +124,7 @@ def test_post_invalid_jwt_header_failing_jwt_auth(self):
123124
'/jwt/', {'example': 'example'},
124125
HTTP_AUTHORIZATION=auth, format='json')
125126

126-
msg = ('Invalid JWT header. Credentials string '
127+
msg = ('Invalid Authorization header. Credentials string '
127128
'should not contain spaces.')
128129

129130
self.assertEqual(response.data['detail'], msg)
@@ -223,3 +224,23 @@ def test_post_form_passing_jwt_invalid_payload(self):
223224

224225
self.assertEqual(response.data['detail'], msg)
225226
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
227+
228+
def test_different_auth_header_prefix(self):
229+
"""
230+
Ensure using a different setting for `JWT_AUTH_HEADER_PREFIX` and
231+
with correct credentials passes.
232+
"""
233+
api_settings.JWT_AUTH_HEADER_PREFIX = 'Bearer'
234+
235+
payload = utils.jwt_payload_handler(self.user)
236+
token = utils.jwt_encode_handler(payload)
237+
238+
auth = 'Bearer {0}'.format(token)
239+
response = self.csrf_client.post(
240+
'/jwt/', {'example': 'example'},
241+
HTTP_AUTHORIZATION=auth, format='json')
242+
243+
self.assertEqual(response.status_code, status.HTTP_200_OK)
244+
245+
# Restore original settings
246+
api_settings.JWT_AUTH_HEADER_PREFIX = DEFAULTS['JWT_AUTH_HEADER_PREFIX']

0 commit comments

Comments
 (0)