exchange_api_post-management-denominations-H_DENOM_PUB-revoke.c (7434B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2015-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-denominations-H_DENOM_PUB-revoke.c 19 * @brief functions to revoke an exchange denomination key 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 \ 28 "taler/taler-exchange/post-management-denominations-H_DENOM_PUB-revoke.h" 29 #include "exchange_api_curl_defaults.h" 30 #include "taler/taler_signatures.h" 31 #include "taler/taler_curl_lib.h" 32 33 34 /** 35 * @brief Handle for a POST /management/denominations/$H_DENOM_PUB/revoke request. 36 */ 37 struct TALER_EXCHANGE_PostManagementDenominationsRevokeHandle 38 { 39 40 /** 41 * The base URL for this request. 42 */ 43 char *base_url; 44 45 /** 46 * The full URL for this request, set during _start. 47 */ 48 char *url; 49 50 /** 51 * Minor context that holds body and headers. 52 */ 53 struct TALER_CURL_PostContext post_ctx; 54 55 /** 56 * Handle for the request. 57 */ 58 struct GNUNET_CURL_Job *job; 59 60 /** 61 * Function to call with the result. 62 */ 63 TALER_EXCHANGE_PostManagementDenominationsRevokeCallback cb; 64 65 /** 66 * Closure for @a cb. 67 */ 68 TALER_EXCHANGE_POST_MANAGEMENT_DENOMINATIONS_REVOKE_RESULT_CLOSURE *cb_cls; 69 70 /** 71 * Reference to the execution context. 72 */ 73 struct GNUNET_CURL_Context *ctx; 74 75 /** 76 * Hash of the denomination public key to revoke. 77 */ 78 struct TALER_DenominationHashP h_denom_pub; 79 80 /** 81 * Signature affirming the revocation. 82 */ 83 struct TALER_MasterSignatureP master_sig; 84 85 }; 86 87 88 /** 89 * Function called when we're done processing the 90 * HTTP POST /management/denominations/$H_DENOM_PUB/revoke request. 91 * 92 * @param cls the `struct TALER_EXCHANGE_PostManagementDenominationsRevokeHandle` 93 * @param response_code HTTP response code, 0 on error 94 * @param response response body, NULL if not in JSON 95 */ 96 static void 97 handle_denominations_revoke_finished (void *cls, 98 long response_code, 99 const void *response) 100 { 101 struct TALER_EXCHANGE_PostManagementDenominationsRevokeHandle *pmdrh = cls; 102 const json_t *json = response; 103 struct TALER_EXCHANGE_PostManagementDenominationsRevokeResponse res = { 104 .hr.http_status = (unsigned int) response_code, 105 .hr.reply = json 106 }; 107 108 pmdrh->job = NULL; 109 switch (response_code) 110 { 111 case 0: 112 /* no reply */ 113 res.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; 114 res.hr.hint = "server offline?"; 115 break; 116 case MHD_HTTP_NO_CONTENT: 117 break; 118 case MHD_HTTP_FORBIDDEN: 119 res.hr.ec = TALER_JSON_get_error_code (json); 120 res.hr.hint = TALER_JSON_get_error_hint (json); 121 break; 122 default: 123 /* unexpected response code */ 124 GNUNET_break_op (0); 125 res.hr.ec = TALER_JSON_get_error_code (json); 126 res.hr.hint = TALER_JSON_get_error_hint (json); 127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 128 "Unexpected response code %u/%d for exchange management revoke denomination\n", 129 (unsigned int) response_code, 130 (int) res.hr.ec); 131 break; 132 } 133 if (NULL != pmdrh->cb) 134 { 135 pmdrh->cb (pmdrh->cb_cls, 136 &res); 137 pmdrh->cb = NULL; 138 } 139 TALER_EXCHANGE_post_management_denominations_revoke_cancel (pmdrh); 140 } 141 142 143 struct TALER_EXCHANGE_PostManagementDenominationsRevokeHandle * 144 TALER_EXCHANGE_post_management_denominations_revoke_create ( 145 struct GNUNET_CURL_Context *ctx, 146 const char *url, 147 const struct TALER_DenominationHashP *h_denom_pub, 148 const struct TALER_MasterSignatureP *master_sig) 149 { 150 struct TALER_EXCHANGE_PostManagementDenominationsRevokeHandle *pmdrh; 151 152 pmdrh = GNUNET_new ( 153 struct TALER_EXCHANGE_PostManagementDenominationsRevokeHandle); 154 pmdrh->ctx = ctx; 155 pmdrh->base_url = GNUNET_strdup (url); 156 pmdrh->h_denom_pub = *h_denom_pub; 157 pmdrh->master_sig = *master_sig; 158 return pmdrh; 159 } 160 161 162 enum TALER_ErrorCode 163 TALER_EXCHANGE_post_management_denominations_revoke_start ( 164 struct TALER_EXCHANGE_PostManagementDenominationsRevokeHandle *pmdrh, 165 TALER_EXCHANGE_PostManagementDenominationsRevokeCallback cb, 166 TALER_EXCHANGE_POST_MANAGEMENT_DENOMINATIONS_REVOKE_RESULT_CLOSURE *cb_cls) 167 { 168 CURL *eh; 169 json_t *body; 170 171 pmdrh->cb = cb; 172 pmdrh->cb_cls = cb_cls; 173 { 174 char epub_str[sizeof (pmdrh->h_denom_pub) * 2]; 175 char arg_str[sizeof (epub_str) + 64]; 176 char *end; 177 178 end = GNUNET_STRINGS_data_to_string (&pmdrh->h_denom_pub, 179 sizeof (pmdrh->h_denom_pub), 180 epub_str, 181 sizeof (epub_str)); 182 *end = '\0'; 183 GNUNET_snprintf (arg_str, 184 sizeof (arg_str), 185 "management/denominations/%s/revoke", 186 epub_str); 187 pmdrh->url = TALER_url_join (pmdrh->base_url, 188 arg_str, 189 NULL); 190 } 191 if (NULL == pmdrh->url) 192 { 193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 194 "Could not construct request URL.\n"); 195 return TALER_EC_GENERIC_CONFIGURATION_INVALID; 196 } 197 body = GNUNET_JSON_PACK ( 198 GNUNET_JSON_pack_data_auto ("master_sig", 199 &pmdrh->master_sig)); 200 eh = TALER_EXCHANGE_curl_easy_get_ (pmdrh->url); 201 if ( (NULL == eh) || 202 (GNUNET_OK != 203 TALER_curl_easy_post (&pmdrh->post_ctx, 204 eh, 205 body)) ) 206 { 207 GNUNET_break (0); 208 if (NULL != eh) 209 curl_easy_cleanup (eh); 210 json_decref (body); 211 GNUNET_free (pmdrh->url); 212 pmdrh->url = NULL; 213 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 214 } 215 json_decref (body); 216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 217 "Requesting URL '%s'\n", 218 pmdrh->url); 219 pmdrh->job = GNUNET_CURL_job_add2 (pmdrh->ctx, 220 eh, 221 pmdrh->post_ctx.headers, 222 &handle_denominations_revoke_finished, 223 pmdrh); 224 if (NULL == pmdrh->job) 225 { 226 GNUNET_break (0); 227 TALER_curl_easy_post_finished (&pmdrh->post_ctx); 228 GNUNET_free (pmdrh->url); 229 pmdrh->url = NULL; 230 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 231 } 232 return TALER_EC_NONE; 233 } 234 235 236 void 237 TALER_EXCHANGE_post_management_denominations_revoke_cancel ( 238 struct TALER_EXCHANGE_PostManagementDenominationsRevokeHandle *pmdrh) 239 { 240 if (NULL != pmdrh->job) 241 { 242 GNUNET_CURL_job_cancel (pmdrh->job); 243 pmdrh->job = NULL; 244 } 245 TALER_curl_easy_post_finished (&pmdrh->post_ctx); 246 GNUNET_free (pmdrh->url); 247 GNUNET_free (pmdrh->base_url); 248 GNUNET_free (pmdrh); 249 } 250 251 252 /* end of exchange_api_post-management-denominations-H_DENOM_PUB-revoke.c */