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

Added some fixes related to algorithm and key choice #109

Merged
merged 6 commits into from
Mar 18, 2015
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 13 additions & 1 deletion jwt/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import hmac

from .compat import constant_time_compare, string_types, text_type
from .exceptions import InvalidKeyError

try:
from cryptography.hazmat.primitives import interfaces, hashes
Expand Down Expand Up @@ -68,7 +69,13 @@ class NoneAlgorithm(Algorithm):
operations are required.
"""
def prepare_key(self, key):
return None
if key == '':
key = None

if key is not None:
raise InvalidKeyError('When alg = "none", key value must be None.')

return key

def sign(self, msg, key):
return b''
Expand Down Expand Up @@ -96,6 +103,11 @@ def prepare_key(self, key):
if isinstance(key, text_type):
key = key.encode('utf-8')

if (b'-----BEGIN PUBLIC KEY-----' in key or b'-----BEGIN CERTIFICATE-----' in key):
raise InvalidAlgorithmError(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be InvalidKeyError or InvalidAlgorithmError?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be InvalidKeyError... changing now

'The specified key is an assymetric key or x509 certificate and'
' should not be used as an HMAC secret.')

return key

def sign(self, msg, key):
Expand Down
4 changes: 4 additions & 0 deletions jwt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class InvalidIssuerError(InvalidTokenError):
pass


class InvalidKeyError(Exception):
pass


# Compatibility aliases (deprecated)
ExpiredSignature = ExpiredSignatureError
InvalidAudience = InvalidAudienceError
Expand Down