Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 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_get_kyc.c
19 : * @brief Implementation of the GET /kyc request of the merchant's HTTP API
20 : * @author Christian Grothoff
21 : */
22 : #include "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_service.h"
29 : #include "merchant_api_curl_defaults.h"
30 : #include <taler/taler_json_lib.h>
31 : #include <taler/taler_signatures.h>
32 :
33 :
34 : /**
35 : * Handle for a GET /kyc operation.
36 : */
37 : struct TALER_MERCHANT_KycGetHandle
38 : {
39 : /**
40 : * The url for this request.
41 : */
42 : char *url;
43 :
44 : /**
45 : * Handle for the request.
46 : */
47 : struct GNUNET_CURL_Job *job;
48 :
49 : /**
50 : * Function to call with the result.
51 : */
52 : TALER_MERCHANT_KycGetCallback cb;
53 :
54 : /**
55 : * Closure for @a cb.
56 : */
57 : void *cb_cls;
58 :
59 : /**
60 : * Reference to the execution context.
61 : */
62 : struct GNUNET_CURL_Context *ctx;
63 :
64 : };
65 :
66 :
67 : /**
68 : * Parse @a kyc response and call the continuation on success.
69 : *
70 : * @param kyc operation handle
71 : * @param[in,out] kr response details
72 : * @param pends pending_kycs array from the reply
73 : * @param touts timeout_kycs array from the reply
74 : * @return #GNUNET_OK on success (callback was called)
75 : */
76 : static enum GNUNET_GenericReturnValue
77 0 : parse_kyc (struct TALER_MERCHANT_KycGetHandle *kyc,
78 : struct TALER_MERCHANT_KycResponse *kr,
79 : json_t *pends,
80 : json_t *touts)
81 0 : {
82 0 : unsigned int num_pends = json_array_size (pends);
83 0 : unsigned int num_touts = json_array_size (touts);
84 0 : struct TALER_MERCHANT_AccountKycRedirectDetail pending_kycs[GNUNET_NZL (
85 : num_pends)];
86 0 : struct TALER_MERCHANT_ExchangeKycFailureDetail timeout_kycs[GNUNET_NZL (
87 : num_touts)];
88 :
89 0 : for (unsigned int i = 0; i<num_pends; i++)
90 : {
91 : struct GNUNET_JSON_Specification spec[] = {
92 0 : GNUNET_JSON_spec_string ("kyc_url",
93 : &pending_kycs[i].kyc_url),
94 0 : GNUNET_JSON_spec_string ("exchange_url",
95 : &pending_kycs[i].exchange_url),
96 0 : GNUNET_JSON_spec_string ("payto_uri",
97 : &pending_kycs[i].payto_uri),
98 0 : GNUNET_JSON_spec_end ()
99 : };
100 :
101 0 : if (GNUNET_OK !=
102 0 : GNUNET_JSON_parse (json_array_get (pends,
103 : i),
104 : spec,
105 : NULL, NULL))
106 : {
107 0 : GNUNET_break (0);
108 0 : return GNUNET_SYSERR;
109 : }
110 : }
111 0 : for (unsigned int i = 0; i<num_touts; i++)
112 : {
113 : uint32_t hs;
114 : uint32_t ec;
115 : struct GNUNET_JSON_Specification spec[] = {
116 0 : GNUNET_JSON_spec_string ("exchange_url",
117 : &timeout_kycs[i].exchange_url),
118 0 : GNUNET_JSON_spec_uint32 ("exchange_code",
119 : &ec),
120 0 : GNUNET_JSON_spec_uint32 ("exchange_http_status",
121 : &hs),
122 0 : GNUNET_JSON_spec_end ()
123 : };
124 :
125 0 : if (GNUNET_OK !=
126 0 : GNUNET_JSON_parse (json_array_get (touts,
127 : i),
128 : spec,
129 : NULL, NULL))
130 : {
131 0 : GNUNET_break (0);
132 0 : return GNUNET_SYSERR;
133 : }
134 0 : timeout_kycs[i].exchange_http_status = (unsigned int) hs;
135 0 : timeout_kycs[i].exchange_code = (enum TALER_ErrorCode) ec;
136 : }
137 0 : kr->details.kyc_status.pending_kycs = pending_kycs;
138 0 : kr->details.kyc_status.timeout_kycs = timeout_kycs;
139 0 : kr->details.kyc_status.pending_kycs_length = num_pends;
140 0 : kr->details.kyc_status.timeout_kycs_length = num_touts;
141 0 : kyc->cb (kyc->cb_cls,
142 : kr);
143 0 : return GNUNET_OK;
144 : }
145 :
146 :
147 : /**
148 : * Function called when we're done processing the
149 : * HTTP /kyc request.
150 : *
151 : * @param cls the `struct TALER_MERCHANT_KycGetHandle`
152 : * @param response_code HTTP response code, 0 on error
153 : * @param response response body, NULL if not in JSON
154 : */
155 : static void
156 0 : handle_get_kyc_finished (void *cls,
157 : long response_code,
158 : const void *response)
159 : {
160 0 : struct TALER_MERCHANT_KycGetHandle *kyc = cls;
161 0 : const json_t *json = response;
162 0 : struct TALER_MERCHANT_KycResponse kr = {
163 0 : .hr.http_status = (unsigned int) response_code,
164 : .hr.reply = json
165 : };
166 :
167 0 : kyc->job = NULL;
168 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 : "Got /kyc response with status code %u\n",
170 : (unsigned int) response_code);
171 0 : switch (response_code)
172 : {
173 0 : case MHD_HTTP_NO_CONTENT:
174 0 : break;
175 0 : case MHD_HTTP_ACCEPTED:
176 : case MHD_HTTP_BAD_GATEWAY:
177 : case MHD_HTTP_GATEWAY_TIMEOUT:
178 : {
179 : json_t *pends;
180 : json_t *touts;
181 : struct GNUNET_JSON_Specification spec[] = {
182 0 : GNUNET_JSON_spec_json ("pending_kycs",
183 : &pends),
184 0 : GNUNET_JSON_spec_json ("timeout_kycs",
185 : &touts),
186 0 : GNUNET_JSON_spec_end ()
187 : };
188 :
189 0 : if (GNUNET_OK !=
190 0 : GNUNET_JSON_parse (json,
191 : spec,
192 : NULL, NULL))
193 : {
194 0 : kr.hr.http_status = 0;
195 0 : kr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
196 0 : break;
197 : }
198 0 : if ( (! json_is_array (pends)) ||
199 0 : (! json_is_array (touts)) ||
200 : (GNUNET_OK !=
201 0 : parse_kyc (kyc,
202 : &kr,
203 : pends,
204 : touts)) )
205 : {
206 0 : kr.hr.http_status = 0;
207 0 : kr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
208 0 : break;
209 : }
210 : /* parse_kyc called the continuation already */
211 0 : GNUNET_JSON_parse_free (spec);
212 0 : TALER_MERCHANT_kyc_get_cancel (kyc);
213 0 : return;
214 : }
215 0 : case MHD_HTTP_UNAUTHORIZED:
216 0 : kr.hr.ec = TALER_JSON_get_error_code (json);
217 0 : kr.hr.hint = TALER_JSON_get_error_hint (json);
218 : /* Nothing really to verify, merchant says we need to authenticate. */
219 0 : break;
220 0 : case MHD_HTTP_SERVICE_UNAVAILABLE:
221 0 : break;
222 0 : default:
223 : /* unexpected response code */
224 0 : kr.hr.ec = TALER_JSON_get_error_code (json);
225 0 : kr.hr.hint = TALER_JSON_get_error_hint (json);
226 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
227 : "Unexpected response code %u/%d\n",
228 : (unsigned int) response_code,
229 : (int) kr.hr.ec);
230 0 : break;
231 : }
232 0 : kyc->cb (kyc->cb_cls,
233 : &kr);
234 0 : TALER_MERCHANT_kyc_get_cancel (kyc);
235 : }
236 :
237 :
238 : /**
239 : * Issue a GET KYC request to the backend.
240 : * Returns KYC status of bank accounts.
241 : *
242 : * @param ctx execution context
243 : * @param[in] url URL to use for the request, consumed!
244 : * @param h_wire which bank account to query, NULL for all
245 : * @param exchange_url which exchange to query, NULL for all
246 : * @param timeout how long to wait for a (positive) reply
247 : * @param cb function to call with the result
248 : * @param cb_cls closure for @a cb
249 : * @return handle for this operation, NULL upon errors
250 : */
251 : static struct TALER_MERCHANT_KycGetHandle *
252 0 : kyc_get (struct GNUNET_CURL_Context *ctx,
253 : char *url,
254 : const struct TALER_MerchantWireHashP *h_wire,
255 : const char *exchange_url,
256 : struct GNUNET_TIME_Relative timeout,
257 : TALER_MERCHANT_KycGetCallback cb,
258 : void *cb_cls)
259 : {
260 : struct TALER_MERCHANT_KycGetHandle *kyc;
261 : CURL *eh;
262 : char timeout_ms[32];
263 :
264 0 : kyc = GNUNET_new (struct TALER_MERCHANT_KycGetHandle);
265 0 : kyc->ctx = ctx;
266 0 : kyc->cb = cb;
267 0 : kyc->cb_cls = cb_cls;
268 0 : GNUNET_snprintf (timeout_ms,
269 : sizeof (timeout_ms),
270 : "%llu",
271 0 : (unsigned long long) (timeout.rel_value_us
272 0 : / GNUNET_TIME_UNIT_MILLISECONDS.
273 : rel_value_us));
274 0 : kyc->url = TALER_url_join (url,
275 : "kyc",
276 : "h_wire",
277 : NULL == h_wire
278 : ? NULL
279 0 : : GNUNET_h2s_full (&h_wire->hash),
280 : "exchange_url",
281 : NULL == exchange_url
282 : ? NULL
283 : : exchange_url,
284 : "timeout_ms",
285 0 : GNUNET_TIME_relative_is_zero (timeout)
286 : ? NULL
287 : : timeout_ms,
288 : NULL);
289 0 : GNUNET_free (url);
290 0 : if (NULL == kyc->url)
291 : {
292 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
293 : "Could not construct request URL.\n");
294 0 : GNUNET_free (kyc);
295 0 : return NULL;
296 : }
297 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 : "Requesting URL '%s'\n",
299 : kyc->url);
300 0 : eh = TALER_MERCHANT_curl_easy_get_ (kyc->url);
301 0 : kyc->job = GNUNET_CURL_job_add (ctx,
302 : eh,
303 : &handle_get_kyc_finished,
304 : kyc);
305 0 : return kyc;
306 : }
307 :
308 :
309 : struct TALER_MERCHANT_KycGetHandle *
310 0 : TALER_MERCHANT_kyc_get (struct GNUNET_CURL_Context *ctx,
311 : const char *backend_url,
312 : const struct TALER_MerchantWireHashP *h_wire,
313 : const char *exchange_url,
314 : struct GNUNET_TIME_Relative timeout,
315 : TALER_MERCHANT_KycGetCallback cb,
316 : void *cb_cls)
317 : {
318 : char *url;
319 :
320 0 : GNUNET_asprintf (&url,
321 : "%sprivate/",
322 : backend_url);
323 0 : return kyc_get (ctx,
324 : url,
325 : h_wire,
326 : exchange_url,
327 : timeout,
328 : cb,
329 : cb_cls);
330 : }
331 :
332 :
333 : struct TALER_MERCHANT_KycGetHandle *
334 0 : TALER_MERCHANT_management_kyc_get (struct GNUNET_CURL_Context *ctx,
335 : const char *backend_url,
336 : const char *instance_id,
337 : const struct TALER_MerchantWireHashP *h_wire,
338 : const char *exchange_url,
339 : struct GNUNET_TIME_Relative timeout,
340 : TALER_MERCHANT_KycGetCallback cb,
341 : void *cb_cls)
342 : {
343 : char *url;
344 :
345 0 : GNUNET_asprintf (&url,
346 : "%smanagement/instances/%s/",
347 : backend_url,
348 : instance_id);
349 0 : return kyc_get (ctx,
350 : url,
351 : h_wire,
352 : exchange_url,
353 : timeout,
354 : cb,
355 : cb_cls);
356 : }
357 :
358 :
359 : void
360 0 : TALER_MERCHANT_kyc_get_cancel (
361 : struct TALER_MERCHANT_KycGetHandle *kyc)
362 : {
363 0 : if (NULL != kyc->job)
364 0 : GNUNET_CURL_job_cancel (kyc->job);
365 0 : GNUNET_free (kyc->url);
366 0 : GNUNET_free (kyc);
367 0 : }
|