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

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-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_transfers_get.c
      19             :  * @brief Implementation of the GET /transfers/ request
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <jansson.h>
      24             : #include <microhttpd.h> /* just for HTTP status codes */
      25             : #include <gnunet/gnunet_util_lib.h>
      26             : #include <gnunet/gnunet_curl_lib.h>
      27             : #include "taler_exchange_service.h"
      28             : #include "taler_json_lib.h"
      29             : #include "exchange_api_handle.h"
      30             : #include "taler_signatures.h"
      31             : #include "exchange_api_curl_defaults.h"
      32             : 
      33             : 
      34             : /**
      35             :  * @brief A /transfers/ GET Handle
      36             :  */
      37             : struct TALER_EXCHANGE_TransfersGetHandle
      38             : {
      39             : 
      40             :   /**
      41             :    * The connection to exchange this request handle will use
      42             :    */
      43             :   struct TALER_EXCHANGE_Handle *exchange;
      44             : 
      45             :   /**
      46             :    * The url for this request.
      47             :    */
      48             :   char *url;
      49             : 
      50             :   /**
      51             :    * Handle for the request.
      52             :    */
      53             :   struct GNUNET_CURL_Job *job;
      54             : 
      55             :   /**
      56             :    * Function to call with the result.
      57             :    */
      58             :   TALER_EXCHANGE_TransfersGetCallback cb;
      59             : 
      60             :   /**
      61             :    * Closure for @a cb.
      62             :    */
      63             :   void *cb_cls;
      64             : 
      65             : };
      66             : 
      67             : 
      68             : /**
      69             :  * We got a #MHD_HTTP_OK response for the /transfers/ request.
      70             :  * Check that the response is well-formed and if it is, call the
      71             :  * callback.  If not, return an error code.
      72             :  *
      73             :  * This code is very similar to
      74             :  * merchant_api_track_transfer.c::check_transfers_get_response_ok.
      75             :  * Any changes should likely be reflected there as well.
      76             :  *
      77             :  * @param wdh handle to the operation
      78             :  * @param json response we got
      79             :  * @return #GNUNET_OK if we are done and all is well,
      80             :  *         #GNUNET_SYSERR if the response was bogus
      81             :  */
      82             : static enum GNUNET_GenericReturnValue
      83           0 : check_transfers_get_response_ok (
      84             :   struct TALER_EXCHANGE_TransfersGetHandle *wdh,
      85             :   const json_t *json)
      86             : {
      87             :   json_t *details_j;
      88             :   struct TALER_EXCHANGE_TransferData td;
      89             :   struct TALER_Amount total_expected;
      90             :   struct TALER_MerchantPublicKeyP merchant_pub;
      91             :   struct GNUNET_JSON_Specification spec[] = {
      92           0 :     TALER_JSON_spec_amount_any ("total", &td.total_amount),
      93           0 :     TALER_JSON_spec_amount_any ("wire_fee", &td.wire_fee),
      94           0 :     GNUNET_JSON_spec_fixed_auto ("merchant_pub", &merchant_pub),
      95           0 :     GNUNET_JSON_spec_fixed_auto ("h_payto", &td.h_payto),
      96           0 :     GNUNET_JSON_spec_timestamp ("execution_time", &td.execution_time),
      97           0 :     GNUNET_JSON_spec_json ("deposits", &details_j),
      98           0 :     GNUNET_JSON_spec_fixed_auto ("exchange_sig", &td.exchange_sig),
      99           0 :     GNUNET_JSON_spec_fixed_auto ("exchange_pub", &td.exchange_pub),
     100           0 :     GNUNET_JSON_spec_end ()
     101             :   };
     102           0 :   struct TALER_EXCHANGE_HttpResponse hr = {
     103             :     .reply = json,
     104             :     .http_status = MHD_HTTP_OK
     105             :   };
     106             : 
     107           0 :   if (GNUNET_OK !=
     108           0 :       GNUNET_JSON_parse (json,
     109             :                          spec,
     110             :                          NULL, NULL))
     111             :   {
     112           0 :     GNUNET_break_op (0);
     113           0 :     return GNUNET_SYSERR;
     114             :   }
     115           0 :   if (GNUNET_OK !=
     116           0 :       TALER_amount_set_zero (td.total_amount.currency,
     117             :                              &total_expected))
     118             :   {
     119           0 :     GNUNET_break_op (0);
     120           0 :     GNUNET_JSON_parse_free (spec);
     121           0 :     return GNUNET_SYSERR;
     122             :   }
     123           0 :   if (GNUNET_OK !=
     124           0 :       TALER_EXCHANGE_test_signing_key (
     125             :         TALER_EXCHANGE_get_keys (wdh->exchange),
     126             :         &td.exchange_pub))
     127             :   {
     128           0 :     GNUNET_break_op (0);
     129           0 :     GNUNET_JSON_parse_free (spec);
     130           0 :     return GNUNET_SYSERR;
     131             :   }
     132           0 :   td.details_length = json_array_size (details_j);
     133             :   {
     134             :     struct GNUNET_HashContext *hash_context;
     135             :     struct TALER_TrackTransferDetails *details;
     136             : 
     137           0 :     details = GNUNET_new_array (td.details_length,
     138             :                                 struct TALER_TrackTransferDetails);
     139           0 :     td.details = details;
     140           0 :     hash_context = GNUNET_CRYPTO_hash_context_start ();
     141           0 :     for (unsigned int i = 0; i<td.details_length; i++)
     142             :     {
     143           0 :       struct TALER_TrackTransferDetails *detail = &details[i];
     144           0 :       struct json_t *detail_j = json_array_get (details_j, i);
     145             :       struct GNUNET_JSON_Specification spec_detail[] = {
     146           0 :         GNUNET_JSON_spec_fixed_auto ("h_contract_terms",
     147             :                                      &detail->h_contract_terms),
     148           0 :         GNUNET_JSON_spec_fixed_auto ("coin_pub", &detail->coin_pub),
     149           0 :         TALER_JSON_spec_amount_any ("deposit_value", &detail->coin_value),
     150           0 :         TALER_JSON_spec_amount_any ("deposit_fee", &detail->coin_fee),
     151           0 :         GNUNET_JSON_spec_end ()
     152             :       };
     153             : 
     154           0 :       if ( (GNUNET_OK !=
     155           0 :             GNUNET_JSON_parse (detail_j,
     156             :                                spec_detail,
     157           0 :                                NULL, NULL)) ||
     158             :            (GNUNET_OK !=
     159           0 :             TALER_amount_cmp_currency (&total_expected,
     160           0 :                                        &detail->coin_value)) ||
     161             :            (GNUNET_OK !=
     162           0 :             TALER_amount_cmp_currency (&total_expected,
     163           0 :                                        &detail->coin_fee)) ||
     164             :            (0 >
     165           0 :             TALER_amount_add (&total_expected,
     166             :                               &total_expected,
     167           0 :                               &detail->coin_value)) ||
     168             :            (0 >
     169           0 :             TALER_amount_subtract (&total_expected,
     170             :                                    &total_expected,
     171           0 :                                    &detail->coin_fee)) )
     172             :       {
     173           0 :         GNUNET_break_op (0);
     174           0 :         GNUNET_CRYPTO_hash_context_abort (hash_context);
     175           0 :         GNUNET_JSON_parse_free (spec);
     176           0 :         GNUNET_free (details);
     177           0 :         return GNUNET_SYSERR;
     178             :       }
     179             :       /* build up big hash for signature checking later */
     180           0 :       TALER_exchange_online_wire_deposit_append (
     181             :         hash_context,
     182           0 :         &detail->h_contract_terms,
     183             :         td.execution_time,
     184           0 :         &detail->coin_pub,
     185           0 :         &detail->coin_value,
     186           0 :         &detail->coin_fee);
     187             :     }
     188             :     /* Check signature */
     189             :     {
     190             :       struct GNUNET_HashCode h_details;
     191             : 
     192           0 :       GNUNET_CRYPTO_hash_context_finish (hash_context,
     193             :                                          &h_details);
     194           0 :       if (GNUNET_OK !=
     195           0 :           TALER_exchange_online_wire_deposit_verify (
     196             :             &td.total_amount,
     197             :             &td.wire_fee,
     198             :             &merchant_pub,
     199             :             &td.h_payto,
     200             :             &h_details,
     201             :             &td.exchange_pub,
     202             :             &td.exchange_sig))
     203             :       {
     204           0 :         GNUNET_break_op (0);
     205           0 :         GNUNET_JSON_parse_free (spec);
     206           0 :         GNUNET_free (details);
     207           0 :         return GNUNET_SYSERR;
     208             :       }
     209             :     }
     210             : 
     211           0 :     if (0 >
     212           0 :         TALER_amount_subtract (&total_expected,
     213             :                                &total_expected,
     214             :                                &td.wire_fee))
     215             :     {
     216           0 :       GNUNET_break_op (0);
     217           0 :       GNUNET_JSON_parse_free (spec);
     218           0 :       GNUNET_free (details);
     219           0 :       return GNUNET_SYSERR;
     220             :     }
     221           0 :     if (0 !=
     222           0 :         TALER_amount_cmp (&total_expected,
     223             :                           &td.total_amount))
     224             :     {
     225           0 :       GNUNET_break_op (0);
     226           0 :       GNUNET_JSON_parse_free (spec);
     227           0 :       GNUNET_free (details);
     228           0 :       return GNUNET_SYSERR;
     229             :     }
     230           0 :     wdh->cb (wdh->cb_cls,
     231             :              &hr,
     232             :              &td);
     233           0 :     GNUNET_free (details);
     234             :   }
     235           0 :   GNUNET_JSON_parse_free (spec);
     236           0 :   TALER_EXCHANGE_transfers_get_cancel (wdh);
     237           0 :   return GNUNET_OK;
     238             : }
     239             : 
     240             : 
     241             : /**
     242             :  * Function called when we're done processing the
     243             :  * HTTP /transfers/ request.
     244             :  *
     245             :  * @param cls the `struct TALER_EXCHANGE_TransfersGetHandle`
     246             :  * @param response_code HTTP response code, 0 on error
     247             :  * @param response parsed JSON result, NULL on error
     248             :  */
     249             : static void
     250           0 : handle_transfers_get_finished (void *cls,
     251             :                                long response_code,
     252             :                                const void *response)
     253             : {
     254           0 :   struct TALER_EXCHANGE_TransfersGetHandle *wdh = cls;
     255           0 :   const json_t *j = response;
     256           0 :   struct TALER_EXCHANGE_HttpResponse hr = {
     257             :     .reply = j,
     258           0 :     .http_status = (unsigned int) response_code
     259             :   };
     260             : 
     261           0 :   wdh->job = NULL;
     262           0 :   switch (response_code)
     263             :   {
     264           0 :   case 0:
     265           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     266           0 :     break;
     267           0 :   case MHD_HTTP_OK:
     268           0 :     if (GNUNET_OK ==
     269           0 :         check_transfers_get_response_ok (wdh,
     270             :                                          j))
     271           0 :       return;
     272           0 :     GNUNET_break_op (0);
     273           0 :     hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     274           0 :     hr.http_status = 0;
     275           0 :     break;
     276           0 :   case MHD_HTTP_BAD_REQUEST:
     277             :     /* This should never happen, either us or the exchange is buggy
     278             :        (or API version conflict); just pass JSON reply to the application */
     279           0 :     hr.ec = TALER_JSON_get_error_code (j);
     280           0 :     hr.hint = TALER_JSON_get_error_hint (j);
     281           0 :     break;
     282           0 :   case MHD_HTTP_FORBIDDEN:
     283             :     /* Nothing really to verify, exchange says one of the signatures is
     284             :        invalid; as we checked them, this should never happen, we
     285             :        should pass the JSON reply to the application */
     286           0 :     hr.ec = TALER_JSON_get_error_code (j);
     287           0 :     hr.hint = TALER_JSON_get_error_hint (j);
     288           0 :     break;
     289           0 :   case MHD_HTTP_NOT_FOUND:
     290             :     /* Exchange does not know about transaction;
     291             :        we should pass the reply to the application */
     292           0 :     hr.ec = TALER_JSON_get_error_code (j);
     293           0 :     hr.hint = TALER_JSON_get_error_hint (j);
     294           0 :     break;
     295           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     296             :     /* Server had an internal issue; we should retry, but this API
     297             :        leaves this to the application */
     298           0 :     hr.ec = TALER_JSON_get_error_code (j);
     299           0 :     hr.hint = TALER_JSON_get_error_hint (j);
     300           0 :     break;
     301           0 :   default:
     302             :     /* unexpected response code */
     303           0 :     GNUNET_break_op (0);
     304           0 :     hr.ec = TALER_JSON_get_error_code (j);
     305           0 :     hr.hint = TALER_JSON_get_error_hint (j);
     306           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     307             :                 "Unexpected response code %u/%d for transfers get\n",
     308             :                 (unsigned int) response_code,
     309             :                 (int) hr.ec);
     310           0 :     break;
     311             :   }
     312           0 :   wdh->cb (wdh->cb_cls,
     313             :            &hr,
     314             :            NULL);
     315           0 :   TALER_EXCHANGE_transfers_get_cancel (wdh);
     316             : }
     317             : 
     318             : 
     319             : struct TALER_EXCHANGE_TransfersGetHandle *
     320           0 : TALER_EXCHANGE_transfers_get (
     321             :   struct TALER_EXCHANGE_Handle *exchange,
     322             :   const struct TALER_WireTransferIdentifierRawP *wtid,
     323             :   TALER_EXCHANGE_TransfersGetCallback cb,
     324             :   void *cb_cls)
     325             : {
     326             :   struct TALER_EXCHANGE_TransfersGetHandle *wdh;
     327             :   struct GNUNET_CURL_Context *ctx;
     328             :   CURL *eh;
     329             :   char arg_str[sizeof (struct TALER_WireTransferIdentifierRawP) * 2 + 32];
     330             : 
     331           0 :   if (GNUNET_YES !=
     332           0 :       TEAH_handle_is_ready (exchange))
     333             :   {
     334           0 :     GNUNET_break (0);
     335           0 :     return NULL;
     336             :   }
     337             : 
     338           0 :   wdh = GNUNET_new (struct TALER_EXCHANGE_TransfersGetHandle);
     339           0 :   wdh->exchange = exchange;
     340           0 :   wdh->cb = cb;
     341           0 :   wdh->cb_cls = cb_cls;
     342             : 
     343             :   {
     344             :     char wtid_str[sizeof (struct TALER_WireTransferIdentifierRawP) * 2];
     345             :     char *end;
     346             : 
     347           0 :     end = GNUNET_STRINGS_data_to_string (wtid,
     348             :                                          sizeof (struct
     349             :                                                  TALER_WireTransferIdentifierRawP),
     350             :                                          wtid_str,
     351             :                                          sizeof (wtid_str));
     352           0 :     *end = '\0';
     353           0 :     GNUNET_snprintf (arg_str,
     354             :                      sizeof (arg_str),
     355             :                      "/transfers/%s",
     356             :                      wtid_str);
     357             :   }
     358           0 :   wdh->url = TEAH_path_to_url (wdh->exchange,
     359             :                                arg_str);
     360           0 :   if (NULL == wdh->url)
     361             :   {
     362           0 :     GNUNET_free (wdh);
     363           0 :     return NULL;
     364             :   }
     365           0 :   eh = TALER_EXCHANGE_curl_easy_get_ (wdh->url);
     366           0 :   if (NULL == eh)
     367             :   {
     368           0 :     GNUNET_break (0);
     369           0 :     GNUNET_free (wdh->url);
     370           0 :     GNUNET_free (wdh);
     371           0 :     return NULL;
     372             :   }
     373           0 :   ctx = TEAH_handle_to_context (exchange);
     374           0 :   wdh->job = GNUNET_CURL_job_add_with_ct_json (ctx,
     375             :                                                eh,
     376             :                                                &handle_transfers_get_finished,
     377             :                                                wdh);
     378           0 :   return wdh;
     379             : }
     380             : 
     381             : 
     382             : /**
     383             :  * Cancel wire deposits request.  This function cannot be used on a request
     384             :  * handle if a response is already served for it.
     385             :  *
     386             :  * @param wdh the wire deposits request handle
     387             :  */
     388             : void
     389           0 : TALER_EXCHANGE_transfers_get_cancel (
     390             :   struct TALER_EXCHANGE_TransfersGetHandle *wdh)
     391             : {
     392           0 :   if (NULL != wdh->job)
     393             :   {
     394           0 :     GNUNET_CURL_job_cancel (wdh->job);
     395           0 :     wdh->job = NULL;
     396             :   }
     397           0 :   GNUNET_free (wdh->url);
     398           0 :   GNUNET_free (wdh);
     399           0 : }
     400             : 
     401             : 
     402             : /* end of exchange_api_transfers_get.c */

Generated by: LCOV version 1.14