From 236743a4e2499c29e6651f6231c5734ccc6d213f Mon Sep 17 00:00:00 2001 From: Shubham Dhumal <54709753+ShadyD45@users.noreply.github.com> Date: Tue, 2 Feb 2021 20:06:04 +0530 Subject: [PATCH] Fix jwt.encode return type (#19776) (#19958) * Fix for issue #19776 PyJWT 2.0.0 does not return `bytes` for `encode()` instead returns `str`. So converted the `str` to `bytes` and returned the resulting value * added check for jwt.encode return type * Update __init__.py * Updated with suggested change * Test to check return type of get_token() The `get_token()` must return a `str` but for PyJWT version < 2.0.0 the `jwt.encode()` returns `bytes`. This test is to make sure if token returned by `get_token()` is `str`. * Update test_get_token.py * Update test_get_token.py * Delete test_get_token.py Co-authored-by: Willem Melching --- common/api/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/common/api/__init__.py b/common/api/__init__.py index b030c333..365e4f5e 100644 --- a/common/api/__init__.py +++ b/common/api/__init__.py @@ -27,7 +27,11 @@ class Api(): 'iat': now, 'exp': now + timedelta(hours=1) } - return jwt.encode(payload, self.private_key, algorithm='RS256').decode('utf8') + token = jwt.encode(payload, self.private_key, algorithm='RS256') + if isinstance(token, bytes): + token = token.decode('utf8') + return token + def api_get(endpoint, method='GET', timeout=None, access_token=None, **params): backend = "https://api.commadotai.com/"