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

jwk construct with hs256 returns 'k' as binary #137

Closed
piedrahitapablo opened this issue Apr 12, 2019 · 4 comments · Fixed by #165
Closed

jwk construct with hs256 returns 'k' as binary #137

piedrahitapablo opened this issue Apr 12, 2019 · 4 comments · Fixed by #165
Labels

Comments

@piedrahitapablo
Copy link

piedrahitapablo commented Apr 12, 2019

Not sure if this is expected behavior or a minor bug, but when jwk.construct is used and the jwk is converted to a dict using .to_dict, the 'k' attribute is returned as a binary string. This is not an issue until the dict is used to decode a JWT and an AttributeError is raised because 'k' does not have a encode method ('bytes' object has no attribute 'encode'). To reproduce:

from jose import jwk, jwt

user_jwt = jwt.encode({'username': 'user'}, 'secret')

jwk_obj = jwk.construct('secret', 'HS256')
jwk_dict = jwk_obj.to_dict()

# to avoid the exception:
# jwk_dict['k'] = jwk_dict['k'].decode('utf-8')

claims = jwt.decode(user_jwt, jwk_dict)
@zejn
Copy link
Collaborator

zejn commented Apr 12, 2019

This is indeed a bug.

@piedrahitapablo
Copy link
Author

Is there any way that I can help to solve this? I don't know much about this topic but two possible solutions would be i) change the to_dict method to decode the key into a regular string or ii) change the necessary code in decode to accept binaries as well.

@blag
Copy link
Contributor

blag commented Apr 15, 2019

The first option:

i) change the to_dict method to decode the key into a regular string

makes the most sense to me.

@blag
Copy link
Contributor

blag commented Apr 15, 2019

Also reported in #127. Fixed in #139.

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