Skip to main content

JSON Web Token implementation in Python

Project description

A Python implementation of JSON Web Token draft 01.

Installing

$ pip install PyJWT

A Note on Dependencies:

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

The Elliptic Curve Digital Signature algorithms depend on Python-ECDSA. If you plan on using any of those algorithms, you’ll need to install it as well.

$ pip install ecdsa

Usage

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

Additional headers may also be specified.

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

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 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

  • ES256 - ECDSA signature algorithm using SHA-256 hash algorithm

  • ES384 - ECDSA signature algorithm using SHA-384 hash algorithm

  • ES512 - ECDSA signature algorithm 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')

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 public or private key as imported with Crypto.PublicKey.RSA.importKey().

When using the ECDSA algorithms, the key argument is expected to be an Elliptic Curve private key as imported with ecdsa.SigningKey.from_pem(), or a public key as imported with ecdsa.VerifyingKey.from_pem().

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

  • “nbf” (Not Before Time) Claim

  • “iss” (Issuer) Claim

  • “aud” (Audience) Claim

Expiration Time Claim

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

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.3.2.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

PyJWT-0.3.2-py2.py3-none-any.whl (11.4 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for PyJWT-0.3.2.tar.gz
Algorithm Hash digest
SHA256 e222ad122a9769786e18ded6e61ec7aa3d9a3d490364a4f79a3a50237eaf72d3
MD5 ce35894c56745f288a559fa75743dd9e
BLAKE2b-256 53d04f117ead643ef86dd5990837397e547c9ec1713eeca14c9e710f988ece4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for PyJWT-0.3.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 60254ffcecfeb77aad99c5fc24ba7452d4658143619b6bfa7aa1737d229f10a5
MD5 93ce772780fdffe0362fc1bd490bc6f6
BLAKE2b-256 c408086186c13c83960297ad09b849ded789a7687169af4d158f85a6629a6bfd

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