Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2017, 2020, 2021 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_tip_authorize.c
19 : * @brief Implementation of the /tip-authorize request of the merchant's HTTP API
20 : * @author Marcello Stanisci
21 : * @author Christian Grothoff
22 : */
23 : #include "platform.h"
24 : #include <curl/curl.h>
25 : #include <jansson.h>
26 : #include <microhttpd.h> /* just for HTTP status codes */
27 : #include <gnunet/gnunet_util_lib.h>
28 : #include <gnunet/gnunet_curl_lib.h>
29 : #include "taler_merchant_service.h"
30 : #include "merchant_api_curl_defaults.h"
31 : #include <taler/taler_json_lib.h>
32 : #include <taler/taler_signatures.h>
33 : #include <taler/taler_curl_lib.h>
34 :
35 :
36 : /**
37 : * @brief A handle for tip authorizations.
38 : */
39 : struct TALER_MERCHANT_TipAuthorizeHandle
40 : {
41 :
42 : /**
43 : * The url for this request.
44 : */
45 : char *url;
46 :
47 : /**
48 : * Handle for the request.
49 : */
50 : struct GNUNET_CURL_Job *job;
51 :
52 : /**
53 : * Function to call with the result.
54 : */
55 : TALER_MERCHANT_TipAuthorizeCallback cb;
56 :
57 : /**
58 : * Closure for @a cb.
59 : */
60 : void *cb_cls;
61 :
62 : /**
63 : * Reference to the execution context.
64 : */
65 : struct GNUNET_CURL_Context *ctx;
66 :
67 : /**
68 : * Minor context that holds body and headers.
69 : */
70 : struct TALER_CURL_PostContext post_ctx;
71 : };
72 :
73 :
74 : /**
75 : * We got a 200 response back from the exchange (or the merchant).
76 : * Now we need to parse the response and if it is well-formed,
77 : * call the callback (and set it to NULL afterwards).
78 : *
79 : * @param tao handle of the original authorization operation
80 : * @param json cryptographic proof returned by the exchange/merchant
81 : * @return #GNUNET_OK if response is valid
82 : */
83 : static int
84 0 : check_ok (struct TALER_MERCHANT_TipAuthorizeHandle *tao,
85 : const json_t *json)
86 : {
87 : const char *tip_status_url;
88 : const char *taler_tip_uri;
89 : struct TALER_TipIdentifierP tip_id;
90 : struct GNUNET_TIME_Timestamp expiration_time;
91 : struct GNUNET_JSON_Specification spec[] = {
92 0 : GNUNET_JSON_spec_string ("tip_status_url", &tip_status_url),
93 0 : GNUNET_JSON_spec_string ("taler_tip_uri", &taler_tip_uri),
94 0 : GNUNET_JSON_spec_timestamp ("tip_expiration", &expiration_time),
95 0 : GNUNET_JSON_spec_fixed_auto ("tip_id", &tip_id),
96 0 : GNUNET_JSON_spec_end ()
97 : };
98 0 : struct TALER_MERCHANT_HttpResponse hr = {
99 : .http_status = MHD_HTTP_OK,
100 : .reply = json
101 : };
102 :
103 0 : if (GNUNET_OK !=
104 0 : GNUNET_JSON_parse (json,
105 : spec,
106 : NULL, NULL))
107 : {
108 : char *log;
109 :
110 0 : GNUNET_break_op (0);
111 0 : log = json_dumps (json,
112 : 0);
113 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
114 : "JSON %s\n",
115 : log);
116 0 : free (log);
117 0 : return GNUNET_SYSERR;
118 : }
119 0 : tao->cb (tao->cb_cls,
120 : &hr,
121 : &tip_id,
122 : taler_tip_uri,
123 : expiration_time);
124 0 : tao->cb = NULL; /* do not call twice */
125 0 : GNUNET_JSON_parse_free (spec);
126 0 : return GNUNET_OK;
127 : }
128 :
129 :
130 : /**
131 : * Function called when we're done processing the
132 : * HTTP /reservers/$TIP_ID/tip-authorize request.
133 : *
134 : * @param cls the `struct TALER_MERCHANT_TipAuthorizeHandle`
135 : * @param response_code HTTP response code, 0 on error
136 : * @param response response body, NULL if not in JSON
137 : */
138 : static void
139 0 : handle_tip_authorize_finished (void *cls,
140 : long response_code,
141 : const void *response)
142 : {
143 0 : struct TALER_MERCHANT_TipAuthorizeHandle *tao = cls;
144 0 : const json_t *json = response;
145 0 : struct TALER_MERCHANT_HttpResponse hr = {
146 0 : .http_status = (unsigned int) response_code,
147 : .reply = json
148 : };
149 :
150 0 : tao->job = NULL;
151 0 : switch (response_code)
152 : {
153 0 : case MHD_HTTP_OK:
154 0 : if (GNUNET_OK ==
155 0 : check_ok (tao,
156 : json))
157 : {
158 0 : TALER_MERCHANT_tip_authorize_cancel (tao);
159 0 : return;
160 : }
161 0 : GNUNET_break_op (0);
162 0 : hr.http_status = 0;
163 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
164 0 : break;
165 0 : case MHD_HTTP_UNAUTHORIZED:
166 0 : hr.ec = TALER_JSON_get_error_code (json);
167 0 : hr.hint = TALER_JSON_get_error_hint (json);
168 : /* Nothing really to verify, merchant says we need to authenticate. */
169 0 : break;
170 0 : case MHD_HTTP_NOT_FOUND:
171 : /* Well-defined status code, pass on to application! */
172 0 : hr.ec = TALER_JSON_get_error_code (json);
173 0 : hr.hint = TALER_JSON_get_error_hint (json);
174 0 : break;
175 0 : case MHD_HTTP_PRECONDITION_FAILED:
176 : /* Well-defined status code, pass on to application! */
177 0 : hr.ec = TALER_JSON_get_error_code (json);
178 0 : hr.hint = TALER_JSON_get_error_hint (json);
179 0 : break;
180 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
181 : /* Server had an internal issue; we should retry, but this API
182 : leaves this to the application */
183 0 : hr.ec = TALER_JSON_get_error_code (json);
184 0 : hr.hint = TALER_JSON_get_error_hint (json);
185 0 : break;
186 0 : case MHD_HTTP_SERVICE_UNAVAILABLE:
187 : /* Server had an unclear (internal or external) issue; we should retry,
188 : but this API leaves this to the application */
189 0 : TALER_MERCHANT_parse_error_details_ (json,
190 : response_code,
191 : &hr);
192 0 : break;
193 0 : default:
194 : /* unexpected response code */
195 0 : GNUNET_break_op (0);
196 0 : TALER_MERCHANT_parse_error_details_ (json,
197 : response_code,
198 : &hr);
199 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
200 : "Unexpected response code %u/%d\n",
201 : (unsigned int) response_code,
202 : (int) hr.ec);
203 0 : break;
204 : }
205 0 : if (NULL != tao->cb)
206 0 : tao->cb (tao->cb_cls,
207 : &hr,
208 : NULL,
209 : NULL,
210 0 : GNUNET_TIME_UNIT_ZERO_TS);
211 0 : TALER_MERCHANT_tip_authorize_cancel (tao);
212 : }
213 :
214 :
215 : struct TALER_MERCHANT_TipAuthorizeHandle *
216 0 : TALER_MERCHANT_tip_authorize2 (
217 : struct GNUNET_CURL_Context *ctx,
218 : const char *backend_url,
219 : const struct TALER_ReservePublicKeyP *reserve_pub,
220 : const char *next_url,
221 : const struct TALER_Amount *amount,
222 : const char *justification,
223 : TALER_MERCHANT_TipAuthorizeCallback authorize_cb,
224 : void *authorize_cb_cls)
225 : {
226 : struct TALER_MERCHANT_TipAuthorizeHandle *tao;
227 : CURL *eh;
228 : json_t *te_obj;
229 :
230 0 : tao = GNUNET_new (struct TALER_MERCHANT_TipAuthorizeHandle);
231 0 : tao->ctx = ctx;
232 0 : tao->cb = authorize_cb;
233 0 : tao->cb_cls = authorize_cb_cls;
234 :
235 : {
236 : char res_str[sizeof (*reserve_pub) * 2];
237 : char arg_str[sizeof (res_str) + 48];
238 : char *end;
239 :
240 0 : end = GNUNET_STRINGS_data_to_string (reserve_pub,
241 : sizeof (*reserve_pub),
242 : res_str,
243 : sizeof (res_str));
244 0 : *end = '\0';
245 0 : GNUNET_snprintf (arg_str,
246 : sizeof (arg_str),
247 : "private/reserves/%s/authorize-tip",
248 : res_str);
249 0 : tao->url = TALER_url_join (backend_url,
250 : arg_str,
251 : NULL);
252 : }
253 0 : if (NULL == tao->url)
254 : {
255 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256 : "Could not construct request URL.\n");
257 0 : GNUNET_free (tao);
258 0 : return NULL;
259 : }
260 0 : te_obj = GNUNET_JSON_PACK (
261 : TALER_JSON_pack_amount ("amount",
262 : amount),
263 : GNUNET_JSON_pack_string ("justification",
264 : justification),
265 : GNUNET_JSON_pack_string ("next_url",
266 : next_url));
267 0 : eh = curl_easy_init ();
268 0 : GNUNET_assert (NULL != eh);
269 0 : if (GNUNET_OK !=
270 0 : TALER_curl_easy_post (&tao->post_ctx,
271 : eh,
272 : te_obj))
273 : {
274 0 : GNUNET_break (0);
275 0 : curl_easy_cleanup (eh);
276 0 : json_decref (te_obj);
277 0 : GNUNET_free (tao->url);
278 0 : GNUNET_free (tao);
279 0 : return NULL;
280 : }
281 :
282 0 : json_decref (te_obj);
283 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 : "Requesting URL '%s'\n",
285 : tao->url);
286 0 : GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
287 : CURLOPT_URL,
288 : tao->url));
289 :
290 0 : tao->job = GNUNET_CURL_job_add2 (ctx,
291 : eh,
292 0 : tao->post_ctx.headers,
293 : &handle_tip_authorize_finished,
294 : tao);
295 0 : return tao;
296 : }
297 :
298 :
299 : struct TALER_MERCHANT_TipAuthorizeHandle *
300 0 : TALER_MERCHANT_tip_authorize (struct GNUNET_CURL_Context *ctx,
301 : const char *backend_url,
302 : const char *next_url,
303 : const struct TALER_Amount *amount,
304 : const char *justification,
305 : TALER_MERCHANT_TipAuthorizeCallback authorize_cb,
306 : void *authorize_cb_cls)
307 : {
308 : struct TALER_MERCHANT_TipAuthorizeHandle *tao;
309 : CURL *eh;
310 : json_t *te_obj;
311 :
312 0 : tao = GNUNET_new (struct TALER_MERCHANT_TipAuthorizeHandle);
313 0 : tao->ctx = ctx;
314 0 : tao->cb = authorize_cb;
315 0 : tao->cb_cls = authorize_cb_cls;
316 :
317 0 : tao->url = TALER_url_join (backend_url,
318 : "private/tips",
319 : NULL);
320 0 : if (NULL == tao->url)
321 : {
322 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
323 : "Could not construct request URL.\n");
324 0 : GNUNET_free (tao);
325 0 : return NULL;
326 : }
327 0 : te_obj = GNUNET_JSON_PACK (
328 : TALER_JSON_pack_amount ("amount",
329 : amount),
330 : GNUNET_JSON_pack_string ("justification",
331 : justification),
332 : GNUNET_JSON_pack_string ("next_url",
333 : next_url));
334 0 : eh = TALER_MERCHANT_curl_easy_get_ (tao->url);
335 0 : if (GNUNET_OK !=
336 0 : TALER_curl_easy_post (&tao->post_ctx,
337 : eh,
338 : te_obj))
339 : {
340 0 : GNUNET_break (0);
341 0 : curl_easy_cleanup (eh);
342 0 : json_decref (te_obj);
343 0 : GNUNET_free (tao->url);
344 0 : GNUNET_free (tao);
345 0 : return NULL;
346 : }
347 0 : json_decref (te_obj);
348 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349 : "Requesting URL '%s'\n",
350 : tao->url);
351 0 : tao->job = GNUNET_CURL_job_add2 (ctx,
352 : eh,
353 0 : tao->post_ctx.headers,
354 : &handle_tip_authorize_finished,
355 : tao);
356 0 : return tao;
357 : }
358 :
359 :
360 : void
361 0 : TALER_MERCHANT_tip_authorize_cancel (
362 : struct TALER_MERCHANT_TipAuthorizeHandle *tao)
363 : {
364 0 : if (NULL != tao->job)
365 : {
366 0 : GNUNET_CURL_job_cancel (tao->job);
367 0 : tao->job = NULL;
368 : }
369 0 : TALER_curl_easy_post_finished (&tao->post_ctx);
370 0 : GNUNET_free (tao->url);
371 0 : GNUNET_free (tao);
372 0 : }
373 :
374 :
375 : /* end of merchant_api_tip_authorize.c */
|