JSON Web Token Implementation (Python)
The pyjwt-tspspi project is a simple JWT implementation that builds upon the pycryptodomex cryptography library. It performs JSON serialization of encrypted (JWE) and signed (JWS) objects as well as accompanying keys (JWK). The library supports both compact and non-compact JWTs.
Installation
To install the package, use the following command:
pip install pyjwt-tspspi
Supported Mechanisms
- Signature Algorithms: RS256, HS256, etc.
- Encryption Algorithms: RSA-OAEP, A128GCM, etc.
Documentation
For more detailed information, please refer to the documentation.
Usage
JSON Web Encryption (JWE)
This example demonstrates how to create and parse a JSON Web Encryption (JWE).
from jwt.jwe import JWE
from jwt.keystore import Keystore
from jwt.jwk import JWK_RSA
# Sample payload
payload = {"sub": "1234567890", "name": "John Doe", "admin": True}
# Create a keystore and add an RSA JWK
keystore = Keystore()
jwk_rsa = JWK_RSA.create(bits=2048, key_ops=["encrypt", "decrypt"], key_id="RSA test key")
keystore.add(jwk_rsa)
# Create a JWE
jwe = JWE.create(payload, [jwk_rsa], compact=False)
print("Created JWE:", jwe.to_json(indent=4))
# Parse the JWE
parsed_jwe = JWE.parse(jwe.to_json(), keystore=keystore)
print("Parsed Payload:", json.dumps(parsed_jwe.get_payload(), indent=2))
JSON Web Key (JWK) Encryption/Decryption
This example demonstrates how to encrypt and decrypt data using JSON Web Key (JWK).
from jwt.jwk import JWK_RSA
# Sample payload
payload = b"This is a secret message."
# Create an RSA JWK for encryption and decryption
jwk = JWK_RSA.create(bits=2048, use="enc")
# Encrypt the payload
encrypted_payload = jwk.encrypt(payload)
print("Encrypted Payload:", encrypted_payload)
# Decrypt the payload
decrypted_payload = jwk.decrypt(encrypted_payload)
print("Decrypted Payload:", decrypted_payload.decode('utf-8'))
JSON Web Signature (JWS)
This example demonstrates how to create and parse a JSON Web Signature (JWS).
from jwt.jws import JWS
from jwt.keystore import Keystore
from jwt.jwk import JWK_Shared
# Sample payload
payload = {"sub": "1234567890", "name": "John Doe", "admin": True}
# Create a JWK for signing
jwk = JWK_Shared.create(secret=b"secret", use="sig", key_id="test-key")
# Create a keystore and add the key
keystore = Keystore()
keystore.add(jwk)
# Create a JWS
jws = JWS.create(payload, signaturekeys=jwk, compact=True)
print("Created JWS:", jws.to_json())
# Parse the JWS
parsed_jws = JWS.parse(jws.to_json(), keystore=keystore)
print("Parsed Payload:", parsed_jws.get_payload())
JSON Web Token (JWT)
This example demonstrates how to create and parse a JSON Web Token (JWT).
from jwt.jwt import parse_jwt
from jwt.keystore import Keystore
from jwt.jwk import JWK_Shared
from jwt.jws import JWS
# Create a JWK for signing
jwk = JWK_Shared.create(secret=b"my-secret-key", use="sig", key_id="test-key")
# Create a keystore and add the key
keystore = Keystore()
keystore.add(jwk)
# Create sample JWT payload (claims)
payload = {
"sub": "1234567890",
"name": "John Doe",
"admin": True,
"iat": 1516239022
}
# Create a JWT
jwt = JWS.create(payload, signaturekeys=jwk, compact=True)
jwt_token = jwt.to_json()
print("Created JWT:", jwt_token)
# Parse the JWT token
parsed_jwt = parse_jwt(jwt_token, keystore=keystore)
print("Parsed JWT payload:", parsed_jwt.get_payload())
Metadata
Release files for pyjwt-tspspi 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyjwt_tspspi-0.0.7.tar.gz | 15.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyjwt_tspspi-0.0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 32.1 kB
Release files / pyjwt_tspspi-0.0.7.tar.gz
| Download URL | pyjwt_tspspi-0.0.7.tar.gz |
|---|---|
| Size | 15.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
913ff38322d244266de843a57d3e76aa7d6433df5e38b1f742e9a7735dbe24de
|
|
BLAKE2b-256 checksum How to use checksums |
d1b75d06183f6429ae057a0628781a620e98efddb50f20f8814e210c6dfe2fa8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|
Release files / pyjwt_tspspi-0.0.7-py3-none-any.whl
| Download URL | pyjwt_tspspi-0.0.7-py3-none-any.whl |
|---|---|
| Size | 16.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b8c47fe61f880d55d7082a107d8461d48f1da82662a9f740ee1874df224de038
|
|
BLAKE2b-256 checksum How to use checksums |
07c4fc827087ae9d8af91a3a4966c28073b0ba597511dc67b298af845244a3f5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|