Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch JWSErrors in jwt.decode() #20

Merged
Merged
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
Catch JWSErrors in jwt.decode()
So far exceptions raised in `jws.verify()` weren't caught in the above
function, which led to it raising (undocumented) exceptions from the
underlying module.

This commit transforms said exceptions. This includes cases of invalid
payload padding, error handling for which had previously been attached
to the `json.loads()` call.
  • Loading branch information
0x64746b committed Apr 27, 2016
commit 52def773297b0a099d245f6aa7adea1c3b514c02
9 changes: 6 additions & 3 deletions jose/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from jose import jws

from .exceptions import JWSError
from .exceptions import JWTClaimsError
from .exceptions import JWTError
from .exceptions import ExpiredSignatureError
Expand Down Expand Up @@ -112,12 +113,14 @@ def decode(token, key, algorithms=None, options=None, audience=None, issuer=None
defaults.update(options)

verify_signature = defaults.get('verify_signature', True)
payload = jws.verify(token, key, algorithms, verify=verify_signature)

try:
payload = jws.verify(token, key, algorithms, verify=verify_signature)
except JWSError as e:
raise JWTError(e)

try:
claims = json.loads(payload.decode('utf-8'))
except (TypeError, binascii.Error):
raise JWTError('Invalid payload padding')
except ValueError as e:
raise JWTError('Invalid payload string: %s' % e)

Expand Down