tessl install tessl/pypi-josepy@2.1.0JOSE protocol implementation in Python with support for JSON Web Algorithms, Keys, and Signatures
Agent Success
Agent success rate when using this tile
73%
Improvement
Agent success rate improvement when using this tile compared to baseline
1.16x
Baseline
Agent success rate without this tile
63%
A small module that signs and verifies payloads using compact JSON Web Signatures across symmetric and asymmetric algorithms.
b"ping" with algorithm "HS256" and secret b"shared-key" produces a compact JWS whose verification with the same secret returns the original payload bytes. @test"RS256" and a provided private key, verification with the matching public key returns the payload bytes and raises ValueError for a token signed with a different RSA key. @test"PS256" token for payload b"hello", changing a character in the signature causes verification to raise ValueError. @testb"data" with "ES256" ensures the protected header contains alg: ES256; verification raises ValueError if the caller requests a different algorithm value when checking the same token. @test@generates
from typing import Mapping, Optional, Union
KeyMaterial = Union[bytes, str]
def sign_compact(payload: bytes, alg: str, key_data: KeyMaterial, protected_headers: Optional[Mapping[str, str]] = None) -> str:
"""
Return a compact JWS string for the payload using the specified algorithm.
key_data is a shared secret for HS* algorithms or a PEM/DER private key for RS*/PS*/ES* algorithms.
Raises ValueError if the algorithm is unsupported or headers conflict with signing requirements.
"""
def verify_compact(token: str, alg: str, key_data: KeyMaterial) -> bytes:
"""
Validate the compact JWS using the provided algorithm and key_data.
key_data is the shared secret for HS* algorithms or a PEM/DER public/private key for asymmetric algorithms.
Returns the payload bytes when validation succeeds; raises ValueError on failure.
"""Provides JWA signing and verification primitives for HS*, RS*, PS*, and ES* algorithms.