Home

Awesome

python-jwt   Build Status Coverage Status PyPI version

Module for generating and verifying JSON Web Tokens.

All versions of python-jwt are now DEPRECATED. I don't have the time to maintain this module.

Example:

import python_jwt as jwt, jwcrypto.jwk as jwk, datetime
key = jwk.JWK.generate(kty='RSA', size=2048)
payload = { 'foo': 'bar', 'wup': 90 };
token = jwt.generate_jwt(payload, key, 'PS256', datetime.timedelta(minutes=5))
header, claims = jwt.verify_jwt(token, key, ['PS256'])
for k in payload: assert claims[k] == payload[k]

The API is described here.

Installation

pip install python_jwt

Another Example

You can read and write keys from and to PEM-format strings:

import python_jwt as jwt, jwcrypto.jwk as jwk, datetime
key = jwk.JWK.generate(kty='RSA', size=2048)
priv_pem = key.export_to_pem(private_key=True, password=None)
pub_pem = key.export_to_pem()
payload = { 'foo': 'bar', 'wup': 90 };
priv_key = jwk.JWK.from_pem(priv_pem)
pub_key = jwk.JWK.from_pem(pub_pem)
token = jwt.generate_jwt(payload, priv_key, 'RS256', datetime.timedelta(minutes=5))
header, claims = jwt.verify_jwt(token, pub_key, ['RS256'])
for k in payload: assert claims[k] == payload[k]

Licence

MIT

Tests

make test

Lint

make lint

Code Coverage

make coverage

coverage.py results are available here.

Coveralls page is here.

Benchmarks

make bench

Here are some results on a laptop with an Intel Core i5-4300M 2.6Ghz CPU and 8Gb RAM running Ubuntu 17.04.

Generate Keyuser (ns)sys (ns)real (ns)
RSA103,100,000200,000103,341,537
Generate Tokenuser (ns)sys (ns)real (ns)
HS256220,0000226,478
HS384220,0000218,233
HS512230,0000225,823
PS2561,530,00010,0001,536,235
PS3841,550,00001,549,844
PS5121,520,00010,0001,524,844
RS2561,520,00010,0001,524,565
RS3841,530,00001,528,074
RS5121,510,00001,526,089
Load Keyuser (ns)sys (ns)real (ns)
RSA210,0003,000210,791
Verify Tokenuser (ns)sys (ns)real (ns)
HS256100,0000101,478
HS384100,00010,000103,014
HS512110,0000104,323
PS256230,0000231,058
PS384240,0000237,551
PS512240,0000232,450
RS256230,0000227,737
RS384230,0000230,698
RS512230,0000228,624