JWSkate (JSON Web Crypto for Python)

0.12.2 · active · verified Mon Apr 13

JWSkate is a Pythonic implementation of the JOSE (JSON Object Signing and Encryption) / JSON Web Crypto related RFCs, including JWS, JWK, JWA, JWT, and JWE. It simplifies cryptographic operations by providing a consistent API built on top of the `cryptography` library. The current version is 0.12.2, with an active release cadence, typically seeing several updates per year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate an RSA key pair, sign a JSON Web Token (JWT) with the private key, and then verify its signature using the corresponding public key. It highlights the importance of specifying the expected algorithm during verification.

from jwskate import Jwk, Jwt, InvalidSignature

# 1. Generate a private RSA key for signing
private_jwk = Jwk.generate(alg="RS256", key_size=2048, kid="my-rsa-key")
public_jwk = private_jwk.public_jwk()

# 2. Define claims for the JWT
claims = {"sub": "user123", "name": "John Doe", "iat": 1678886400, "exp": 1678890000}

# 3. Sign the JWT
try:
    signed_jwt = Jwt.sign(claims, private_jwk)
    print(f"Signed JWT: {signed_jwt.compact()}")
except Exception as e:
    print(f"Error signing JWT: {e}")
    exit(1)

# 4. Verify the JWT signature (using the public key)
try:
    # The verify_signature method requires the expected algorithm for security
    if signed_jwt.verify_signature(public_jwk, alg="RS256"):
        print("JWT signature is valid!")
        print(f"Decoded claims: {signed_jwt.claims}")
        assert signed_jwt.claims == claims
    else:
        print("JWT signature verification failed.")
except InvalidSignature:
    print("Invalid signature detected!")
except Exception as e:
    print(f"Error verifying JWT: {e}")

view raw JSON →