Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2017--2021 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or
6 : modify it under the terms of the GNU General Public License
7 : as published by the Free Software Foundation; either version 3,
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 General Public License for more details.
14 :
15 : You should have received a copy of the GNU General Public
16 : License along with TALER; see the file COPYING. If not,
17 : see <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file bank-lib/bank_api_debit.c
21 : * @brief Implementation of the /history/outgoing
22 : * requests of the bank's HTTP API.
23 : * @author Christian Grothoff
24 : * @author Marcello Stanisci
25 : */
26 : #include "platform.h"
27 : #include "bank_api_common.h"
28 : #include <microhttpd.h> /* just for HTTP status codes */
29 : #include "taler_signatures.h"
30 :
31 :
32 : /**
33 : * How much longer than the application-specified timeout
34 : * do we wait (giving the server a chance to respond)?
35 : */
36 : #define GRACE_PERIOD_MS 1000
37 :
38 :
39 : /**
40 : * @brief A /history/outgoing Handle
41 : */
42 : struct TALER_BANK_DebitHistoryHandle
43 : {
44 :
45 : /**
46 : * The url for this request.
47 : */
48 : char *request_url;
49 :
50 : /**
51 : * Handle for the request.
52 : */
53 : struct GNUNET_CURL_Job *job;
54 :
55 : /**
56 : * Function to call with the result.
57 : */
58 : TALER_BANK_DebitHistoryCallback hcb;
59 :
60 : /**
61 : * Closure for @a cb.
62 : */
63 : void *hcb_cls;
64 : };
65 :
66 :
67 : /**
68 : * Parse history given in JSON format and invoke the callback on each item.
69 : *
70 : * @param hh handle to the account history request
71 : * @param history JSON array with the history
72 : * @return #GNUNET_OK if history was valid and @a rhistory and @a balance
73 : * were set,
74 : * #GNUNET_SYSERR if there was a protocol violation in @a history
75 : */
76 : static enum GNUNET_GenericReturnValue
77 5 : parse_account_history (struct TALER_BANK_DebitHistoryHandle *hh,
78 : const json_t *history)
79 : {
80 : json_t *history_array;
81 :
82 5 : if (NULL == (history_array = json_object_get (history,
83 : "outgoing_transactions")))
84 : {
85 0 : GNUNET_break_op (0);
86 0 : return GNUNET_SYSERR;
87 : }
88 5 : if (! json_is_array (history_array))
89 : {
90 0 : GNUNET_break_op (0);
91 0 : return GNUNET_SYSERR;
92 : }
93 8 : for (unsigned int i = 0; i<json_array_size (history_array); i++)
94 : {
95 : struct TALER_BANK_DebitDetails td;
96 : uint64_t row_id;
97 : struct GNUNET_JSON_Specification hist_spec[] = {
98 3 : TALER_JSON_spec_amount_any ("amount",
99 : &td.amount),
100 3 : GNUNET_JSON_spec_timestamp ("date",
101 : &td.execution_date),
102 3 : GNUNET_JSON_spec_uint64 ("row_id",
103 : &row_id),
104 3 : GNUNET_JSON_spec_fixed_auto ("wtid",
105 : &td.wtid),
106 3 : GNUNET_JSON_spec_string ("credit_account",
107 : &td.credit_account_uri),
108 3 : GNUNET_JSON_spec_string ("debit_account",
109 : &td.debit_account_uri),
110 3 : GNUNET_JSON_spec_string ("exchange_base_url",
111 : &td.exchange_base_url),
112 3 : GNUNET_JSON_spec_end ()
113 : };
114 3 : json_t *transaction = json_array_get (history_array,
115 : i);
116 :
117 3 : if (GNUNET_OK !=
118 3 : GNUNET_JSON_parse (transaction,
119 : hist_spec,
120 : NULL, NULL))
121 : {
122 0 : GNUNET_break_op (0);
123 0 : return GNUNET_SYSERR;
124 : }
125 3 : if (GNUNET_OK !=
126 3 : hh->hcb (hh->hcb_cls,
127 : MHD_HTTP_OK,
128 : TALER_EC_NONE,
129 : row_id,
130 : &td,
131 : transaction))
132 : {
133 0 : hh->hcb = NULL;
134 0 : GNUNET_JSON_parse_free (hist_spec);
135 0 : return GNUNET_OK;
136 : }
137 3 : GNUNET_JSON_parse_free (hist_spec);
138 : }
139 5 : return GNUNET_OK;
140 : }
141 :
142 :
143 : /**
144 : * Function called when we're done processing the
145 : * HTTP /history/outgoing request.
146 : *
147 : * @param cls the `struct TALER_BANK_DebitHistoryHandle`
148 : * @param response_code HTTP response code, 0 on error
149 : * @param response parsed JSON result, NULL on error
150 : */
151 : static void
152 5 : handle_debit_history_finished (void *cls,
153 : long response_code,
154 : const void *response)
155 : {
156 5 : struct TALER_BANK_DebitHistoryHandle *hh = cls;
157 5 : const json_t *j = response;
158 : enum TALER_ErrorCode ec;
159 :
160 5 : hh->job = NULL;
161 5 : switch (response_code)
162 : {
163 0 : case 0:
164 0 : ec = TALER_EC_GENERIC_INVALID_RESPONSE;
165 0 : break;
166 5 : case MHD_HTTP_OK:
167 5 : if (GNUNET_OK !=
168 5 : parse_account_history (hh,
169 : j))
170 : {
171 0 : GNUNET_break_op (0);
172 0 : response_code = 0;
173 0 : ec = TALER_EC_GENERIC_INVALID_RESPONSE;
174 0 : break;
175 : }
176 5 : response_code = MHD_HTTP_NO_CONTENT; /* signal end of list */
177 5 : ec = TALER_EC_NONE;
178 5 : break;
179 0 : case MHD_HTTP_NO_CONTENT:
180 0 : ec = TALER_EC_NONE;
181 0 : break;
182 0 : case MHD_HTTP_BAD_REQUEST:
183 : /* This should never happen, either us or the bank is buggy
184 : (or API version conflict); just pass JSON reply to the application */
185 0 : GNUNET_break_op (0);
186 0 : ec = TALER_JSON_get_error_code (j);
187 0 : break;
188 0 : case MHD_HTTP_UNAUTHORIZED:
189 : /* Nothing really to verify, bank says the HTTP Authentication
190 : failed. May happen if HTTP authentication is used and the
191 : user supplied a wrong username/password combination. */
192 0 : ec = TALER_JSON_get_error_code (j);
193 0 : break;
194 0 : case MHD_HTTP_NOT_FOUND:
195 : /* Nothing really to verify: the bank is either unaware
196 : of the endpoint (not a bank), or of the account.
197 : We should pass the JSON (?) reply to the application */
198 0 : ec = TALER_JSON_get_error_code (j);
199 0 : break;
200 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
201 : /* Server had an internal issue; we should retry, but this API
202 : leaves this to the application */
203 0 : ec = TALER_JSON_get_error_code (j);
204 0 : break;
205 0 : default:
206 : /* unexpected response code */
207 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
208 : "Unexpected response code %u\n",
209 : (unsigned int) response_code);
210 0 : ec = TALER_JSON_get_error_code (j);
211 0 : break;
212 : }
213 5 : if (NULL != hh->hcb)
214 5 : hh->hcb (hh->hcb_cls,
215 : response_code,
216 : ec,
217 : 0LLU,
218 : NULL,
219 : j);
220 5 : TALER_BANK_debit_history_cancel (hh);
221 5 : }
222 :
223 :
224 : struct TALER_BANK_DebitHistoryHandle *
225 5 : TALER_BANK_debit_history (struct GNUNET_CURL_Context *ctx,
226 : const struct TALER_BANK_AuthenticationData *auth,
227 : uint64_t start_row,
228 : int64_t num_results,
229 : struct GNUNET_TIME_Relative timeout,
230 : TALER_BANK_DebitHistoryCallback hres_cb,
231 : void *hres_cb_cls)
232 : {
233 : char url[128];
234 : struct TALER_BANK_DebitHistoryHandle *hh;
235 : CURL *eh;
236 : unsigned long long tms;
237 :
238 5 : if (0 == num_results)
239 : {
240 0 : GNUNET_break (0);
241 0 : return NULL;
242 : }
243 :
244 10 : tms = (unsigned long long) (timeout.rel_value_us
245 5 : / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
246 5 : if ( ( (UINT64_MAX == start_row) &&
247 4 : (0 > num_results) ) ||
248 4 : ( (0 == start_row) &&
249 : (0 < num_results) ) )
250 : {
251 5 : if ( (0 < num_results) &&
252 4 : (! GNUNET_TIME_relative_is_zero (timeout)) )
253 0 : GNUNET_snprintf (url,
254 : sizeof (url),
255 : "history/outgoing?delta=%lld&long_poll_ms=%llu",
256 : (long long) num_results,
257 : tms);
258 : else
259 5 : GNUNET_snprintf (url,
260 : sizeof (url),
261 : "history/outgoing?delta=%lld",
262 : (long long) num_results);
263 : }
264 : else
265 : {
266 0 : if ( (0 < num_results) &&
267 0 : (! GNUNET_TIME_relative_is_zero (timeout)) )
268 0 : GNUNET_snprintf (url,
269 : sizeof (url),
270 : "history/outgoing?delta=%lld&start=%llu&long_poll_ms=%llu",
271 : (long long) num_results,
272 : (unsigned long long) start_row,
273 : tms);
274 : else
275 0 : GNUNET_snprintf (url,
276 : sizeof (url),
277 : "history/outgoing?delta=%lld&start=%llu",
278 : (long long) num_results,
279 : (unsigned long long) start_row);
280 : }
281 5 : hh = GNUNET_new (struct TALER_BANK_DebitHistoryHandle);
282 5 : hh->hcb = hres_cb;
283 5 : hh->hcb_cls = hres_cb_cls;
284 5 : hh->request_url = TALER_url_join (auth->wire_gateway_url,
285 : url,
286 : NULL);
287 5 : if (NULL == hh->request_url)
288 : {
289 0 : GNUNET_free (hh);
290 0 : GNUNET_break (0);
291 0 : return NULL;
292 : }
293 5 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
294 : "Requesting debit history at `%s'\n",
295 : hh->request_url);
296 5 : eh = curl_easy_init ();
297 10 : if ( (NULL == eh) ||
298 : (GNUNET_OK !=
299 5 : TALER_BANK_setup_auth_ (eh,
300 5 : auth)) ||
301 : (CURLE_OK !=
302 5 : curl_easy_setopt (eh,
303 : CURLOPT_URL,
304 : hh->request_url)) )
305 : {
306 0 : GNUNET_break (0);
307 0 : TALER_BANK_debit_history_cancel (hh);
308 0 : if (NULL != eh)
309 0 : curl_easy_cleanup (eh);
310 0 : return NULL;
311 : }
312 5 : if (0 != tms)
313 : {
314 0 : GNUNET_break (CURLE_OK ==
315 : curl_easy_setopt (eh,
316 : CURLOPT_TIMEOUT_MS,
317 : (long) tms + GRACE_PERIOD_MS));
318 : }
319 5 : hh->job = GNUNET_CURL_job_add2 (ctx,
320 : eh,
321 : NULL,
322 : &handle_debit_history_finished,
323 : hh);
324 5 : return hh;
325 : }
326 :
327 :
328 : void
329 5 : TALER_BANK_debit_history_cancel (struct TALER_BANK_DebitHistoryHandle *hh)
330 : {
331 5 : if (NULL != hh->job)
332 : {
333 0 : GNUNET_CURL_job_cancel (hh->job);
334 0 : hh->job = NULL;
335 : }
336 5 : GNUNET_free (hh->request_url);
337 5 : GNUNET_free (hh);
338 5 : }
339 :
340 :
341 : /* end of bank_api_debit.c */
|