diff --git a/flask_jwt/__init__.py b/flask_jwt/__init__.py index f864b78..dd5688b 100644 --- a/flask_jwt/__init__.py +++ b/flask_jwt/__init__.py @@ -50,7 +50,7 @@ def _default_jwt_payload_handler(identity): iat = datetime.utcnow() exp = iat + current_app.config.get('JWT_EXPIRATION_DELTA') nbf = iat + current_app.config.get('JWT_NOT_BEFORE_DELTA') - identity = getattr(identity, 'id') or identity['id'] + identity = getattr(identity, 'id', None) or identity['id'] return {'exp': exp, 'iat': iat, 'nbf': nbf, 'identity': identity} diff --git a/tests/test_jwt.py b/tests/test_jwt.py index 2157003..29ba038 100644 --- a/tests/test_jwt.py +++ b/tests/test_jwt.py @@ -291,3 +291,16 @@ def custom_auth_request_handler(): with app.test_client() as c: resp, jdata = post_json(c, '/auth', {}) assert jdata == {'hello': 'world'} + + +def test_authentication_handler_with_dictionary_result(client, jwt, user): + @jwt.authentication_handler + def authenticate(username, password): + if username == user.username and password == user.password: + return dict(id=user.id, username=user.username, password=user.password) + return None + + resp, jdata = post_json( + client, '/auth', {'username': user.username, 'password': user.password}) + assert resp.status_code == 200 + assert 'access_token' in jdata diff --git a/tox.ini b/tox.ini index e9fda0a..93423eb 100644 --- a/tox.ini +++ b/tox.ini @@ -7,4 +7,4 @@ deps = -r{toxinidir}/requirements-dev.txt commands = - py.test --clearcache {posargs} ./tests + py.test --cache-clear {posargs} ./tests