-
Notifications
You must be signed in to change notification settings - Fork 63
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
fix(): Support for PyJWT v1.7.1 or later #57
Conversation
phone_verify/backends/base.py
Outdated
try: | ||
encoded_str = encoded_str.decode() | ||
except AttributeError: | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we catching AttributeError
here? In what case does it cause an AttributeError
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we catching
AttributeError
here? In what case does it cause anAttributeError
?
Thanks for the review.
It was a mistake, at first the if
condition was different in my local (before commit), but later I updated the if
conditional block.
encoded_str = jwt.encode(data, django_settings.SECRET_KEY)
if type(encoded_str) is not str:
# jwt v2.0.0 or greater: `jwt.encode()` no longer return <class 'bytes'>
# instead it return <class 'str'> and that's why `jwt.encode().decode()` fails
# To support jwt v1.7.1 or greater: if `jwt.encode()` return <class 'bytes'>
# convert it to <class 'str'>
# Details: https://github.com/jpadilla/pyjwt
try:
encoded_str = str(encoded_str, encoding='utf-8')
except TypeError:
try:
encoded_str = encoded_str.decode()
except AttributeError:
pass
return encoded_str
In that case PyJWT v2.0.1
can through AttributeError
because str object does not have decode(). But I noticed It will not happen. But I have forgotten to remove that line.
It should be removed.
By the way, it was my first pull request. I am not good at testing. But it worked for me. Please apologize for my mistake.
I am updating this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for coming forward to contribute to this library. I really appreciate it. I've mentioned some comments and suggestions inline. Please have a look at them.
phone_verify/backends/base.py
Outdated
@@ -51,18 +51,15 @@ def generate_session_token(cls, phone_number): | |||
data = {"phone_number": phone_number, "nonce": random.random()} | |||
encoded_str = jwt.encode(data, django_settings.SECRET_KEY) | |||
if type(encoded_str) is bytes: | |||
# jwt v2.0.0 or greater: `jwt.encode()` no longer return <class 'bytes'> | |||
# jwt v2.0.0 or greater: `jwt.encode()` no longer return <class 'bytes'> | |||
# instead it return <class 'str'> and that's why `jwt.encode().decode()` fails | |||
# To support jwt v1.7.1 or greater: if `jwt.encode()` return <class 'bytes'> | |||
# convert it to <class 'str'> | |||
# Details: https://github.com/jpadilla/pyjwt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you've the link to the issue, that this change was made in, that would be helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you've the link to the issue, that this change was made in, that would be helpful.
Sorry for late. I think you wanted to know about PyJWT issue.
At first, I found it from the documentation https://pyjwt.readthedocs.io/en/stable/changelog.html.
PyJWT Github issue: jwt.encode returns str instead of bytes in 2.0.0a1 #529
And PyJWT Github PR:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for these. I'll modify the comment in the changes to reflect these and merge when the other PR on tox and other dependency upgrade is ready.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @belal-bh
I've mentioned my comments inline. Once you take care of those and ensure that the code is working fine, we'd ideally also want to include the new version of Python/Django in the .tox file so that we are testing this library on the most recent versions.
Please have a look at the file here: https://github.com/CuriousLearner/django-phone-verify/blob/master/tox.ini
Ideally we'd like to test Python 3.8 and Python 3.9 version.
Once you're done, please ensure that CI is passing and we can then merge it in.
Thank you once again for your contribution. If you need any help, please let me know.
Co-authored-by: Sanyam Khurana <8039608+CuriousLearner@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I'll work on the tox stuff and merge this when those changes are ready.
Thanks a lot. And thanks a lot for helping me and contributing this great package! |
I'm working on upgrades in this PR: #58 It is almost done. I'll test the comments that we added in this PR and then merge your changes in. Many thanks! |
I have added the changes to #58 and will merge it once the CI passes. I'm closing this one. |
Hi @CuriousLearner
I was facing the same issue #51
I found that the main problem is the dependency package PyJWT version change.
I have tried to fix this to support PyJWT v1.7.1 or later. It worked on python 3.8, 3.9.
Could you please check the changes?
And thanks for the great package.