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

Commit 3e2568d

Browse files
committed
Fix WWW-Authenticate header when non default JWT_AUTH_HEADER_PREFIX
1 parent df2a01c commit 3e2568d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

rest_framework_jwt/authentication.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,4 @@ def authenticate_header(self, request):
101101
header in a `401 Unauthenticated` response, or `None` if the
102102
authentication scheme should return `403 Permission Denied` responses.
103103
"""
104-
return 'JWT realm="{0}"'.format(self.www_authenticate_realm)
104+
return '{0} realm="{1}"'.format(api_settings.JWT_AUTH_HEADER_PREFIX, self.www_authenticate_realm)

tests/test_authentication.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def test_post_form_failing_jwt_auth(self):
114114
"""
115115
response = self.csrf_client.post('/jwt/', {'example': 'example'})
116116
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
117+
self.assertEqual(response['WWW-Authenticate'], 'JWT realm="api"')
117118

118119
def test_post_json_failing_jwt_auth(self):
119120
"""
@@ -268,3 +269,18 @@ def test_different_auth_header_prefix(self):
268269

269270
# Restore original settings
270271
api_settings.JWT_AUTH_HEADER_PREFIX = DEFAULTS['JWT_AUTH_HEADER_PREFIX']
272+
273+
def test_post_form_failing_jwt_auth_different_auth_header_prefix(self):
274+
"""
275+
Ensure using a different setting for `JWT_AUTH_HEADER_PREFIX` and
276+
POSTing form over JWT auth without correct credentials fails and
277+
generated correct WWW-Authenticate header
278+
"""
279+
api_settings.JWT_AUTH_HEADER_PREFIX = 'Bearer'
280+
281+
response = self.csrf_client.post('/jwt/', {'example': 'example'})
282+
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
283+
self.assertEqual(response['WWW-Authenticate'], 'Bearer realm="api"')
284+
285+
# Restore original settings
286+
api_settings.JWT_AUTH_HEADER_PREFIX = DEFAULTS['JWT_AUTH_HEADER_PREFIX']

0 commit comments

Comments
 (0)