Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2015-2023 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_management_wire_enable.c
19 : * @brief functions to enable an exchange wire method / bank account
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include "taler_json_lib.h"
24 : #include <gnunet/gnunet_curl_lib.h>
25 : #include <microhttpd.h>
26 : #include "taler_exchange_service.h"
27 : #include "exchange_api_curl_defaults.h"
28 : #include "taler_signatures.h"
29 : #include "taler_curl_lib.h"
30 : #include "taler_json_lib.h"
31 :
32 :
33 : struct TALER_EXCHANGE_ManagementWireEnableHandle
34 : {
35 :
36 : /**
37 : * The url for this request.
38 : */
39 : char *url;
40 :
41 : /**
42 : * Minor context that holds body and headers.
43 : */
44 : struct TALER_CURL_PostContext post_ctx;
45 :
46 : /**
47 : * Handle for the request.
48 : */
49 : struct GNUNET_CURL_Job *job;
50 :
51 : /**
52 : * Function to call with the result.
53 : */
54 : TALER_EXCHANGE_ManagementWireEnableCallback cb;
55 :
56 : /**
57 : * Closure for @a cb.
58 : */
59 : void *cb_cls;
60 :
61 : /**
62 : * Reference to the execution context.
63 : */
64 : struct GNUNET_CURL_Context *ctx;
65 : };
66 :
67 :
68 : /**
69 : * Function called when we're done processing the
70 : * HTTP /management/wire request.
71 : *
72 : * @param cls the `struct TALER_EXCHANGE_ManagementAuditorEnableHandle *`
73 : * @param response_code HTTP response code, 0 on error
74 : * @param response response body, NULL if not in JSON
75 : */
76 : static void
77 27 : handle_auditor_enable_finished (void *cls,
78 : long response_code,
79 : const void *response)
80 : {
81 27 : struct TALER_EXCHANGE_ManagementWireEnableHandle *wh = cls;
82 27 : const json_t *json = response;
83 27 : struct TALER_EXCHANGE_ManagementWireEnableResponse wer = {
84 27 : .hr.http_status = (unsigned int) response_code,
85 : .hr.reply = json
86 : };
87 :
88 27 : wh->job = NULL;
89 27 : switch (response_code)
90 : {
91 0 : case 0:
92 : /* no reply */
93 0 : wer.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
94 0 : wer.hr.hint = "server offline?";
95 0 : break;
96 25 : case MHD_HTTP_NO_CONTENT:
97 25 : break;
98 2 : case MHD_HTTP_FORBIDDEN:
99 2 : wer.hr.ec = TALER_JSON_get_error_code (json);
100 2 : wer.hr.hint = TALER_JSON_get_error_hint (json);
101 2 : break;
102 0 : case MHD_HTTP_NOT_FOUND:
103 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
104 : "Server did not find handler at `%s'. Did you configure the correct exchange base URL?\n",
105 : wh->url);
106 0 : if (NULL != json)
107 : {
108 0 : wer.hr.ec = TALER_JSON_get_error_code (json);
109 0 : wer.hr.hint = TALER_JSON_get_error_hint (json);
110 : }
111 : else
112 : {
113 0 : wer.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
114 0 : wer.hr.hint = TALER_ErrorCode_get_hint (wer.hr.ec);
115 : }
116 0 : break;
117 0 : case MHD_HTTP_CONFLICT:
118 0 : wer.hr.ec = TALER_JSON_get_error_code (json);
119 0 : wer.hr.hint = TALER_JSON_get_error_hint (json);
120 0 : break;
121 0 : default:
122 : /* unexpected response code */
123 0 : GNUNET_break_op (0);
124 0 : wer.hr.ec = TALER_JSON_get_error_code (json);
125 0 : wer.hr.hint = TALER_JSON_get_error_hint (json);
126 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
127 : "Unexpected response code %u/%d for exchange management enable wire\n",
128 : (unsigned int) response_code,
129 : (int) wer.hr.ec);
130 0 : break;
131 : }
132 27 : if (NULL != wh->cb)
133 : {
134 27 : wh->cb (wh->cb_cls,
135 : &wer);
136 27 : wh->cb = NULL;
137 : }
138 27 : TALER_EXCHANGE_management_enable_wire_cancel (wh);
139 27 : }
140 :
141 :
142 : struct TALER_EXCHANGE_ManagementWireEnableHandle *
143 27 : TALER_EXCHANGE_management_enable_wire (
144 : struct GNUNET_CURL_Context *ctx,
145 : const char *url,
146 : const struct TALER_FullPayto payto_uri,
147 : const char *conversion_url,
148 : const json_t *debit_restrictions,
149 : const json_t *credit_restrictions,
150 : struct GNUNET_TIME_Timestamp validity_start,
151 : const struct TALER_MasterSignatureP *master_sig1,
152 : const struct TALER_MasterSignatureP *master_sig2,
153 : const char *bank_label,
154 : int64_t priority,
155 : TALER_EXCHANGE_ManagementWireEnableCallback cb,
156 : void *cb_cls)
157 : {
158 : struct TALER_EXCHANGE_ManagementWireEnableHandle *wh;
159 : CURL *eh;
160 : json_t *body;
161 :
162 : {
163 27 : char *msg = TALER_payto_validate (payto_uri);
164 :
165 27 : if (NULL != msg)
166 : {
167 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168 : "payto URI is malformed: %s\n",
169 : msg);
170 0 : GNUNET_free (msg);
171 0 : return NULL;
172 : }
173 : }
174 27 : wh = GNUNET_new (struct TALER_EXCHANGE_ManagementWireEnableHandle);
175 27 : wh->cb = cb;
176 27 : wh->cb_cls = cb_cls;
177 27 : wh->ctx = ctx;
178 27 : wh->url = TALER_url_join (url,
179 : "management/wire",
180 : NULL);
181 27 : if (NULL == wh->url)
182 : {
183 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
184 : "Could not construct request URL.\n");
185 0 : GNUNET_free (wh);
186 0 : return NULL;
187 : }
188 27 : body = GNUNET_JSON_PACK (
189 : TALER_JSON_pack_full_payto ("payto_uri",
190 : payto_uri),
191 : GNUNET_JSON_pack_array_incref ("debit_restrictions",
192 : (json_t *) debit_restrictions),
193 : GNUNET_JSON_pack_array_incref ("credit_restrictions",
194 : (json_t *) credit_restrictions),
195 : GNUNET_JSON_pack_allow_null (
196 : GNUNET_JSON_pack_string ("conversion_url",
197 : conversion_url)),
198 : GNUNET_JSON_pack_allow_null (
199 : GNUNET_JSON_pack_string ("bank_label",
200 : bank_label)),
201 : GNUNET_JSON_pack_int64 ("priority",
202 : priority),
203 : GNUNET_JSON_pack_data_auto ("master_sig_add",
204 : master_sig1),
205 : GNUNET_JSON_pack_data_auto ("master_sig_wire",
206 : master_sig2),
207 : GNUNET_JSON_pack_timestamp ("validity_start",
208 : validity_start));
209 27 : eh = TALER_EXCHANGE_curl_easy_get_ (wh->url);
210 54 : if ( (NULL == eh) ||
211 : (GNUNET_OK !=
212 27 : TALER_curl_easy_post (&wh->post_ctx,
213 : eh,
214 : body)) )
215 : {
216 0 : GNUNET_break (0);
217 0 : if (NULL != eh)
218 0 : curl_easy_cleanup (eh);
219 0 : json_decref (body);
220 0 : GNUNET_free (wh->url);
221 0 : GNUNET_free (wh);
222 0 : return NULL;
223 : }
224 27 : json_decref (body);
225 27 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 : "Requesting URL '%s'\n",
227 : wh->url);
228 54 : wh->job = GNUNET_CURL_job_add2 (ctx,
229 : eh,
230 27 : wh->post_ctx.headers,
231 : &handle_auditor_enable_finished,
232 : wh);
233 27 : if (NULL == wh->job)
234 : {
235 0 : TALER_EXCHANGE_management_enable_wire_cancel (wh);
236 0 : return NULL;
237 : }
238 27 : return wh;
239 : }
240 :
241 :
242 : void
243 27 : TALER_EXCHANGE_management_enable_wire_cancel (
244 : struct TALER_EXCHANGE_ManagementWireEnableHandle *wh)
245 : {
246 27 : if (NULL != wh->job)
247 : {
248 0 : GNUNET_CURL_job_cancel (wh->job);
249 0 : wh->job = NULL;
250 : }
251 27 : TALER_curl_easy_post_finished (&wh->post_ctx);
252 27 : GNUNET_free (wh->url);
253 27 : GNUNET_free (wh);
254 27 : }
|