Skip to content

Commit e0aa10e

Browse files
mark-adamsjpadilla
authored andcommitted
docs: Add example of encoding and decoding tokens with RSA (jpadilla#313)
* 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
1 parent 7f7d524 commit e0aa10e

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

docs/usage.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
Usage Examples
22
==============
33

4-
Encoding & Decoding Tokens
4+
Encoding & Decoding Tokens with HS256
55
---------------------------------
66

77
.. code-block:: python
88
99
>>import jwt
10-
>>encoded = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256')
10+
>>key = 'secret'
11+
>>encoded = jwt.encode({'some': 'payload'}, key, algorithm='HS256')
1112
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.4twFt5NiznN84AWoo1d7KO1T_yoc0Z6XOpOVswacPZg'
13+
>>decoded = jwt.decode(encoded, key, algorithms='HS256')
14+
{'some': 'payload'}
1215
16+
Encoding & Decoding Tokens with RS256 (RSA)
17+
---------------------------------
18+
19+
.. code-block:: python
20+
21+
>>import jwt
22+
>>private_key = b'-----BEGIN PRIVATE KEY-----\nMIGEAgEAMBAGByqGSM49AgEGBS...'
23+
>>public_key = b'-----BEGIN PUBLIC KEY-----\nMHYwEAYHKoZIzj0CAQYFK4EEAC...'
24+
>>encoded = jwt.encode({'some': 'payload'}, private_key, algorithm='RS256)
25+
'eyJhbGciOiJIU...'
26+
>>decoded = jwt.decode(encoded, public_key, algorithms='RS256')
27+
{'some': 'payload'}
1328
1429
Specifying Additional Headers
1530
---------------------------------

0 commit comments

Comments
 (0)