LCOV - code coverage report
Current view: top level - lib - merchant_api_tip_pickup2.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 133 0.0 %
Date: 2021-04-12 06:04:58 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-2017, 2020 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 <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : #include <taler/taler_curl_lib.h>
      33             : 
      34             : 
      35             : /**
      36             :  * @brief A handle for tracking transactions.
      37             :  */
      38             : struct TALER_MERCHANT_TipPickup2Handle
      39             : {
      40             : 
      41             :   /**
      42             :    * The url for this request.
      43             :    */
      44             :   char *url;
      45             : 
      46             :   /**
      47             :    * Minor context that holds body and headers.
      48             :    */
      49             :   struct TALER_CURL_PostContext post_ctx;
      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_MERCHANT_TipPickup2Callback cb;
      60             : 
      61             :   /**
      62             :    * Closure for @a cb.
      63             :    */
      64             :   void *cb_cls;
      65             : 
      66             :   /**
      67             :    * Reference to the execution context.
      68             :    */
      69             :   struct GNUNET_CURL_Context *ctx;
      70             : 
      71             :   /**
      72             :    * Expected number of planchets.
      73             :    */
      74             :   unsigned int num_planchets;
      75             : };
      76             : 
      77             : 
      78             : /**
      79             :  * We got a 200 response back from the exchange (or the merchant).
      80             :  * Now we need to parse the response and if it is well-formed,
      81             :  * call the callback (and set it to NULL afterwards).
      82             :  *
      83             :  * @param tpo handle of the original authorization operation
      84             :  * @param json cryptographic proof returned by the exchange/merchant
      85             :  * @return #GNUNET_OK if response is valid
      86             :  */
      87             : static int
      88           0 : check_ok (struct TALER_MERCHANT_TipPickup2Handle *tpo,
      89             :           const json_t *json)
      90             : {
      91             :   json_t *ja;
      92             :   unsigned int ja_len;
      93             :   struct GNUNET_JSON_Specification spec[] = {
      94           0 :     GNUNET_JSON_spec_json ("blind_sigs", &ja),
      95           0 :     GNUNET_JSON_spec_end ()
      96             :   };
      97           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      98             :     .http_status = MHD_HTTP_OK,
      99             :     .reply = json
     100             :   };
     101             : 
     102           0 :   if (GNUNET_OK !=
     103           0 :       GNUNET_JSON_parse (json,
     104             :                          spec,
     105             :                          NULL, NULL))
     106             :   {
     107           0 :     GNUNET_break_op (0);
     108           0 :     return GNUNET_SYSERR;
     109             :   }
     110           0 :   ja_len = json_array_size (ja);
     111           0 :   if (ja_len != tpo->num_planchets)
     112             :   {
     113           0 :     GNUNET_break_op (0);
     114           0 :     GNUNET_JSON_parse_free (spec);
     115           0 :     return GNUNET_SYSERR;
     116             :   }
     117           0 :   {
     118           0 :     struct TALER_MERCHANT_BlindSignature mblind_sigs[ja_len];
     119           0 :     struct GNUNET_CRYPTO_RsaSignature *blind_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 :         GNUNET_JSON_spec_rsa_signature ("blind_sig", &blind_sigs[i]),
     126           0 :         GNUNET_JSON_spec_end ()
     127             :       };
     128             : 
     129           0 :       if (GNUNET_OK !=
     130           0 :           GNUNET_JSON_parse (pj,
     131             :                              ispec,
     132             :                              NULL, NULL))
     133             :       {
     134           0 :         GNUNET_break_op (0);
     135           0 :         GNUNET_JSON_parse_free (spec);
     136           0 :         return GNUNET_SYSERR;
     137             :       }
     138           0 :       mblind_sigs[i].blind_sig = blind_sigs[i];
     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 :       GNUNET_CRYPTO_rsa_signature_free (blind_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 GNUNET_HashCode *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 = json_pack ("{"
     258             :                    " s:o," /* denom_pub_hash */
     259             :                    " s:o," /* coin_ev */
     260             :                    "}",
     261           0 :                    "denom_pub_hash", GNUNET_JSON_from_data_auto (
     262             :                      &planchet->denom_pub_hash),
     263           0 :                    "coin_ev", GNUNET_JSON_from_data (planchet->coin_ev,
     264             :                                                      planchet->coin_ev_size));
     265           0 :     if (NULL == p)
     266             :     {
     267           0 :       GNUNET_break (0);
     268           0 :       json_decref (pa);
     269           0 :       return NULL;
     270             :     }
     271           0 :     if (0 !=
     272           0 :         json_array_append_new (pa,
     273             :                                p))
     274             :     {
     275           0 :       GNUNET_break (0);
     276           0 :       json_decref (pa);
     277           0 :       return NULL;
     278             :     }
     279             :   }
     280           0 :   tp_obj = json_pack ("{s:o}",
     281             :                       "planchets", pa);
     282           0 :   if (NULL == tp_obj)
     283             :   {
     284           0 :     GNUNET_break (0);
     285           0 :     return NULL;
     286             :   }
     287           0 :   tpo = GNUNET_new (struct TALER_MERCHANT_TipPickup2Handle);
     288           0 :   tpo->num_planchets = num_planchets;
     289           0 :   tpo->ctx = ctx;
     290           0 :   tpo->cb = pickup_cb;
     291           0 :   tpo->cb_cls = pickup_cb_cls;
     292             : 
     293             :   {
     294             :     char tip_str[sizeof (*tip_id) * 2];
     295             :     char arg_str[sizeof (tip_str) + 32];
     296             :     char *end;
     297             : 
     298           0 :     end = GNUNET_STRINGS_data_to_string (tip_id,
     299             :                                          sizeof (*tip_id),
     300             :                                          tip_str,
     301             :                                          sizeof (tip_str));
     302           0 :     *end = '\0';
     303           0 :     GNUNET_snprintf (arg_str,
     304             :                      sizeof (arg_str),
     305             :                      "tips/%s/pickup",
     306             :                      tip_str);
     307           0 :     tpo->url = TALER_url_join (backend_url,
     308             :                                arg_str,
     309             :                                NULL);
     310             :   }
     311           0 :   if (NULL == tpo->url)
     312             :   {
     313           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     314             :                 "Could not construct request URL.\n");
     315           0 :     json_decref (tp_obj);
     316           0 :     GNUNET_free (tpo);
     317           0 :     return NULL;
     318             :   }
     319           0 :   eh = curl_easy_init ();
     320           0 :   if (GNUNET_OK != TALER_curl_easy_post (&tpo->post_ctx,
     321             :                                          eh,
     322             :                                          tp_obj))
     323             :   {
     324           0 :     GNUNET_break (0);
     325           0 :     json_decref (tp_obj);
     326           0 :     GNUNET_free (tpo->url);
     327           0 :     GNUNET_free (tpo);
     328           0 :     return NULL;
     329             :   }
     330           0 :   json_decref (tp_obj);
     331             : 
     332           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     333             :               "Requesting URL '%s'\n",
     334             :               tpo->url);
     335             : 
     336           0 :   GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     337             :                                                CURLOPT_URL,
     338             :                                                tpo->url));
     339           0 :   tpo->job = GNUNET_CURL_job_add2 (ctx,
     340             :                                    eh,
     341           0 :                                    tpo->post_ctx.headers,
     342             :                                    &handle_tip_pickup_finished,
     343             :                                    tpo);
     344           0 :   if (NULL == tpo->job)
     345             :   {
     346           0 :     TALER_MERCHANT_tip_pickup2_cancel (tpo);
     347           0 :     return NULL;
     348             :   }
     349           0 :   return tpo;
     350             : }
     351             : 
     352             : 
     353             : void
     354           0 : TALER_MERCHANT_tip_pickup2_cancel (
     355             :   struct TALER_MERCHANT_TipPickup2Handle *tpo)
     356             : {
     357           0 :   if (NULL != tpo->job)
     358             :   {
     359           0 :     GNUNET_CURL_job_cancel (tpo->job);
     360           0 :     tpo->job = NULL;
     361             :   }
     362           0 :   TALER_curl_easy_post_finished (&tpo->post_ctx);
     363           0 :   GNUNET_free (tpo->url);
     364           0 :   GNUNET_free (tpo);
     365           0 : }
     366             : 
     367             : 
     368             : /* end of merchant_api_tip_pickup2.c */

Generated by: LCOV version 1.14