LCOV - code coverage report
Current view: top level - lib - merchant_api_tip_pickup.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 66 96 68.8 %
Date: 2018-09-19 06:18:31 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2017 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_pickup.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             : 
      33             : 
      34             : /**
      35             :  * @brief A handle for tracking transactions.
      36             :  */
      37             : struct TALER_MERCHANT_TipPickupOperation
      38             : {
      39             : 
      40             :   /**
      41             :    * The url for this request.
      42             :    */
      43             :   char *url;
      44             : 
      45             :   /**
      46             :    * JSON encoding of the request to POST.
      47             :    */
      48             :   char *json_enc;
      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_MERCHANT_TipPickupCallback cb;
      59             : 
      60             :   /**
      61             :    * Closure for @a cb.
      62             :    */
      63             :   void *cb_cls;
      64             : 
      65             :   /**
      66             :    * Reference to the execution context.
      67             :    */
      68             :   struct GNUNET_CURL_Context *ctx;
      69             : 
      70             :   /**
      71             :    * Expected number of planchets.
      72             :    */
      73             :   unsigned int num_planchets;
      74             : };
      75             : 
      76             : 
      77             : /**
      78             :  * We got a 200 response back from the exchange (or the merchant).
      79             :  * Now we need to parse the response and if it is well-formed,
      80             :  * call the callback (and set it to NULL afterwards).
      81             :  *
      82             :  * @param tpo handle of the original authorization operation
      83             :  * @param json cryptographic proof returned by the exchange/merchant
      84             :  * @return #GNUNET_OK if response is valid
      85             :  */
      86             : static int
      87           8 : check_ok (struct TALER_MERCHANT_TipPickupOperation *tpo,
      88             :           const json_t *json)
      89             : {
      90             :   struct TALER_ReservePublicKeyP reserve_pub;
      91             :   json_t *ja;
      92           8 :   struct GNUNET_JSON_Specification spec[] = {
      93             :     GNUNET_JSON_spec_fixed_auto ("reserve_pub", &reserve_pub),
      94             :     GNUNET_JSON_spec_json ("reserve_sigs", &ja),
      95             :     GNUNET_JSON_spec_end()
      96             :   };
      97             :   unsigned int ja_len;
      98             : 
      99           8 :   if (GNUNET_OK !=
     100           8 :       GNUNET_JSON_parse (json,
     101             :                          spec,
     102             :                          NULL, NULL))
     103             :   {
     104           0 :     GNUNET_break_op (0);
     105           0 :     return GNUNET_SYSERR;
     106             :   }
     107           8 :   ja_len = json_array_size (ja);
     108           8 :   if (ja_len != tpo->num_planchets)
     109             :   {
     110           0 :     GNUNET_break_op (0);
     111           0 :     return GNUNET_SYSERR;
     112             :   }
     113           8 :   {
     114           8 :     struct TALER_ReserveSignatureP reserve_sigs[ja_len];
     115             : 
     116          16 :     for (unsigned int i=0;i<ja_len;i++)
     117             :     {
     118           8 :       json_t *pj = json_array_get (ja, i);
     119             : 
     120           8 :       struct GNUNET_JSON_Specification spec[] = {
     121           8 :         GNUNET_JSON_spec_fixed_auto ("reserve_sig", &reserve_sigs[i]),
     122             :         GNUNET_JSON_spec_end()
     123             :       };
     124             : 
     125           8 :       if (GNUNET_OK !=
     126           8 :           GNUNET_JSON_parse (pj,
     127             :                              spec,
     128             :                              NULL, NULL))
     129             :       {
     130           0 :         GNUNET_break_op (0);
     131           0 :         return GNUNET_SYSERR;
     132             :       }
     133             :     }
     134           8 :     tpo->cb (tpo->cb_cls,
     135             :              MHD_HTTP_OK,
     136             :              TALER_JSON_get_error_code (json),
     137             :              &reserve_pub,
     138             :              ja_len,
     139             :              reserve_sigs,
     140             :              json);
     141           8 :     tpo->cb = NULL; /* do not call twice */
     142             :   }
     143           8 :   GNUNET_JSON_parse_free (spec);
     144           8 :   return GNUNET_OK;
     145             : }
     146             : 
     147             : 
     148             : /**
     149             :  * Function called when we're done processing the
     150             :  * HTTP /track/transaction request.
     151             :  *
     152             :  * @param cls the `struct TALER_MERCHANT_TipPickupOperation`
     153             :  * @param response_code HTTP response code, 0 on error
     154             :  * @param json response body, NULL if not in JSON
     155             :  */
     156             : static void
     157          12 : handle_tip_pickup_finished (void *cls,
     158             :                             long response_code,
     159             :                             const json_t *json)
     160             : {
     161          12 :   struct TALER_MERCHANT_TipPickupOperation *tpo = cls;
     162             : 
     163          12 :   tpo->job = NULL;
     164          12 :   switch (response_code)
     165             :   {
     166             :   case MHD_HTTP_OK:
     167           8 :     if (GNUNET_OK != check_ok (tpo,
     168             :                                json))
     169             :     {
     170           0 :       GNUNET_break_op (0);
     171           0 :       response_code = 0;
     172             :     }
     173           8 :     break;
     174             :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     175             :     /* Server had an internal issue; we should retry, but this API
     176             :        leaves this to the application */
     177           0 :     break;
     178             :   case MHD_HTTP_CONFLICT:
     179             :     /* legal, can happen if we pickup a tip twice... */
     180           3 :     break;
     181             :   case MHD_HTTP_NOT_FOUND:
     182             :     /* legal, can happen if tip ID is unknown */
     183           1 :     break;
     184             :   default:
     185             :     /* unexpected response code */
     186           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     187             :                 "Unexpected response code %u\n",
     188             :                 (unsigned int) response_code);
     189           0 :     GNUNET_break (0);
     190           0 :     response_code = 0;
     191           0 :     break;
     192             :   }
     193          12 :   if (NULL != tpo->cb)
     194             :   {
     195           4 :     tpo->cb (tpo->cb_cls,
     196             :              response_code,
     197             :              TALER_JSON_get_error_code (json),
     198             :              NULL,
     199             :              0,
     200             :              NULL,
     201             :              json);
     202           4 :     tpo->cb = NULL;
     203             :   }
     204          12 :   TALER_MERCHANT_tip_pickup_cancel (tpo);
     205          12 : }
     206             : 
     207             : 
     208             : /**
     209             :  * Issue a /tip-pickup request to the backend.  Informs the backend
     210             :  * that a customer wants to pick up a tip.
     211             :  *
     212             :  * @param ctx execution context
     213             :  * @param backend_url base URL of the merchant backend
     214             :  * @param tip_id unique identifier for the tip
     215             :  * @param num_planches number of planchets provided in @a planchets
     216             :  * @param planchets array of planchets to be signed into existence for the tip
     217             :  * @param pickup_cb callback which will work the response gotten from the backend
     218             :  * @param pickup_cb_cls closure to pass to @a pickup_cb
     219             :  * @return handle for this operation, NULL upon errors
     220             :  */
     221             : struct TALER_MERCHANT_TipPickupOperation *
     222          12 : TALER_MERCHANT_tip_pickup (struct GNUNET_CURL_Context *ctx,
     223             :                            const char *backend_url,
     224             :                            const struct GNUNET_HashCode *tip_id,
     225             :                            unsigned int num_planchets,
     226             :                            struct TALER_PlanchetDetail *planchets,
     227             :                            TALER_MERCHANT_TipPickupCallback pickup_cb,
     228             :                            void *pickup_cb_cls)
     229             : {
     230             :   struct TALER_MERCHANT_TipPickupOperation *tpo;
     231             :   CURL *eh;
     232             :   json_t *pa;
     233             :   json_t *tp_obj;
     234             : 
     235          12 :   if (0 == num_planchets)
     236             :   {
     237           0 :     GNUNET_break (0);
     238           0 :     return NULL;
     239             :   }
     240          12 :   pa = json_array ();
     241          24 :   for (unsigned int i=0;i<num_planchets;i++)
     242             :   {
     243          12 :     const struct TALER_PlanchetDetail *planchet = &planchets[i];
     244             :     json_t *p;
     245             : 
     246          36 :     p = json_pack ("{"
     247             :                    " s:o," /* denom_pub_hash */
     248             :                    " s:o," /* coin_ev */
     249             :                    "}",
     250          12 :                    "denom_pub_hash", GNUNET_JSON_from_data_auto (&planchet->denom_pub_hash),
     251          12 :                    "coin_ev", GNUNET_JSON_from_data (planchet->coin_ev,
     252             :                                                      planchet->coin_ev_size));
     253          12 :     if (NULL == p)
     254             :     {
     255           0 :       GNUNET_break (0);
     256           0 :       json_decref (pa);
     257           0 :       return NULL;
     258             :     }
     259          12 :     if (0 !=
     260          12 :         json_array_append_new (pa,
     261             :                                p))
     262             :     {
     263           0 :       GNUNET_break (0);
     264           0 :       json_decref (pa);
     265           0 :       return NULL;
     266             :     }
     267             :   }
     268          12 :   tp_obj = json_pack ("{"
     269             :                       " s:o," /* tip_id */
     270             :                       " s:o," /* planchets */
     271             :                       "}",
     272             :                       "tip_id", GNUNET_JSON_from_data_auto (tip_id),
     273             :                       "planchets", pa);
     274          12 :   if (NULL == tp_obj)
     275             :   {
     276           0 :     GNUNET_break (0);
     277           0 :     return NULL;
     278             :   }
     279          12 :   tpo = GNUNET_new (struct TALER_MERCHANT_TipPickupOperation);
     280          12 :   tpo->num_planchets = num_planchets;
     281          12 :   tpo->ctx = ctx;
     282          12 :   tpo->cb = pickup_cb;
     283          12 :   tpo->cb_cls = pickup_cb_cls;
     284          12 :   tpo->url = TALER_url_join (backend_url, "/public/tip-pickup", NULL);
     285          12 :   if (NULL == (tpo->json_enc =
     286          12 :                json_dumps (tp_obj,
     287             :                            JSON_COMPACT)))
     288             :   {
     289           0 :     GNUNET_break (0);
     290           0 :     json_decref (tp_obj);
     291           0 :     GNUNET_free (tpo->url);
     292           0 :     GNUNET_free (tpo);
     293           0 :     return NULL;
     294             :   }
     295          12 :   json_decref (tp_obj);
     296          12 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     297             :               "Requesting URL '%s'\n",
     298             :               tpo->url);
     299          12 :   eh = curl_easy_init ();
     300          12 :   GNUNET_assert (CURLE_OK ==
     301             :                  curl_easy_setopt (eh,
     302             :                                    CURLOPT_URL,
     303             :                                    tpo->url));
     304          12 :   GNUNET_assert (CURLE_OK ==
     305             :                  curl_easy_setopt (eh,
     306             :                                    CURLOPT_POSTFIELDS,
     307             :                                    tpo->json_enc));
     308          12 :   GNUNET_assert (CURLE_OK ==
     309             :                  curl_easy_setopt (eh,
     310             :                                    CURLOPT_POSTFIELDSIZE,
     311             :                                    strlen (tpo->json_enc)));
     312          12 :   tpo->job = GNUNET_CURL_job_add (ctx,
     313             :                                   eh,
     314             :                                   GNUNET_YES,
     315             :                                   &handle_tip_pickup_finished,
     316             :                                   tpo);
     317          12 :   return tpo;
     318             : }
     319             : 
     320             : 
     321             : /**
     322             :  * Cancel a /track/transaction request.  This function cannot be used
     323             :  * on a request handle if a response is already served for it.
     324             :  *
     325             :  * @param tpo handle to the tracking operation being cancelled
     326             :  */
     327             : void
     328          12 : TALER_MERCHANT_tip_pickup_cancel (struct TALER_MERCHANT_TipPickupOperation *tpo)
     329             : {
     330          12 :   if (NULL != tpo->job)
     331             :   {
     332           0 :     GNUNET_CURL_job_cancel (tpo->job);
     333           0 :     tpo->job = NULL;
     334             :   }
     335          12 :   GNUNET_free_non_null (tpo->json_enc);
     336          12 :   GNUNET_free (tpo->url);
     337          12 :   GNUNET_free (tpo);
     338          12 : }
     339             : 
     340             : /* end of merchant_api_tip_pickup.c */

Generated by: LCOV version 1.13