Skip to main content

JSON Web Token implementation in Python

Project description

# PyJWT [![Build Status](https://travis-ci.org/progrium/pyjwt.png?branch=master)](https://travis-ci.org/progrium/pyjwt)

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

## Installing

```
$ pip 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.

```
$ pip install PyCrypto
```

## Usage

```python
import jwt
jwt.encode({'some': 'payload'}, 'secret')
```

Additional headers may also be specified.

```python
jwt.encode({'some': 'payload'}, 'secret', headers={'kid': '230498151c214b788dd97f22b85410a5'})
```

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

```python
jwt.decode('someJWTstring', 'secret')
```

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

```python
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:

```python
jwt.encode({'some': 'payload'}, 'secret', 'HS512')
```

When using the RSASSA-PKCS1-v1_5 algorithms, the `key` argument in both
`jwt.encode()` and `jwt.decode()` (`"secret"` in the examples) is expected to
be an RSA private key as imported with `Crypto.PublicKey.RSA.importKey()`.

## Tests

You can run tests from the project root after cloning 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 supports these reserved claim names:

- "exp" (Expiration Time) Claim

### Expiration Time Claim

From [draft 01 of the JWT spec](http://self-issued.info/docs/draft-jones-json-web-token-01.html#ReservedClaimName):

> 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 UTC UNIX timestamp (an int) or as a
datetime, which will be converted into an int. For example:

```python
jwt.encode({'exp': 1371720939}, 'secret')

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

Expiration time is automatically verified in `jwt.decode()` and raises
`jwt.ExpiredSignature` if the expiration time is in the past:

```python
import jwt

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

Expiration time will be compared to the current UTC time (as given by
`timegm(datetime.utcnow().utctimetuple())`), so be sure to use a UTC timestamp
or datetime in encoding.

You can turn off expiration time verification with the `verify_expiration` argument.

PyJWT also supports the leeway part of the expiration time definition, which
means you can validate a expiration time which is in the past but not 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:

```python
import time
import jwt

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 still validate
jwt.decode(jwt_payload, 'secret', leeway=10)
```

## License

MIT

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-0.2.3.tar.gz (6.7 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.2.3-py3.3.egg (10.3 kB view details)

Uploaded Egg

PyJWT-0.2.3-py2.py3-none-any.whl (9.3 kB view details)

Uploaded Python 2Python 3

PyJWT-0.2.3-py2.7.egg (10.1 kB view details)

Uploaded Egg

PyJWT-0.2.3-py2.6.egg (10.1 kB view details)

Uploaded Egg

File details

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

File metadata

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

File hashes

Hashes for PyJWT-0.2.3.tar.gz
Algorithm Hash digest
SHA256 70df30ffa19fffb3d23703ddbc9916c8281e399a5d82e97bd14b91e852dfcd90
MD5 2590b4713ee53a57c8fa32fbbb03d794
BLAKE2b-256 4d9934c66fc98377f0e50dd2e61d852c3db076562d0935dacf55e589f0a1b90d

See more details on using hashes here.

File details

Details for the file PyJWT-0.2.3-py3.3.egg.

File metadata

  • Download URL: PyJWT-0.2.3-py3.3.egg
  • Upload date:
  • Size: 10.3 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PyJWT-0.2.3-py3.3.egg
Algorithm Hash digest
SHA256 a6a4a91fcde3e0fe7f7459cf17bd85d5cdfabda199baa98a6c0d6b6a509a8599
MD5 d1b8b73c5f296037e4078333c81987f5
BLAKE2b-256 f4141804de3b8a60504336bf71cd1d4e29cbeeddfcb8479af19f9750329ccd2b

See more details on using hashes here.

File details

Details for the file PyJWT-0.2.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for PyJWT-0.2.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ccd91f0b92d3e5ccad7fc6fefe7512ee64fefcb835bf056a27391cdb7b4bfec9
MD5 f6fc37cf8093f0495f9290bf075978db
BLAKE2b-256 5c8c473d189e877eb23b231233474c3d6eb4f721e81924309a7415afcaca7e3a

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for PyJWT-0.2.3-py2.7.egg
Algorithm Hash digest
SHA256 d0390feda0d576c06386fc4457145097bef38dc9d5ac8a7dc132910f57c3df6f
MD5 81dbcacba41e45a731c332a05a3a3074
BLAKE2b-256 9b816329e73e45e6ba7ebf43a50baed6d76c80cfcb2d97e8fceaa56be733f25a

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for PyJWT-0.2.3-py2.6.egg
Algorithm Hash digest
SHA256 a0850240afcc6d4180db43793bb2a4af91046918b3bee575a624fb69c8792ff5
MD5 324cb5f3576c3505a99d29984af1eb20
BLAKE2b-256 65628745bdf778cd2d6df80e5697c23c03bb9d28d2dac8ae3f4ca9c86e1331fa

See more details on using hashes here.

Supported by

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