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

Documents how to use private keys with passphrases #525

Merged
merged 1 commit into from
Oct 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ Encoding & Decoding Tokens with RS256 (RSA)
>>decoded = jwt.decode(encoded, public_key, algorithms='RS256')
{'some': 'payload'}

If your private key needs a passphrase, you need to pass in a ``PrivateKey`` object from ``cryptography``.

.. code-block:: python

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend

pem_bytes = b'-----BEGIN PRIVATE KEY-----\nMIGEAgEAMBAGByqGSM49AgEGBS...'
Copy link
Collaborator

Choose a reason for hiding this comment

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

is b'' needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That line was copied from an already-existing line here. :-)

Copy link
Contributor Author

@rayluo rayluo Oct 28, 2020

Choose a reason for hiding this comment

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

I even dig into cryptography's official doc. They requires a bytes-like pem data. So, there is no problem of this line.

Can someone provide more reviews if any, or simply merge this in?

CC: @mark-adams @jpadilla

passphrase = b'your password'

private_key = serialization.load_pem_private_key(
pem_bytes, password=passphrase, backend=default_backend())
encoded = jwt.encode({'some': 'payload'}, private_key, algorithm='RS256')


Specifying Additional Headers
-----------------------------

Expand Down