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_enable.c (7346B)


      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_enable.c
     19  * @brief functions to enable 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_ManagementWireEnableHandle
     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_ManagementWireEnableCallback 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 request.
     71  *
     72  * @param cls the `struct TALER_EXCHANGE_ManagementAuditorEnableHandle *`
     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_enable_finished (void *cls,
     78                                 long response_code,
     79                                 const void *response)
     80 {
     81   struct TALER_EXCHANGE_ManagementWireEnableHandle *wh = cls;
     82   const json_t *json = response;
     83   struct TALER_EXCHANGE_ManagementWireEnableResponse wer = {
     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     wer.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     94     wer.hr.hint = "server offline?";
     95     break;
     96   case MHD_HTTP_NO_CONTENT:
     97     break;
     98   case MHD_HTTP_FORBIDDEN:
     99     wer.hr.ec = TALER_JSON_get_error_code (json);
    100     wer.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       wer.hr.ec = TALER_JSON_get_error_code (json);
    109       wer.hr.hint = TALER_JSON_get_error_hint (json);
    110     }
    111     else
    112     {
    113       wer.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    114       wer.hr.hint = TALER_ErrorCode_get_hint (wer.hr.ec);
    115     }
    116     break;
    117   case MHD_HTTP_CONFLICT:
    118     wer.hr.ec = TALER_JSON_get_error_code (json);
    119     wer.hr.hint = TALER_JSON_get_error_hint (json);
    120     break;
    121   default:
    122     /* unexpected response code */
    123     GNUNET_break_op (0);
    124     wer.hr.ec = TALER_JSON_get_error_code (json);
    125     wer.hr.hint = TALER_JSON_get_error_hint (json);
    126     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    127                 "Unexpected response code %u/%d for exchange management enable wire\n",
    128                 (unsigned int) response_code,
    129                 (int) wer.hr.ec);
    130     break;
    131   }
    132   if (NULL != wh->cb)
    133   {
    134     wh->cb (wh->cb_cls,
    135             &wer);
    136     wh->cb = NULL;
    137   }
    138   TALER_EXCHANGE_management_enable_wire_cancel (wh);
    139 }
    140 
    141 
    142 struct TALER_EXCHANGE_ManagementWireEnableHandle *
    143 TALER_EXCHANGE_management_enable_wire (
    144   struct GNUNET_CURL_Context *ctx,
    145   const char *url,
    146   const struct TALER_FullPayto payto_uri,
    147   const char *conversion_url,
    148   const json_t *debit_restrictions,
    149   const json_t *credit_restrictions,
    150   struct GNUNET_TIME_Timestamp validity_start,
    151   const struct TALER_MasterSignatureP *master_sig1,
    152   const struct TALER_MasterSignatureP *master_sig2,
    153   const char *bank_label,
    154   int64_t priority,
    155   TALER_EXCHANGE_ManagementWireEnableCallback cb,
    156   void *cb_cls)
    157 {
    158   struct TALER_EXCHANGE_ManagementWireEnableHandle *wh;
    159   CURL *eh;
    160   json_t *body;
    161 
    162   {
    163     char *msg = TALER_payto_validate (payto_uri);
    164 
    165     if (NULL != msg)
    166     {
    167       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    168                   "payto URI is malformed: %s\n",
    169                   msg);
    170       GNUNET_free (msg);
    171       return NULL;
    172     }
    173   }
    174   wh = GNUNET_new (struct TALER_EXCHANGE_ManagementWireEnableHandle);
    175   wh->cb = cb;
    176   wh->cb_cls = cb_cls;
    177   wh->ctx = ctx;
    178   wh->url = TALER_url_join (url,
    179                             "management/wire",
    180                             NULL);
    181   if (NULL == wh->url)
    182   {
    183     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    184                 "Could not construct request URL.\n");
    185     GNUNET_free (wh);
    186     return NULL;
    187   }
    188   body = GNUNET_JSON_PACK (
    189     TALER_JSON_pack_full_payto ("payto_uri",
    190                                 payto_uri),
    191     GNUNET_JSON_pack_array_incref ("debit_restrictions",
    192                                    (json_t *) debit_restrictions),
    193     GNUNET_JSON_pack_array_incref ("credit_restrictions",
    194                                    (json_t *) credit_restrictions),
    195     GNUNET_JSON_pack_allow_null (
    196       GNUNET_JSON_pack_string ("conversion_url",
    197                                conversion_url)),
    198     GNUNET_JSON_pack_allow_null (
    199       GNUNET_JSON_pack_string ("bank_label",
    200                                bank_label)),
    201     GNUNET_JSON_pack_int64 ("priority",
    202                             priority),
    203     GNUNET_JSON_pack_data_auto ("master_sig_add",
    204                                 master_sig1),
    205     GNUNET_JSON_pack_data_auto ("master_sig_wire",
    206                                 master_sig2),
    207     GNUNET_JSON_pack_timestamp ("validity_start",
    208                                 validity_start));
    209   eh = TALER_EXCHANGE_curl_easy_get_ (wh->url);
    210   if ( (NULL == eh) ||
    211        (GNUNET_OK !=
    212         TALER_curl_easy_post (&wh->post_ctx,
    213                               eh,
    214                               body)) )
    215   {
    216     GNUNET_break (0);
    217     if (NULL != eh)
    218       curl_easy_cleanup (eh);
    219     json_decref (body);
    220     GNUNET_free (wh->url);
    221     GNUNET_free (wh);
    222     return NULL;
    223   }
    224   json_decref (body);
    225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    226               "Requesting URL '%s'\n",
    227               wh->url);
    228   wh->job = GNUNET_CURL_job_add2 (ctx,
    229                                   eh,
    230                                   wh->post_ctx.headers,
    231                                   &handle_auditor_enable_finished,
    232                                   wh);
    233   if (NULL == wh->job)
    234   {
    235     TALER_EXCHANGE_management_enable_wire_cancel (wh);
    236     return NULL;
    237   }
    238   return wh;
    239 }
    240 
    241 
    242 void
    243 TALER_EXCHANGE_management_enable_wire_cancel (
    244   struct TALER_EXCHANGE_ManagementWireEnableHandle *wh)
    245 {
    246   if (NULL != wh->job)
    247   {
    248     GNUNET_CURL_job_cancel (wh->job);
    249     wh->job = NULL;
    250   }
    251   TALER_curl_easy_post_finished (&wh->post_ctx);
    252   GNUNET_free (wh->url);
    253   GNUNET_free (wh);
    254 }