JSON Web Token implementation
Project description
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())
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyjwt_tspspi-0.0.7.tar.gz.
File metadata
- Download URL: pyjwt_tspspi-0.0.7.tar.gz
- Upload date:
- Size: 15.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
913ff38322d244266de843a57d3e76aa7d6433df5e38b1f742e9a7735dbe24de
|
|
| MD5 |
8bf368cc50a6e467fcbe5d4cb71328da
|
|
| BLAKE2b-256 |
d1b75d06183f6429ae057a0628781a620e98efddb50f20f8814e210c6dfe2fa8
|
File details
Details for the file pyjwt_tspspi-0.0.7-py3-none-any.whl.
File metadata
- Download URL: pyjwt_tspspi-0.0.7-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8c47fe61f880d55d7082a107d8461d48f1da82662a9f740ee1874df224de038
|
|
| MD5 |
294ff9dbec535be680075e20c9416ed6
|
|
| BLAKE2b-256 |
07c4fc827087ae9d8af91a3a4966c28073b0ba597511dc67b298af845244a3f5
|