0

I'm using pyjwt as follows:

def encode_auth_token(self, user_id):
    '''Generates the auth token.'''

    try:
        payload = {
            'exp': datetime.utcnow() + datetime.timedelta(
                days = current_app.config.get('TOKEN_EXPIRATION_DAYS'),
                seconds = current_app.config.get('TOKEN_EXPIRATION_SECONDS')
            ),
            'iat': datetime.datetime.utcnow(),
            'sub': user_id
        }
        return jwt.encode(
            payload,
            current_app.config.get('SECRET_KEY'),
            algorithm='HS256'
        )
    except Exception as e:
        return e

the problem with this is that according to docs instance.encode() should return bytes and according to another resource it should return str. When I run it through unitttests:

auth_token = user.encode_auth_token(user.id)
self.assertTrue(isinstance(auth_token, str))

I get: AssertionError: False is not true and when I replace str with bytes I get the same error. So what type this method should be returning ?

Mark
  • 161
  • 11

1 Answers1

0

its prolly returning byte data. If you can confirm that it does, you can force it return string by calling the decode method on the token instance itself.

token = jwt.encode(payload,secret).decode('utf-8')
return token