Skip to main content

RSA helpers for PyJWT

Project description

Helpers for JWT tokens with RSA.

>>> from jwt_rsa.token import JWT
>>> from jwt_rsa.rsa import generate_rsa
>>>
>>> bits = 2048
>>>
>>> private_key, public_key = generate_rsa(bits)
>>>
>>> jwt = JWT(private_key, public_key)
>>>
>>> token = jwt.encode(foo='bar')
>>> result = jwt.decode(token)
>>> result
{'foo': 'bar', 'exp': 1525941819.638339, 'nbf': 1523349799.638342}
>>> # Expired token
>>> token = jwt.encode(foo='bar', expired=-1)
>>> jwt.decode(token)
Traceback (most recent call last):
...
jwt.exceptions.ExpiredSignatureError: Signature has expired
>>> # No verify token signature and expiration
>>> jwt.decode(token, verify=False)
{'foo': 'bar', 'exp': -1, 'nbf': 1523350046.935803}

Command line utilities

Module provides following utilities

jwt-rsa-keygen

Creates a new key pair:

$ jwt-rsa-keygen -h                                                                                                                                                                                              ±8 ?3 master
usage: jwt-rsa-keygen [-h] [-b BITS] [-P]

optional arguments:
  -h, --help            show this help message and exit
  -b BITS, --bits BITS
  -P, --pem

By default this utility return JSON-serialized key pair:

$ jwt-rsa-keygen                                                                                                                                                                                                 ±8 ?3 master
{
   "private": "MIIEvgIBADANBg......h3MBsSzx",
   "public": "MIIBCgKCAQEAxUU......5niBEjAB"
}

Add parameter -P for return in PEM format:

$ jwt-rsa-keygen -P                                                                                                                                                                                              ±8 ?3 master
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDDeiI5V/O/Mbff
...
LGQgWf5ch0t1+Rh3tjIuuSc=
-----END PRIVATE KEY-----

-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAw3oiOVfzvzG331nAL5hGHbblcCaV3pbfoCiFRgwpNPf7snIJtw97
...
3k2mMT1z6NFO6e6LMxg2zrqs3zgqwx5/9wIDAQAB
-----END RSA PUBLIC KEY-----

jwt-rsa-verify

Verify JSON serialized key pair:

$ jwt-rsa-keygen | jwt-rsa-verify                                                                                                                                                                                ±8 ?3 master
INFO:root:Awaiting JSON on stdin...
INFO:root:Signing OK
INFO:root:Verifying OK

Or failed when key pair is invalid or doesn’t match:

$ jwt-rsa-keygen | sed 's/M/j/' | jwt-rsa-verify                                                                                                                                                                 ±8 ?3 master
INFO:root:Awaiting JSON on stdin...
Traceback (most recent call last):
  ...
ValueError: Could not deserialize key data.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyjwt-rsa-0.1.6.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

pyjwt_rsa-0.1.6-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file pyjwt-rsa-0.1.6.tar.gz.

File metadata

  • Download URL: pyjwt-rsa-0.1.6.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/33.1.1 requests-toolbelt/0.8.0 tqdm/4.20.0 CPython/3.6.5

File hashes

Hashes for pyjwt-rsa-0.1.6.tar.gz
Algorithm Hash digest
SHA256 aa7f80567de00b852d359d7b39de0ac7b1f725d0dca444fee70a102a21975883
MD5 c86c63615def470d1d3f5100b6ee5b44
BLAKE2b-256 d139f73f7778af34f21fe6de1e5daa84bf92aa7ef9d0d95735f18fa28857bcfd

See more details on using hashes here.

File details

Details for the file pyjwt_rsa-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: pyjwt_rsa-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/33.1.1 requests-toolbelt/0.8.0 tqdm/4.20.0 CPython/3.6.5

File hashes

Hashes for pyjwt_rsa-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 9b3c5b3bfacb2daa2040719a1923db322e153550db3f3a23b36a672ef7837aae
MD5 ac8574b354e7e4b258fbc413d2be994f
BLAKE2b-256 3f0f4d6b4f2b303ea90c930f1d90572f26303e09abfb13735391d5ba7fd9b1bb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page