LCOV - code coverage report
Current view: top level - lib - merchant_api_tip_pickup2.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 123 0.0 %
Date: 2022-08-25 06:17:04 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 Lesser General Public License as published by the Free Software
       7             :   Foundation; either version 2.1, 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 Lesser General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Lesser General Public License along with
      14             :   TALER; see the file COPYING.LGPL.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file merchant_api_tip_pickup2.c
      19             :  * @brief Implementation of the /tip-pickup request of the merchant's HTTP API
      20             :  * @author Marcello Stanisci
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <curl/curl.h>
      25             : #include <jansson.h>
      26             : #include <microhttpd.h> /* just for HTTP status codes */
      27             : #include <gnunet/gnunet_util_lib.h>
      28             : #include <gnunet/gnunet_curl_lib.h>
      29             : #include "taler_merchant_service.h"
      30             : #include "merchant_api_curl_defaults.h"
      31             : #include <taler/taler_json_lib.h>
      32             : #include <taler/taler_signatures.h>
      33             : #include <taler/taler_curl_lib.h>
      34             : 
      35             : 
      36             : /**
      37             :  * @brief A handle for tracking transactions.
      38             :  */
      39             : struct TALER_MERCHANT_TipPickup2Handle
      40             : {
      41             : 
      42             :   /**
      43             :    * The url for this request.
      44             :    */
      45             :   char *url;
      46             : 
      47             :   /**
      48             :    * Minor context that holds body and headers.
      49             :    */
      50             :   struct TALER_CURL_PostContext post_ctx;
      51             : 
      52             :   /**
      53             :    * Handle for the request.
      54             :    */
      55             :   struct GNUNET_CURL_Job *job;
      56             : 
      57             :   /**
      58             :    * Function to call with the result.
      59             :    */
      60             :   TALER_MERCHANT_TipPickup2Callback cb;
      61             : 
      62             :   /**
      63             :    * Closure for @a cb.
      64             :    */
      65             :   void *cb_cls;
      66             : 
      67             :   /**
      68             :    * Reference to the execution context.
      69             :    */
      70             :   struct GNUNET_CURL_Context *ctx;
      71             : 
      72             :   /**
      73             :    * Expected number of planchets.
      74             :    */
      75             :   unsigned int num_planchets;
      76             : };
      77             : 
      78             : 
      79             : /**
      80             :  * We got a 200 response back from the exchange (or the merchant).
      81             :  * Now we need to parse the response and if it is well-formed,
      82             :  * call the callback (and set it to NULL afterwards).
      83             :  *
      84             :  * @param tpo handle of the original authorization operation
      85             :  * @param json cryptographic proof returned by the exchange/merchant
      86             :  * @return #GNUNET_OK if response is valid
      87             :  */
      88             : static int
      89           0 : check_ok (struct TALER_MERCHANT_TipPickup2Handle *tpo,
      90             :           const json_t *json)
      91             : {
      92             :   json_t *ja;
      93             :   unsigned int ja_len;
      94             :   struct GNUNET_JSON_Specification spec[] = {
      95           0 :     GNUNET_JSON_spec_json ("blind_sigs", &ja),
      96           0 :     GNUNET_JSON_spec_end ()
      97             :   };
      98           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      99             :     .http_status = MHD_HTTP_OK,
     100             :     .reply = json
     101             :   };
     102             : 
     103           0 :   if (GNUNET_OK !=
     104           0 :       GNUNET_JSON_parse (json,
     105             :                          spec,
     106             :                          NULL, NULL))
     107             :   {
     108           0 :     GNUNET_break_op (0);
     109           0 :     return GNUNET_SYSERR;
     110             :   }
     111           0 :   ja_len = json_array_size (ja);
     112           0 :   if (ja_len != tpo->num_planchets)
     113             :   {
     114           0 :     GNUNET_break_op (0);
     115           0 :     GNUNET_JSON_parse_free (spec);
     116           0 :     return GNUNET_SYSERR;
     117             :   }
     118           0 :   {
     119           0 :     struct TALER_BlindedDenominationSignature mblind_sigs[ja_len];
     120             : 
     121           0 :     for (unsigned int i = 0; i<ja_len; i++)
     122             :     {
     123           0 :       json_t *pj = json_array_get (ja, i);
     124             :       struct GNUNET_JSON_Specification ispec[] = {
     125           0 :         TALER_JSON_spec_blinded_denom_sig ("blind_sig",
     126             :                                            &mblind_sigs[i]),
     127           0 :         GNUNET_JSON_spec_end ()
     128             :       };
     129             : 
     130           0 :       if (GNUNET_OK !=
     131           0 :           GNUNET_JSON_parse (pj,
     132             :                              ispec,
     133             :                              NULL, NULL))
     134             :       {
     135           0 :         GNUNET_break_op (0);
     136           0 :         GNUNET_JSON_parse_free (spec);
     137           0 :         return GNUNET_SYSERR;
     138             :       }
     139             :     }
     140           0 :     tpo->cb (tpo->cb_cls,
     141             :              &hr,
     142             :              ja_len,
     143             :              mblind_sigs);
     144           0 :     for (unsigned int i = 0; i<ja_len; i++)
     145           0 :       TALER_blinded_denom_sig_free (&mblind_sigs[i]);
     146           0 :     tpo->cb = NULL; /* do not call twice */
     147             :   }
     148           0 :   GNUNET_JSON_parse_free (spec);
     149           0 :   return GNUNET_OK;
     150             : }
     151             : 
     152             : 
     153             : /**
     154             :  * Function called when we're done processing the
     155             :  * HTTP /track/transaction request.
     156             :  *
     157             :  * @param cls the `struct TALER_MERCHANT_TipPickupHandle`
     158             :  * @param response_code HTTP response code, 0 on error
     159             :  * @param response response body, NULL if not in JSON
     160             :  */
     161             : static void
     162           0 : handle_tip_pickup_finished (void *cls,
     163             :                             long response_code,
     164             :                             const void *response)
     165             : {
     166           0 :   struct TALER_MERCHANT_TipPickup2Handle *tpo = cls;
     167           0 :   const json_t *json = response;
     168           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     169           0 :     .http_status = (unsigned int) response_code,
     170             :     .reply = json
     171             :   };
     172             : 
     173           0 :   tpo->job = NULL;
     174           0 :   switch (response_code)
     175             :   {
     176           0 :   case MHD_HTTP_OK:
     177           0 :     if (GNUNET_OK != check_ok (tpo,
     178             :                                json))
     179             :     {
     180           0 :       GNUNET_break_op (0);
     181           0 :       hr.http_status = 0;
     182           0 :       hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     183             :     }
     184           0 :     break;
     185           0 :   case MHD_HTTP_BAD_REQUEST:
     186             :     /* Can happen if we pickup an amount that exceeds the tip... */
     187           0 :     hr.ec = TALER_JSON_get_error_code (json);
     188           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     189           0 :     GNUNET_break (TALER_EC_MERCHANT_TIP_PICKUP_AMOUNT_EXCEEDS_TIP_REMAINING ==
     190             :                   hr.ec);
     191           0 :     break;
     192           0 :   case MHD_HTTP_CONFLICT:
     193             :     /* legal, can happen if we pickup a tip twice... */
     194           0 :     hr.ec = TALER_JSON_get_error_code (json);
     195           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     196           0 :     break;
     197           0 :   case MHD_HTTP_NOT_FOUND:
     198             :     /* legal, can happen if tip ID is unknown */
     199           0 :     hr.ec = TALER_JSON_get_error_code (json);
     200           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     201           0 :     break;
     202           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     203             :     /* Server had an internal issue; we should retry, but this API
     204             :        leaves this to the application */
     205           0 :     hr.ec = TALER_JSON_get_error_code (json);
     206           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     207           0 :     break;
     208           0 :   default:
     209             :     /* unexpected response code */
     210           0 :     GNUNET_break_op (0);
     211           0 :     TALER_MERCHANT_parse_error_details_ (json,
     212             :                                          response_code,
     213             :                                          &hr);
     214           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     215             :                 "Unexpected response code %u/%d\n",
     216             :                 (unsigned int) response_code,
     217             :                 (int) hr.ec);
     218           0 :     break;
     219             :   }
     220           0 :   if (NULL != tpo->cb)
     221             :   {
     222           0 :     tpo->cb (tpo->cb_cls,
     223             :              &hr,
     224             :              0,
     225             :              NULL);
     226           0 :     tpo->cb = NULL;
     227             :   }
     228           0 :   TALER_MERCHANT_tip_pickup2_cancel (tpo);
     229           0 : }
     230             : 
     231             : 
     232             : struct TALER_MERCHANT_TipPickup2Handle *
     233           0 : TALER_MERCHANT_tip_pickup2 (struct GNUNET_CURL_Context *ctx,
     234             :                             const char *backend_url,
     235             :                             const struct TALER_TipIdentifierP *tip_id,
     236             :                             unsigned int num_planchets,
     237             :                             const struct TALER_PlanchetDetail planchets[],
     238             :                             TALER_MERCHANT_TipPickup2Callback pickup_cb,
     239             :                             void *pickup_cb_cls)
     240             : {
     241             :   struct TALER_MERCHANT_TipPickup2Handle *tpo;
     242             :   CURL *eh;
     243             :   json_t *pa;
     244             :   json_t *tp_obj;
     245             : 
     246           0 :   if (0 == num_planchets)
     247             :   {
     248           0 :     GNUNET_break (0);
     249           0 :     return NULL;
     250             :   }
     251           0 :   pa = json_array ();
     252           0 :   for (unsigned int i = 0; i<num_planchets; i++)
     253             :   {
     254           0 :     const struct TALER_PlanchetDetail *planchet = &planchets[i];
     255             :     json_t *p;
     256             : 
     257           0 :     p = GNUNET_JSON_PACK (
     258             :       GNUNET_JSON_pack_data_auto ("denom_pub_hash",
     259             :                                   &planchet->denom_pub_hash),
     260             :       TALER_JSON_pack_blinded_planchet ("coin_ev",
     261             :                                         &planchet->blinded_planchet));
     262           0 :     if (0 !=
     263           0 :         json_array_append_new (pa,
     264             :                                p))
     265             :     {
     266           0 :       GNUNET_break (0);
     267           0 :       json_decref (pa);
     268           0 :       return NULL;
     269             :     }
     270             :   }
     271           0 :   tp_obj = GNUNET_JSON_PACK (
     272             :     GNUNET_JSON_pack_array_steal ("planchets",
     273             :                                   pa));
     274           0 :   tpo = GNUNET_new (struct TALER_MERCHANT_TipPickup2Handle);
     275           0 :   tpo->num_planchets = num_planchets;
     276           0 :   tpo->ctx = ctx;
     277           0 :   tpo->cb = pickup_cb;
     278           0 :   tpo->cb_cls = pickup_cb_cls;
     279             : 
     280             :   {
     281             :     char tip_str[sizeof (*tip_id) * 2];
     282             :     char arg_str[sizeof (tip_str) + 32];
     283             :     char *end;
     284             : 
     285           0 :     end = GNUNET_STRINGS_data_to_string (tip_id,
     286             :                                          sizeof (*tip_id),
     287             :                                          tip_str,
     288             :                                          sizeof (tip_str));
     289           0 :     *end = '\0';
     290           0 :     GNUNET_snprintf (arg_str,
     291             :                      sizeof (arg_str),
     292             :                      "tips/%s/pickup",
     293             :                      tip_str);
     294           0 :     tpo->url = TALER_url_join (backend_url,
     295             :                                arg_str,
     296             :                                NULL);
     297             :   }
     298           0 :   if (NULL == tpo->url)
     299             :   {
     300           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     301             :                 "Could not construct request URL.\n");
     302           0 :     json_decref (tp_obj);
     303           0 :     GNUNET_free (tpo);
     304           0 :     return NULL;
     305             :   }
     306           0 :   eh = TALER_MERCHANT_curl_easy_get_ (tpo->url);
     307           0 :   if (GNUNET_OK !=
     308           0 :       TALER_curl_easy_post (&tpo->post_ctx,
     309             :                             eh,
     310             :                             tp_obj))
     311             :   {
     312           0 :     GNUNET_break (0);
     313           0 :     json_decref (tp_obj);
     314           0 :     curl_easy_cleanup (eh);
     315           0 :     GNUNET_free (tpo->url);
     316           0 :     GNUNET_free (tpo);
     317           0 :     return NULL;
     318             :   }
     319           0 :   json_decref (tp_obj);
     320           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     321             :               "Requesting URL '%s'\n",
     322             :               tpo->url);
     323           0 :   tpo->job = GNUNET_CURL_job_add2 (ctx,
     324             :                                    eh,
     325           0 :                                    tpo->post_ctx.headers,
     326             :                                    &handle_tip_pickup_finished,
     327             :                                    tpo);
     328           0 :   if (NULL == tpo->job)
     329             :   {
     330           0 :     TALER_MERCHANT_tip_pickup2_cancel (tpo);
     331           0 :     return NULL;
     332             :   }
     333           0 :   return tpo;
     334             : }
     335             : 
     336             : 
     337             : void
     338           0 : TALER_MERCHANT_tip_pickup2_cancel (
     339             :   struct TALER_MERCHANT_TipPickup2Handle *tpo)
     340             : {
     341           0 :   if (NULL != tpo->job)
     342             :   {
     343           0 :     GNUNET_CURL_job_cancel (tpo->job);
     344           0 :     tpo->job = NULL;
     345             :   }
     346           0 :   TALER_curl_easy_post_finished (&tpo->post_ctx);
     347           0 :   GNUNET_free (tpo->url);
     348           0 :   GNUNET_free (tpo);
     349           0 : }
     350             : 
     351             : 
     352             : /* end of merchant_api_tip_pickup2.c */

Generated by: LCOV version 1.14