exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

exchange_api_management_revoke_denomination_key.c (6290B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2015-2024 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_revoke_denomination_key.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 "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/denominations/$H_DENOM_PUB/revoke request.
     35  */
     36 struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle
     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_ManagementRevokeDenominationKeyCallback 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 /management/denominations/$H_DENOM_PUB/revoke request.
     74  *
     75  * @param cls the `struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *`
     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_revoke_denomination_finished (void *cls,
     81                                      long response_code,
     82                                      const void *response)
     83 {
     84   struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *rh = cls;
     85   const json_t *json = response;
     86   struct TALER_EXCHANGE_ManagementRevokeDenominationResponse rdr = {
     87     .hr.http_status = (unsigned int) response_code,
     88     .hr.reply = json
     89   };
     90 
     91   rh->job = NULL;
     92   switch (response_code)
     93   {
     94   case 0:
     95     /* no reply */
     96     rdr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     97     rdr.hr.hint = "server offline?";
     98     break;
     99   case MHD_HTTP_NO_CONTENT:
    100     break;
    101   case MHD_HTTP_FORBIDDEN:
    102     rdr.hr.ec = TALER_JSON_get_error_code (json);
    103     rdr.hr.hint = TALER_JSON_get_error_hint (json);
    104     break;
    105   default:
    106     /* unexpected response code */
    107     GNUNET_break_op (0);
    108     rdr.hr.ec = TALER_JSON_get_error_code (json);
    109     rdr.hr.hint = TALER_JSON_get_error_hint (json);
    110     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    111                 "Unexpected response code %u/%d for exchange management revoke denomination\n",
    112                 (unsigned int) response_code,
    113                 (int) rdr.hr.ec);
    114     break;
    115   }
    116   if (NULL != rh->cb)
    117   {
    118     rh->cb (rh->cb_cls,
    119             &rdr);
    120     rh->cb = NULL;
    121   }
    122   TALER_EXCHANGE_management_revoke_denomination_key_cancel (rh);
    123 }
    124 
    125 
    126 struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *
    127 TALER_EXCHANGE_management_revoke_denomination_key (
    128   struct GNUNET_CURL_Context *ctx,
    129   const char *url,
    130   const struct TALER_DenominationHashP *h_denom_pub,
    131   const struct TALER_MasterSignatureP *master_sig,
    132   TALER_EXCHANGE_ManagementRevokeDenominationKeyCallback cb,
    133   void *cb_cls)
    134 {
    135   struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *rh;
    136   CURL *eh;
    137   json_t *body;
    138 
    139   rh = GNUNET_new (struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle);
    140   rh->cb = cb;
    141   rh->cb_cls = cb_cls;
    142   rh->ctx = ctx;
    143   {
    144     char epub_str[sizeof (*h_denom_pub) * 2];
    145     char arg_str[sizeof (epub_str) + 64];
    146     char *end;
    147 
    148     end = GNUNET_STRINGS_data_to_string (h_denom_pub,
    149                                          sizeof (*h_denom_pub),
    150                                          epub_str,
    151                                          sizeof (epub_str));
    152     *end = '\0';
    153     GNUNET_snprintf (arg_str,
    154                      sizeof (arg_str),
    155                      "management/denominations/%s/revoke",
    156                      epub_str);
    157     rh->url = TALER_url_join (url,
    158                               arg_str,
    159                               NULL);
    160   }
    161   if (NULL == rh->url)
    162   {
    163     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    164                 "Could not construct request URL.\n");
    165     GNUNET_free (rh);
    166     return NULL;
    167   }
    168   body = GNUNET_JSON_PACK (
    169     GNUNET_JSON_pack_data_auto ("master_sig",
    170                                 master_sig));
    171   if (NULL == body)
    172   {
    173     GNUNET_break (0);
    174     GNUNET_free (rh->url);
    175     GNUNET_free (rh);
    176     return NULL;
    177   }
    178   eh = TALER_EXCHANGE_curl_easy_get_ (rh->url);
    179   if ( (NULL == eh) ||
    180        (GNUNET_OK !=
    181         TALER_curl_easy_post (&rh->post_ctx,
    182                               eh,
    183                               body)) )
    184   {
    185     GNUNET_break (0);
    186     if (NULL != eh)
    187       curl_easy_cleanup (eh);
    188     json_decref (body);
    189     GNUNET_free (rh->url);
    190     GNUNET_free (rh);
    191     return NULL;
    192   }
    193   json_decref (body);
    194   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    195               "Requesting URL '%s'\n",
    196               rh->url);
    197   rh->job = GNUNET_CURL_job_add2 (ctx,
    198                                   eh,
    199                                   rh->post_ctx.headers,
    200                                   &handle_revoke_denomination_finished,
    201                                   rh);
    202   if (NULL == rh->job)
    203   {
    204     TALER_EXCHANGE_management_revoke_denomination_key_cancel (rh);
    205     return NULL;
    206   }
    207   return rh;
    208 }
    209 
    210 
    211 void
    212 TALER_EXCHANGE_management_revoke_denomination_key_cancel (
    213   struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *rh)
    214 {
    215   if (NULL != rh->job)
    216   {
    217     GNUNET_CURL_job_cancel (rh->job);
    218     rh->job = NULL;
    219   }
    220   TALER_curl_easy_post_finished (&rh->post_ctx);
    221   GNUNET_free (rh->url);
    222   GNUNET_free (rh);
    223 }