Typing stubs for jwcrypto (types-jwcrypto)

1.5.7.20260409 · active · verified Fri Apr 10

types-jwcrypto provides static type annotations for the jwcrypto library, enabling type checkers like Mypy and Pyright to validate code that uses jwcrypto. jwcrypto is a Python implementation of the JOSE (Javascript Object Signing and Encryption) Web Standards, including JWK, JWS, JWE, and JWT, and leverages the Cryptography package for its cryptographic operations. This stub package is part of the typeshed project and is released automatically, often daily. The current version is 1.5.7.20260409, targeting jwcrypto==1.5.*.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate a symmetric key, create claims, sign a JSON Web Token (JWT) using that key, serialize it into its compact form, and then deserialize and validate it. For asymmetric keys (RSA, EC), the key generation and signing steps would differ slightly, requiring separate public/private key components.

from jwcrypto import jwk, jwt
import json

# 1. Generate a symmetric key
key = jwk.JWK.generate(kty='oct', size=256)

# 2. Define claims (payload)
claims = {
    'iss': 'my-app',
    'aud': 'your-service',
    'sub': 'user123',
    'exp': 1678886400  # Example expiration time (Unix timestamp)
}

# 3. Create a JWT object with header and claims
token = jwt.JWT(header={'alg': 'HS256'}, claims=claims)

# 4. Sign the token
token.make_signed_token(key)

# 5. Serialize the token to a compact string
compact_token = token.serialize()
print(f"Generated JWT: {compact_token}")

# 6. Deserialize and validate the token
# In a real application, you would receive 'compact_token' from a client
# and validate it with a trusted key.

decoded_token = jwt.JWT(jwt=compact_token, key=key)
# No explicit validate() call needed if key is provided during instantiation
# However, it's good practice to call it if you deserialize without a key first.
# decoded_token.validate(key) # This can be used if `jwt` was created without `key`

print(f"Decoded claims: {json.dumps(json.loads(decoded_token.claims), indent=2)}")

view raw JSON →