Skip to content

Commit dc0d5d0

Browse files
committed
have jwt_optional catch and ignore InvalidHeaderError
This fixes the case of a different authorization header (for example, into another authorization system) causing jwt_optional to return the invalid header error handler. This is technically a breaking change, but I would argue that this is more of a bug fix and that no one is (or should be) relying on jwt_optional to send back an InvalidHeaderError if they send in a different header then this extension expects. Refs #82
1 parent 36e7dc3 commit dc0d5d0

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

flask_jwt_extended/view_decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def wrapper(*args, **kwargs):
5555
jwt_data = _decode_jwt_from_request(request_type='access')
5656
ctx_stack.top.jwt = jwt_data
5757
_load_user(jwt_data[config.identity_claim])
58-
except NoAuthorizationError:
58+
except (NoAuthorizationError, InvalidHeaderError):
5959
pass
6060
return fn(*args, **kwargs)
6161
return wrapper

tests/test_protected_endpoints.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,26 +272,26 @@ def test_optional_bad_jwt_requests(self):
272272
headers={'Authorization': auth_header})
273273
data = json.loads(response.get_data(as_text=True))
274274
status_code = response.status_code
275-
self.assertEqual(status_code, 422)
276-
self.assertIn('msg', data)
275+
self.assertEqual(data, {'msg': 'unprotected hello world'})
276+
self.assertEqual(status_code, 200)
277277

278278
# Test with type not being Bearer in authorization header
279279
auth_header = "BANANA {}".format(access_token)
280280
response = self.client.get('/partially-protected',
281281
headers={'Authorization': auth_header})
282282
data = json.loads(response.get_data(as_text=True))
283283
status_code = response.status_code
284-
self.assertEqual(status_code, 422)
285-
self.assertIn('msg', data)
284+
self.assertEqual(data, {'msg': 'unprotected hello world'})
285+
self.assertEqual(status_code, 200)
286286

287287
# Test with too many items in auth header
288288
auth_header = "Bearer {} BANANA".format(access_token)
289289
response = self.client.get('/partially-protected',
290290
headers={'Authorization': auth_header})
291291
data = json.loads(response.get_data(as_text=True))
292292
status_code = response.status_code
293-
self.assertEqual(status_code, 422)
294-
self.assertIn('msg', data)
293+
self.assertEqual(data, {'msg': 'unprotected hello world'})
294+
self.assertEqual(status_code, 200)
295295

296296
def test_bad_tokens(self):
297297
# Test expired access token
@@ -527,8 +527,8 @@ def test_different_headers_jwt_optional(self):
527527
self.app.config['JWT_HEADER_TYPE'] = ''
528528
status, data = self._jwt_get('/partially-protected', access_token,
529529
header_type='Bearer')
530-
self.assertIn('msg', data)
531-
self.assertEqual(status, 422)
530+
self.assertEqual(data, {'msg': 'unprotected hello world'})
531+
self.assertEqual(status, 200)
532532

533533
self.app.config['JWT_HEADER_TYPE'] = 'Bearer'
534534
self.app.config['JWT_HEADER_NAME'] = 'Auth'

0 commit comments

Comments
 (0)