|
| 1 | +# python-jwt [](https://travis-ci.org/davedoesdev/python-jwt) [](https://coveralls.io/r/davedoesdev/python-jwt?branch=master) [](http://badge.fury.io/py/jwt) |
| 2 | + |
| 3 | +Module for generating and verifying JSON Web Tokens. |
| 4 | + |
| 5 | +- Uses [jws](https://github.com/brianloveswords/python-jws) to do the heavy lifting. |
| 6 | +- Supports [__RS256__, __RS384__, __RS512__](http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-14#section-3.3), [__PS256__, __PS384__, __PS512__](http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-14#section-3.5), [__HS256__, __HS384__ and __HS512__](http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-14#section-3.2) signature algorithms. |
| 7 | +- Unit tests, including tests for interoperability with [node-jsjws](https://github.com/davedoesdev/node-jsjws). |
| 8 | + |
| 9 | +Example: |
| 10 | + |
| 11 | +```python |
| 12 | +import jwt, Crypto.PublicKey.RSA as RSA, datetime |
| 13 | +key = RSA.generate(2048) |
| 14 | +payload = { 'foo': 'bar', 'wup': 90 }; |
| 15 | +token = jwt.generate_jwt(payload, key, 'PS256', datetime.timedelta(minutes=5)) |
| 16 | +header, claims = jwt.verify_jwt(token, key) |
| 17 | +for k in payload: assert claims[k] == payload[k] |
| 18 | +``` |
| 19 | + |
| 20 | +The API is described [here](docs/_build/html/index.html). |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +```shell |
| 25 | +pip install jwt |
| 26 | +``` |
| 27 | + |
| 28 | +## Another Example |
| 29 | + |
| 30 | +You can read and write keys from and to [PEM-format](http://www.openssl.org/docs/crypto/pem.html) strings: |
| 31 | + |
| 32 | +```python |
| 33 | +import jwt, Crypto.PublicKey.RSA as RSA, datetime |
| 34 | +key = RSA.generate(2048) |
| 35 | +priv_pem = key.exportKey() |
| 36 | +pub_pem = key.publickey().exportKey() |
| 37 | +payload = { 'foo': 'bar', 'wup': 90 }; |
| 38 | +priv_key = RSA.importKey(priv_pem) |
| 39 | +pub_key = RSA.importKey(pub_pem) |
| 40 | +token = jwt.generate_jwt(payload, priv_key, 'RS256', datetime.timedelta(minutes=5)) |
| 41 | +header, claims = jwt.verify_jwt(token, pub_key) |
| 42 | +for k in payload: assert claims[k] == payload[k] |
| 43 | +``` |
| 44 | + |
| 45 | +## Licence |
| 46 | + |
| 47 | +[MIT](LICENCE) |
| 48 | + |
| 49 | +## Tests |
| 50 | + |
| 51 | +```shell |
| 52 | +make test |
| 53 | +``` |
| 54 | + |
| 55 | +## Lint |
| 56 | + |
| 57 | +```shell |
| 58 | +make lint |
| 59 | +``` |
| 60 | + |
| 61 | +## Code Coverage |
| 62 | + |
| 63 | +```shell |
| 64 | +make coverage |
| 65 | +``` |
| 66 | + |
| 67 | +[coverage.py](http://nedbatchelder.com/code/coverage/) results are available [here](http://htmlpreview.github.io/?https://github.com/davedoesdev/python-jwt/blob/master/coverage/html/index.html). |
| 68 | + |
| 69 | +Coveralls page is [here](https://coveralls.io/r/davedoesdev/python-jwt). |
| 70 | + |
| 71 | +## Benchmarks |
| 72 | + |
| 73 | +```shell |
| 74 | +make bench |
| 75 | +``` |
| 76 | + |
| 77 | +Here are some results on a laptop with an Intel Core i5-3210M 2.5Ghz CPU and 6Gb RAM running Ubuntu 13.04. |
| 78 | + |
| 79 | +Generate Key|user (ns)|sys (ns)|real (ns) |
| 80 | +:--|--:|--:|--: |
| 81 | +RSA|152,700,000|300,000|152,906,095 |
| 82 | + |
| 83 | +Generate Token|user (ns)|sys (ns)|real (ns) |
| 84 | +:--|--:|--:|--: |
| 85 | +HS256|140,000|10,000|157,202 |
| 86 | +HS384|160,000|10,000|156,403 |
| 87 | +HS512|139,999|20,000|153,212 |
| 88 | +PS256|3,159,999|49,999|3,218,649 |
| 89 | +PS384|3,170,000|10,000|3,176,899 |
| 90 | +PS512|3,120,000|9,999|3,141,219 |
| 91 | +RS256|3,070,000|20,000|3,094,644 |
| 92 | +RS384|3,090,000|0|3,092,471 |
| 93 | +RS512|3,079,999|20,000|3,095,314 |
| 94 | + |
| 95 | +Load Key|user (ns)|sys (ns)|real (ns) |
| 96 | +:--|--:|--:|--: |
| 97 | +RSA|811,000|0|810,139 |
| 98 | + |
| 99 | +Verify Token|user (ns)|sys (ns)|real (ns) |
| 100 | +:--|--:|--:|--: |
| 101 | +HS256|140,000|0|129,947 |
| 102 | +HS384|130,000|0|130,161 |
| 103 | +HS512|119,999|0|128,850 |
| 104 | +PS256|780,000|10,000|775,609 |
| 105 | +PS384|759,999|0|752,933 |
| 106 | +PS512|739,999|0|738,118 |
| 107 | +RS256|700,000|0|719,365 |
| 108 | +RS384|719,999|0|721,524 |
| 109 | +RS512|730,000|0|719,706 |
| 110 | + |
0 commit comments