Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

raise exception if invalid json in body #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions flask_jwt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
13 changes: 13 additions & 0 deletions tests/test_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down