Skip to main content

jwtpie - Easily encrypt and decrypt JWT tokens

JWT easy as pie.

jwtpie is a highly opinionated library that makes encrypting and decrypting JWT tokes super easy. Perfect addition to your API backends.

jwtpie does the following:

  • Serialize and possibly compress your data.

  • Generate a signed JWT token with your serialized data.

  • Generate an encrypted JWT token with your signed JWT token.

And also:

  • Decrypt your encrypted JWT token, and validate it is still valid.

  • Verify signature of decrypted JWT token and validate it is still valid.

  • De-serialize and possibly decompress your data.

Install

pip3 install jwtpie

Optionally, install either orjson (preferred) or ujson (good) for a faster JSON decoding:

pip3 install orjson

Or …

pip3 install ujson

Usage

>>> from jwtpie import JWTPie
>>> mgr = JWTPie()
>>> token = mgr.encrypt({
...    'user': 'jane_doe@anonymous.io',
...    'name': 'Jane Doe',
})
>>> token
'eyJhbGciO[.........]iJBMjU2S1ldbM'
>>> mgr.decrypt(token)
{
    'user': 'jane_doe@anonymous.io',
    'name': 'Jane Doe',
}

So easy.

The above will generate a new signing and encryption key on-the-fly. If the process is restarted or killed, the key will be lost and all tokens generated with it will no longer be decryptable.

To allow to decode previously generated tokens pass the signing and encryption key in the constructor:

>>> mgr = JWTPie(
...     issuer='myapp',
...     signkey='YOUR PRIVATE SIGNING KEY',
...     encryptkey='YOUR PRIVATE ENCRYPTION KEY',
...     expiration_s=1209600,  # In seconds. 60 * 60 * 24 * 14 = Two weeks
... )

To generate a private key execute:

>>> from jwtpie import JWTPie
>>> size = 256
>>> JWTPie.generate(size)
noOIbGe_WLbTfrLIH_grNu0bf5u8Xx-bERELm2TLRaM

Or launch the interactive wizard:

$ python3 -m jwtpie

1. Craft a session.
2. Generate a new encryption or signing key.

What do you need? 2
Size: 256

Your key:

R5Co9mHaxURSzhryvvx8JqgpFLinhvd6L3rb2TxRx7o

Bye!

KEYS ARE SECRET!

Save it in your secret management system!

  • Do not hard code it.

  • Do not commit it to version control.

  • Never log it.

jwtpie is opinionated because it selects the signing and encryption algorithms for you. If you need to change this or change advanced parameters like the leeway please read the documentation in the docstring of the JWTPie class.

Repository

https://github.com/kuralabs/jwtpie

Acknowledgements

JWTPie is just an easy to use abstraction layer built on top of the great JWCrypto library. JWCrypto does the actual work of creating and verifying the tokens according to the JWT specification. JWCrypto is itself built on top of the secure cryptography library.

Changelog

1.2.0 (2022-09-15)

Changes

1.1.0 (2021-06-08)

New

  • New method decrypt_with_metadata() that will return the encrypted data along with the standard JWT claims.

1.0.0 (2021-06-01)

New

  • Initial release.

License

Copyright (C) 2016-2021 KuraLabs S.R.L

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.

Metadata

Release files for jwtpie 1.3.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 jwtpie 1.3.0
File Size Uploaded Provenance Actions
jwtpie-1.3.0.tar.gz 13.0 kB Without provenance Details for jwtpie-1.3.0.tar.gz

Built distribution (wheel)

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

Total release size: 25.4 kB

Release files / jwtpie-1.3.0.tar.gz

Download URL jwtpie-1.3.0.tar.gz
Size 13.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3897b68f516ed22cdda58c16593613583ac9445deb432da986c386a096706398
BLAKE2b-256 checksum
How to use checksums
44f2f97d58f3da9b24479778147db348d983e3f875dce557dab75bb920a648f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release files / jwtpie-1.3.0-py3-none-any.whl

Download URL jwtpie-1.3.0-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aa1b97b227de822d76941e04ff4942e3268ad73bbe57163002d3541ad980d676
BLAKE2b-256 checksum
How to use checksums
8cd54cf316d57cdfbf8f2779dac1f1345665cfb1fb1020653d3c222f5e8f3bd9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Security for jwtpie 1.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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