Skip to content

Commit

Permalink
Merge pull request #6 from jkarneges/master
Browse files Browse the repository at this point in the history
allow non-ascii keys
  • Loading branch information
progrium committed Dec 12, 2011
2 parents 4ecdc1e + e297b5d commit 4502e96
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 6 additions & 4 deletions jwt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ def encode(payload, key, algorithm='HS256'):
segments.append(base64url_encode(json.dumps(payload)))
signing_input = '.'.join(segments)
try:
ascii_key = unicode(key).encode('utf8')
signature = signing_methods[algorithm](signing_input, ascii_key)
if isinstance(key, unicode):
key = key.encode('utf-8')
signature = signing_methods[algorithm](signing_input, key)
except KeyError:
raise NotImplementedError("Algorithm not supported")
segments.append(base64url_encode(signature))
Expand All @@ -64,8 +65,9 @@ def decode(jwt, key='', verify=True):
raise DecodeError("Invalid segment encoding")
if verify:
try:
ascii_key = unicode(key).encode('utf8')
if not signature == signing_methods[header['alg']](signing_input, ascii_key):
if isinstance(key, unicode):
key = key.encode('utf-8')
if not signature == signing_methods[header['alg']](signing_input, key):
raise DecodeError("Signature verification failed")
except KeyError:
raise DecodeError("Algorithm not supported")
Expand Down
9 changes: 7 additions & 2 deletions tests/test_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ def test_unicode_secret(self):
jwt_message = jwt.encode(self.payload, secret)
decoded_payload = jwt.decode(jwt_message, secret)
self.assertEqual(decoded_payload, self.payload)


def test_nonascii_secret(self):
secret = '\xc2' # char value that ascii codec cannot decode
jwt_message = jwt.encode(self.payload, secret)
decoded_payload = jwt.decode(jwt_message, secret)
self.assertEqual(decoded_payload, self.payload)

if __name__ == '__main__':
unittest.main()
unittest.main()

0 comments on commit 4502e96

Please sign in to comment.