exchange

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

exchange_api_post-management-denominations-H_DENOM_PUB-revoke.c (7322B)


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