LCOV - code coverage report
Current view: top level - lib - exchange_api_contracts_get.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 0 92 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) 2022 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_contracts_get.c
      19             :  * @brief Implementation of the /contracts/ GET 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_json_lib.h>
      27             : #include <gnunet/gnunet_curl_lib.h>
      28             : #include "taler_json_lib.h"
      29             : #include "taler_exchange_service.h"
      30             : #include "exchange_api_handle.h"
      31             : #include "taler_signatures.h"
      32             : #include "exchange_api_curl_defaults.h"
      33             : 
      34             : 
      35             : /**
      36             :  * @brief A Contract Get Handle
      37             :  */
      38             : struct TALER_EXCHANGE_ContractsGetHandle
      39             : {
      40             : 
      41             :   /**
      42             :    * The connection to exchange this request handle will use
      43             :    */
      44             :   struct TALER_EXCHANGE_Handle *exchange;
      45             : 
      46             :   /**
      47             :    * The url for this request.
      48             :    */
      49             :   char *url;
      50             : 
      51             :   /**
      52             :    * Handle for the request.
      53             :    */
      54             :   struct GNUNET_CURL_Job *job;
      55             : 
      56             :   /**
      57             :    * Function to call with the result.
      58             :    */
      59             :   TALER_EXCHANGE_ContractGetCallback cb;
      60             : 
      61             :   /**
      62             :    * Closure for @a cb.
      63             :    */
      64             :   void *cb_cls;
      65             : 
      66             :   /**
      67             :    * Private key needed to decrypt the contract.
      68             :    */
      69             :   struct TALER_ContractDiffiePrivateP contract_priv;
      70             : 
      71             :   /**
      72             :    * Public key matching @e contract_priv.
      73             :    */
      74             :   struct TALER_ContractDiffiePublicP cpub;
      75             : 
      76             : };
      77             : 
      78             : 
      79             : /**
      80             :  * Function called when we're done processing the
      81             :  * HTTP /track/transaction request.
      82             :  *
      83             :  * @param cls the `struct TALER_EXCHANGE_ContractsGetHandle`
      84             :  * @param response_code HTTP response code, 0 on error
      85             :  * @param response parsed JSON result, NULL on error
      86             :  */
      87             : static void
      88           0 : handle_contract_get_finished (void *cls,
      89             :                               long response_code,
      90             :                               const void *response)
      91             : {
      92           0 :   struct TALER_EXCHANGE_ContractsGetHandle *cgh = cls;
      93           0 :   const json_t *j = response;
      94           0 :   struct TALER_EXCHANGE_ContractGetResponse dr = {
      95             :     .hr.reply = j,
      96           0 :     .hr.http_status = (unsigned int) response_code
      97             :   };
      98             : 
      99           0 :   cgh->job = NULL;
     100           0 :   switch (response_code)
     101             :   {
     102           0 :   case 0:
     103           0 :     dr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     104           0 :     break;
     105           0 :   case MHD_HTTP_OK:
     106             :     {
     107             :       void *econtract;
     108             :       size_t econtract_size;
     109             :       struct TALER_PurseContractSignatureP econtract_sig;
     110             :       struct GNUNET_JSON_Specification spec[] = {
     111           0 :         GNUNET_JSON_spec_fixed_auto ("purse_pub",
     112             :                                      &dr.details.success.purse_pub),
     113           0 :         GNUNET_JSON_spec_fixed_auto ("econtract_sig",
     114             :                                      &econtract_sig),
     115           0 :         GNUNET_JSON_spec_varsize ("econtract",
     116             :                                   &econtract,
     117             :                                   &econtract_size),
     118           0 :         GNUNET_JSON_spec_end ()
     119             :       };
     120             : 
     121           0 :       if (GNUNET_OK !=
     122           0 :           GNUNET_JSON_parse (j,
     123             :                              spec,
     124             :                              NULL, NULL))
     125             :       {
     126           0 :         GNUNET_break_op (0);
     127           0 :         dr.hr.http_status = 0;
     128           0 :         dr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     129           0 :         break;
     130             :       }
     131           0 :       if (GNUNET_OK !=
     132           0 :           TALER_wallet_econtract_upload_verify (
     133             :             econtract,
     134             :             econtract_size,
     135           0 :             &cgh->cpub,
     136             :             &dr.details.success.purse_pub,
     137             :             &econtract_sig))
     138             :       {
     139           0 :         GNUNET_break (0);
     140           0 :         dr.hr.http_status = 0;
     141           0 :         dr.hr.ec = TALER_EC_EXCHANGE_CONTRACTS_SIGNATURE_INVALID;
     142           0 :         GNUNET_JSON_parse_free (spec);
     143           0 :         break;
     144             :       }
     145           0 :       dr.details.success.econtract = econtract;
     146           0 :       dr.details.success.econtract_size = econtract_size;
     147           0 :       cgh->cb (cgh->cb_cls,
     148             :                &dr);
     149           0 :       GNUNET_JSON_parse_free (spec);
     150           0 :       TALER_EXCHANGE_contract_get_cancel (cgh);
     151           0 :       return;
     152             :     }
     153           0 :   case MHD_HTTP_BAD_REQUEST:
     154           0 :     dr.hr.ec = TALER_JSON_get_error_code (j);
     155           0 :     dr.hr.hint = TALER_JSON_get_error_hint (j);
     156             :     /* This should never happen, either us or the exchange is buggy
     157             :        (or API version conflict); just pass JSON reply to the application */
     158           0 :     break;
     159           0 :   case MHD_HTTP_FORBIDDEN:
     160           0 :     dr.hr.ec = TALER_JSON_get_error_code (j);
     161           0 :     dr.hr.hint = TALER_JSON_get_error_hint (j);
     162             :     /* Nothing really to verify, exchange says one of the signatures is
     163             :        invalid; as we checked them, this should never happen, we
     164             :        should pass the JSON reply to the application */
     165           0 :     break;
     166           0 :   case MHD_HTTP_NOT_FOUND:
     167           0 :     dr.hr.ec = TALER_JSON_get_error_code (j);
     168           0 :     dr.hr.hint = TALER_JSON_get_error_hint (j);
     169             :     /* Exchange does not know about transaction;
     170             :        we should pass the reply to the application */
     171           0 :     break;
     172           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     173           0 :     dr.hr.ec = TALER_JSON_get_error_code (j);
     174           0 :     dr.hr.hint = TALER_JSON_get_error_hint (j);
     175             :     /* Server had an internal issue; we should retry, but this API
     176             :        leaves this to the application */
     177           0 :     break;
     178           0 :   default:
     179             :     /* unexpected response code */
     180           0 :     dr.hr.ec = TALER_JSON_get_error_code (j);
     181           0 :     dr.hr.hint = TALER_JSON_get_error_hint (j);
     182           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     183             :                 "Unexpected response code %u/%d for exchange GET contracts\n",
     184             :                 (unsigned int) response_code,
     185             :                 (int) dr.hr.ec);
     186           0 :     GNUNET_break_op (0);
     187           0 :     break;
     188             :   }
     189           0 :   cgh->cb (cgh->cb_cls,
     190             :            &dr);
     191           0 :   TALER_EXCHANGE_contract_get_cancel (cgh);
     192             : }
     193             : 
     194             : 
     195             : struct TALER_EXCHANGE_ContractsGetHandle *
     196           0 : TALER_EXCHANGE_contract_get (
     197             :   struct TALER_EXCHANGE_Handle *exchange,
     198             :   const struct TALER_ContractDiffiePrivateP *contract_priv,
     199             :   TALER_EXCHANGE_ContractGetCallback cb,
     200             :   void *cb_cls)
     201             : {
     202             :   struct TALER_EXCHANGE_ContractsGetHandle *cgh;
     203             :   CURL *eh;
     204             :   char arg_str[sizeof (cgh->cpub) * 2 + 48];
     205             : 
     206           0 :   if (GNUNET_YES !=
     207           0 :       TEAH_handle_is_ready (exchange))
     208             :   {
     209           0 :     GNUNET_break (0);
     210           0 :     return NULL;
     211             :   }
     212           0 :   cgh = GNUNET_new (struct TALER_EXCHANGE_ContractsGetHandle);
     213           0 :   cgh->exchange = exchange;
     214           0 :   cgh->cb = cb;
     215           0 :   cgh->cb_cls = cb_cls;
     216           0 :   GNUNET_CRYPTO_ecdhe_key_get_public (&contract_priv->ecdhe_priv,
     217             :                                       &cgh->cpub.ecdhe_pub);
     218             :   {
     219             :     char cpub_str[sizeof (cgh->cpub) * 2];
     220             :     char *end;
     221             : 
     222           0 :     end = GNUNET_STRINGS_data_to_string (&cgh->cpub,
     223             :                                          sizeof (cgh->cpub),
     224             :                                          cpub_str,
     225             :                                          sizeof (cpub_str));
     226           0 :     *end = '\0';
     227           0 :     GNUNET_snprintf (arg_str,
     228             :                      sizeof (arg_str),
     229             :                      "/contracts/%s",
     230             :                      cpub_str);
     231             :   }
     232             : 
     233           0 :   cgh->url = TEAH_path_to_url (exchange,
     234             :                                arg_str);
     235           0 :   if (NULL == cgh->url)
     236             :   {
     237           0 :     GNUNET_free (cgh);
     238           0 :     return NULL;
     239             :   }
     240           0 :   cgh->contract_priv = *contract_priv;
     241             : 
     242           0 :   eh = TALER_EXCHANGE_curl_easy_get_ (cgh->url);
     243           0 :   if (NULL == eh)
     244             :   {
     245           0 :     GNUNET_break (0);
     246           0 :     GNUNET_free (cgh->url);
     247           0 :     GNUNET_free (cgh);
     248           0 :     return NULL;
     249             :   }
     250           0 :   cgh->job = GNUNET_CURL_job_add (TEAH_handle_to_context (exchange),
     251             :                                   eh,
     252             :                                   &handle_contract_get_finished,
     253             :                                   cgh);
     254           0 :   return cgh;
     255             : }
     256             : 
     257             : 
     258             : void
     259           0 : TALER_EXCHANGE_contract_get_cancel (
     260             :   struct TALER_EXCHANGE_ContractsGetHandle *cgh)
     261             : {
     262           0 :   if (NULL != cgh->job)
     263             :   {
     264           0 :     GNUNET_CURL_job_cancel (cgh->job);
     265           0 :     cgh->job = NULL;
     266             :   }
     267           0 :   GNUNET_free (cgh->url);
     268           0 :   GNUNET_free (cgh);
     269           0 : }
     270             : 
     271             : 
     272             : /* end of exchange_api_contracts_get.c */

Generated by: LCOV version 1.14