Skip to content

Commit

Permalink
docs: Add example of encoding and decoding tokens with RSA (#313)
Browse files Browse the repository at this point in the history
* docs: Add example of encoding and decoding tokens with RSA

Some users have complained that the docs don't make it very clear that
the private key / public key need to be a byte string. This change makes
that clearer by adding an example to usage.rst.

* flake8: Fix a couple of linting errors due to a new version of flake8-import-order
  • Loading branch information
mark-adams authored and jpadilla committed Nov 28, 2017
1 parent 7f7d524 commit e0aa10e
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions docs/usage.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
Usage Examples
==============

Encoding & Decoding Tokens
Encoding & Decoding Tokens with HS256
---------------------------------

.. code-block:: python
>>import jwt
>>encoded = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256')
>>key = 'secret'
>>encoded = jwt.encode({'some': 'payload'}, key, algorithm='HS256')
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.4twFt5NiznN84AWoo1d7KO1T_yoc0Z6XOpOVswacPZg'
>>decoded = jwt.decode(encoded, key, algorithms='HS256')
{'some': 'payload'}
Encoding & Decoding Tokens with RS256 (RSA)
---------------------------------

.. code-block:: python
>>import jwt
>>private_key = b'-----BEGIN PRIVATE KEY-----\nMIGEAgEAMBAGByqGSM49AgEGBS...'
>>public_key = b'-----BEGIN PUBLIC KEY-----\nMHYwEAYHKoZIzj0CAQYFK4EEAC...'
>>encoded = jwt.encode({'some': 'payload'}, private_key, algorithm='RS256)
'eyJhbGciOiJIU...'
>>decoded = jwt.decode(encoded, public_key, algorithms='RS256')
{'some': 'payload'}
Specifying Additional Headers
---------------------------------
Expand Down

0 comments on commit e0aa10e

Please sign in to comment.