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 it under the
6 : terms of the GNU Lesser General Public License as published by the Free Software
7 : Foundation; either version 2.1, or (at your option) any later version.
8 :
9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 : A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
12 :
13 : You should have received a copy of the GNU Lesser General Public License along with
14 : TALER; see the file COPYING.LGPL. If not, see
15 : <http://www.gnu.org/licenses/>
16 : */
17 : /**
18 : * @file merchant_api_get-private-webhooks-new.c
19 : * @brief Implementation of the GET /private/webhooks request
20 : * @author Christian Grothoff
21 : */
22 : #include "taler/platform.h"
23 : #include <curl/curl.h>
24 : #include <jansson.h>
25 : #include <microhttpd.h> /* just for HTTP status codes */
26 : #include <gnunet/gnunet_util_lib.h>
27 : #include <gnunet/gnunet_curl_lib.h>
28 : #include <taler/merchant/get-private-webhooks.h>
29 : #include "merchant_api_curl_defaults.h"
30 : #include <taler/taler_json_lib.h>
31 :
32 :
33 : /**
34 : * Maximum number of webhooks permitted.
35 : */
36 : #define MAX_WEBHOOKS 1024
37 :
38 :
39 : /**
40 : * Handle for a GET /private/webhooks operation.
41 : */
42 : struct TALER_MERCHANT_GetPrivateWebhooksHandle
43 : {
44 : /**
45 : * Base URL of the merchant backend.
46 : */
47 : char *base_url;
48 :
49 : /**
50 : * The full URL for this request.
51 : */
52 : char *url;
53 :
54 : /**
55 : * Handle for the request.
56 : */
57 : struct GNUNET_CURL_Job *job;
58 :
59 : /**
60 : * Function to call with the result.
61 : */
62 : TALER_MERCHANT_GetPrivateWebhooksCallback cb;
63 :
64 : /**
65 : * Closure for @a cb.
66 : */
67 : TALER_MERCHANT_GET_PRIVATE_WEBHOOKS_RESULT_CLOSURE *cb_cls;
68 :
69 : /**
70 : * Reference to the execution context.
71 : */
72 : struct GNUNET_CURL_Context *ctx;
73 : };
74 :
75 :
76 : /**
77 : * Parse webhook information from @a ia.
78 : *
79 : * @param ia JSON array (or NULL!) with webhook data
80 : * @param[in] wgr partially filled webhook response
81 : * @param gpwh operation handle
82 : * @return #GNUNET_OK on success
83 : */
84 : static enum GNUNET_GenericReturnValue
85 0 : parse_webhooks (const json_t *ia,
86 : struct TALER_MERCHANT_GetPrivateWebhooksResponse *wgr,
87 : struct TALER_MERCHANT_GetPrivateWebhooksHandle *gpwh)
88 : {
89 0 : unsigned int whook_len = (unsigned int) json_array_size (ia);
90 :
91 0 : if ( (json_array_size (ia) != (size_t) whook_len) ||
92 : (whook_len > MAX_WEBHOOKS) )
93 : {
94 0 : GNUNET_break (0);
95 0 : return GNUNET_SYSERR;
96 : }
97 0 : {
98 0 : struct TALER_MERCHANT_GetPrivateWebhooksWebhookEntry whook[
99 0 : GNUNET_NZL (whook_len)];
100 : size_t index;
101 : json_t *value;
102 :
103 0 : json_array_foreach (ia, index, value) {
104 0 : struct TALER_MERCHANT_GetPrivateWebhooksWebhookEntry *ie =
105 : &whook[index];
106 : struct GNUNET_JSON_Specification spec[] = {
107 0 : GNUNET_JSON_spec_string ("webhook_id",
108 : &ie->webhook_id),
109 0 : GNUNET_JSON_spec_string ("event_type",
110 : &ie->event_type),
111 0 : GNUNET_JSON_spec_end ()
112 : };
113 :
114 0 : if (GNUNET_OK !=
115 0 : GNUNET_JSON_parse (value,
116 : spec,
117 : NULL, NULL))
118 : {
119 0 : GNUNET_break_op (0);
120 0 : return GNUNET_SYSERR;
121 : }
122 : }
123 0 : wgr->details.ok.webhooks_length = whook_len;
124 0 : wgr->details.ok.webhooks = whook;
125 0 : gpwh->cb (gpwh->cb_cls,
126 : wgr);
127 0 : gpwh->cb = NULL;
128 : }
129 0 : return GNUNET_OK;
130 : }
131 :
132 :
133 : /**
134 : * Function called when we're done processing the
135 : * HTTP GET /private/webhooks request.
136 : *
137 : * @param cls the `struct TALER_MERCHANT_GetPrivateWebhooksHandle`
138 : * @param response_code HTTP response code, 0 on error
139 : * @param response response body, NULL if not in JSON
140 : */
141 : static void
142 0 : handle_get_webhooks_finished (void *cls,
143 : long response_code,
144 : const void *response)
145 : {
146 0 : struct TALER_MERCHANT_GetPrivateWebhooksHandle *gpwh = cls;
147 0 : const json_t *json = response;
148 0 : struct TALER_MERCHANT_GetPrivateWebhooksResponse wgr = {
149 0 : .hr.http_status = (unsigned int) response_code,
150 : .hr.reply = json
151 : };
152 :
153 0 : gpwh->job = NULL;
154 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 : "Got /private/webhooks response with status code %u\n",
156 : (unsigned int) response_code);
157 0 : switch (response_code)
158 : {
159 0 : case MHD_HTTP_OK:
160 : {
161 : const json_t *webhooks;
162 : struct GNUNET_JSON_Specification spec[] = {
163 0 : GNUNET_JSON_spec_array_const ("webhooks",
164 : &webhooks),
165 0 : GNUNET_JSON_spec_end ()
166 : };
167 :
168 0 : if (GNUNET_OK !=
169 0 : GNUNET_JSON_parse (json,
170 : spec,
171 : NULL, NULL))
172 : {
173 0 : wgr.hr.http_status = 0;
174 0 : wgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
175 0 : break;
176 : }
177 0 : if (GNUNET_OK ==
178 0 : parse_webhooks (webhooks,
179 : &wgr,
180 : gpwh))
181 : {
182 0 : TALER_MERCHANT_get_private_webhooks_cancel (gpwh);
183 0 : return;
184 : }
185 0 : wgr.hr.http_status = 0;
186 0 : wgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
187 0 : break;
188 : }
189 0 : case MHD_HTTP_UNAUTHORIZED:
190 0 : wgr.hr.ec = TALER_JSON_get_error_code (json);
191 0 : wgr.hr.hint = TALER_JSON_get_error_hint (json);
192 : /* Nothing really to verify, merchant says we need to authenticate. */
193 0 : break;
194 0 : default:
195 : /* unexpected response code */
196 0 : wgr.hr.ec = TALER_JSON_get_error_code (json);
197 0 : wgr.hr.hint = TALER_JSON_get_error_hint (json);
198 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
199 : "Unexpected response code %u/%d\n",
200 : (unsigned int) response_code,
201 : (int) wgr.hr.ec);
202 0 : break;
203 : }
204 0 : gpwh->cb (gpwh->cb_cls,
205 : &wgr);
206 0 : TALER_MERCHANT_get_private_webhooks_cancel (gpwh);
207 : }
208 :
209 :
210 : struct TALER_MERCHANT_GetPrivateWebhooksHandle *
211 0 : TALER_MERCHANT_get_private_webhooks_create (
212 : struct GNUNET_CURL_Context *ctx,
213 : const char *url)
214 : {
215 : struct TALER_MERCHANT_GetPrivateWebhooksHandle *gpwh;
216 :
217 0 : gpwh = GNUNET_new (struct TALER_MERCHANT_GetPrivateWebhooksHandle);
218 0 : gpwh->ctx = ctx;
219 0 : gpwh->base_url = GNUNET_strdup (url);
220 0 : return gpwh;
221 : }
222 :
223 :
224 : enum TALER_ErrorCode
225 0 : TALER_MERCHANT_get_private_webhooks_start (
226 : struct TALER_MERCHANT_GetPrivateWebhooksHandle *gpwh,
227 : TALER_MERCHANT_GetPrivateWebhooksCallback cb,
228 : TALER_MERCHANT_GET_PRIVATE_WEBHOOKS_RESULT_CLOSURE *cb_cls)
229 : {
230 : CURL *eh;
231 :
232 0 : gpwh->cb = cb;
233 0 : gpwh->cb_cls = cb_cls;
234 0 : gpwh->url = TALER_url_join (gpwh->base_url,
235 : "private/webhooks",
236 : NULL);
237 0 : if (NULL == gpwh->url)
238 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
239 0 : eh = TALER_MERCHANT_curl_easy_get_ (gpwh->url);
240 0 : if (NULL == eh)
241 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
242 0 : gpwh->job = GNUNET_CURL_job_add (gpwh->ctx,
243 : eh,
244 : &handle_get_webhooks_finished,
245 : gpwh);
246 0 : if (NULL == gpwh->job)
247 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
248 0 : return TALER_EC_NONE;
249 : }
250 :
251 :
252 : void
253 0 : TALER_MERCHANT_get_private_webhooks_cancel (
254 : struct TALER_MERCHANT_GetPrivateWebhooksHandle *gpwh)
255 : {
256 0 : if (NULL != gpwh->job)
257 : {
258 0 : GNUNET_CURL_job_cancel (gpwh->job);
259 0 : gpwh->job = NULL;
260 : }
261 0 : GNUNET_free (gpwh->url);
262 0 : GNUNET_free (gpwh->base_url);
263 0 : GNUNET_free (gpwh);
264 0 : }
265 :
266 :
267 : /* end of merchant_api_get-private-webhooks-new.c */
|