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_update_aml_officer.c
19 : * @brief functions to update AML officer status
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_ManagementUpdateAmlOfficer
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_ManagementUpdateAmlOfficerCallback 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 4 : handle_update_aml_officer_finished (void *cls,
78 : long response_code,
79 : const void *response)
80 : {
81 4 : struct TALER_EXCHANGE_ManagementUpdateAmlOfficer *wh = cls;
82 4 : const json_t *json = response;
83 4 : struct TALER_EXCHANGE_ManagementUpdateAmlOfficerResponse uar = {
84 4 : .hr.http_status = (unsigned int) response_code,
85 : .hr.reply = json
86 : };
87 :
88 4 : wh->job = NULL;
89 4 : switch (response_code)
90 : {
91 0 : case 0:
92 : /* no reply */
93 0 : uar.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
94 0 : uar.hr.hint = "server offline?";
95 0 : break;
96 4 : case MHD_HTTP_NO_CONTENT:
97 4 : break;
98 0 : case MHD_HTTP_FORBIDDEN:
99 0 : uar.hr.ec = TALER_JSON_get_error_code (json);
100 0 : uar.hr.hint = TALER_JSON_get_error_hint (json);
101 0 : 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 : uar.hr.ec = TALER_JSON_get_error_code (json);
109 0 : uar.hr.hint = TALER_JSON_get_error_hint (json);
110 : }
111 : else
112 : {
113 0 : uar.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
114 0 : uar.hr.hint = TALER_ErrorCode_get_hint (uar.hr.ec);
115 : }
116 0 : break;
117 0 : case MHD_HTTP_CONFLICT:
118 0 : uar.hr.ec = TALER_JSON_get_error_code (json);
119 0 : uar.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 : uar.hr.ec = TALER_JSON_get_error_code (json);
125 0 : uar.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 update AML officer\n",
128 : (unsigned int) response_code,
129 : (int) uar.hr.ec);
130 0 : break;
131 : }
132 4 : if (NULL != wh->cb)
133 : {
134 4 : wh->cb (wh->cb_cls,
135 : &uar);
136 4 : wh->cb = NULL;
137 : }
138 4 : TALER_EXCHANGE_management_update_aml_officer_cancel (wh);
139 4 : }
140 :
141 :
142 : struct TALER_EXCHANGE_ManagementUpdateAmlOfficer *
143 4 : TALER_EXCHANGE_management_update_aml_officer (
144 : struct GNUNET_CURL_Context *ctx,
145 : const char *url,
146 : const struct TALER_AmlOfficerPublicKeyP *officer_pub,
147 : const char *officer_name,
148 : struct GNUNET_TIME_Timestamp change_date,
149 : bool is_active,
150 : bool read_only,
151 : const struct TALER_MasterSignatureP *master_sig,
152 : TALER_EXCHANGE_ManagementUpdateAmlOfficerCallback cb,
153 : void *cb_cls)
154 : {
155 : struct TALER_EXCHANGE_ManagementUpdateAmlOfficer *wh;
156 : CURL *eh;
157 : json_t *body;
158 :
159 4 : wh = GNUNET_new (struct TALER_EXCHANGE_ManagementUpdateAmlOfficer);
160 4 : wh->cb = cb;
161 4 : wh->cb_cls = cb_cls;
162 4 : wh->ctx = ctx;
163 4 : wh->url = TALER_url_join (url,
164 : "management/aml-officers",
165 : NULL);
166 4 : if (NULL == wh->url)
167 : {
168 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
169 : "Could not construct request URL.\n");
170 0 : GNUNET_free (wh);
171 0 : return NULL;
172 : }
173 4 : body = GNUNET_JSON_PACK (
174 : GNUNET_JSON_pack_string ("officer_name",
175 : officer_name),
176 : GNUNET_JSON_pack_data_auto ("officer_pub",
177 : officer_pub),
178 : GNUNET_JSON_pack_data_auto ("master_sig",
179 : master_sig),
180 : GNUNET_JSON_pack_bool ("is_active",
181 : is_active),
182 : GNUNET_JSON_pack_bool ("read_only",
183 : read_only),
184 : GNUNET_JSON_pack_timestamp ("change_date",
185 : change_date));
186 4 : eh = TALER_EXCHANGE_curl_easy_get_ (wh->url);
187 8 : if ( (NULL == eh) ||
188 : (GNUNET_OK !=
189 4 : TALER_curl_easy_post (&wh->post_ctx,
190 : eh,
191 : body)) )
192 : {
193 0 : GNUNET_break (0);
194 0 : if (NULL != eh)
195 0 : curl_easy_cleanup (eh);
196 0 : json_decref (body);
197 0 : GNUNET_free (wh->url);
198 0 : GNUNET_free (wh);
199 0 : return NULL;
200 : }
201 4 : json_decref (body);
202 4 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 : "Requesting URL '%s'\n",
204 : wh->url);
205 8 : wh->job = GNUNET_CURL_job_add2 (ctx,
206 : eh,
207 4 : wh->post_ctx.headers,
208 : &handle_update_aml_officer_finished,
209 : wh);
210 4 : if (NULL == wh->job)
211 : {
212 0 : TALER_EXCHANGE_management_update_aml_officer_cancel (wh);
213 0 : return NULL;
214 : }
215 4 : return wh;
216 : }
217 :
218 :
219 : void
220 4 : TALER_EXCHANGE_management_update_aml_officer_cancel (
221 : struct TALER_EXCHANGE_ManagementUpdateAmlOfficer *wh)
222 : {
223 4 : if (NULL != wh->job)
224 : {
225 0 : GNUNET_CURL_job_cancel (wh->job);
226 0 : wh->job = NULL;
227 : }
228 4 : TALER_curl_easy_post_finished (&wh->post_ctx);
229 4 : GNUNET_free (wh->url);
230 4 : GNUNET_free (wh);
231 4 : }
|