Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 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_add_partner.c
19 : * @brief functions to add an partner by an AML officer
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_ManagementAddPartner
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_ManagementAddPartnerCallback 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 POST /management/partners request.
71 : *
72 : * @param cls the `struct TALER_EXCHANGE_ManagementAddPartner *`
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 0 : handle_add_partner_finished (void *cls,
78 : long response_code,
79 : const void *response)
80 : {
81 0 : struct TALER_EXCHANGE_ManagementAddPartner *wh = cls;
82 0 : const json_t *json = response;
83 0 : struct TALER_EXCHANGE_ManagementAddPartnerResponse apr = {
84 0 : .hr.http_status = (unsigned int) response_code,
85 : .hr.reply = json
86 : };
87 :
88 0 : wh->job = NULL;
89 0 : switch (response_code)
90 : {
91 0 : case 0:
92 : /* no reply */
93 0 : apr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
94 0 : apr.hr.hint = "server offline?";
95 0 : break;
96 0 : case MHD_HTTP_NO_CONTENT:
97 0 : break;
98 0 : case MHD_HTTP_FORBIDDEN:
99 0 : apr.hr.ec = TALER_JSON_get_error_code (json);
100 0 : apr.hr.hint = TALER_JSON_get_error_hint (json);
101 0 : break;
102 0 : case MHD_HTTP_CONFLICT:
103 0 : apr.hr.ec = TALER_JSON_get_error_code (json);
104 0 : apr.hr.hint = TALER_JSON_get_error_hint (json);
105 0 : break;
106 0 : default:
107 : /* unexpected response code */
108 0 : GNUNET_break_op (0);
109 0 : apr.hr.ec = TALER_JSON_get_error_code (json);
110 0 : apr.hr.hint = TALER_JSON_get_error_hint (json);
111 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
112 : "Unexpected response code %u/%d for adding exchange partner\n",
113 : (unsigned int) response_code,
114 : (int) apr.hr.ec);
115 0 : break;
116 : }
117 0 : if (NULL != wh->cb)
118 : {
119 0 : wh->cb (wh->cb_cls,
120 : &apr);
121 0 : wh->cb = NULL;
122 : }
123 0 : TALER_EXCHANGE_management_add_partner_cancel (wh);
124 0 : }
125 :
126 :
127 : struct TALER_EXCHANGE_ManagementAddPartner *
128 0 : TALER_EXCHANGE_management_add_partner (
129 : struct GNUNET_CURL_Context *ctx,
130 : const char *url,
131 : const struct TALER_MasterPublicKeyP *partner_pub,
132 : struct GNUNET_TIME_Timestamp start_date,
133 : struct GNUNET_TIME_Timestamp end_date,
134 : struct GNUNET_TIME_Relative wad_frequency,
135 : const struct TALER_Amount *wad_fee,
136 : const char *partner_base_url,
137 : const struct TALER_MasterSignatureP *master_sig,
138 : TALER_EXCHANGE_ManagementAddPartnerCallback cb,
139 : void *cb_cls)
140 : {
141 : struct TALER_EXCHANGE_ManagementAddPartner *wh;
142 : CURL *eh;
143 : json_t *body;
144 :
145 0 : wh = GNUNET_new (struct TALER_EXCHANGE_ManagementAddPartner);
146 0 : wh->cb = cb;
147 0 : wh->cb_cls = cb_cls;
148 0 : wh->ctx = ctx;
149 0 : wh->url = TALER_url_join (url,
150 : "management/partners",
151 : NULL);
152 0 : if (NULL == wh->url)
153 : {
154 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155 : "Could not construct request URL.\n");
156 0 : GNUNET_free (wh);
157 0 : return NULL;
158 : }
159 0 : body = GNUNET_JSON_PACK (
160 : GNUNET_JSON_pack_string ("partner_base_url",
161 : partner_base_url),
162 : GNUNET_JSON_pack_timestamp ("start_date",
163 : start_date),
164 : GNUNET_JSON_pack_timestamp ("end_date",
165 : end_date),
166 : GNUNET_JSON_pack_time_rel ("wad_frequency",
167 : wad_frequency),
168 : GNUNET_JSON_pack_data_auto ("partner_pub",
169 : &partner_pub),
170 : GNUNET_JSON_pack_data_auto ("master_sig",
171 : &master_sig),
172 : TALER_JSON_pack_amount ("wad_fee",
173 : wad_fee)
174 : );
175 0 : eh = TALER_EXCHANGE_curl_easy_get_ (wh->url);
176 0 : if ( (NULL == eh) ||
177 : (GNUNET_OK !=
178 0 : TALER_curl_easy_post (&wh->post_ctx,
179 : eh,
180 : body)) )
181 : {
182 0 : GNUNET_break (0);
183 0 : if (NULL != eh)
184 0 : curl_easy_cleanup (eh);
185 0 : json_decref (body);
186 0 : GNUNET_free (wh->url);
187 0 : return NULL;
188 : }
189 0 : json_decref (body);
190 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
191 : "Requesting URL '%s'\n",
192 : wh->url);
193 0 : wh->job = GNUNET_CURL_job_add2 (ctx,
194 : eh,
195 0 : wh->post_ctx.headers,
196 : &handle_add_partner_finished,
197 : wh);
198 0 : if (NULL == wh->job)
199 : {
200 0 : TALER_EXCHANGE_management_add_partner_cancel (wh);
201 0 : return NULL;
202 : }
203 0 : return wh;
204 : }
205 :
206 :
207 : void
208 0 : TALER_EXCHANGE_management_add_partner_cancel (
209 : struct TALER_EXCHANGE_ManagementAddPartner *wh)
210 : {
211 0 : if (NULL != wh->job)
212 : {
213 0 : GNUNET_CURL_job_cancel (wh->job);
214 0 : wh->job = NULL;
215 : }
216 0 : TALER_curl_easy_post_finished (&wh->post_ctx);
217 0 : GNUNET_free (wh->url);
218 0 : GNUNET_free (wh);
219 0 : }
|