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_wire_disable.c (6235B)


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