Skip to content

Commit

Permalink
Merge pull request #15 from edx/mattdrayer/catch-token-exception
Browse files Browse the repository at this point in the history
mattdrayer/catch-token-exception: Also handle InvalidTokenError
  • Loading branch information
mattdrayer authored Jul 1, 2016
2 parents 7ee6832 + e358207 commit d329de3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion edx_rest_framework_extensions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
""" edx Django REST Framework extensions. """

__version__ = '1.1.0' # pragma: no cover
__version__ = '1.1.1' # pragma: no cover
22 changes: 22 additions & 0 deletions edx_rest_framework_extensions/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,25 @@ def test_decode_failure(self):

msg = "All combinations of JWT issuers and secret keys failed to validate the token."
patched_log.error.assert_any_call(msg)

def test_decode_failure_invalid_token(self):
"""
Verifies the function logs decode failures, and raises an InvalidTokenError if the token cannot be decoded
"""

# Create tokens using each invalid issuer and attempt to decode them against
# the valid issuers list, which won't work
with mock.patch('edx_rest_framework_extensions.utils.logger') as patched_log:
with self.assertRaises(jwt.InvalidTokenError):
# Attempt to decode an invalid token, which will fail with an InvalidTokenError
utils.jwt_decode_handler("invalid.token")

# Verify that the proper entries were written to the log file
msg = "Token decode failed for issuer 'test-issuer-1'"
patched_log.info.assert_any_call(msg, exc_info=True)

msg = "Token decode failed for issuer 'test-issuer-2'"
patched_log.info.assert_any_call(msg, exc_info=True)

msg = "All combinations of JWT issuers and secret keys failed to validate the token."
patched_log.error.assert_any_call(msg)
2 changes: 1 addition & 1 deletion edx_rest_framework_extensions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def jwt_decode_handler(token):
algorithms=[api_settings.JWT_ALGORITHM]
)
return decoded
except jwt.DecodeError:
except jwt.InvalidTokenError:
msg = "Token decode failed for issuer '{issuer}'".format(issuer=jwt_issuer['ISSUER'])
logger.info(msg, exc_info=True)

Expand Down

0 comments on commit d329de3

Please sign in to comment.