JOSE RFCs Implementation

1.6.3 · active · verified Sun Mar 29

joserfc is a Python library that provides a comprehensive implementation of several essential JSON Object Signing and Encryption (JOSE) standards, including JWS, JWE, JWK, JWA, and JWT. It is derived from Authlib, but features a redesigned API specific to JOSE functionality. It strictly follows the latest versions of the JOSE standards, guaranteeing interoperability and compliance. The current version is 1.6.3 and it maintains an active release cadence with regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to encode and decode a JSON Web Token (JWT) using a symmetric key. It also includes an example of explicit claims validation, which is a crucial step for production environments.

import os
from joserfc import jwt, jwk

# For demonstration, use a simple symmetric key. In production, use a secure, generated key.
secret_key = os.environ.get('JOSERFC_SECRET_KEY', 'your-super-secret-key-that-is-at-least-32-chars')

# 1. Import or generate a JWK
# For symmetric keys, use 'oct' (octet) key type
key = jwk.import_key(secret_key, 'oct')

# 2. Define JWT header and claims
header = {"alg": "HS256", "typ": "JWT"}
claims = {"sub": "1234567890", "name": "John Doe", "iat": 1516239022}

# 3. Encode the JWT
encoded_jwt = jwt.encode(header, claims, key)
print(f"Encoded JWT: {encoded_jwt}")

# 4. Decode the JWT
token = jwt.decode(encoded_jwt, key)
print(f"Decoded Header: {token.header}")
print(f"Decoded Claims: {token.claims}")

# 5. Validate claims (important for production)
claims_registry = jwt.JWTClaimsRegistry()
try:
    claims_registry.validate(token.claims, now=1516239022) # 'now' for reproducible example
    print("Claims validated successfully.")
except jwt.InvalidClaimError as e:
    print(f"Claim validation failed: {e}")

view raw JSON →