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

Add examples to documentation on how to decode private keys with passphrases #286

Closed
mark-adams opened this issue Aug 26, 2017 · 3 comments

Comments

@mark-adams
Copy link
Contributor

The current API supports either passing in an unencrypted RSA key in PEM format or passing in a PublicKey / PrivateKey from cryptography. A common feature request has been to add the ability to accept a passphrase for encrypted keys as an argument to encode() or decode() but we've decided against that to avoid cluttering the API further.

Since the existing API supports passing in PublicKey and PrivateKey objects, we probably should document how to create PublicKey / PrivateKey objects using cryptography with passphrases so users of the library can reference the examples and use similar application code when interacting with PyJWT.

@desertkun
Copy link

from cryptography.hazmat.backends.openssl.backend import backend
from cryptography.hazmat.primitives import serialization

# how to load private key with passphrase
with open(private_key_file, "rb") as f:
    private_key = serialization.load_pem_private_key(
        f.read(), password=password.encode(), backend=backend)

token = jwt.encode(..., private_key, ...)

# how to load public key
with open(public_key_file, "rb") as f:
    public_key = serialization.load_pem_public_key(
        f.read(), backend=backend)

# decode back
header = jwt.get_unverified_header(token)
# ... figure out algorithm you need ...
data = jwt.decode(token, public_key, algorithms=alg)

@rayluo
Copy link
Contributor

rayluo commented Oct 20, 2020

@mark-adams @jpadilla The rationale in this comment of issue 199 makes sense, and we now need to formally document it into here. Do you have any specific comment on @desertkun 's attempt above? I can make it a PR for you to review.

rayluo added a commit to rayluo/pyjwt that referenced this issue Oct 20, 2020
jpadilla pushed a commit that referenced this issue Oct 29, 2020
@rayluo
Copy link
Contributor

rayluo commented Oct 29, 2020

@jpadilla This issue can be closed now, since we merged in #525?

rylanhall33 added a commit to rylanhall33/pyjwt that referenced this issue Jun 15, 2022
xmas7 pushed a commit to RubyOnWorld/pyjwt that referenced this issue Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants