merchant_api_delete_account.c (5265B)
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 Lesser General Public License as published by the Free Software 7 Foundation; either version 2.1, 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 Lesser General Public License for more details. 12 13 You should have received a copy of the GNU Lesser General Public License along with 14 TALER; see the file COPYING.LGPL. If not, see 15 <http://www.gnu.org/licenses/> 16 */ 17 /** 18 * @file merchant_api_delete_account.c 19 * @brief Implementation of the DELETE /private/account/$H_WIRE request of the merchant's HTTP API 20 * @author Christian Grothoff 21 */ 22 #include "platform.h" 23 #include <curl/curl.h> 24 #include <jansson.h> 25 #include <microhttpd.h> /* just for HTTP status codes */ 26 #include <gnunet/gnunet_util_lib.h> 27 #include <gnunet/gnunet_curl_lib.h> 28 #include "taler_merchant_service.h" 29 #include "merchant_api_curl_defaults.h" 30 #include <taler/taler_json_lib.h> 31 #include <taler/taler_signatures.h> 32 33 34 /** 35 * Handle for a DELETE /accounts/$ID operation. 36 */ 37 struct TALER_MERCHANT_AccountDeleteHandle 38 { 39 /** 40 * The url for this request. 41 */ 42 char *url; 43 44 /** 45 * Handle for the request. 46 */ 47 struct GNUNET_CURL_Job *job; 48 49 /** 50 * Function to call with the result. 51 */ 52 TALER_MERCHANT_AccountDeleteCallback cb; 53 54 /** 55 * Closure for @a cb. 56 */ 57 void *cb_cls; 58 59 /** 60 * Reference to the execution context. 61 */ 62 struct GNUNET_CURL_Context *ctx; 63 64 }; 65 66 67 /** 68 * Function called when we're done processing the 69 * HTTP DELETE /accounts/$H_WIRE request. 70 * 71 * @param cls the `struct TALER_MERCHANT_AccountDeleteHandle` 72 * @param response_code HTTP response code, 0 on error 73 * @param response response body, NULL if not in JSON 74 */ 75 static void 76 handle_delete_account_finished (void *cls, 77 long response_code, 78 const void *response) 79 { 80 struct TALER_MERCHANT_AccountDeleteHandle *adh = cls; 81 const json_t *json = response; 82 struct TALER_MERCHANT_AccountDeleteResponse adr = { 83 .hr.http_status = (unsigned int) response_code, 84 .hr.reply = json 85 }; 86 87 adh->job = NULL; 88 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 89 "Got /accounts/$H_WIRE response with status code %u\n", 90 (unsigned int) response_code); 91 switch (response_code) 92 { 93 case MHD_HTTP_NO_CONTENT: 94 break; 95 case MHD_HTTP_UNAUTHORIZED: 96 adr.hr.ec = TALER_JSON_get_error_code (json); 97 adr.hr.hint = TALER_JSON_get_error_hint (json); 98 /* Nothing really to verify, merchant says we need to authenticate. */ 99 break; 100 case MHD_HTTP_NOT_FOUND: 101 break; 102 default: 103 /* unexpected response code */ 104 adr.hr.ec = TALER_JSON_get_error_code (json); 105 adr.hr.hint = TALER_JSON_get_error_hint (json); 106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 107 "Unexpected response code %u/%d for DELETE /account/ID\n", 108 (unsigned int) response_code, 109 (int) adr.hr.ec); 110 break; 111 } 112 adh->cb (adh->cb_cls, 113 &adr); 114 TALER_MERCHANT_account_delete_cancel (adh); 115 } 116 117 118 struct TALER_MERCHANT_AccountDeleteHandle * 119 TALER_MERCHANT_account_delete ( 120 struct GNUNET_CURL_Context *ctx, 121 const char *backend_url, 122 const struct TALER_MerchantWireHashP *h_wire, 123 TALER_MERCHANT_AccountDeleteCallback cb, 124 void *cb_cls) 125 { 126 struct TALER_MERCHANT_AccountDeleteHandle *adh; 127 128 adh = GNUNET_new (struct TALER_MERCHANT_AccountDeleteHandle); 129 adh->ctx = ctx; 130 adh->cb = cb; 131 adh->cb_cls = cb_cls; 132 { 133 char h_wire_str[sizeof (*h_wire) * 2]; 134 char *path; 135 char *end; 136 137 end = GNUNET_STRINGS_data_to_string (h_wire, 138 sizeof (*h_wire), 139 h_wire_str, 140 sizeof (h_wire_str)); 141 *end = '\0'; 142 GNUNET_asprintf (&path, 143 "private/account/%s", 144 h_wire_str); 145 adh->url = TALER_url_join (backend_url, 146 path, 147 NULL); 148 GNUNET_free (path); 149 } 150 if (NULL == adh->url) 151 { 152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 153 "Could not construct request URL.\n"); 154 GNUNET_free (adh); 155 return NULL; 156 } 157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 158 "Requesting URL '%s'\n", 159 adh->url); 160 { 161 CURL *eh; 162 163 eh = TALER_MERCHANT_curl_easy_get_ (adh->url); 164 GNUNET_assert (CURLE_OK == 165 curl_easy_setopt (eh, 166 CURLOPT_CUSTOMREQUEST, 167 MHD_HTTP_METHOD_DELETE)); 168 adh->job = GNUNET_CURL_job_add (ctx, 169 eh, 170 &handle_delete_account_finished, 171 adh); 172 } 173 return adh; 174 } 175 176 177 void 178 TALER_MERCHANT_account_delete_cancel ( 179 struct TALER_MERCHANT_AccountDeleteHandle *adh) 180 { 181 if (NULL != adh->job) 182 GNUNET_CURL_job_cancel (adh->job); 183 GNUNET_free (adh->url); 184 GNUNET_free (adh); 185 }