Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022-2026 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU Lesser General Public License as
7 : published by the Free Software Foundation; either version 2.1,
8 : or (at your option) any later version.
9 :
10 : TALER is distributed in the hope that it will be useful,
11 : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : GNU Lesser General Public License for more details.
14 :
15 : You should have received a copy of the GNU Lesser General
16 : Public License along with TALER; see the file COPYING.LGPL.
17 : If not, see <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file src/lib/merchant_api_patch-private-webhooks-WEBHOOK_ID.c
21 : * @brief Implementation of the PATCH /private/webhooks/$WEBHOOK_ID request
22 : * @author Christian Grothoff
23 : */
24 : #include "platform.h"
25 : #include <curl/curl.h>
26 : #include <jansson.h>
27 : #include <microhttpd.h> /* just for HTTP status codes */
28 : #include <gnunet/gnunet_util_lib.h>
29 : #include <gnunet/gnunet_curl_lib.h>
30 : #include <taler/merchant/patch-private-webhooks-WEBHOOK_ID.h>
31 : #include "merchant_api_curl_defaults.h"
32 : #include "merchant_api_common.h"
33 : #include <taler/taler_json_lib.h>
34 : #include <taler/taler_curl_lib.h>
35 :
36 :
37 : /**
38 : * Handle for a PATCH /private/webhooks/$WEBHOOK_ID operation.
39 : */
40 : struct TALER_MERCHANT_PatchPrivateWebhookHandle
41 : {
42 : /**
43 : * Base URL of the merchant backend.
44 : */
45 : char *base_url;
46 :
47 : /**
48 : * The full URL for this request.
49 : */
50 : char *url;
51 :
52 : /**
53 : * Handle for the request.
54 : */
55 : struct GNUNET_CURL_Job *job;
56 :
57 : /**
58 : * Function to call with the result.
59 : */
60 : TALER_MERCHANT_PatchPrivateWebhookCallback cb;
61 :
62 : /**
63 : * Closure for @a cb.
64 : */
65 : TALER_MERCHANT_PATCH_PRIVATE_WEBHOOK_RESULT_CLOSURE *cb_cls;
66 :
67 : /**
68 : * Reference to the execution context.
69 : */
70 : struct GNUNET_CURL_Context *ctx;
71 :
72 : /**
73 : * Minor context that holds body and headers.
74 : */
75 : struct TALER_CURL_PostContext post_ctx;
76 :
77 : /**
78 : * Identifier of the webhook to update.
79 : */
80 : char *webhook_id;
81 :
82 : /**
83 : * New event type.
84 : */
85 : char *event_type;
86 :
87 : /**
88 : * New notification URL template.
89 : */
90 : char *url_template;
91 :
92 : /**
93 : * New HTTP method.
94 : */
95 : char *http_method;
96 :
97 : /**
98 : * Optional new header template.
99 : */
100 : char *header_template;
101 :
102 : /**
103 : * Optional new body template.
104 : */
105 : char *body_template;
106 : };
107 :
108 :
109 : /**
110 : * Function called when we're done processing the
111 : * HTTP PATCH /private/webhooks/$WEBHOOK_ID request.
112 : *
113 : * @param cls the `struct TALER_MERCHANT_PatchPrivateWebhookHandle`
114 : * @param response_code HTTP response code, 0 on error
115 : * @param response response body, NULL if not in JSON
116 : */
117 : static void
118 4 : handle_patch_webhook_finished (void *cls,
119 : long response_code,
120 : const void *response)
121 : {
122 4 : struct TALER_MERCHANT_PatchPrivateWebhookHandle *wph = cls;
123 4 : const json_t *json = response;
124 4 : struct TALER_MERCHANT_PatchPrivateWebhookResponse wpr = {
125 4 : .hr.http_status = (unsigned int) response_code,
126 : .hr.reply = json
127 : };
128 :
129 4 : wph->job = NULL;
130 4 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
131 : "PATCH /private/webhooks/$WEBHOOK_ID completed with response code %u\n",
132 : (unsigned int) response_code);
133 4 : switch (response_code)
134 : {
135 0 : case 0:
136 0 : wpr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
137 0 : break;
138 2 : case MHD_HTTP_NO_CONTENT:
139 2 : break;
140 0 : case MHD_HTTP_BAD_REQUEST:
141 0 : wpr.hr.ec = TALER_JSON_get_error_code (json);
142 0 : wpr.hr.hint = TALER_JSON_get_error_hint (json);
143 0 : GNUNET_break_op (0);
144 : /* This should never happen, either us
145 : * or the merchant is buggy (or API version conflict);
146 : * just pass JSON reply to the application */
147 0 : break;
148 0 : case MHD_HTTP_UNAUTHORIZED:
149 0 : wpr.hr.ec = TALER_JSON_get_error_code (json);
150 0 : wpr.hr.hint = TALER_JSON_get_error_hint (json);
151 : /* Nothing really to verify, merchant says we need to authenticate. */
152 0 : break;
153 0 : case MHD_HTTP_FORBIDDEN:
154 0 : wpr.hr.ec = TALER_JSON_get_error_code (json);
155 0 : wpr.hr.hint = TALER_JSON_get_error_hint (json);
156 0 : break;
157 2 : case MHD_HTTP_NOT_FOUND:
158 2 : wpr.hr.ec = TALER_JSON_get_error_code (json);
159 2 : wpr.hr.hint = TALER_JSON_get_error_hint (json);
160 2 : break;
161 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
162 0 : wpr.hr.ec = TALER_JSON_get_error_code (json);
163 0 : wpr.hr.hint = TALER_JSON_get_error_hint (json);
164 : /* Server had an internal issue; we should retry,
165 : but this API leaves this to the application */
166 0 : break;
167 0 : default:
168 0 : TALER_MERCHANT_parse_error_details_ (json,
169 : response_code,
170 : &wpr.hr);
171 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172 : "Unexpected response code %u/%d\n",
173 : (unsigned int) response_code,
174 : (int) wpr.hr.ec);
175 0 : GNUNET_break_op (0);
176 0 : break;
177 : }
178 4 : wph->cb (wph->cb_cls,
179 : &wpr);
180 4 : TALER_MERCHANT_patch_private_webhook_cancel (wph);
181 4 : }
182 :
183 :
184 : struct TALER_MERCHANT_PatchPrivateWebhookHandle *
185 4 : TALER_MERCHANT_patch_private_webhook_create (
186 : struct GNUNET_CURL_Context *ctx,
187 : const char *url,
188 : const char *webhook_id,
189 : const char *event_type,
190 : const char *url_template,
191 : const char *http_method)
192 : {
193 : struct TALER_MERCHANT_PatchPrivateWebhookHandle *wph;
194 :
195 4 : wph = GNUNET_new (struct TALER_MERCHANT_PatchPrivateWebhookHandle);
196 4 : wph->ctx = ctx;
197 4 : wph->base_url = GNUNET_strdup (url);
198 4 : wph->webhook_id = GNUNET_strdup (webhook_id);
199 4 : wph->event_type = GNUNET_strdup (event_type);
200 4 : wph->url_template = GNUNET_strdup (url_template);
201 4 : wph->http_method = GNUNET_strdup (http_method);
202 4 : return wph;
203 : }
204 :
205 :
206 : enum GNUNET_GenericReturnValue
207 8 : TALER_MERCHANT_patch_private_webhook_set_options_ (
208 : struct TALER_MERCHANT_PatchPrivateWebhookHandle *wph,
209 : unsigned int num_options,
210 : const struct TALER_MERCHANT_PatchPrivateWebhookOptionValue *options)
211 : {
212 16 : for (unsigned int i = 0; i < num_options; i++)
213 : {
214 16 : switch (options[i].option)
215 : {
216 8 : case TALER_MERCHANT_PATCH_PRIVATE_WEBHOOK_OPTION_END:
217 8 : return GNUNET_OK;
218 4 : case TALER_MERCHANT_PATCH_PRIVATE_WEBHOOK_OPTION_HEADER_TEMPLATE:
219 4 : GNUNET_free (wph->header_template);
220 4 : wph->header_template = GNUNET_strdup (options[i].details.header_template);
221 4 : break;
222 4 : case TALER_MERCHANT_PATCH_PRIVATE_WEBHOOK_OPTION_BODY_TEMPLATE:
223 4 : GNUNET_free (wph->body_template);
224 4 : wph->body_template = GNUNET_strdup (options[i].details.body_template);
225 4 : break;
226 0 : default:
227 0 : GNUNET_break (0);
228 0 : return GNUNET_SYSERR;
229 : }
230 : }
231 0 : return GNUNET_OK;
232 : }
233 :
234 :
235 : enum TALER_ErrorCode
236 4 : TALER_MERCHANT_patch_private_webhook_start (
237 : struct TALER_MERCHANT_PatchPrivateWebhookHandle *wph,
238 : TALER_MERCHANT_PatchPrivateWebhookCallback cb,
239 : TALER_MERCHANT_PATCH_PRIVATE_WEBHOOK_RESULT_CLOSURE *cb_cls)
240 : {
241 : json_t *req_obj;
242 : CURL *eh;
243 :
244 4 : wph->cb = cb;
245 4 : wph->cb_cls = cb_cls;
246 : {
247 : char *path;
248 :
249 4 : GNUNET_asprintf (&path,
250 : "private/webhooks/%s",
251 : wph->webhook_id);
252 4 : wph->url = TALER_url_join (wph->base_url,
253 : path,
254 : NULL);
255 4 : GNUNET_free (path);
256 : }
257 4 : if (NULL == wph->url)
258 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
259 4 : req_obj = GNUNET_JSON_PACK (
260 : GNUNET_JSON_pack_string ("event_type",
261 : wph->event_type),
262 : GNUNET_JSON_pack_string ("url",
263 : wph->url_template),
264 : GNUNET_JSON_pack_string ("http_method",
265 : wph->http_method),
266 : GNUNET_JSON_pack_allow_null (
267 : GNUNET_JSON_pack_string ("header_template",
268 : wph->header_template)),
269 : GNUNET_JSON_pack_allow_null (
270 : GNUNET_JSON_pack_string ("body_template",
271 : wph->body_template)));
272 4 : eh = TALER_MERCHANT_curl_easy_get_ (wph->url);
273 8 : if ( (NULL == eh) ||
274 : (GNUNET_OK !=
275 4 : TALER_curl_easy_post (&wph->post_ctx,
276 : eh,
277 : req_obj)) )
278 : {
279 0 : GNUNET_break (0);
280 0 : json_decref (req_obj);
281 0 : if (NULL != eh)
282 0 : curl_easy_cleanup (eh);
283 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
284 : }
285 4 : json_decref (req_obj);
286 4 : GNUNET_assert (CURLE_OK ==
287 : curl_easy_setopt (eh,
288 : CURLOPT_CUSTOMREQUEST,
289 : MHD_HTTP_METHOD_PATCH));
290 8 : wph->job = GNUNET_CURL_job_add2 (wph->ctx,
291 : eh,
292 4 : wph->post_ctx.headers,
293 : &handle_patch_webhook_finished,
294 : wph);
295 4 : if (NULL == wph->job)
296 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
297 4 : return TALER_EC_NONE;
298 : }
299 :
300 :
301 : void
302 4 : TALER_MERCHANT_patch_private_webhook_cancel (
303 : struct TALER_MERCHANT_PatchPrivateWebhookHandle *wph)
304 : {
305 4 : if (NULL != wph->job)
306 : {
307 0 : GNUNET_CURL_job_cancel (wph->job);
308 0 : wph->job = NULL;
309 : }
310 4 : TALER_curl_easy_post_finished (&wph->post_ctx);
311 4 : GNUNET_free (wph->url);
312 4 : GNUNET_free (wph->header_template);
313 4 : GNUNET_free (wph->body_template);
314 4 : GNUNET_free (wph->base_url);
315 4 : GNUNET_free (wph->webhook_id);
316 4 : GNUNET_free (wph->event_type);
317 4 : GNUNET_free (wph->url_template);
318 4 : GNUNET_free (wph->http_method);
319 4 : GNUNET_free (wph);
320 4 : }
321 :
322 :
323 : /* end of merchant_api_patch-private-webhooks-WEBHOOK_ID.c */
|