diff --git a/docs/usage.rst b/docs/usage.rst index efae248b..3cf5ed7e 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -49,6 +49,22 @@ If you are repeatedly encoding with the same private key, reusing the same ``RSAPrivateKey`` also has performance benefits because it avoids the CPU-intensive ``RSA_check_key`` primality test. +Encoding & Decoding Tokens with PS256 (RSA) +------------------------------------------- + +RSA encoding and decoding require the ``cryptography`` module. See :ref:`installation_cryptography`. + +.. code-block:: pycon + + >>> import jwt + >>> private_key = "-----BEGIN RSA PRIVATE KEY-----\nMIIEogIBAAKCAQEAuNhCS6bodtd+PvKqNj+tYZYqTNMDkf0rcptgHhecSsMP9Vay\n+6NvJk1tC+IajPaE4yRJVY4jFqEt3A0MJ9sKe5mWDYFmzW/L6VzQvQ+0nrMc1YTE\nDpOf7BQhlW5W0mDj5SwSR50Lxg/acb+SMWq6zmhuAoLRapH17K2RWONA2vr2frox\nJ6N9TGtrQHygDb0p9D6jPnXEe4y+zBuj6o0bCkJgCVNM+CU19xBepj5caetYV28/\n49yl5XPi93n1ATU+7aGAKxuvjudODuHhF/UsZScMFSHeZW367eQldTB2w9uoIIzW\nO46tKimr21zYifMimjwnBQ/PLDqc7HqY0Y/rLQIDAQABAoIBAAdu0CD7/Iu61/LE\nDfV8fgZXOYA5WVgSLCBsVbh1Y+2FsStBFJVrLwRanLCbo6GuJWMqNGC3ryWGebJI\nPAg7lfepEhBHodClAY1yvq9mOvHJa2Fn+KegEWWMMbAxQwCBW5NS6waXhBUE0i3n\ncYOB3TKA9IYuqH52kW22VQqT/imlWEb28pJJT49YfggmOOtAkrKerokO53lAfrJA\ntm8lYvxXnfnuYh7zI835RpZJ1PeaYrMqyAwT+StD9hPKGWGpN1gCJijjcK0aapvq\nMLET/JxMxxcLsINOeLtGhMKawmET3J/esJTumOE2L77MFG83rlCPbsSfLdSAI2WD\nSe3Q2ikCgYEA7JzmVrPh7G/oILLzIfk8GHFACRTtlE5SDEpFq+ARMprfcBXpkl+Q\naWqQ3vuS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0mDj5SwSR50Lxg/acb+SMWq6\nzmhuAoLRapH17K2RWONA2vr2froxJ6N9TGtrQHygDb0p9D6jPnXEe4y+zBuj6o0b\nCkJgCVNM+CU19xBepj5caetYV28/49yl5XPi93n1ATU+7aGAKxuvjudODuHhF/Us\nZScMFSHeZW367eQldTB2w9uoIIzWO46tKimr21zYifMimjwnBQ/PLDqc7HqY0Y/r\nLQIDAQAB\n-----END PUBLIC KEY-----\n" + >>> encoded = jwt.encode({"some": "payload"}, private_key, algorithm="PS256") + >>> print(encoded) + eyJhbGciOiJQUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.s0cBw3PYkVp1H1Bh1tyYkghhGdR02TazHF1iyLlK8XZXy1A9Oy_4LJduzt7MggAsiqU4S4OmeiApgs0bLvv_cZiATVVfuqiWKaCzkIEYP9J2eDUhohEwy4i-lI3EpvlmoD_Kcxgzhcwv2pjxGGNNTdDDqr4AvYLOENd-UltUMI9YkVROTM6zPIvaBXZ1Kix3BOCSLOU8fZU8Z9PT8KTsNQYCvs-9ZKml1sr-yQtNvt6YlSqaq0w6TYRUMkClEs3rdz5QrKrdcXYM00ksAi5gWwzyI5ITMkq3HgI3fF7ytsSoLxJTUHkPeZM0PwJ1DvNLAGg5y36jk697tLA--1g09w + >>> jwt.decode(encoded, public_key, algorithms=["PS256"]) + {'some': 'payload'} + Specifying Additional Headers -----------------------------