Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2.0.0 breaking changes jwt.encode() #587

Closed
arberx opened this issue Dec 23, 2020 · 6 comments
Closed

v2.0.0 breaking changes jwt.encode() #587

arberx opened this issue Dec 23, 2020 · 6 comments

Comments

@arberx
Copy link

arberx commented Dec 23, 2020

Expected Result

jwt.encode() to return an object with a decode property.

Actual Result

E       AttributeError: 'str' object has no attribute 'decode'**

Reproduction Steps

import jwt
from datetime import datetime

secret, algorithm = 'secret', 'hs256'
now = int(datetime.now().timestamp())
payload = {'sub': 'Expiry Test', 'iat': now, 'exp': now + 2}

token = jwt.encode(payload, secret, algorithm=algorithm.upper()).decode()

System Information

$ python -m jwt.help
{
  "cryptography": {
    "version": "2.8"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.8.6"
  },
  "platform": {
    "release": "19.6.0",
    "system": "Darwin"
  },
  "pyjwt": {
    "version": "2.0.0"
  }
}
        
@arberx
Copy link
Author

arberx commented Dec 23, 2020

Seems this commit: 94d102b broke the API, and was intentional...

@arberx
Copy link
Author

arberx commented Dec 23, 2020

For backwards compatibility, decode() should be a method on the class, and simply call .decode_complete().

In its current form in v2.0.0, this is a significant change to this interface and will break a lot of projects.

@jpadilla
Copy link
Owner

Unrelated to 94d102b

jwt.encode(...) now returns a string instead of a byte string.

https://github.com/jpadilla/pyjwt/blob/master/CHANGELOG.md#jwtencode-return-type

@dvf
Copy link

dvf commented Apr 19, 2021

This looks like a breaking issue. Has this been fixed?

@arberx
Copy link
Author

arberx commented Apr 20, 2021

I do think this is an issue, no reason the code snippet I posted in the summary shouldn't work.

I ended up pinning to the previous release.

@charan-r
Copy link

charan-r commented Feb 21, 2023

Hi.

Recently we upgraded the PyJWT version from 1.7.1 ---> 2.6.0. I'm aware that there was a source code change happened from version 2.x.x.

Could you please provide us the solution to handle this error.

AttributeError: 'str' object has no attribute 'decode'

This Error is getting raised when we calling this method - super().post(request)

class loginAuthentication(ObtainJSONWebToken):
	def post(self, request):
		response = super().post(request)

Im using Python 3.8.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants