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_post-private-templates.c
21 : * @brief Implementation of the POST /private/templates 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/post-private-templates.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 POST /private/templates operation.
39 : */
40 : struct TALER_MERCHANT_PostPrivateTemplatesHandle
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_PostPrivateTemplatesCallback cb;
61 :
62 : /**
63 : * Closure for @a cb.
64 : */
65 : TALER_MERCHANT_POST_PRIVATE_TEMPLATES_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 : * Template identifier.
79 : */
80 : char *template_id;
81 :
82 : /**
83 : * Human-readable description.
84 : */
85 : char *template_description;
86 :
87 : /**
88 : * Template contract (JSON).
89 : */
90 : json_t *template_contract;
91 :
92 : /**
93 : * Optional OTP device ID.
94 : */
95 : char *otp_id;
96 :
97 : /**
98 : * Optional editable defaults (JSON object).
99 : */
100 : json_t *editable_defaults;
101 : };
102 :
103 :
104 : /**
105 : * Function called when we're done processing the
106 : * HTTP POST /private/templates request.
107 : *
108 : * @param cls the `struct TALER_MERCHANT_PostPrivateTemplatesHandle`
109 : * @param response_code HTTP response code, 0 on error
110 : * @param response response body, NULL if not in JSON
111 : */
112 : static void
113 26 : handle_post_templates_finished (void *cls,
114 : long response_code,
115 : const void *response)
116 : {
117 26 : struct TALER_MERCHANT_PostPrivateTemplatesHandle *ppth = cls;
118 26 : const json_t *json = response;
119 26 : struct TALER_MERCHANT_PostPrivateTemplatesResponse ptr = {
120 26 : .hr.http_status = (unsigned int) response_code,
121 : .hr.reply = json
122 : };
123 :
124 26 : ppth->job = NULL;
125 26 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
126 : "POST /private/templates completed with response code %u\n",
127 : (unsigned int) response_code);
128 26 : switch (response_code)
129 : {
130 0 : case 0:
131 0 : ptr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
132 0 : break;
133 22 : case MHD_HTTP_NO_CONTENT:
134 22 : break;
135 2 : case MHD_HTTP_BAD_REQUEST:
136 2 : GNUNET_break_op (0);
137 2 : ptr.hr.ec = TALER_JSON_get_error_code (json);
138 2 : ptr.hr.hint = TALER_JSON_get_error_hint (json);
139 2 : break;
140 0 : case MHD_HTTP_UNAUTHORIZED:
141 0 : ptr.hr.ec = TALER_JSON_get_error_code (json);
142 0 : ptr.hr.hint = TALER_JSON_get_error_hint (json);
143 0 : break;
144 0 : case MHD_HTTP_FORBIDDEN:
145 0 : ptr.hr.ec = TALER_JSON_get_error_code (json);
146 0 : ptr.hr.hint = TALER_JSON_get_error_hint (json);
147 0 : break;
148 0 : case MHD_HTTP_NOT_FOUND:
149 0 : ptr.hr.ec = TALER_JSON_get_error_code (json);
150 0 : ptr.hr.hint = TALER_JSON_get_error_hint (json);
151 0 : break;
152 2 : case MHD_HTTP_CONFLICT:
153 2 : ptr.hr.ec = TALER_JSON_get_error_code (json);
154 2 : ptr.hr.hint = TALER_JSON_get_error_hint (json);
155 2 : break;
156 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
157 0 : ptr.hr.ec = TALER_JSON_get_error_code (json);
158 0 : ptr.hr.hint = TALER_JSON_get_error_hint (json);
159 0 : break;
160 0 : default:
161 0 : TALER_MERCHANT_parse_error_details_ (json,
162 : response_code,
163 : &ptr.hr);
164 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
165 : "Unexpected response code %u/%d\n",
166 : (unsigned int) response_code,
167 : (int) ptr.hr.ec);
168 0 : GNUNET_break_op (0);
169 0 : break;
170 : }
171 26 : ppth->cb (ppth->cb_cls,
172 : &ptr);
173 26 : TALER_MERCHANT_post_private_templates_cancel (ppth);
174 26 : }
175 :
176 :
177 : struct TALER_MERCHANT_PostPrivateTemplatesHandle *
178 26 : TALER_MERCHANT_post_private_templates_create (
179 : struct GNUNET_CURL_Context *ctx,
180 : const char *url,
181 : const char *template_id,
182 : const char *template_description,
183 : const json_t *template_contract)
184 : {
185 : struct TALER_MERCHANT_PostPrivateTemplatesHandle *ppth;
186 :
187 26 : ppth = GNUNET_new (struct TALER_MERCHANT_PostPrivateTemplatesHandle);
188 26 : ppth->ctx = ctx;
189 26 : ppth->base_url = GNUNET_strdup (url);
190 26 : ppth->template_id = GNUNET_strdup (template_id);
191 26 : ppth->template_description = GNUNET_strdup (template_description);
192 26 : ppth->template_contract = json_incref ((json_t *) template_contract);
193 26 : return ppth;
194 : }
195 :
196 :
197 : enum GNUNET_GenericReturnValue
198 2 : TALER_MERCHANT_post_private_templates_set_options_ (
199 : struct TALER_MERCHANT_PostPrivateTemplatesHandle *ppth,
200 : unsigned int num_options,
201 : const struct TALER_MERCHANT_PostPrivateTemplatesOptionValue *options)
202 : {
203 4 : for (unsigned int i = 0; i < num_options; i++)
204 : {
205 4 : switch (options[i].option)
206 : {
207 2 : case TALER_MERCHANT_POST_PRIVATE_TEMPLATES_OPTION_END:
208 2 : return GNUNET_OK;
209 2 : case TALER_MERCHANT_POST_PRIVATE_TEMPLATES_OPTION_OTP_ID:
210 2 : GNUNET_free (ppth->otp_id);
211 2 : ppth->otp_id = GNUNET_strdup (options[i].details.otp_id);
212 2 : break;
213 0 : case TALER_MERCHANT_POST_PRIVATE_TEMPLATES_OPTION_EDITABLE_DEFAULTS:
214 0 : json_decref (ppth->editable_defaults);
215 : ppth->editable_defaults
216 0 : = json_incref ((json_t *) options[i].details.editable_defaults);
217 0 : break;
218 0 : default:
219 0 : GNUNET_break (0);
220 0 : return GNUNET_SYSERR;
221 : }
222 : }
223 0 : return GNUNET_OK;
224 : }
225 :
226 :
227 : enum TALER_ErrorCode
228 26 : TALER_MERCHANT_post_private_templates_start (
229 : struct TALER_MERCHANT_PostPrivateTemplatesHandle *ppth,
230 : TALER_MERCHANT_PostPrivateTemplatesCallback cb,
231 : TALER_MERCHANT_POST_PRIVATE_TEMPLATES_RESULT_CLOSURE *cb_cls)
232 : {
233 : json_t *req_obj;
234 : CURL *eh;
235 :
236 26 : ppth->cb = cb;
237 26 : ppth->cb_cls = cb_cls;
238 26 : ppth->url = TALER_url_join (ppth->base_url,
239 : "private/templates",
240 : NULL);
241 26 : if (NULL == ppth->url)
242 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
243 26 : req_obj = GNUNET_JSON_PACK (
244 : GNUNET_JSON_pack_string ("template_id",
245 : ppth->template_id),
246 : GNUNET_JSON_pack_string ("template_description",
247 : ppth->template_description),
248 : GNUNET_JSON_pack_allow_null (
249 : GNUNET_JSON_pack_string ("otp_id",
250 : ppth->otp_id)),
251 : GNUNET_JSON_pack_object_incref ("template_contract",
252 : ppth->template_contract),
253 : GNUNET_JSON_pack_allow_null (
254 : GNUNET_JSON_pack_object_incref ("editable_defaults",
255 : ppth->editable_defaults)));
256 26 : eh = TALER_MERCHANT_curl_easy_get_ (ppth->url);
257 52 : if ( (NULL == eh) ||
258 : (GNUNET_OK !=
259 26 : TALER_curl_easy_post (&ppth->post_ctx,
260 : eh,
261 : req_obj)) )
262 : {
263 0 : GNUNET_break (0);
264 0 : json_decref (req_obj);
265 0 : if (NULL != eh)
266 0 : curl_easy_cleanup (eh);
267 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
268 : }
269 26 : json_decref (req_obj);
270 52 : ppth->job = GNUNET_CURL_job_add2 (ppth->ctx,
271 : eh,
272 26 : ppth->post_ctx.headers,
273 : &handle_post_templates_finished,
274 : ppth);
275 26 : if (NULL == ppth->job)
276 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
277 26 : return TALER_EC_NONE;
278 : }
279 :
280 :
281 : void
282 26 : TALER_MERCHANT_post_private_templates_cancel (
283 : struct TALER_MERCHANT_PostPrivateTemplatesHandle *ppth)
284 : {
285 26 : if (NULL != ppth->job)
286 : {
287 0 : GNUNET_CURL_job_cancel (ppth->job);
288 0 : ppth->job = NULL;
289 : }
290 26 : TALER_curl_easy_post_finished (&ppth->post_ctx);
291 26 : json_decref (ppth->template_contract);
292 26 : GNUNET_free (ppth->template_id);
293 26 : GNUNET_free (ppth->template_description);
294 26 : GNUNET_free (ppth->otp_id);
295 26 : json_decref (ppth->editable_defaults);
296 26 : GNUNET_free (ppth->url);
297 26 : GNUNET_free (ppth->base_url);
298 26 : GNUNET_free (ppth);
299 26 : }
300 :
301 :
302 : /* end of merchant_api_post-private-templates-new.c */
|