Skip to content

Commit 5c7558e

Browse files
committed
Fix using alternative header name for JWTs
1 parent 3902ab4 commit 5c7558e

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

flask_jwt_extended/utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
get_cookie_csrf_protect, get_access_csrf_cookie_name, \
1919
get_refresh_cookie_name, get_refresh_cookie_path, \
2020
get_refresh_csrf_cookie_name, get_token_location, \
21-
get_csrf_header_name
21+
get_csrf_header_name, get_jwt_header_name
2222
from flask_jwt_extended.exceptions import JWTEncodeError, JWTDecodeError, \
2323
InvalidHeaderError, NoAuthorizationError, WrongTokenError, \
2424
FreshTokenRequired
@@ -153,13 +153,14 @@ def _decode_jwt(token, secret, algorithm):
153153

154154
def _decode_jwt_from_headers():
155155
# Verify we have the auth header
156-
auth_header = request.headers.get('Authorization', None)
157-
if not auth_header:
156+
header_name = get_jwt_header_name()
157+
jwt_header = request.headers.get(header_name, None)
158+
if not jwt_header:
158159
raise NoAuthorizationError("Missing Authorization Header")
159160

160161
# Make sure the header is valid
161162
expected_header = get_jwt_header_type()
162-
parts = auth_header.split()
163+
parts = jwt_header.split()
163164
if not expected_header:
164165
if len(parts) != 1:
165166
msg = "Badly formatted authorization header. Should be '<JWT>'"

tests/test_protected_endpoints.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,16 @@ def test_different_headers(self):
312312

313313
self.app.config['JWT_HEADER_TYPE'] = 'Bearer'
314314
self.app.config['JWT_HEADER_NAME'] = 'Auth'
315-
status, data = self._jwt_get('/protected', access_token, header_name='Auth')
315+
status, data = self._jwt_get('/protected', access_token, header_name='Auth',
316+
header_type='Bearer')
316317
self.assertIn('msg', data)
317-
self.assertEqual(status, 401)
318-
319-
status, data = self._jwt_get('/protected', access_token, header_name='Authorization')
320-
self.assertEqual(data, {'msg': 'hello world'})
321318
self.assertEqual(status, 200)
322319

320+
status, data = self._jwt_get('/protected', access_token, header_name='Authorization',
321+
header_type='Bearer')
322+
self.assertIn('msg', data)
323+
self.assertEqual(status, 401)
324+
323325

324326
class TestEndpointsWithCookies(unittest.TestCase):
325327

0 commit comments

Comments
 (0)