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_auditor_disable.c (6275B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2015-2021 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_auditor_disable.c
     19  * @brief functions to disable an auditor
     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  * @brief Handle for a POST /management/auditors/$AUDITOR_PUB/disable request.
     34  */
     35 struct TALER_EXCHANGE_ManagementAuditorDisableHandle
     36 {
     37 
     38   /**
     39    * The url for this request.
     40    */
     41   char *url;
     42 
     43   /**
     44    * Minor context that holds body and headers.
     45    */
     46   struct TALER_CURL_PostContext post_ctx;
     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_EXCHANGE_ManagementAuditorDisableCallback cb;
     57 
     58   /**
     59    * Closure for @a cb.
     60    */
     61   void *cb_cls;
     62 
     63   /**
     64    * Reference to the execution context.
     65    */
     66   struct GNUNET_CURL_Context *ctx;
     67 };
     68 
     69 
     70 /**
     71  * Function called when we're done processing the
     72  * HTTP /management/auditors/%s/disable request.
     73  *
     74  * @param cls the `struct TALER_EXCHANGE_ManagementAuditorDisableHandle *`
     75  * @param response_code HTTP response code, 0 on error
     76  * @param response response body, NULL if not in JSON
     77  */
     78 static void
     79 handle_auditor_disable_finished (void *cls,
     80                                  long response_code,
     81                                  const void *response)
     82 {
     83   struct TALER_EXCHANGE_ManagementAuditorDisableHandle *ah = cls;
     84   const json_t *json = response;
     85   struct TALER_EXCHANGE_ManagementAuditorDisableResponse adr = {
     86     .hr.http_status = (unsigned int) response_code,
     87     .hr.reply = json
     88   };
     89 
     90   ah->job = NULL;
     91   switch (response_code)
     92   {
     93   case MHD_HTTP_NO_CONTENT:
     94     break;
     95   case MHD_HTTP_FORBIDDEN:
     96     adr.hr.ec = TALER_JSON_get_error_code (json);
     97     adr.hr.hint = TALER_JSON_get_error_hint (json);
     98     break;
     99   case MHD_HTTP_NOT_FOUND:
    100     adr.hr.ec = TALER_JSON_get_error_code (json);
    101     adr.hr.hint = TALER_JSON_get_error_hint (json);
    102     break;
    103   case MHD_HTTP_CONFLICT:
    104     adr.hr.ec = TALER_JSON_get_error_code (json);
    105     adr.hr.hint = TALER_JSON_get_error_hint (json);
    106     break;
    107   default:
    108     /* unexpected response code */
    109     GNUNET_break_op (0);
    110     adr.hr.ec = TALER_JSON_get_error_code (json);
    111     adr.hr.hint = TALER_JSON_get_error_hint (json);
    112     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    113                 "Unexpected response code %u/%d for exchange management auditor disable\n",
    114                 (unsigned int) response_code,
    115                 (int) adr.hr.ec);
    116     break;
    117   }
    118   if (NULL != ah->cb)
    119   {
    120     ah->cb (ah->cb_cls,
    121             &adr);
    122     ah->cb = NULL;
    123   }
    124   TALER_EXCHANGE_management_disable_auditor_cancel (ah);
    125 }
    126 
    127 
    128 struct TALER_EXCHANGE_ManagementAuditorDisableHandle *
    129 TALER_EXCHANGE_management_disable_auditor (
    130   struct GNUNET_CURL_Context *ctx,
    131   const char *url,
    132   const struct TALER_AuditorPublicKeyP *auditor_pub,
    133   struct GNUNET_TIME_Timestamp validity_end,
    134   const struct TALER_MasterSignatureP *master_sig,
    135   TALER_EXCHANGE_ManagementAuditorDisableCallback cb,
    136   void *cb_cls)
    137 {
    138   struct TALER_EXCHANGE_ManagementAuditorDisableHandle *ah;
    139   CURL *eh;
    140   json_t *body;
    141 
    142   ah = GNUNET_new (struct TALER_EXCHANGE_ManagementAuditorDisableHandle);
    143   ah->cb = cb;
    144   ah->cb_cls = cb_cls;
    145   ah->ctx = ctx;
    146   {
    147     char epub_str[sizeof (*auditor_pub) * 2];
    148     char arg_str[sizeof (epub_str) + 64];
    149     char *end;
    150 
    151     end = GNUNET_STRINGS_data_to_string (auditor_pub,
    152                                          sizeof (*auditor_pub),
    153                                          epub_str,
    154                                          sizeof (epub_str));
    155     *end = '\0';
    156     GNUNET_snprintf (arg_str,
    157                      sizeof (arg_str),
    158                      "management/auditors/%s/disable",
    159                      epub_str);
    160     ah->url = TALER_url_join (url,
    161                               arg_str,
    162                               NULL);
    163   }
    164   if (NULL == ah->url)
    165   {
    166     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    167                 "Could not construct request URL.\n");
    168     GNUNET_free (ah);
    169     return NULL;
    170   }
    171   body = GNUNET_JSON_PACK (
    172     GNUNET_JSON_pack_data_auto ("master_sig",
    173                                 master_sig),
    174     GNUNET_JSON_pack_timestamp ("validity_end",
    175                                 validity_end));
    176   eh = TALER_EXCHANGE_curl_easy_get_ (ah->url);
    177   if ( (NULL == eh) ||
    178        (GNUNET_OK !=
    179         TALER_curl_easy_post (&ah->post_ctx,
    180                               eh,
    181                               body)))
    182   {
    183     GNUNET_break (0);
    184     if (NULL != eh)
    185       curl_easy_cleanup (eh);
    186     json_decref (body);
    187     GNUNET_free (ah->url);
    188     return NULL;
    189   }
    190   json_decref (body);
    191   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    192               "Requesting URL '%s'\n",
    193               ah->url);
    194   ah->job = GNUNET_CURL_job_add2 (ctx,
    195                                   eh,
    196                                   ah->post_ctx.headers,
    197                                   &handle_auditor_disable_finished,
    198                                   ah);
    199   if (NULL == ah->job)
    200   {
    201     TALER_EXCHANGE_management_disable_auditor_cancel (ah);
    202     return NULL;
    203   }
    204   return ah;
    205 }
    206 
    207 
    208 void
    209 TALER_EXCHANGE_management_disable_auditor_cancel (
    210   struct TALER_EXCHANGE_ManagementAuditorDisableHandle *ah)
    211 {
    212   if (NULL != ah->job)
    213   {
    214     GNUNET_CURL_job_cancel (ah->job);
    215     ah->job = NULL;
    216   }
    217   TALER_curl_easy_post_finished (&ah->post_ctx);
    218   GNUNET_free (ah->url);
    219   GNUNET_free (ah);
    220 }