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