From c7be83c284e783d7ff484cf1c3e976fd5f5b7eb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= Date: Wed, 8 Apr 2020 00:01:58 -0400 Subject: [PATCH] Return string instead of byte --- README.rst | 4 ++-- docs/index.rst | 5 +++-- src/jwt/api_jws.py | 5 ++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 91859de9..636b49c1 100644 --- a/README.rst +++ b/README.rst @@ -44,8 +44,8 @@ Usage >>> import jwt >>> encoded = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256') - 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.4twFt5NiznN84AWoo1d7KO1T_yoc0Z6XOpOVswacPZg' - + >>> print(encoded) + eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzb21lIjoicGF5bG9hZCJ9.Joh1R2dYzkRvDkqv3sygm5YyK8Gi4ShZqbhK2gxcs2U >>> jwt.decode(encoded, 'secret', algorithms=['HS256']) {'some': 'payload'} diff --git a/docs/index.rst b/docs/index.rst index 1076a935..536d93e6 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -30,12 +30,13 @@ Example Usage ------------- .. code-block:: python +.. doctest:: >>> import jwt >>> encoded_jwt = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256') - >>> encoded_jwt - 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.4twFt5NiznN84AWoo1d7KO1T_yoc0Z6XOpOVswacPZg' + >>> print(encoded_jwt) + eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzb21lIjoicGF5bG9hZCJ9.Joh1R2dYzkRvDkqv3sygm5YyK8Gi4ShZqbhK2gxcs2U >>> jwt.decode(encoded_jwt, 'secret', algorithms=['HS256']) {'some': 'payload'} diff --git a/src/jwt/api_jws.py b/src/jwt/api_jws.py index d5ee2cdd..4b18f3f4 100644 --- a/src/jwt/api_jws.py +++ b/src/jwt/api_jws.py @@ -110,6 +110,7 @@ def encode( # Segments signing_input = b".".join(segments) + try: alg_obj = self._algorithms[algorithm] key = alg_obj.prepare_key(key) @@ -126,7 +127,9 @@ def encode( segments.append(base64url_encode(signature)) - return b".".join(segments) + encoded_string = b".".join(segments) + + return encoded_string.decode("utf-8") def decode( self,