Skip to main content
PyJWT
=====
A Python implementation of [JSON Web Token draft 01](http://self-issued.info/docs/draft-jones-json-web-token-01.html).

Installing
----------

sudo easy_install PyJWT

**Note**: The RSASSA-PKCS1-v1_5 algorithms depend on PyCrypto. If you plan on using any of those algorithms you'll need to install it as well.

sudo easy_install PyCrypto

Usage
-----

import jwt
jwt.encode({"some": "payload"}, "secret")

Note the resulting JWT will not be encrypted, but verifiable with a secret key.

jwt.decode("someJWTstring", "secret")

If the secret is wrong, it will raise a `jwt.DecodeError` telling you as such. You can still get at the payload by setting the verify argument to false.

jwt.decode("someJWTstring", verify=False)

Algorithms
----------

The JWT spec supports several algorithms for cryptographic signing. This library currently supports:

* HS256 - HMAC using SHA-256 hash algorithm (default)
* HS384 - HMAC using SHA-384 hash algorithm
* HS512 - HMAC using SHA-512 hash algorithm
* RS256 - RSASSA-PKCS1-v1_5 signature algorithm using SHA-256 hash algorithm
* RS384 - RSASSA-PKCS1-v1_5 signature algorithm using SHA-384 hash algorithm
* RS512 - RSASSA-PKCS1-v1_5 signature algorithm using SHA-512 hash algorithm

Change the algorithm with by setting it in encode:

jwt.encode({"some": "payload"}, "secret", "HS512")

For the RSASSA-PKCS1-v1_5 algorithms, the "secret" argument in jwt.encode is supposed to be a private RSA key as
imported with Crypto.PublicKey.RSA.importKey. Likewise, the "secret" argument in jwt.decode is supposed to be the public RSA key imported with the same method.

Tests
-----

You can run tests from the project root after installed with:

python tests/test_jwt.py

Support of reserved claim names
-------------------------------

Json Web Token defines some reserved claim names and defines how they should be used. PyJWT support theses reserved claim names:

- "exp" (Expiration Time) Claim

Expiration Time Claim
=====================

From JWT RFC:

The exp (expiration time) claim identifies the expiration time on or after which the JWT MUST NOT be accepted for processing. The processing of the exp claim requires that the current date/time MUST be before the expiration date/time listed in the exp claim. Implementers MAY provide for some small leeway, usually no more than a few minutes, to account for clock skew. Its value MUST be a number containing an IntDate value. Use of this claim is OPTIONAL.

You can pass the expiration time as a timestamp (an int) or as a datetime. Expiration time will be casted into a timestamp. For example:

jwt.encode({"exp": 1371720939}, "secret")

jwt.encode({"exp": datetime.utcnow()}, "secret")

Expiration time will be automatically verified in pyjwt.decode and raises a jwt.ExpiredSignature if expiration time is in past:

import jwt
try:
jwt.decode('JWT_STRING', "secret")
except jwt.ExpiredSignature:
# Signature has expired

Expiration time will be compared to utc timestamp (as given by timegm(datetime.utcnow().utctimetuple())) so be sure to use utc timestamp or datetime in encoding.

You can turn off expiration time verification with verify_expiration argument.

Pyjwt also support the leeway part of expiration time definition, which means you can validate a expiration time which is in the past but no very far. For example, if you have a jwt payload with a expiration time set to 30 seconds after creation but you know that sometimes you will process it after 30 seconds, you can set a leeway of 10 seconds in order to have some margin:

import jwt, time
jwt_payload = jwt.encode({'exp': datetime.utcnow() + datetime.timedelta(seconds=30)}, 'secret')
time.sleep(32)
# Jwt payload is now expired
# But with some leeway, it will be correclt validated
jwt.decode(jwt_payload, 'secret', leeway=10)


License
-------

MIT

Metadata

Release files for PyJWT 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyJWT 0.1.8
File Size Uploaded
PyJWT-0.1.8.tar.gz 5.7 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for PyJWT 0.1.8
File
PyJWT-0.1.8-py3.3.egg Legacy Egg format - - Details
PyJWT-0.1.8-py2.7.egg Legacy Egg format - - Details
PyJWT-0.1.8-py2.6.egg Legacy Egg format - - Details
PyJWT-0.1.8-py2.5.egg Legacy Egg format - - Details

Total release size: 38.6 kB

Release files / PyJWT-0.1.8.tar.gz

Download URL PyJWT-0.1.8.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e50d95fa810900b3a973d58b14ab750805c60dd37fb410bc4900d010aae21645
BLAKE2b-256 checksum
How to use checksums
d15c01e9f45f28a00f1f8559c743951f33d199a0a76d831a692f6694a74278a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / PyJWT-0.1.8-py3.3.egg

Download URL PyJWT-0.1.8-py3.3.egg
Size 9.1 kB
Tags Egg
SHA-256 checksum
How to use checksums
987ffea48e790451252cabadd959bbc1a4e6fdfe9a22fea148ba1a1d1bbfde76
BLAKE2b-256 checksum
How to use checksums
fdd80978685803195d352f96e1bb83a5d49b65c744bebd6be6af93f6a15aa0bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / PyJWT-0.1.8-py2.7.egg

Download URL PyJWT-0.1.8-py2.7.egg
Size 9.0 kB
Tags Egg
SHA-256 checksum
How to use checksums
0347b20993b440290376af52a3498741a36953035e34c732aaddf57cddb83dd3
BLAKE2b-256 checksum
How to use checksums
af5fdcdada7de4146fdba02766704ba144c2e640b8ee324fd373a0a3031bd4e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / PyJWT-0.1.8-py2.6.egg

Download URL PyJWT-0.1.8-py2.6.egg
Size 8.9 kB
Tags Egg
SHA-256 checksum
How to use checksums
e2baff703721cfcb5271c1a8b855de8902da192e3f958269d2b724b2d8957b17
BLAKE2b-256 checksum
How to use checksums
c0b49b92a8af5510f132f08b862f3f1ca340f1e81dcdddbfb1b2b56d76bfc029
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / PyJWT-0.1.8-py2.5.egg

Download URL PyJWT-0.1.8-py2.5.egg
Size 5.9 kB
Tags Egg
SHA-256 checksum
How to use checksums
3f25f181348d72136a15c0c4ba67c69798f12d59bcbfae5f76edb9fcc47a293f
BLAKE2b-256 checksum
How to use checksums
93cc5847b94808b64e10034c11de57aa756867209ca0bca59793c8c8207881fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

2.15.1

2 release files

2.15.0

2 release files

2.14.0

2 release files

2.13.0

2 release files

2.12.1

2 release files

2.12.0

2 release files

2.11.0

2 release files

2.10.1

2 release files

2.10.0

2 release files

2.9.0

2 release files

2.8.0

2 release files

2.7.0

2 release files

2.6.0

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.4

2 release files

1.6.3

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.3

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.3

5 release files

0.2.1

4 release files

0.2.0

4 release files

0.1.9

4 release files

This release

0.1.8 This release

5 release files

0.1.7

5 release files

0.1.6

4 release files

0.1.5

4 release files

0.1.4

4 release files

0.1.3

4 release files

0.1.2

4 release files

0.1.1

4 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page