Python JOSE (Demonware)

1.0.0 · abandoned · verified Thu Apr 16

The `jose` library by Demonware is an older Python implementation of the JSON Object Signing and Encryption (JOSE) framework, supporting JSON Web Signatures (JWS) and JSON Web Encryptions (JWE). Last released in 2015 with version 1.0.0, it was primarily developed for Python 2 and relies on the unmaintained `pycrypto` library. Due to its inactivity and dependency on outdated cryptographic components, it is not recommended for new projects or secure applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic JWS signing/verification and JWE encryption/decryption using the `jose` library (Demonware). It illustrates how to define headers, use RSA keys (generated for demonstration), and call the core `sign`, `verify`, `encrypt`, and `decrypt` functions. Note the explicit requirement for `pycrypto` and the caveats regarding its suitability for modern, secure applications.

import jose
from time import time
# NOTE: For modern Python and security, consider `pip install python-jose[cryptography]` instead.
# This example is for the *Demonware jose* library, which uses the deprecated `pycrypto`.
# Ensure `pycrypto` is installed (e.g., `pip install pycrypto==2.6.1`)
# For generating RSA keys safely, a more robust library like `cryptography` or `PyCryptodome`
# would be used in a real application, not the raw `Crypto.PublicKey.RSA` as it relies on pycrypto.

try:
    from Crypto.PublicKey import RSA
except ImportError:
    print("Error: `pycrypto` not found. Please install it (pip install pycrypto==2.6.1).")
    # Exit or handle error gracefully in a real script
    exit(1)

# Generate a new RSA key pair for demonstration purposes
# In a real app, you would load pre-existing keys securely.
key = RSA.generate(2048) # Generates an RSA key with 2048 bits

claims = {
    'iss': 'http://www.example.com',
    'exp': int(time() + 3600), # Expiration time (1 hour from now)
    'aud': 'http://www.example.org',
    'sub': 'testuser',
    'nbf': int(time()) # Not Before time
}

# --- JWS (JSON Web Signature) Example ---
# Define JWS Protected Header
protected_jws_header = {
    'alg': 'RS256' # RSA Signature with SHA-256
}

try:
    # Sign the claims with the private key
    signed_jws = jose.sign(protected_jws_header, claims, key)
    print("Signed JWS:", signed_jws)

    # Verify the JWS with the public key
    verified_claims = jose.verify(signed_jws, key.publickey())
    print("Verified JWS Claims:", verified_claims)

    # --- JWE (JSON Web Encryption) Example ---
    # Define JWE Protected Header
    protected_jwe_header = {
        'alg': 'RSA-OAEP',       # Algorithm for Content Encryption Key (CEK) encryption
        'enc': 'A128CBC-HS256' # Algorithm for content encryption
    }

    # Encrypt the claims with the recipient's public key
    encrypted_jwe = jose.encrypt(protected_jwe_header, claims, key.publickey())
    print("Encrypted JWE:", encrypted_jwe)

    # Decrypt the JWE with the recipient's private key
    decrypted_jwe_payload = jose.decrypt(encrypted_jwe, key)
    print("Decrypted JWE Payload:", decrypted_jwe_payload)

except jose.Error as e:
    print(f"JOSE Error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →