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

Generated by: LCOV version 1.14