Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 3, 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 General Public License for more details.
12 :
13 : You should have received a copy of the GNU General Public License along with
14 : TALER; see the file COPYING. If not, see
15 : <http://www.gnu.org/licenses/>
16 : */
17 : /**
18 : * @file lib/exchange_api_purses_get.c
19 : * @brief Implementation of the /purses/ GET request
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include <jansson.h>
24 : #include <microhttpd.h> /* just for HTTP status codes */
25 : #include <gnunet/gnunet_util_lib.h>
26 : #include <gnunet/gnunet_json_lib.h>
27 : #include <gnunet/gnunet_curl_lib.h>
28 : #include "taler_json_lib.h"
29 : #include "taler_exchange_service.h"
30 : #include "exchange_api_handle.h"
31 : #include "taler_signatures.h"
32 : #include "exchange_api_curl_defaults.h"
33 :
34 :
35 : /**
36 : * @brief A Contract Get Handle
37 : */
38 : struct TALER_EXCHANGE_PurseGetHandle
39 : {
40 :
41 : /**
42 : * The connection to exchange this request handle will use
43 : */
44 : struct TALER_EXCHANGE_Handle *exchange;
45 :
46 : /**
47 : * The url for this request.
48 : */
49 : char *url;
50 :
51 : /**
52 : * Handle for the request.
53 : */
54 : struct GNUNET_CURL_Job *job;
55 :
56 : /**
57 : * Function to call with the result.
58 : */
59 : TALER_EXCHANGE_PurseGetCallback cb;
60 :
61 : /**
62 : * Closure for @a cb.
63 : */
64 : void *cb_cls;
65 :
66 : };
67 :
68 :
69 : /**
70 : * Function called when we're done processing the
71 : * HTTP /purses/$PID GET request.
72 : *
73 : * @param cls the `struct TALER_EXCHANGE_PurseGetHandle`
74 : * @param response_code HTTP response code, 0 on error
75 : * @param response parsed JSON result, NULL on error
76 : */
77 : static void
78 0 : handle_purse_get_finished (void *cls,
79 : long response_code,
80 : const void *response)
81 : {
82 0 : struct TALER_EXCHANGE_PurseGetHandle *pgh = cls;
83 0 : const json_t *j = response;
84 0 : struct TALER_EXCHANGE_PurseGetResponse dr = {
85 : .hr.reply = j,
86 0 : .hr.http_status = (unsigned int) response_code
87 : };
88 :
89 0 : pgh->job = NULL;
90 0 : switch (response_code)
91 : {
92 0 : case 0:
93 0 : dr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
94 0 : break;
95 0 : case MHD_HTTP_OK:
96 : {
97 0 : bool no_merge = false;
98 0 : bool no_deposit = false;
99 : struct TALER_ExchangePublicKeyP exchange_pub;
100 : struct TALER_ExchangeSignatureP exchange_sig;
101 : struct GNUNET_JSON_Specification spec[] = {
102 0 : GNUNET_JSON_spec_mark_optional (
103 : GNUNET_JSON_spec_timestamp ("merge_timestamp",
104 : &dr.details.success.merge_timestamp),
105 : &no_merge),
106 0 : GNUNET_JSON_spec_mark_optional (
107 : GNUNET_JSON_spec_timestamp ("deposit_timestamp",
108 : &dr.details.success.deposit_timestamp),
109 : &no_deposit),
110 0 : TALER_JSON_spec_amount_any ("balance",
111 : &dr.details.success.balance),
112 0 : GNUNET_JSON_spec_fixed_auto ("exchange_pub",
113 : &exchange_pub),
114 0 : GNUNET_JSON_spec_fixed_auto ("exchange_sig",
115 : &exchange_sig),
116 0 : GNUNET_JSON_spec_end ()
117 : };
118 : const struct TALER_EXCHANGE_Keys *key_state;
119 :
120 0 : if (GNUNET_OK !=
121 0 : GNUNET_JSON_parse (j,
122 : spec,
123 : NULL, NULL))
124 : {
125 0 : GNUNET_break_op (0);
126 0 : dr.hr.http_status = 0;
127 0 : dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
128 0 : break;
129 : }
130 :
131 0 : key_state = TALER_EXCHANGE_get_keys (pgh->exchange);
132 0 : if (GNUNET_OK !=
133 0 : TALER_EXCHANGE_test_signing_key (key_state,
134 : &exchange_pub))
135 : {
136 0 : GNUNET_break_op (0);
137 0 : dr.hr.http_status = 0;
138 0 : dr.hr.ec = TALER_EC_EXCHANGE_PURSES_GET_INVALID_SIGNATURE_BY_EXCHANGE;
139 0 : break;
140 : }
141 0 : if (GNUNET_OK !=
142 0 : TALER_exchange_online_purse_status_verify (
143 : dr.details.success.merge_timestamp,
144 : dr.details.success.deposit_timestamp,
145 : &dr.details.success.balance,
146 : &exchange_pub,
147 : &exchange_sig))
148 : {
149 0 : GNUNET_break_op (0);
150 0 : dr.hr.http_status = 0;
151 0 : dr.hr.ec = TALER_EC_EXCHANGE_PURSES_GET_INVALID_SIGNATURE_BY_EXCHANGE;
152 0 : break;
153 : }
154 0 : pgh->cb (pgh->cb_cls,
155 : &dr);
156 0 : TALER_EXCHANGE_purse_get_cancel (pgh);
157 0 : return;
158 : }
159 0 : case MHD_HTTP_BAD_REQUEST:
160 0 : dr.hr.ec = TALER_JSON_get_error_code (j);
161 0 : dr.hr.hint = TALER_JSON_get_error_hint (j);
162 : /* This should never happen, either us or the exchange is buggy
163 : (or API version conflict); just pass JSON reply to the application */
164 0 : break;
165 0 : case MHD_HTTP_FORBIDDEN:
166 0 : dr.hr.ec = TALER_JSON_get_error_code (j);
167 0 : dr.hr.hint = TALER_JSON_get_error_hint (j);
168 : /* Nothing really to verify, exchange says one of the signatures is
169 : invalid; as we checked them, this should never happen, we
170 : should pass the JSON reply to the application */
171 0 : break;
172 0 : case MHD_HTTP_NOT_FOUND:
173 0 : dr.hr.ec = TALER_JSON_get_error_code (j);
174 0 : dr.hr.hint = TALER_JSON_get_error_hint (j);
175 : /* Exchange does not know about transaction;
176 : we should pass the reply to the application */
177 0 : break;
178 0 : case MHD_HTTP_GONE:
179 : /* purse expired */
180 0 : dr.hr.ec = TALER_JSON_get_error_code (j);
181 0 : dr.hr.hint = TALER_JSON_get_error_hint (j);
182 0 : break;
183 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
184 0 : dr.hr.ec = TALER_JSON_get_error_code (j);
185 0 : dr.hr.hint = TALER_JSON_get_error_hint (j);
186 : /* Server had an internal issue; we should retry, but this API
187 : leaves this to the application */
188 0 : break;
189 0 : default:
190 : /* unexpected response code */
191 0 : dr.hr.ec = TALER_JSON_get_error_code (j);
192 0 : dr.hr.hint = TALER_JSON_get_error_hint (j);
193 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
194 : "Unexpected response code %u/%d for exchange GET purses\n",
195 : (unsigned int) response_code,
196 : (int) dr.hr.ec);
197 0 : GNUNET_break_op (0);
198 0 : break;
199 : }
200 0 : pgh->cb (pgh->cb_cls,
201 : &dr);
202 0 : TALER_EXCHANGE_purse_get_cancel (pgh);
203 : }
204 :
205 :
206 : struct TALER_EXCHANGE_PurseGetHandle *
207 0 : TALER_EXCHANGE_purse_get (
208 : struct TALER_EXCHANGE_Handle *exchange,
209 : const struct TALER_PurseContractPublicKeyP *purse_pub,
210 : struct GNUNET_TIME_Relative timeout,
211 : bool wait_for_merge,
212 : TALER_EXCHANGE_PurseGetCallback cb,
213 : void *cb_cls)
214 : {
215 : struct TALER_EXCHANGE_PurseGetHandle *pgh;
216 : CURL *eh;
217 : char arg_str[sizeof (*purse_pub) * 2 + 64];
218 :
219 0 : if (GNUNET_YES !=
220 0 : TEAH_handle_is_ready (exchange))
221 : {
222 0 : GNUNET_break (0);
223 0 : return NULL;
224 : }
225 0 : pgh = GNUNET_new (struct TALER_EXCHANGE_PurseGetHandle);
226 0 : pgh->exchange = exchange;
227 0 : pgh->cb = cb;
228 0 : pgh->cb_cls = cb_cls;
229 : {
230 : char cpub_str[sizeof (*purse_pub) * 2];
231 : char *end;
232 : char timeout_str[32];
233 :
234 0 : end = GNUNET_STRINGS_data_to_string (purse_pub,
235 : sizeof (*purse_pub),
236 : cpub_str,
237 : sizeof (cpub_str));
238 0 : *end = '\0';
239 0 : GNUNET_snprintf (timeout_str,
240 : sizeof (timeout_str),
241 : "%llu",
242 : (unsigned long long)
243 0 : (timeout.rel_value_us
244 0 : / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us));
245 0 : if (GNUNET_TIME_relative_is_zero (timeout))
246 0 : GNUNET_snprintf (arg_str,
247 : sizeof (arg_str),
248 : "/purses/%s/%s",
249 : cpub_str,
250 : wait_for_merge ? "merge" : "deposit");
251 : else
252 0 : GNUNET_snprintf (arg_str,
253 : sizeof (arg_str),
254 : "/purses/%s/%s?timeout_ms=%s",
255 : cpub_str,
256 : wait_for_merge ? "merge" : "deposit",
257 : timeout_str);
258 : }
259 0 : pgh->url = TEAH_path_to_url (exchange,
260 : arg_str);
261 0 : if (NULL == pgh->url)
262 : {
263 0 : GNUNET_free (pgh);
264 0 : return NULL;
265 : }
266 0 : eh = TALER_EXCHANGE_curl_easy_get_ (pgh->url);
267 0 : if (NULL == eh)
268 : {
269 0 : GNUNET_break (0);
270 0 : GNUNET_free (pgh->url);
271 0 : GNUNET_free (pgh);
272 0 : return NULL;
273 : }
274 0 : pgh->job = GNUNET_CURL_job_add (TEAH_handle_to_context (exchange),
275 : eh,
276 : &handle_purse_get_finished,
277 : pgh);
278 0 : return pgh;
279 : }
280 :
281 :
282 : void
283 0 : TALER_EXCHANGE_purse_get_cancel (
284 : struct TALER_EXCHANGE_PurseGetHandle *pgh)
285 : {
286 0 : if (NULL != pgh->job)
287 : {
288 0 : GNUNET_CURL_job_cancel (pgh->job);
289 0 : pgh->job = NULL;
290 : }
291 0 : GNUNET_free (pgh->url);
292 0 : GNUNET_free (pgh);
293 0 : }
294 :
295 :
296 : /* end of exchange_api_purses_get.c */
|