This Python script is a utility for creating and sending...
August 29, 2025 at 02:47 PM
This Python script is a utility for creating and sending a detached JSON Web Signature (JWS) along with a JSON payload via a POST request to an API endpoint. Here's what the code does:
High-Level Workflow:
-
Input Handling & Argument Parsing:
- It uses
argparse
to parse command-line arguments for:- Private key file (
--key
) - Public certificate file (
--cert
) - JSON payload file (
--payload
) - API URL (
--url
).
- Private key file (
- It uses
-
Payload Processing:
- The provided JSON file is loaded and modified to include a
sendDate
timestamp in ISO 8601 format under therequestHeader
section.
- The provided JSON file is loaded and modified to include a
-
JWS Signature Generation:
- Reads the X.509 certificate and private key.
- Computes a SHA-256 thumbprint of the certificate (base64 URL-encoded).
- Constructs a JWS header containing the signature algorithm (
RS256
) and the certificate thumbprint. - Encodes the payload, signs it with the private key, and generates a detached JWS signature (header and signature without the payload).
- Verifies the generated JWS against the certificate’s public key.
-
API Request Sending:
- Sends the JSON payload (minified) as a POST request to the specified API URL.
- Includes the detached JWS signature (
x-jws-signature
) and other custom headers (x-request-id
andContent-Type
).
-
Response Handling:
- Prints the response status code.
- If the status code is not 204 (No Content), it also prints the response body.
Key Functions Explained:
minify_json(data)
- Converts the input JSON (dictionary or string) into a minified JSON string (removing whitespace, newlines, etc.).
load_file(path, parse_json)
- Reads the content of a file.
- If
parse_json=True
, it parses the file as JSON and returns a Python dictionary.
get_send_date()
- Generates the current UTC time in ISO 8601 format with millisecond precision.
generate_detached_jws(private_key, certificate, payload)
- Generates a detached JWS signature:
- Extracts public key information from the provided certificate.
- Computes the certificate's thumbprint.
- Constructs a JWS header based on
RS256
algorithm. - Signs the JSON payload with the provided private key.
- Verifies the signature using the public key from the certificate.
- Returns the detached JWS (header and signature).
send_request(url, payload, detached_jws, request_id)
- Sends a HTTP POST request containing the minified JSON payload to the given API URL.
- Sets various headers, including:
x-jws-signature
with the detached JWS.x-request-id
for tracing.
- Prints the response details.
Main Flow (main
Function):
- Loads key, certificate, and payload from files.
- Updates the
sendDate
in the payload request header. - Generates a detached JWS for the payload.
- Sends the payload and JWS signature to the specified API endpoint.
Use Case:
This script is useful in scenarios where secure communication and integrity verification are required, such as interacting with an API that enforces digital signatures on payloads. The detached JWS mechanism ensures the payload remains unaltered during transmission.
Generate your own explanations
Download our vscode extension
Read other generated explanations
Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node