From e0aa10ee586bf010ce9a99c5582ee8dc9293ba1e Mon Sep 17 00:00:00 2001 From: Mark Adams Date: Tue, 28 Nov 2017 09:36:37 -0600 Subject: [PATCH] docs: Add example of encoding and decoding tokens with RSA (#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 --- docs/usage.rst | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index dfb27285..bb1377e0 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -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 ---------------------------------