exchange_api_management_auditor_enable.c (6691B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2015-2021 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_auditor_enable.c 19 * @brief functions to enable an auditor 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 "exchange_api_curl_defaults.h" 28 #include "taler/taler_signatures.h" 29 #include "taler/taler_curl_lib.h" 30 #include "taler/taler_json_lib.h" 31 32 33 /** 34 * @brief Handle for a POST /management/auditors request. 35 */ 36 struct TALER_EXCHANGE_ManagementAuditorEnableHandle 37 { 38 39 /** 40 * The url for this request. 41 */ 42 char *url; 43 44 /** 45 * Minor context that holds body and headers. 46 */ 47 struct TALER_CURL_PostContext post_ctx; 48 49 /** 50 * Handle for the request. 51 */ 52 struct GNUNET_CURL_Job *job; 53 54 /** 55 * Function to call with the result. 56 */ 57 TALER_EXCHANGE_ManagementAuditorEnableCallback cb; 58 59 /** 60 * Closure for @a cb. 61 */ 62 void *cb_cls; 63 64 /** 65 * Reference to the execution context. 66 */ 67 struct GNUNET_CURL_Context *ctx; 68 }; 69 70 71 /** 72 * Function called when we're done processing the 73 * HTTP POST /management/auditors request. 74 * 75 * @param cls the `struct TALER_EXCHANGE_ManagementAuditorEnableHandle *` 76 * @param response_code HTTP response code, 0 on error 77 * @param response response body, NULL if not in JSON 78 */ 79 static void 80 handle_auditor_enable_finished (void *cls, 81 long response_code, 82 const void *response) 83 { 84 struct TALER_EXCHANGE_ManagementAuditorEnableHandle *ah = cls; 85 const json_t *json = response; 86 struct TALER_EXCHANGE_ManagementAuditorEnableResponse auditor_enable_response 87 = { 88 .hr.http_status = (unsigned int) response_code, 89 .hr.reply = json 90 }; 91 92 ah->job = NULL; 93 switch (response_code) 94 { 95 case MHD_HTTP_NO_CONTENT: 96 break; 97 case MHD_HTTP_FORBIDDEN: 98 auditor_enable_response.hr.ec = TALER_JSON_get_error_code (json); 99 auditor_enable_response.hr.hint = TALER_JSON_get_error_hint (json); 100 break; 101 case MHD_HTTP_NOT_FOUND: 102 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 103 "Server did not find handler at `%s'. Did you configure the correct exchange base URL?\n", 104 ah->url); 105 if (NULL != json) 106 { 107 auditor_enable_response.hr.ec = TALER_JSON_get_error_code (json); 108 auditor_enable_response.hr.hint = TALER_JSON_get_error_hint (json); 109 } 110 else 111 { 112 auditor_enable_response.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 113 auditor_enable_response.hr.hint = TALER_ErrorCode_get_hint ( 114 auditor_enable_response.hr.ec); 115 } 116 break; 117 case MHD_HTTP_CONFLICT: 118 auditor_enable_response.hr.ec = TALER_JSON_get_error_code (json); 119 auditor_enable_response.hr.hint = TALER_JSON_get_error_hint (json); 120 break; 121 default: 122 /* unexpected response code */ 123 GNUNET_break_op (0); 124 auditor_enable_response.hr.ec = TALER_JSON_get_error_code (json); 125 auditor_enable_response.hr.hint = TALER_JSON_get_error_hint (json); 126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 127 "Unexpected response code %u/%d for exchange management auditor enable\n", 128 (unsigned int) response_code, 129 (int) auditor_enable_response.hr.ec); 130 break; 131 } 132 if (NULL != ah->cb) 133 { 134 ah->cb (ah->cb_cls, 135 &auditor_enable_response); 136 ah->cb = NULL; 137 } 138 TALER_EXCHANGE_management_enable_auditor_cancel (ah); 139 } 140 141 142 struct TALER_EXCHANGE_ManagementAuditorEnableHandle * 143 TALER_EXCHANGE_management_enable_auditor ( 144 struct GNUNET_CURL_Context *ctx, 145 const char *url, 146 const struct TALER_AuditorPublicKeyP *auditor_pub, 147 const char *auditor_url, 148 const char *auditor_name, 149 struct GNUNET_TIME_Timestamp validity_start, 150 const struct TALER_MasterSignatureP *master_sig, 151 TALER_EXCHANGE_ManagementAuditorEnableCallback cb, 152 void *cb_cls) 153 { 154 struct TALER_EXCHANGE_ManagementAuditorEnableHandle *ah; 155 CURL *eh; 156 json_t *body; 157 158 ah = GNUNET_new (struct TALER_EXCHANGE_ManagementAuditorEnableHandle); 159 ah->cb = cb; 160 ah->cb_cls = cb_cls; 161 ah->ctx = ctx; 162 ah->url = TALER_url_join (url, 163 "management/auditors", 164 NULL); 165 if (NULL == ah->url) 166 { 167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 168 "Could not construct request URL.\n"); 169 GNUNET_free (ah); 170 return NULL; 171 } 172 body = GNUNET_JSON_PACK ( 173 GNUNET_JSON_pack_string ("auditor_url", 174 auditor_url), 175 GNUNET_JSON_pack_string ("auditor_name", 176 auditor_name), 177 GNUNET_JSON_pack_data_auto ("auditor_pub", 178 auditor_pub), 179 GNUNET_JSON_pack_data_auto ("master_sig", 180 master_sig), 181 GNUNET_JSON_pack_timestamp ("validity_start", 182 validity_start)); 183 eh = TALER_EXCHANGE_curl_easy_get_ (ah->url); 184 if ( (NULL == eh) || 185 (GNUNET_OK != 186 TALER_curl_easy_post (&ah->post_ctx, 187 eh, 188 body)) ) 189 { 190 GNUNET_break (0); 191 json_decref (body); 192 if (NULL != eh) 193 curl_easy_cleanup (eh); 194 GNUNET_free (ah->url); 195 return NULL; 196 } 197 json_decref (body); 198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 199 "Requesting URL '%s'\n", 200 ah->url); 201 ah->job = GNUNET_CURL_job_add2 (ctx, 202 eh, 203 ah->post_ctx.headers, 204 &handle_auditor_enable_finished, 205 ah); 206 if (NULL == ah->job) 207 { 208 TALER_EXCHANGE_management_enable_auditor_cancel (ah); 209 return NULL; 210 } 211 return ah; 212 } 213 214 215 void 216 TALER_EXCHANGE_management_enable_auditor_cancel ( 217 struct TALER_EXCHANGE_ManagementAuditorEnableHandle *ah) 218 { 219 if (NULL != ah->job) 220 { 221 GNUNET_CURL_job_cancel (ah->job); 222 ah->job = NULL; 223 } 224 TALER_curl_easy_post_finished (&ah->post_ctx); 225 GNUNET_free (ah->url); 226 GNUNET_free (ah); 227 }