-
-
Notifications
You must be signed in to change notification settings - Fork 696
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
encode()/decode() key parameter type should not be str #602
Comments
It should also accept the private key types from Cryptography (on encode) and the public key types (on decode). Example: import jwt
from cryptography.hazmat.primitives.asymmetric import rsa
private_key = rsa.generate_private_key(public_exponent=65537, key_size=4096)
public_key = private_key.public_key()
encoded = jwt.encode({"foo": "bar"}, key=private_key, algorithm="RS256")
decoded = jwt.decode(encoded, key=public_key, algorithms=["RS256"]) |
Yeah, was discussed over in the PR. Seems like the only practical solution at this time is going to |
EDIT: Looks like this was changed in the PR already, sorry! Hoping it gets merged soon :D |
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days |
This still seems to be an issue |
The proper way to do it would be to use generics, and would require two I'm currently writing a solution, and will make a PR, we'll be able to see if this is a reliable solution. Edit: After a quick look, generics are not the only reliable option |
The
jwt.encode()
andjwt.decode()
functions declare parameterkey
to bestr
. This seems to have been the case for a while, but with new release mypy will now complain thatArgument "key" has incompatible type "bytes"; expected "str" [arg-type]
when this parameter is given a bytes object.
Giving it a string does not seem to make sense though, the key is inherently binary (and indeed the first thing HMAC does is throw the key into
force_bytes
: https://github.com/jpadilla/pyjwt/blob/fdfd6871/jwt/algorithms.py#L173-174).Everything works as intended when you pass in
bytes
, it's just the type declaration.Expected Result
Parameter should accept
bytes
(and possibly not acceptstr
)Actual Result
mypy reports a type error when using bytes.
When using str it is impossible to pass random bits (since it will be run through
.encode('utf-8')
)Reproduction Steps
Runs fine, but mypy will report errors.
The text was updated successfully, but these errors were encountered: