exchange_api_post-management-aml-officers.c (8297B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2023-2026 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_post-management-aml-officers.c 19 * @brief functions to update AML officer status via POST /management/aml-officers 20 * @author Christian Grothoff 21 */ 22 #include "taler/platform.h" 23 #include "taler/taler_json_lib.h" 24 #include <gnunet/gnunet_curl_lib.h> 25 #include <microhttpd.h> 26 #include "taler/taler_exchange_service.h" 27 #include "taler/taler-exchange/post-management-aml-officers.h" 28 #include "exchange_api_curl_defaults.h" 29 #include "taler/taler_signatures.h" 30 #include "taler/taler_curl_lib.h" 31 32 33 /** 34 * Handle for a POST /management/aml-officers request. 35 */ 36 struct TALER_EXCHANGE_PostManagementAmlOfficersHandle 37 { 38 39 /** 40 * The base URL for this request. 41 */ 42 char *base_url; 43 44 /** 45 * The full URL for this request, set during _start. 46 */ 47 char *url; 48 49 /** 50 * Minor context that holds body and headers. 51 */ 52 struct TALER_CURL_PostContext post_ctx; 53 54 /** 55 * Handle for the request. 56 */ 57 struct GNUNET_CURL_Job *job; 58 59 /** 60 * Function to call with the result. 61 */ 62 TALER_EXCHANGE_PostManagementAmlOfficersCallback cb; 63 64 /** 65 * Closure for @a cb. 66 */ 67 TALER_EXCHANGE_POST_MANAGEMENT_AML_OFFICERS_RESULT_CLOSURE *cb_cls; 68 69 /** 70 * Reference to the execution context. 71 */ 72 struct GNUNET_CURL_Context *ctx; 73 74 /** 75 * Public key of the officer. 76 */ 77 struct TALER_AmlOfficerPublicKeyP officer_pub; 78 79 /** 80 * Name of the officer. 81 */ 82 char *officer_name; 83 84 /** 85 * When to affect the status change. 86 */ 87 struct GNUNET_TIME_Timestamp change_date; 88 89 /** 90 * Is the officer active? 91 */ 92 bool is_active; 93 94 /** 95 * Is the officer read-only? 96 */ 97 bool read_only; 98 99 /** 100 * Master signature affirming the change. 101 */ 102 struct TALER_MasterSignatureP master_sig; 103 104 }; 105 106 107 /** 108 * Function called when we're done processing the 109 * HTTP POST /management/aml-officers request. 110 * 111 * @param cls the `struct TALER_EXCHANGE_PostManagementAmlOfficersHandle` 112 * @param response_code HTTP response code, 0 on error 113 * @param response response body, NULL if not in JSON 114 */ 115 static void 116 handle_post_aml_officer_finished (void *cls, 117 long response_code, 118 const void *response) 119 { 120 struct TALER_EXCHANGE_PostManagementAmlOfficersHandle *pmaoh = cls; 121 const json_t *json = response; 122 struct TALER_EXCHANGE_PostManagementAmlOfficersResponse res = { 123 .hr.http_status = (unsigned int) response_code, 124 .hr.reply = json 125 }; 126 127 pmaoh->job = NULL; 128 switch (response_code) 129 { 130 case 0: 131 /* no reply */ 132 res.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 133 res.hr.hint = "server offline?"; 134 break; 135 case MHD_HTTP_NO_CONTENT: 136 break; 137 case MHD_HTTP_FORBIDDEN: 138 res.hr.ec = TALER_JSON_get_error_code (json); 139 res.hr.hint = TALER_JSON_get_error_hint (json); 140 break; 141 case MHD_HTTP_NOT_FOUND: 142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 143 "Server did not find handler at `%s'. Did you configure the correct exchange base URL?\n", 144 pmaoh->url); 145 if (NULL != json) 146 { 147 res.hr.ec = TALER_JSON_get_error_code (json); 148 res.hr.hint = TALER_JSON_get_error_hint (json); 149 } 150 else 151 { 152 res.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 153 res.hr.hint = TALER_ErrorCode_get_hint (res.hr.ec); 154 } 155 break; 156 case MHD_HTTP_CONFLICT: 157 res.hr.ec = TALER_JSON_get_error_code (json); 158 res.hr.hint = TALER_JSON_get_error_hint (json); 159 break; 160 default: 161 /* unexpected response code */ 162 GNUNET_break_op (0); 163 res.hr.ec = TALER_JSON_get_error_code (json); 164 res.hr.hint = TALER_JSON_get_error_hint (json); 165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 166 "Unexpected response code %u/%d for exchange POST management AML officer\n", 167 (unsigned int) response_code, 168 (int) res.hr.ec); 169 break; 170 } 171 if (NULL != pmaoh->cb) 172 { 173 pmaoh->cb (pmaoh->cb_cls, 174 &res); 175 pmaoh->cb = NULL; 176 } 177 TALER_EXCHANGE_post_management_aml_officers_cancel (pmaoh); 178 } 179 180 181 struct TALER_EXCHANGE_PostManagementAmlOfficersHandle * 182 TALER_EXCHANGE_post_management_aml_officers_create ( 183 struct GNUNET_CURL_Context *ctx, 184 const char *url, 185 const struct TALER_AmlOfficerPublicKeyP *officer_pub, 186 const char *officer_name, 187 struct GNUNET_TIME_Timestamp change_date, 188 bool is_active, 189 bool read_only, 190 const struct TALER_MasterSignatureP *master_sig) 191 { 192 struct TALER_EXCHANGE_PostManagementAmlOfficersHandle *pmaoh; 193 194 pmaoh = GNUNET_new (struct TALER_EXCHANGE_PostManagementAmlOfficersHandle); 195 pmaoh->ctx = ctx; 196 pmaoh->base_url = GNUNET_strdup (url); 197 pmaoh->officer_pub = *officer_pub; 198 pmaoh->officer_name = GNUNET_strdup (officer_name); 199 pmaoh->change_date = change_date; 200 pmaoh->is_active = is_active; 201 pmaoh->read_only = read_only; 202 pmaoh->master_sig = *master_sig; 203 return pmaoh; 204 } 205 206 207 enum TALER_ErrorCode 208 TALER_EXCHANGE_post_management_aml_officers_start ( 209 struct TALER_EXCHANGE_PostManagementAmlOfficersHandle *pmaoh, 210 TALER_EXCHANGE_PostManagementAmlOfficersCallback cb, 211 TALER_EXCHANGE_POST_MANAGEMENT_AML_OFFICERS_RESULT_CLOSURE *cb_cls) 212 { 213 CURL *eh; 214 json_t *body; 215 216 pmaoh->cb = cb; 217 pmaoh->cb_cls = cb_cls; 218 pmaoh->url = TALER_url_join (pmaoh->base_url, 219 "management/aml-officers", 220 NULL); 221 if (NULL == pmaoh->url) 222 { 223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 224 "Could not construct request URL.\n"); 225 return TALER_EC_GENERIC_CONFIGURATION_INVALID; 226 } 227 body = GNUNET_JSON_PACK ( 228 GNUNET_JSON_pack_string ("officer_name", 229 pmaoh->officer_name), 230 GNUNET_JSON_pack_data_auto ("officer_pub", 231 &pmaoh->officer_pub), 232 GNUNET_JSON_pack_data_auto ("master_sig", 233 &pmaoh->master_sig), 234 GNUNET_JSON_pack_bool ("is_active", 235 pmaoh->is_active), 236 GNUNET_JSON_pack_bool ("read_only", 237 pmaoh->read_only), 238 GNUNET_JSON_pack_timestamp ("change_date", 239 pmaoh->change_date)); 240 eh = TALER_EXCHANGE_curl_easy_get_ (pmaoh->url); 241 if ( (NULL == eh) || 242 (GNUNET_OK != 243 TALER_curl_easy_post (&pmaoh->post_ctx, 244 eh, 245 body)) ) 246 { 247 GNUNET_break (0); 248 if (NULL != eh) 249 curl_easy_cleanup (eh); 250 json_decref (body); 251 GNUNET_free (pmaoh->url); 252 pmaoh->url = NULL; 253 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 254 } 255 json_decref (body); 256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 257 "Requesting URL '%s'\n", 258 pmaoh->url); 259 pmaoh->job = GNUNET_CURL_job_add2 (pmaoh->ctx, 260 eh, 261 pmaoh->post_ctx.headers, 262 &handle_post_aml_officer_finished, 263 pmaoh); 264 if (NULL == pmaoh->job) 265 { 266 TALER_curl_easy_post_finished (&pmaoh->post_ctx); 267 GNUNET_free (pmaoh->url); 268 pmaoh->url = NULL; 269 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 270 } 271 return TALER_EC_NONE; 272 } 273 274 275 void 276 TALER_EXCHANGE_post_management_aml_officers_cancel ( 277 struct TALER_EXCHANGE_PostManagementAmlOfficersHandle *pmaoh) 278 { 279 if (NULL != pmaoh->job) 280 { 281 GNUNET_CURL_job_cancel (pmaoh->job); 282 pmaoh->job = NULL; 283 } 284 TALER_curl_easy_post_finished (&pmaoh->post_ctx); 285 GNUNET_free (pmaoh->url); 286 GNUNET_free (pmaoh->officer_name); 287 GNUNET_free (pmaoh->base_url); 288 GNUNET_free (pmaoh); 289 } 290 291 292 /* end of exchange_api_post-management-aml-officers.c */