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

fix(): Support for PyJWT v1.7.1 or later #57

Closed
wants to merge 3 commits into from

Conversation

belal-bh
Copy link
Contributor

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.

Comment on lines 62 to 65
try:
encoded_str = encoded_str.decode()
except AttributeError:
pass
Copy link
Owner

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?

Copy link
Contributor Author

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?

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.

Copy link
Owner

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.

@@ -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
Copy link
Owner

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.

Copy link
Contributor Author

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:

Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 😊

Copy link
Owner

@CuriousLearner CuriousLearner left a 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>
Copy link
Owner

@CuriousLearner CuriousLearner left a 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.

@belal-bh belal-bh closed this Mar 25, 2021
@belal-bh belal-bh reopened this Mar 25, 2021
@belal-bh
Copy link
Contributor Author

Thanks!

I'll work on the tox stuff and merge this when those changes are ready.

Thanks a lot.
I want to learn more about it. Actually, I am very new to CI and testing.
I have mistakenly closed the pull request! But I have reopened it.
I've given the PyJWT issue and PR links the previous comment. Please check it.

And thanks a lot for helping me and contributing this great package!

@CuriousLearner
Copy link
Owner

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!

@CuriousLearner
Copy link
Owner

I have added the changes to #58 and will merge it once the CI passes.

I'm closing this one.

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

Successfully merging this pull request may close these issues.

2 participants