From 415c8ea9be4c8e510fb859fe632d1de018da6ca8 Mon Sep 17 00:00:00 2001 From: "lago-claude-ai-agent[bot]" <297187938+lago-claude-ai-agent[bot]@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:31:33 +0000 Subject: [PATCH] feat(webhook-endpoints): Add signing_keys to webhook endpoint responses ## Context Webhook endpoints can now sign payloads with the Standard Webhooks scheme. The v1 API accepts `standard_webhooks` as a `signature_algo` value. It also returns a `signing_keys` list on the endpoint, with the `whsec_...` secrets a consumer needs to verify deliveries. Python client users cannot read these keys today, because the response model drops the field. ## Description `WebhookEndpointResponse` now has an optional `signing_keys` list. Each item is a new `WebhookEndpointSigningKeyResponse` with `lago_id`, `secret_key` and `created_at`. The order of the API is kept. Both response models are exported from `lago_python_client.models`. The field is optional on purpose. An endpoint using `jwt` or `hmac` returns an empty list. A response from a read-only API key, a list call or a destroy call has no `signing_keys`, and the field is then `None`. Existing responses still parse. `signature_algo` was already a free string, so creating or updating an endpoint with `standard_webhooks` needed no code change. A test now checks that the value is sent in the request body. Tests and fixtures cover find, create, update and list. The fixture `tests/fixtures/webhook_endpoint_index_standard_webhooks.json` is outside the planned files. It was added so the list call is tested with both a signing key endpoint and an empty one. ## Agent decisions - No verification helper and no README section. The ticket asks for helpers only in clients that already have one for `jwt` or `hmac`, and this client has none (`webhooks.public_key()` only fetches and decodes the RSA key). A README pointer to the `standardwebhooks` library could still be added later. Confidence: high. - The ticket also asks for v2 webhook endpoints with `expands[]=signing_keys`. This change covers v1 only, because the client sends every request to `api/v1/` and has no v2 support to extend. Confidence: high. - `signing_keys` is `Optional[List[...]]`, since read-only keys get no field. The item fields are required strings. Confidence: high. - `signature_algo` stays a free `Optional[str]` and no enum is added. Confidence: high. The OpenAPI schema and the verification documentation are handled in their own repositories. Fixes BIL-1020 Signed-off-by: lago-claude-ai-agent[bot] <297187938+lago-claude-ai-agent[bot]@users.noreply.github.com> --- lago_python_client/models/__init__.py | 2 + lago_python_client/models/webhook_endpoint.py | 7 + .../webhook_endpoint_empty_signing_keys.json | 12 ++ ...hook_endpoint_index_standard_webhooks.json | 42 +++++ .../webhook_endpoint_standard_webhooks.json | 23 +++ tests/test_webhook_endpoint_client.py | 150 +++++++++++++++++- 6 files changed, 235 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/webhook_endpoint_empty_signing_keys.json create mode 100644 tests/fixtures/webhook_endpoint_index_standard_webhooks.json create mode 100644 tests/fixtures/webhook_endpoint_standard_webhooks.json diff --git a/lago_python_client/models/__init__.py b/lago_python_client/models/__init__.py index bf1c43ac..71d9f75f 100644 --- a/lago_python_client/models/__init__.py +++ b/lago_python_client/models/__init__.py @@ -354,3 +354,5 @@ ) from .wallet_transaction import WalletTransaction as WalletTransaction from .webhook_endpoint import WebhookEndpoint as WebhookEndpoint +from .webhook_endpoint import WebhookEndpointResponse as WebhookEndpointResponse +from .webhook_endpoint import WebhookEndpointSigningKeyResponse as WebhookEndpointSigningKeyResponse diff --git a/lago_python_client/models/webhook_endpoint.py b/lago_python_client/models/webhook_endpoint.py index a948ef8d..cd80823c 100644 --- a/lago_python_client/models/webhook_endpoint.py +++ b/lago_python_client/models/webhook_endpoint.py @@ -12,6 +12,12 @@ class WebhookEndpoint(BaseModel): event_types: Optional[List[str]] +class WebhookEndpointSigningKeyResponse(BaseModel): + lago_id: str + secret_key: str + created_at: str + + class WebhookEndpointResponse(BaseResponseModel): lago_id: str lago_organization_id: str @@ -20,3 +26,4 @@ class WebhookEndpointResponse(BaseResponseModel): name: Optional[str] event_types: Optional[List[str]] created_at: str + signing_keys: Optional[List[WebhookEndpointSigningKeyResponse]] diff --git a/tests/fixtures/webhook_endpoint_empty_signing_keys.json b/tests/fixtures/webhook_endpoint_empty_signing_keys.json new file mode 100644 index 00000000..60be2e43 --- /dev/null +++ b/tests/fixtures/webhook_endpoint_empty_signing_keys.json @@ -0,0 +1,12 @@ +{ + "webhook_endpoint": { + "lago_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "lago_organization_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "webhook_url": "https://foo.bar", + "signature_algo": "hmac", + "name": "My Webhook Endpoint", + "event_types": ["customer.created"], + "created_at": "2022-04-29T08:59:51Z", + "signing_keys": [] + } +} diff --git a/tests/fixtures/webhook_endpoint_index_standard_webhooks.json b/tests/fixtures/webhook_endpoint_index_standard_webhooks.json new file mode 100644 index 00000000..cdad0552 --- /dev/null +++ b/tests/fixtures/webhook_endpoint_index_standard_webhooks.json @@ -0,0 +1,42 @@ +{ + "webhook_endpoints": [ + { + "lago_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "lago_organization_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "webhook_url": "https://foo.bar", + "signature_algo": "standard_webhooks", + "name": "My Webhook Endpoint", + "event_types": ["customer.created"], + "created_at": "2022-04-29T08:59:51Z", + "signing_keys": [ + { + "lago_id": "5f0b0a8e-3c39-4a2f-9a8d-1d0c6b1e2a01", + "secret_key": "whsec_fake_test_secret_1", + "created_at": "2022-04-29T08:59:51Z" + }, + { + "lago_id": "9c4e7d21-6b8f-4e0a-b5d3-2f1a7c9e3b02", + "secret_key": "whsec_fake_test_secret_2", + "created_at": "2022-05-03T10:15:00Z" + } + ] + }, + { + "lago_id": "3d6f1a2b-8c4e-4f7a-9b1d-5e2c7a9f0b13", + "lago_organization_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "webhook_url": "https://bar.foo", + "signature_algo": "hmac", + "name": null, + "event_types": null, + "created_at": "2022-04-29T08:59:51Z", + "signing_keys": [] + } + ], + "meta": { + "current_page": 1, + "next_page": null, + "prev_page": null, + "total_pages": 1, + "total_count": 2 + } +} diff --git a/tests/fixtures/webhook_endpoint_standard_webhooks.json b/tests/fixtures/webhook_endpoint_standard_webhooks.json new file mode 100644 index 00000000..2a9bba93 --- /dev/null +++ b/tests/fixtures/webhook_endpoint_standard_webhooks.json @@ -0,0 +1,23 @@ +{ + "webhook_endpoint": { + "lago_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "lago_organization_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "webhook_url": "https://foo.bar", + "signature_algo": "standard_webhooks", + "name": "My Webhook Endpoint", + "event_types": ["customer.created"], + "created_at": "2022-04-29T08:59:51Z", + "signing_keys": [ + { + "lago_id": "5f0b0a8e-3c39-4a2f-9a8d-1d0c6b1e2a01", + "secret_key": "whsec_fake_test_secret_1", + "created_at": "2022-04-29T08:59:51Z" + }, + { + "lago_id": "9c4e7d21-6b8f-4e0a-b5d3-2f1a7c9e3b02", + "secret_key": "whsec_fake_test_secret_2", + "created_at": "2022-05-03T10:15:00Z" + } + ] + } +} diff --git a/tests/test_webhook_endpoint_client.py b/tests/test_webhook_endpoint_client.py index 7e74af7f..d2bb06f1 100644 --- a/tests/test_webhook_endpoint_client.py +++ b/tests/test_webhook_endpoint_client.py @@ -1,3 +1,4 @@ +import json import os import pytest @@ -5,7 +6,7 @@ from lago_python_client.client import Client from lago_python_client.exceptions import LagoApiError -from lago_python_client.models import WebhookEndpoint +from lago_python_client.models import WebhookEndpoint, WebhookEndpointSigningKeyResponse def webhook_endpoint_object(): @@ -25,6 +26,33 @@ def mock_response(): return webhook_endpoint_response.read() +def mock_standard_webhooks_response(): + this_dir = os.path.dirname(os.path.abspath(__file__)) + data_path = os.path.join(this_dir, "fixtures/webhook_endpoint_standard_webhooks.json") + + with open(data_path, "rb") as webhook_endpoint_response: + return webhook_endpoint_response.read() + + +def mock_empty_signing_keys_response(): + this_dir = os.path.dirname(os.path.abspath(__file__)) + data_path = os.path.join(this_dir, "fixtures/webhook_endpoint_empty_signing_keys.json") + + with open(data_path, "rb") as webhook_endpoint_response: + return webhook_endpoint_response.read() + + +def assert_standard_webhooks_signing_keys(signing_keys): + assert len(signing_keys) == 2 + assert all(isinstance(key, WebhookEndpointSigningKeyResponse) for key in signing_keys) + assert signing_keys[0].lago_id == "5f0b0a8e-3c39-4a2f-9a8d-1d0c6b1e2a01" + assert signing_keys[0].secret_key == "whsec_fake_test_secret_1" + assert signing_keys[0].created_at == "2022-04-29T08:59:51Z" + assert signing_keys[1].lago_id == "9c4e7d21-6b8f-4e0a-b5d3-2f1a7c9e3b02" + assert signing_keys[1].secret_key == "whsec_fake_test_secret_2" + assert signing_keys[1].created_at == "2022-05-03T10:15:00Z" + + def mock_collection_response(): this_dir = os.path.dirname(os.path.abspath(__file__)) data_path = os.path.join(this_dir, "fixtures/webhook_endpoint_index.json") @@ -33,6 +61,14 @@ def mock_collection_response(): return webhook_endpoint_response.read() +def mock_collection_standard_webhooks_response(): + this_dir = os.path.dirname(os.path.abspath(__file__)) + data_path = os.path.join(this_dir, "fixtures/webhook_endpoint_index_standard_webhooks.json") + + with open(data_path, "rb") as webhook_endpoint_response: + return webhook_endpoint_response.read() + + def test_valid_create_webhook_endpoint_request(httpx_mock: HTTPXMock): client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") @@ -173,3 +209,115 @@ def test_invalid_find_all_webhook_endpoint_request(httpx_mock: HTTPXMock): with pytest.raises(LagoApiError): client.webhook_endpoints.find_all() + + +def test_valid_find_webhook_endpoint_request_with_signing_keys(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + arg = "b7ab2926-1de8-4428-9bcd-779314ac129b" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhook_endpoints/" + arg, + content=mock_standard_webhooks_response(), + ) + response = client.webhook_endpoints.find(arg) + + assert response.signature_algo == "standard_webhooks" + assert_standard_webhooks_signing_keys(response.signing_keys) + + +def test_valid_update_webhook_endpoint_request_with_signing_keys(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + arg = "b7ab2926-1de8-4428-9bcd-779314ac129b" + + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/webhook_endpoints/" + arg, + content=mock_standard_webhooks_response(), + ) + response = client.webhook_endpoints.update(webhook_endpoint_object(), arg) + + assert_standard_webhooks_signing_keys(response.signing_keys) + + +def test_valid_create_standard_webhooks_endpoint_request(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/webhook_endpoints", + content=mock_standard_webhooks_response(), + ) + response = client.webhook_endpoints.create( + WebhookEndpoint( + webhook_url="https://foo.bar", + signature_algo="standard_webhooks", + name="My Webhook Endpoint", + event_types=["customer.created"], + ) + ) + + request_body = json.loads(httpx_mock.get_request().content) + assert request_body["webhook_endpoint"]["signature_algo"] == "standard_webhooks" + assert response.signature_algo == "standard_webhooks" + assert_standard_webhooks_signing_keys(response.signing_keys) + + +def test_valid_find_webhook_endpoint_request_with_empty_signing_keys(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + arg = "b7ab2926-1de8-4428-9bcd-779314ac129b" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhook_endpoints/" + arg, + content=mock_empty_signing_keys_response(), + ) + response = client.webhook_endpoints.find(arg) + + assert response.signing_keys == [] + + +def test_valid_find_webhook_endpoint_request_without_signing_keys(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + arg = "b7ab2926-1de8-4428-9bcd-779314ac129b" + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhook_endpoints/" + arg, + content=mock_response(), + ) + response = client.webhook_endpoints.find(arg) + + assert response.signing_keys is None + + +def test_valid_find_all_webhook_endpoint_request_without_signing_keys(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhook_endpoints", + content=mock_collection_response(), + ) + response = client.webhook_endpoints.find_all() + + assert len(response["webhook_endpoints"]) > 0 + assert all(endpoint.signing_keys is None for endpoint in response["webhook_endpoints"]) + + +def test_valid_find_all_webhook_endpoint_request_with_signing_keys(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/webhook_endpoints", + content=mock_collection_standard_webhooks_response(), + ) + response = client.webhook_endpoints.find_all() + + endpoints = response["webhook_endpoints"] + assert len(endpoints) == 2 + assert endpoints[0].signature_algo == "standard_webhooks" + assert_standard_webhooks_signing_keys(endpoints[0].signing_keys) + assert endpoints[1].signing_keys == [] + assert response["meta"]["current_page"] == 1