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

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

Download files

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

Source Distribution

PyJWT-0.1.6.tar.gz (5.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

PyJWT-0.1.6-py2.7.egg (7.7 kB view details)

Uploaded Egg

PyJWT-0.1.6-py2.6.egg (7.7 kB view details)

Uploaded Egg

PyJWT-0.1.6-py2.5.egg (5.3 kB view details)

Uploaded Egg

File details

Details for the file PyJWT-0.1.6.tar.gz.

File metadata

  • Download URL: PyJWT-0.1.6.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PyJWT-0.1.6.tar.gz
Algorithm Hash digest
SHA256 c3d811821e33a6c3c0d40810d4d34ce66aeefb36fa60418d2590b6c08e611c44
MD5 ba939c846135a51289f4a4854df935ad
BLAKE2b-256 6d79fe8fc0c85c9dbd9b08ab4f4b795c49f99d7120f28f9d9f5d0053cd485d6f

See more details on using hashes here.

File details

Details for the file PyJWT-0.1.6-py2.7.egg.

File metadata

  • Download URL: PyJWT-0.1.6-py2.7.egg
  • Upload date:
  • Size: 7.7 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PyJWT-0.1.6-py2.7.egg
Algorithm Hash digest
SHA256 72031bcd0fe24b07f63d6e2087ee2adb4921b56987579107291ed9ecf1d7b2b0
MD5 4220a08f54cff89718fc97a8cc23839e
BLAKE2b-256 383607c10e952cbafc92c332433e574ea5b33598cbb8bbc0cdf666309338dbcd

See more details on using hashes here.

File details

Details for the file PyJWT-0.1.6-py2.6.egg.

File metadata

  • Download URL: PyJWT-0.1.6-py2.6.egg
  • Upload date:
  • Size: 7.7 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PyJWT-0.1.6-py2.6.egg
Algorithm Hash digest
SHA256 6c7d18b8fb78991802af1b043207583dc177dd177c61a5d915fc01af0d6db1e3
MD5 d41853fd2adddd65c5424f6a6c212c5b
BLAKE2b-256 aca4a797af3b49e8224a167ae422a4b91a999acb737d94695db01865d3e9bb6b

See more details on using hashes here.

File details

Details for the file PyJWT-0.1.6-py2.5.egg.

File metadata

  • Download URL: PyJWT-0.1.6-py2.5.egg
  • Upload date:
  • Size: 5.3 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PyJWT-0.1.6-py2.5.egg
Algorithm Hash digest
SHA256 6801bbfb99fcfa05ad16c3cd64173b2a916d8688549c43081ffba1c58c8ad70d
MD5 80b8210e530f257fc509c86a00444850
BLAKE2b-256 f05650ce1db8d583652289e2c2e29d2475850768f6fc757843e6f9d0cd93c76d

See more details on using hashes here.

Release history Release notifications | RSS feed

2.14.0

2 files

2.13.0

2 files

2.12.1

2 files

2.12.0

2 files

2.11.0

2 files

2.10.1

2 files

2.10.0

2 files

2.9.0

2 files

2.8.0

2 files

2.7.0

2 files

2.6.0

2 files

2.5.0

2 files

2.4.0

2 files

2.3.0

2 files

2.2.0

2 files

2.1.0

2 files

2.0.1

2 files

2.0.0

2 files

1.7.1

2 files

1.7.0

2 files

1.6.4

2 files

1.6.3

2 files

1.6.1

2 files

1.6.0

2 files

1.5.3

2 files

1.5.2

2 files

1.5.1

2 files

1.5.0

2 files

1.4.2

2 files

1.4.1

2 files

1.4.0

2 files

1.3.0

2 files

1.1.0

2 files

1.0.1

2 files

1.0.0

2 files

0.4.3

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.3

5 files

0.2.1

4 files

0.2.0

4 files

0.1.9

4 files

0.1.8

5 files

0.1.7

5 files

This release

0.1.6 This release

4 files

0.1.5

4 files

0.1.4

4 files

0.1.3

4 files

0.1.2

4 files

0.1.1

4 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