Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2021-2023 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU General Public License as
7 : published by the Free Software Foundation; either version 3, or
8 : (at your option) any later version.
9 :
10 : TALER is distributed in the hope that it will be useful, but
11 : 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, see
17 : <http://www.gnu.org/licenses/>
18 : */
19 :
20 : /**
21 : * @file testing/testing_api_cmd_kyc_check_get.c
22 : * @brief Implement the testing CMDs for the /kyc_check/ GET operations.
23 : * @author Christian Grothoff
24 : */
25 : #include "platform.h"
26 : #include "taler_json_lib.h"
27 : #include <gnunet/gnunet_curl_lib.h>
28 : #include "taler_testing_lib.h"
29 :
30 : /**
31 : * State for a "track transaction" CMD.
32 : */
33 : struct KycCheckGetState
34 : {
35 :
36 : /**
37 : * Set to the KYC URL *if* the exchange replied with
38 : * a request for KYC (#MHD_HTTP_ACCEPTED or #MHD_HTTP_OK).
39 : */
40 : struct TALER_AccountAccessTokenP access_token;
41 :
42 : /**
43 : * Handle to the "track transaction" pending operation.
44 : */
45 : struct TALER_EXCHANGE_KycCheckHandle *kwh;
46 :
47 : /**
48 : * Interpreter state.
49 : */
50 : struct TALER_TESTING_Interpreter *is;
51 :
52 : /**
53 : * Command to get a reserve private key from.
54 : */
55 : const char *payment_target_reference;
56 :
57 : /**
58 : * Command to get an account private key from.
59 : */
60 : const char *account_reference;
61 :
62 : /**
63 : * Expected HTTP response code.
64 : */
65 : unsigned int expected_response_code;
66 :
67 : /**
68 : * What are we waiting for when long-polling?
69 : */
70 : enum TALER_EXCHANGE_KycLongPollTarget lpt;
71 :
72 : };
73 :
74 :
75 : /**
76 : * Handle response to the command.
77 : *
78 : * @param cls closure.
79 : * @param ks GET KYC status response details
80 : */
81 : static void
82 14 : check_kyc_cb (void *cls,
83 : const struct TALER_EXCHANGE_KycStatus *ks)
84 : {
85 14 : struct KycCheckGetState *kcg = cls;
86 14 : struct TALER_TESTING_Interpreter *is = kcg->is;
87 :
88 14 : kcg->kwh = NULL;
89 14 : if (kcg->expected_response_code != ks->hr.http_status)
90 : {
91 0 : TALER_TESTING_unexpected_status (is,
92 : ks->hr.http_status,
93 : kcg->expected_response_code);
94 0 : return;
95 : }
96 14 : switch (ks->hr.http_status)
97 : {
98 3 : case MHD_HTTP_OK:
99 3 : kcg->access_token = ks->details.ok.access_token;
100 3 : break;
101 11 : case MHD_HTTP_ACCEPTED:
102 11 : kcg->access_token = ks->details.accepted.access_token;
103 11 : break;
104 0 : case MHD_HTTP_NO_CONTENT:
105 0 : break;
106 0 : default:
107 0 : GNUNET_break (0);
108 0 : break;
109 : }
110 14 : TALER_TESTING_interpreter_next (kcg->is);
111 : }
112 :
113 :
114 : /**
115 : * Run the command.
116 : *
117 : * @param cls closure.
118 : * @param cmd the command to execute.
119 : * @param is the interpreter state.
120 : */
121 : static void
122 14 : check_kyc_run (void *cls,
123 : const struct TALER_TESTING_Command *cmd,
124 : struct TALER_TESTING_Interpreter *is)
125 : {
126 14 : struct KycCheckGetState *kcg = cls;
127 : const struct TALER_TESTING_Command *res_cmd;
128 : const struct TALER_TESTING_Command *acc_cmd;
129 : const struct TALER_NormalizedPaytoHashP *h_payto;
130 : const union TALER_AccountPrivateKeyP *account_priv;
131 :
132 : (void) cmd;
133 14 : kcg->is = is;
134 14 : res_cmd = TALER_TESTING_interpreter_lookup_command (
135 : kcg->is,
136 : kcg->payment_target_reference);
137 14 : if (NULL == res_cmd)
138 : {
139 0 : GNUNET_break (0);
140 0 : TALER_TESTING_interpreter_fail (kcg->is);
141 0 : return;
142 : }
143 14 : acc_cmd = TALER_TESTING_interpreter_lookup_command (
144 : kcg->is,
145 : kcg->account_reference);
146 14 : if (NULL == acc_cmd)
147 : {
148 0 : GNUNET_break (0);
149 0 : TALER_TESTING_interpreter_fail (kcg->is);
150 0 : return;
151 : }
152 14 : if (GNUNET_OK !=
153 14 : TALER_TESTING_get_trait_h_normalized_payto (
154 : res_cmd,
155 : &h_payto))
156 : {
157 0 : GNUNET_break (0);
158 0 : TALER_TESTING_interpreter_fail (kcg->is);
159 0 : return;
160 : }
161 14 : if (GNUNET_OK !=
162 14 : TALER_TESTING_get_trait_account_priv (acc_cmd,
163 : &account_priv))
164 : {
165 0 : GNUNET_break (0);
166 0 : TALER_TESTING_interpreter_fail (kcg->is);
167 0 : return;
168 : }
169 14 : if (0 == h_payto)
170 : {
171 0 : GNUNET_break (0);
172 0 : TALER_TESTING_interpreter_fail (kcg->is);
173 0 : return;
174 : }
175 14 : kcg->kwh = TALER_EXCHANGE_kyc_check (
176 : TALER_TESTING_interpreter_get_context (is),
177 : TALER_TESTING_get_exchange_url (is),
178 : h_payto,
179 : account_priv,
180 14 : kcg->lpt,
181 : 0,
182 14 : TALER_EXCHANGE_KLPT_NONE == kcg->lpt
183 : ? GNUNET_TIME_UNIT_ZERO
184 14 : : GNUNET_TIME_UNIT_MINUTES,
185 : &check_kyc_cb,
186 : kcg);
187 14 : GNUNET_assert (NULL != kcg->kwh);
188 : }
189 :
190 :
191 : /**
192 : * Cleanup the state from a "track transaction" CMD, and possibly
193 : * cancel a operation thereof.
194 : *
195 : * @param cls closure.
196 : * @param cmd the command which is being cleaned up.
197 : */
198 : static void
199 14 : check_kyc_cleanup (void *cls,
200 : const struct TALER_TESTING_Command *cmd)
201 : {
202 14 : struct KycCheckGetState *kcg = cls;
203 :
204 14 : if (NULL != kcg->kwh)
205 : {
206 0 : TALER_TESTING_command_incomplete (kcg->is,
207 : cmd->label);
208 0 : TALER_EXCHANGE_kyc_check_cancel (kcg->kwh);
209 0 : kcg->kwh = NULL;
210 : }
211 14 : GNUNET_free (kcg);
212 14 : }
213 :
214 :
215 : /**
216 : * Offer internal data from a "check KYC" CMD.
217 : *
218 : * @param cls closure.
219 : * @param[out] ret result (could be anything).
220 : * @param trait name of the trait.
221 : * @param index index number of the object to offer.
222 : * @return #GNUNET_OK on success.
223 : */
224 : static enum GNUNET_GenericReturnValue
225 36 : check_kyc_traits (void *cls,
226 : const void **ret,
227 : const char *trait,
228 : unsigned int index)
229 : {
230 36 : struct KycCheckGetState *kcg = cls;
231 : struct TALER_TESTING_Trait traits[] = {
232 36 : TALER_TESTING_make_trait_account_access_token (&kcg->access_token),
233 36 : TALER_TESTING_trait_end ()
234 : };
235 :
236 36 : return TALER_TESTING_get_trait (traits,
237 : ret,
238 : trait,
239 : index);
240 : }
241 :
242 :
243 : struct TALER_TESTING_Command
244 14 : TALER_TESTING_cmd_check_kyc_get (
245 : const char *label,
246 : const char *payment_target_reference,
247 : const char *account_reference,
248 : enum TALER_EXCHANGE_KycLongPollTarget lpt,
249 : unsigned int expected_response_code)
250 : {
251 : struct KycCheckGetState *kcg;
252 :
253 14 : kcg = GNUNET_new (struct KycCheckGetState);
254 14 : kcg->payment_target_reference = payment_target_reference;
255 14 : kcg->account_reference = account_reference;
256 14 : kcg->expected_response_code = expected_response_code;
257 14 : kcg->lpt = lpt;
258 : {
259 14 : struct TALER_TESTING_Command cmd = {
260 : .cls = kcg,
261 : .label = label,
262 : .run = &check_kyc_run,
263 : .cleanup = &check_kyc_cleanup,
264 : .traits = &check_kyc_traits
265 : };
266 :
267 14 : return cmd;
268 : }
269 : }
270 :
271 :
272 : /* end of testing_api_cmd_kyc_check_get.c */
|