summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosé Padilla <jpadilla@webapplicate.com>2014-09-22 22:25:49 -0400
committerJosé Padilla <jpadilla@webapplicate.com>2014-09-22 22:25:49 -0400
commit5bb8d3c345de6bc59663a4bd249fe11f8f54a477 (patch)
tree0ff743d72d876349074c2be3e8bbcb3801cab631
parentcf7eacccb433eb1600f5e2cf1199241f0725eccc (diff)
parent4c80f250b5819793a870c593d13992708ff08052 (diff)
downloadpyjwt-5bb8d3c345de6bc59663a4bd249fe11f8f54a477.tar.gz
Merge pull request #39 from cjlarose/master
Allow keys of type "bytes" in Python 3
-rw-r--r--jwt/__init__.py9
-rw-r--r--tests/test_jwt.py15
2 files changed, 19 insertions, 5 deletions
diff --git a/jwt/__init__.py b/jwt/__init__.py
index a68055d..4ffb1c4 100644
--- a/jwt/__init__.py
+++ b/jwt/__init__.py
@@ -48,11 +48,10 @@ verify_methods = {
}
def prepare_HS_key(key):
- if isinstance(key, basestring):
- if isinstance(key, unicode):
- key = key.encode('utf-8')
- else:
- raise TypeError("Expecting a string-formatted key.")
+ if not isinstance(key, basestring) and not isinstance(key, bytes):
+ raise TypeError("Expecting a string- or bytes-formatted key.")
+ if isinstance(key, unicode):
+ key = key.encode('utf-8')
return key
prepare_key_methods = {
diff --git a/tests/test_jwt.py b/tests/test_jwt.py
index 8d93440..29b88f0 100644
--- a/tests/test_jwt.py
+++ b/tests/test_jwt.py
@@ -166,6 +166,21 @@ class TestJWT(unittest.TestCase):
self.assertEqual(decoded_payload, self.payload)
+ def test_bytes_secret(self):
+ secret = b'\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)
+
+ decoded_payload, signing, header, signature = jwt.load(jwt_message)
+
+ jwt.verify_signature(decoded_payload, signing,
+ header, signature, secret)
+
+ self.assertEqual(decoded_payload, self.payload)
+
def test_decode_unicode_value(self):
example_payload = {"hello": "world"}
example_secret = "secret"