LCOV - code coverage report
Current view: top level - lib - exchange_api_management_wire_enable.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 0 73 0.0 %
Date: 2022-08-25 06:15:09 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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_wire_enable.c
      19             :  * @brief functions to enable an exchange wire method / bank account
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include "taler_json_lib.h"
      24             : #include <gnunet/gnunet_curl_lib.h>
      25             : #include "taler_exchange_service.h"
      26             : #include "exchange_api_curl_defaults.h"
      27             : #include "taler_signatures.h"
      28             : #include "taler_curl_lib.h"
      29             : #include "taler_json_lib.h"
      30             : 
      31             : 
      32             : struct TALER_EXCHANGE_ManagementWireEnableHandle
      33             : {
      34             : 
      35             :   /**
      36             :    * The url for this request.
      37             :    */
      38             :   char *url;
      39             : 
      40             :   /**
      41             :    * Minor context that holds body and headers.
      42             :    */
      43             :   struct TALER_CURL_PostContext post_ctx;
      44             : 
      45             :   /**
      46             :    * Handle for the request.
      47             :    */
      48             :   struct GNUNET_CURL_Job *job;
      49             : 
      50             :   /**
      51             :    * Function to call with the result.
      52             :    */
      53             :   TALER_EXCHANGE_ManagementWireEnableCallback cb;
      54             : 
      55             :   /**
      56             :    * Closure for @a cb.
      57             :    */
      58             :   void *cb_cls;
      59             : 
      60             :   /**
      61             :    * Reference to the execution context.
      62             :    */
      63             :   struct GNUNET_CURL_Context *ctx;
      64             : };
      65             : 
      66             : 
      67             : /**
      68             :  * Function called when we're done processing the
      69             :  * HTTP /management/wire request.
      70             :  *
      71             :  * @param cls the `struct TALER_EXCHANGE_ManagementAuditorEnableHandle *`
      72             :  * @param response_code HTTP response code, 0 on error
      73             :  * @param response response body, NULL if not in JSON
      74             :  */
      75             : static void
      76           0 : handle_auditor_enable_finished (void *cls,
      77             :                                 long response_code,
      78             :                                 const void *response)
      79             : {
      80           0 :   struct TALER_EXCHANGE_ManagementWireEnableHandle *wh = cls;
      81           0 :   const json_t *json = response;
      82           0 :   struct TALER_EXCHANGE_HttpResponse hr = {
      83           0 :     .http_status = (unsigned int) response_code,
      84             :     .reply = json
      85             :   };
      86             : 
      87           0 :   wh->job = NULL;
      88           0 :   switch (response_code)
      89             :   {
      90           0 :   case 0:
      91             :     /* no reply */
      92           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
      93           0 :     hr.hint = "server offline?";
      94           0 :     break;
      95           0 :   case MHD_HTTP_NO_CONTENT:
      96           0 :     break;
      97           0 :   case MHD_HTTP_FORBIDDEN:
      98           0 :     hr.ec = TALER_JSON_get_error_code (json);
      99           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     100           0 :     break;
     101           0 :   case MHD_HTTP_CONFLICT:
     102           0 :     hr.ec = TALER_JSON_get_error_code (json);
     103           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     104           0 :     break;
     105           0 :   default:
     106             :     /* unexpected response code */
     107           0 :     GNUNET_break_op (0);
     108           0 :     hr.ec = TALER_JSON_get_error_code (json);
     109           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     110           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     111             :                 "Unexpected response code %u/%d for exchange management enable wire\n",
     112             :                 (unsigned int) response_code,
     113             :                 (int) hr.ec);
     114           0 :     break;
     115             :   }
     116           0 :   if (NULL != wh->cb)
     117             :   {
     118           0 :     wh->cb (wh->cb_cls,
     119             :             &hr);
     120           0 :     wh->cb = NULL;
     121             :   }
     122           0 :   TALER_EXCHANGE_management_enable_wire_cancel (wh);
     123           0 : }
     124             : 
     125             : 
     126             : struct TALER_EXCHANGE_ManagementWireEnableHandle *
     127           0 : TALER_EXCHANGE_management_enable_wire (
     128             :   struct GNUNET_CURL_Context *ctx,
     129             :   const char *url,
     130             :   const char *payto_uri,
     131             :   struct GNUNET_TIME_Timestamp validity_start,
     132             :   const struct TALER_MasterSignatureP *master_sig1,
     133             :   const struct TALER_MasterSignatureP *master_sig2,
     134             :   TALER_EXCHANGE_ManagementWireEnableCallback cb,
     135             :   void *cb_cls)
     136             : {
     137             :   struct TALER_EXCHANGE_ManagementWireEnableHandle *wh;
     138             :   CURL *eh;
     139             :   json_t *body;
     140             : 
     141             :   {
     142           0 :     char *msg = TALER_payto_validate (payto_uri);
     143             : 
     144           0 :     if (NULL != msg)
     145             :     {
     146           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     147             :                   "payto URI is malformed: %s\n",
     148             :                   msg);
     149           0 :       GNUNET_free (msg);
     150           0 :       return NULL;
     151             :     }
     152             :   }
     153           0 :   wh = GNUNET_new (struct TALER_EXCHANGE_ManagementWireEnableHandle);
     154           0 :   wh->cb = cb;
     155           0 :   wh->cb_cls = cb_cls;
     156           0 :   wh->ctx = ctx;
     157           0 :   wh->url = TALER_url_join (url,
     158             :                             "management/wire",
     159             :                             NULL);
     160           0 :   if (NULL == wh->url)
     161             :   {
     162           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     163             :                 "Could not construct request URL.\n");
     164           0 :     GNUNET_free (wh);
     165           0 :     return NULL;
     166             :   }
     167           0 :   body = GNUNET_JSON_PACK (
     168             :     GNUNET_JSON_pack_string ("payto_uri",
     169             :                              payto_uri),
     170             :     GNUNET_JSON_pack_data_auto ("master_sig_add",
     171             :                                 master_sig1),
     172             :     GNUNET_JSON_pack_data_auto ("master_sig_wire",
     173             :                                 master_sig2),
     174             :     GNUNET_JSON_pack_timestamp ("validity_start",
     175             :                                 validity_start));
     176           0 :   eh = TALER_EXCHANGE_curl_easy_get_ (wh->url);
     177           0 :   if ( (NULL == eh) ||
     178             :        (GNUNET_OK !=
     179           0 :         TALER_curl_easy_post (&wh->post_ctx,
     180             :                               eh,
     181             :                               body)) )
     182             :   {
     183           0 :     GNUNET_break (0);
     184           0 :     if (NULL != eh)
     185           0 :       curl_easy_cleanup (eh);
     186           0 :     json_decref (body);
     187           0 :     GNUNET_free (wh->url);
     188           0 :     return NULL;
     189             :   }
     190           0 :   json_decref (body);
     191           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     192             :               "Requesting URL '%s'\n",
     193             :               wh->url);
     194           0 :   wh->job = GNUNET_CURL_job_add2 (ctx,
     195             :                                   eh,
     196           0 :                                   wh->post_ctx.headers,
     197             :                                   &handle_auditor_enable_finished,
     198             :                                   wh);
     199           0 :   if (NULL == wh->job)
     200             :   {
     201           0 :     TALER_EXCHANGE_management_enable_wire_cancel (wh);
     202           0 :     return NULL;
     203             :   }
     204           0 :   return wh;
     205             : }
     206             : 
     207             : 
     208             : void
     209           0 : TALER_EXCHANGE_management_enable_wire_cancel (
     210             :   struct TALER_EXCHANGE_ManagementWireEnableHandle *wh)
     211             : {
     212           0 :   if (NULL != wh->job)
     213             :   {
     214           0 :     GNUNET_CURL_job_cancel (wh->job);
     215           0 :     wh->job = NULL;
     216             :   }
     217           0 :   TALER_curl_easy_post_finished (&wh->post_ctx);
     218           0 :   GNUNET_free (wh->url);
     219           0 :   GNUNET_free (wh);
     220           0 : }

Generated by: LCOV version 1.14