Skip to content

Commit f511e0a

Browse files
authored
Add custom header in settings (jazzband#309)
1 parent 9691182 commit f511e0a

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

docs/settings.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Some of Simple JWT's behavior can be customized through settings variables in
2828
'ISSUER': None,
2929
3030
'AUTH_HEADER_TYPES': ('Bearer',),
31+
'AUTH_HEADER_NAME': 'HTTP_AUTHORIZATION',
3132
'USER_ID_FIELD': 'id',
3233
'USER_ID_CLAIM': 'user_id',
3334
@@ -155,6 +156,11 @@ tuple of possible header types (e.g. ``('Bearer', 'JWT')``). If a list or
155156
tuple is used in this way, and authentication fails, the first item in the
156157
collection will be used to build the "WWW-Authenticate" header in the response.
157158

159+
``AUTH_HEADER_NAME``
160+
----------------------------
161+
162+
The authorization header name to be used for authentication. The default is ``HTTP_AUTHORIZATION`` which will accept the ``Authorization`` header in the request. For example if you'd like to use ``X_Access_Token`` in the header of your requests please specify the ``AUTH_HEADER_NAME`` to be ``HTTP_X_ACCESS_TOKEN`` in your settings.
163+
158164
``USER_ID_FIELD``
159165
-----------------
160166

rest_framework_simplejwt/authentication.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def get_header(self, request):
4747
Extracts the header containing the JSON web token from the given
4848
request.
4949
"""
50-
header = request.META.get('HTTP_AUTHORIZATION')
50+
header = request.META.get(api_settings.AUTH_HEADER_NAME)
5151

5252
if isinstance(header, str):
5353
# Work around django test client oddness

rest_framework_simplejwt/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
'ISSUER': None,
2424

2525
'AUTH_HEADER_TYPES': ('Bearer',),
26+
'AUTH_HEADER_NAME': 'HTTP_AUTHORIZATION',
2627
'USER_ID_FIELD': 'id',
2728
'USER_ID_CLAIM': 'user_id',
2829
'USER_AUTHENTICATION_RULE': 'rest_framework_simplejwt.authentication.default_user_authentication_rule',

tests/test_authentication.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ def test_get_header(self):
4040
request = self.factory.get('/test-url/', HTTP_AUTHORIZATION=self.fake_header.decode('utf-8'))
4141
self.assertEqual(self.backend.get_header(request), self.fake_header)
4242

43+
# Should work with the x_access_token
44+
with override_api_settings(AUTH_HEADER_NAME='HTTP_X_ACCESS_TOKEN'):
45+
# Should pull correct header off request when using X_ACCESS_TOKEN
46+
request = self.factory.get('/test-url/', HTTP_X_ACCESS_TOKEN=self.fake_header)
47+
self.assertEqual(self.backend.get_header(request), self.fake_header)
48+
49+
# Should work for unicode headers when using
50+
request = self.factory.get('/test-url/', HTTP_X_ACCESS_TOKEN=self.fake_header.decode('utf-8'))
51+
self.assertEqual(self.backend.get_header(request), self.fake_header)
52+
4353
def test_get_raw_token(self):
4454
# Should return None if header lacks correct type keyword
4555
with override_api_settings(AUTH_HEADER_TYPES='JWT'):

0 commit comments

Comments
 (0)