diff --git a/flask_jwt/__init__.py b/flask_jwt/__init__.py index f864b78..d3c50fb 100644 --- a/flask_jwt/__init__.py +++ b/flask_jwt/__init__.py @@ -112,6 +112,9 @@ def _default_request_handler(): def _default_auth_request_handler(): data = request.get_json() + if data is None: + raise JWTError('Bad Request', 'Invalid JSON Body') + username = data.get(current_app.config.get('JWT_AUTH_USERNAME_KEY'), None) password = data.get(current_app.config.get('JWT_AUTH_PASSWORD_KEY'), None) criterion = [username, password, len(data) == 2] diff --git a/tests/test_jwt.py b/tests/test_jwt.py index 2157003..ef0fb05 100644 --- a/tests/test_jwt.py +++ b/tests/test_jwt.py @@ -93,6 +93,19 @@ def test_auth_endpoint_with_invalid_credentials(client): assert jdata['status_code'] == 401 +def test_auth_endpoint_with_invalid_body_post(client): + resp = client.post('/auth', headers={}) + jdata = json.loads(resp.data) + + assert resp.status_code == 401 + assert 'error' in jdata + assert jdata['error'] == 'Bad Request' + assert 'description' in jdata + assert jdata['description'] == 'Invalid JSON Body' + assert 'status_code' in jdata + assert jdata['status_code'] == 401 + + def test_jwt_required_decorator_with_valid_token(app, client, user): resp, jdata = post_json( client, '/auth', {'username': user.username, 'password': user.password})