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 124 0.0 %
Date: 2020-08-15 06:12:35 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 lib/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 json 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_INVALID_RESPONSE;
     183             :     }
     184           0 :     break;
     185           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     186             :     /* Server had an internal issue; we should retry, but this API
     187             :        leaves this to the application */
     188           0 :     hr.ec = TALER_JSON_get_error_code (json);
     189           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     190           0 :     break;
     191           0 :   case MHD_HTTP_CONFLICT:
     192             :     /* legal, can happen if we pickup a tip twice... */
     193           0 :     hr.ec = TALER_JSON_get_error_code (json);
     194           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     195           0 :     break;
     196           0 :   case MHD_HTTP_NOT_FOUND:
     197             :     /* legal, can happen if tip ID is unknown */
     198           0 :     hr.ec = TALER_JSON_get_error_code (json);
     199           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     200           0 :     break;
     201           0 :   default:
     202             :     /* unexpected response code */
     203           0 :     GNUNET_break_op (0);
     204           0 :     TALER_MERCHANT_parse_error_details_ (json,
     205             :                                          response_code,
     206             :                                          &hr);
     207           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     208             :                 "Unexpected response code %u/%d\n",
     209             :                 (unsigned int) response_code,
     210             :                 (int) hr.ec);
     211           0 :     break;
     212             :   }
     213           0 :   if (NULL != tpo->cb)
     214             :   {
     215           0 :     tpo->cb (tpo->cb_cls,
     216             :              &hr,
     217             :              0,
     218             :              NULL);
     219           0 :     tpo->cb = NULL;
     220             :   }
     221           0 :   TALER_MERCHANT_tip_pickup2_cancel (tpo);
     222           0 : }
     223             : 
     224             : 
     225             : /**
     226             :  * Issue a /tips/$ID/tip-pickup request to the backend.  Informs the backend
     227             :  * that a customer wants to pick up a tip.
     228             :  *
     229             :  * @param ctx execution context
     230             :  * @param backend_url base URL of the merchant backend
     231             :  * @param tip_id unique identifier for the tip
     232             :  * @param num_planches number of planchets provided in @a planchets
     233             :  * @param planchets array of planchets to be signed into existence for the tip
     234             :  * @param pickup_cb callback which will work the response gotten from the backend
     235             :  * @param pickup_cb_cls closure to pass to @a pickup_cb
     236             :  * @return handle for this operation, NULL upon errors
     237             :  */
     238             : struct TALER_MERCHANT_TipPickup2Handle *
     239           0 : TALER_MERCHANT_tip_pickup2 (struct GNUNET_CURL_Context *ctx,
     240             :                             const char *backend_url,
     241             :                             const struct GNUNET_HashCode *tip_id,
     242             :                             unsigned int num_planchets,
     243             :                             const struct TALER_PlanchetDetail planchets[],
     244             :                             TALER_MERCHANT_TipPickup2Callback pickup_cb,
     245             :                             void *pickup_cb_cls)
     246             : {
     247             :   struct TALER_MERCHANT_TipPickup2Handle *tpo;
     248             :   CURL *eh;
     249             :   json_t *pa;
     250             :   json_t *tp_obj;
     251             : 
     252           0 :   if (0 == num_planchets)
     253             :   {
     254           0 :     GNUNET_break (0);
     255           0 :     return NULL;
     256             :   }
     257           0 :   pa = json_array ();
     258           0 :   for (unsigned int i = 0; i<num_planchets; i++)
     259             :   {
     260           0 :     const struct TALER_PlanchetDetail *planchet = &planchets[i];
     261             :     json_t *p;
     262             : 
     263           0 :     p = json_pack ("{"
     264             :                    " s:o," /* denom_pub_hash */
     265             :                    " s:o," /* coin_ev */
     266             :                    "}",
     267           0 :                    "denom_pub_hash", GNUNET_JSON_from_data_auto (
     268             :                      &planchet->denom_pub_hash),
     269           0 :                    "coin_ev", GNUNET_JSON_from_data (planchet->coin_ev,
     270             :                                                      planchet->coin_ev_size));
     271           0 :     if (NULL == p)
     272             :     {
     273           0 :       GNUNET_break (0);
     274           0 :       json_decref (pa);
     275           0 :       return NULL;
     276             :     }
     277           0 :     if (0 !=
     278           0 :         json_array_append_new (pa,
     279             :                                p))
     280             :     {
     281           0 :       GNUNET_break (0);
     282           0 :       json_decref (pa);
     283           0 :       return NULL;
     284             :     }
     285             :   }
     286           0 :   tp_obj = json_pack ("{s:o}",
     287             :                       "planchets", pa);
     288           0 :   if (NULL == tp_obj)
     289             :   {
     290           0 :     GNUNET_break (0);
     291           0 :     return NULL;
     292             :   }
     293           0 :   tpo = GNUNET_new (struct TALER_MERCHANT_TipPickup2Handle);
     294           0 :   tpo->num_planchets = num_planchets;
     295           0 :   tpo->ctx = ctx;
     296           0 :   tpo->cb = pickup_cb;
     297           0 :   tpo->cb_cls = pickup_cb_cls;
     298             : 
     299             :   {
     300             :     char tip_str[sizeof (*tip_id) * 2];
     301             :     char arg_str[sizeof (tip_str) + 32];
     302             :     char *end;
     303             : 
     304           0 :     end = GNUNET_STRINGS_data_to_string (tip_id,
     305             :                                          sizeof (*tip_id),
     306             :                                          tip_str,
     307             :                                          sizeof (tip_str));
     308           0 :     *end = '\0';
     309           0 :     GNUNET_snprintf (arg_str,
     310             :                      sizeof (arg_str),
     311             :                      "tips/%s/pickup",
     312             :                      tip_str);
     313           0 :     tpo->url = TALER_url_join (backend_url,
     314             :                                arg_str,
     315             :                                NULL);
     316             :   }
     317           0 :   if (NULL == tpo->url)
     318             :   {
     319           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     320             :                 "Could not construct request URL.\n");
     321           0 :     json_decref (tp_obj);
     322           0 :     GNUNET_free (tpo);
     323           0 :     return NULL;
     324             :   }
     325           0 :   eh = curl_easy_init ();
     326           0 :   if (GNUNET_OK != TALER_curl_easy_post (&tpo->post_ctx,
     327             :                                          eh,
     328             :                                          tp_obj))
     329             :   {
     330           0 :     GNUNET_break (0);
     331           0 :     json_decref (tp_obj);
     332           0 :     GNUNET_free (tpo);
     333           0 :     return NULL;
     334             :   }
     335           0 :   json_decref (tp_obj);
     336             : 
     337           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     338             :               "Requesting URL '%s'\n",
     339             :               tpo->url);
     340             : 
     341           0 :   GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     342             :                                                CURLOPT_URL,
     343             :                                                tpo->url));
     344           0 :   tpo->job = GNUNET_CURL_job_add2 (ctx,
     345             :                                    eh,
     346           0 :                                    tpo->post_ctx.headers,
     347             :                                    &handle_tip_pickup_finished,
     348             :                                    tpo);
     349           0 :   return tpo;
     350             : }
     351             : 
     352             : 
     353             : /**
     354             :  * Cancel a /tips/$TIP_ID/pickup request.  This function cannot be used
     355             :  * on a request handle if a response is already served for it.
     356             :  *
     357             :  * @param tpo handle to the tracking operation being cancelled
     358             :  */
     359             : void
     360           0 : TALER_MERCHANT_tip_pickup2_cancel (
     361             :   struct TALER_MERCHANT_TipPickup2Handle *tpo)
     362             : {
     363           0 :   if (NULL != tpo->job)
     364             :   {
     365           0 :     GNUNET_CURL_job_cancel (tpo->job);
     366           0 :     tpo->job = NULL;
     367             :   }
     368           0 :   TALER_curl_easy_post_finished (&tpo->post_ctx);
     369           0 :   GNUNET_free (tpo->url);
     370           0 :   GNUNET_free (tpo);
     371           0 : }
     372             : 
     373             : 
     374             : /* end of merchant_api_tip_pickup2.c */

Generated by: LCOV version 1.14