jwcrypto: JOSE Web Standards Implementation
JWCrypto is a Python library that implements the Javascript Object Signing and Encryption (JOSE) Web Standards, including JSON Web Key (JWK), JSON Web Signature (JWS), JSON Web Encryption (JWE), and JSON Web Token (JWT). It leverages the `cryptography` package for its underlying cryptographic functions, ensuring strong security. The library is actively maintained, with frequent releases addressing security vulnerabilities and compatibility, currently at version 1.5.6.
Warnings
- breaking The JWT module introduced security fixes in v1.4.0 (CVE-2022-3102) that prevent token type substitution attacks. This required changes to token validation, defaulting to 'JWS' unless explicitly set or inferred. Old applications might break if they relied on implicit auto-detection without specifying `expect_type` or allowing mixed-type tokens.
- deprecated The `RSA1_5` algorithm is considered deprecated due to severe security vulnerabilities (Bleichenbacher RSA padding oracle, Million messages attack). Using it can lead to decryption of intercepted messages or forging signatures.
- breaking Support for Python 3.6 and 3.7 was dropped starting with version 1.5.3. Installations on these Python versions will likely encounter compatibility issues.
- breaking The minimum required version for the `cryptography` library was raised to 3.4 in `jwcrypto` v1.5.0. Older versions of `cryptography` will cause dependency resolution failures or runtime errors.
- gotcha JWT payloads are base64-encoded, not encrypted. This means anyone with the token can easily decode and read its contents. Storing sensitive information directly in a JWT payload is a major security risk.
- gotcha Lack of proper validation for the `kid` (Key ID) header parameter can lead to key confusion attacks, where an attacker might influence which key is used for verification.
- gotcha Versions 1.5.1 and 1.5.6 addressed potential Denial of Service (DoS) vulnerabilities related to PBKDF2 symmetric keys (v1.5.1) and high compression ratios (v1.5.6). Older versions might be susceptible.
Install
-
pip install jwcrypto
Imports
- JWK
from jwcrypto import jwk
- JWS
from jwcrypto import jws
- JWE
from jwcrypto import jwe
- JWT
from jwcrypto import jwt
- json_encode, json_decode
from jwcrypto.common import json_encode, json_decode
Quickstart
from jwcrypto import jwk, jws
from jwcrypto.common import json_encode
# 1. Generate a symmetric key
key = jwk.JWK.generate(kty='oct', size=256)
# 2. Define the payload and JWS headers
payload = "My Integrity protected message"
jwstoken = jws.JWS(payload.encode('utf-8'))
# 3. Add signature to the token
jwstoken.add_signature(
key,
None,
json_encode({"alg": "HS256"}),
json_encode({"kid": key.thumbprint()})
)
# 4. Serialize the JWS token
signed_token = jwstoken.serialize()
print(f"Signed JWS: {signed_token}")
# 5. Verify the JWS token
verifier_token = jws.JWS()
verifier_token.deserialize(signed_token)
verifier_token.verify(key)
# 6. Access the verified payload
verified_payload = verifier_token.payload.decode('utf-8')
print(f"Verified Payload: {verified_payload}")