merchant_api_delete-private-tokens-SERIAL.c (6150B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2025-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 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-private-tokens-SERIAL.c 19 * @brief Implementation of the DELETE /private/tokens/$SERIAL request 20 * @author Christian Grothoff 21 */ 22 #include "taler/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/taler-merchant/delete-private-tokens-SERIAL.h> 29 #include "merchant_api_curl_defaults.h" 30 #include <taler/taler_json_lib.h> 31 32 33 /** 34 * Handle for a DELETE /private/tokens/$SERIAL operation. 35 */ 36 struct TALER_MERCHANT_DeletePrivateTokensSerialHandle 37 { 38 /** 39 * Base URL of the merchant backend. 40 */ 41 char *base_url; 42 43 /** 44 * The full URL for this request. 45 */ 46 char *url; 47 48 /** 49 * Handle for the request. 50 */ 51 struct GNUNET_CURL_Job *job; 52 53 /** 54 * Function to call with the result. 55 */ 56 TALER_MERCHANT_DeletePrivateTokensSerialCallback cb; 57 58 /** 59 * Closure for @a cb. 60 */ 61 TALER_MERCHANT_DELETE_PRIVATE_TOKENS_SERIAL_RESULT_CLOSURE *cb_cls; 62 63 /** 64 * Reference to the execution context. 65 */ 66 struct GNUNET_CURL_Context *ctx; 67 68 /** 69 * Identifier of the instance. 70 */ 71 char *instance_id; 72 73 /** 74 * Serial number of the token to delete. 75 */ 76 uint64_t serial; 77 }; 78 79 80 /** 81 * Function called when we're done processing the 82 * HTTP DELETE /private/tokens/$SERIAL request. 83 * 84 * @param cls the `struct TALER_MERCHANT_DeletePrivateTokensSerialHandle` 85 * @param response_code HTTP response code, 0 on error 86 * @param response response body, NULL if not in JSON 87 */ 88 static void 89 handle_delete_tokens_serial_finished (void *cls, 90 long response_code, 91 const void *response) 92 { 93 struct TALER_MERCHANT_DeletePrivateTokensSerialHandle *dtsh = cls; 94 const json_t *json = response; 95 struct TALER_MERCHANT_DeletePrivateTokensSerialResponse dtsr = { 96 .hr.http_status = (unsigned int) response_code, 97 .hr.reply = json 98 }; 99 100 dtsh->job = NULL; 101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 102 "Got DELETE /instances/$ID/private/tokens/$SERIAL response with status code %u\n", 103 (unsigned int) response_code); 104 switch (response_code) 105 { 106 case MHD_HTTP_NO_CONTENT: 107 break; 108 case MHD_HTTP_UNAUTHORIZED: 109 dtsr.hr.ec = TALER_JSON_get_error_code (json); 110 dtsr.hr.hint = TALER_JSON_get_error_hint (json); 111 break; 112 case MHD_HTTP_FORBIDDEN: 113 dtsr.hr.ec = TALER_JSON_get_error_code (json); 114 dtsr.hr.hint = TALER_JSON_get_error_hint (json); 115 break; 116 case MHD_HTTP_NOT_FOUND: 117 dtsr.hr.ec = TALER_JSON_get_error_code (json); 118 dtsr.hr.hint = TALER_JSON_get_error_hint (json); 119 break; 120 default: 121 dtsr.hr.ec = TALER_JSON_get_error_code (json); 122 dtsr.hr.hint = TALER_JSON_get_error_hint (json); 123 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 124 "Unexpected response code %u/%d for DELETE /instances/$ID/private/tokens/$SERIAL\n", 125 (unsigned int) response_code, 126 (int) dtsr.hr.ec); 127 break; 128 } 129 dtsh->cb (dtsh->cb_cls, 130 &dtsr); 131 TALER_MERCHANT_delete_private_tokens_serial_cancel (dtsh); 132 } 133 134 135 struct TALER_MERCHANT_DeletePrivateTokensSerialHandle * 136 TALER_MERCHANT_delete_private_tokens_serial_create ( 137 struct GNUNET_CURL_Context *ctx, 138 const char *url, 139 const char *instance_id, 140 uint64_t serial) 141 { 142 struct TALER_MERCHANT_DeletePrivateTokensSerialHandle *dtsh; 143 144 dtsh = GNUNET_new (struct TALER_MERCHANT_DeletePrivateTokensSerialHandle); 145 dtsh->ctx = ctx; 146 dtsh->base_url = GNUNET_strdup (url); 147 dtsh->instance_id = GNUNET_strdup (instance_id); 148 dtsh->serial = serial; 149 return dtsh; 150 } 151 152 153 enum TALER_ErrorCode 154 TALER_MERCHANT_delete_private_tokens_serial_start ( 155 struct TALER_MERCHANT_DeletePrivateTokensSerialHandle *dtsh, 156 TALER_MERCHANT_DeletePrivateTokensSerialCallback cb, 157 TALER_MERCHANT_DELETE_PRIVATE_TOKENS_SERIAL_RESULT_CLOSURE *cb_cls) 158 { 159 CURL *eh; 160 161 dtsh->cb = cb; 162 dtsh->cb_cls = cb_cls; 163 { 164 char *path; 165 166 GNUNET_asprintf (&path, 167 "instances/%s/private/tokens/%llu", 168 dtsh->instance_id, 169 (unsigned long long) dtsh->serial); 170 dtsh->url = TALER_url_join (dtsh->base_url, 171 path, 172 NULL); 173 GNUNET_free (path); 174 } 175 if (NULL == dtsh->url) 176 return TALER_EC_GENERIC_CONFIGURATION_INVALID; 177 eh = TALER_MERCHANT_curl_easy_get_ (dtsh->url); 178 if (NULL == eh) 179 return TALER_EC_GENERIC_CONFIGURATION_INVALID; 180 GNUNET_assert (CURLE_OK == 181 curl_easy_setopt (eh, 182 CURLOPT_CUSTOMREQUEST, 183 MHD_HTTP_METHOD_DELETE)); 184 dtsh->job = GNUNET_CURL_job_add (dtsh->ctx, 185 eh, 186 &handle_delete_tokens_serial_finished, 187 dtsh); 188 if (NULL == dtsh->job) 189 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 190 return TALER_EC_NONE; 191 } 192 193 194 void 195 TALER_MERCHANT_delete_private_tokens_serial_cancel ( 196 struct TALER_MERCHANT_DeletePrivateTokensSerialHandle *dtsh) 197 { 198 if (NULL != dtsh->job) 199 { 200 GNUNET_CURL_job_cancel (dtsh->job); 201 dtsh->job = NULL; 202 } 203 GNUNET_free (dtsh->url); 204 GNUNET_free (dtsh->instance_id); 205 GNUNET_free (dtsh->base_url); 206 GNUNET_free (dtsh); 207 } 208 209 210 /* end of merchant_api_delete-private-tokens-SERIAL.c */