Skip to main content
https://travis-ci.org/GehirnInc/python-jwt.svg?branch=master https://coveralls.io/repos/GehirnInc/python-jwt/badge.png?branch=master https://badge.fury.io/py/jwt.svg?dummy

python-jwt

python-jwt is a JSON Web Token (JWT) implementation in Python developed by Gehirn Inc.

Examples

import json
from datetime import datetime, timedelta, timezone

from jwt import (
    JWT,
    jwk_from_dict,
    jwk_from_pem,
)
from jwt.utils import get_int_from_datetime


instance = JWT()

message = {
    'iss': 'https://example.com/',
    'sub': 'yosida95',
    'iat': get_int_from_datetime(datetime.now(timezone.utc)),
    'exp': get_int_from_datetime(
        datetime.now(timezone.utc) + timedelta(hours=1)),
}

"""
Encode the message to JWT(JWS).
"""

# Load a RSA key from a JWK dict.
signing_key = jwk_from_dict({
    'kty': 'RSA',
    'e': 'AQAB',
    'n': '...',
    'd': '...'})
# Or load a RSA key from a PEM file.
with open('rsa_private_key.pem', 'rb') as fh:
    signing_key = jwk_from_pem(fh.read())
# You can also load an octet key in the same manner as the RSA.
# signing_key = jwk_from_dict({'kty': 'oct', 'k': '...'})

compact_jws = instance.encode(message, signing_key, alg='RS256')

"""
Decode the JWT with verifying the signature.
"""

# Load a public key from PEM file corresponding to the signing private key.
with open('rsa_public_key.json', 'r') as fh:
    verifying_key = jwk_from_dict(json.load(fh))

message_received = instance.decode(
    compact_jws, verifying_key, do_time_check=True)

"""
Successfuly retrieved the `message` from the `compact_jws`
"""
assert message == message_received

Installation

You can install python-jwt with pip.

$ pip install jwt

Implementation Details

Supported Algorithms

  • Unsecured

    • none (disabled by default for security)

  • Symmetric

    • HS256

    • HS384

    • HS512

  • Asymmetric

    • PS256

    • PS384

    • PS512

    • RS256

    • RS384

    • RS512

Supported Python Versions

  • Python 3.6+

License

python-jwt is licensed under the Apache License version 2. See ./LICENSE.rst.

Release files for jwt 1.4.0

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

Source distribution (sdist)

Source distribution for jwt 1.4.0
File Size Uploaded
jwt-1.4.0.tar.gz 24.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jwt 1.4.0
File Interpreter ABI Platform
jwt-1.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 43.2 kB

Release files / jwt-1.4.0.tar.gz

Download URL jwt-1.4.0.tar.gz
Size 24.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f6f789128ac247142c79ee10f3dba6e366ec4e77c9920d18c1592e28aa0a7952
BLAKE2b-256 checksum
How to use checksums
7f2021254c9e601e6c29445d1e8854c2a81bdb554e07a82fb1f9846137a6965c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.16

Release files / jwt-1.4.0-py3-none-any.whl

Download URL jwt-1.4.0-py3-none-any.whl
Size 18.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7560a7f1de4f90de94ac645ee0303ac60c95b9e08e058fb69f6c330f71d71b11
BLAKE2b-256 checksum
How to use checksums
b98034e3fae850adb0b7b8b9b1cf02b2d975fcb68e0e8eb7d56d6b4fc23f7433
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.16

Release history Release notifications | RSS feed

This release

1.4.0 This release

2 release files

1.3.1

1 release file

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

1 release file

0.6.1

1 release file

0.6.0

1 release file

0.5.4

4 release files

0.5.3

4 release files

0.5.2

4 release files

0.5.1

4 release files

0.5.0

0.3.2

1 release file

0.3.1

1 release file

0.2.3

2 release files

0.2.1

2 release files

0.1.1

2 release files

0.0.1

2 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