LCOV - code coverage report
Current view: top level - lib - merchant_api_proposal.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 70 111 63.1 %
Date: 2018-05-16 06:16:58 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2017 GNUnet e.V. and INRIA
       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_proposal.c
      19             :  * @brief Implementation of the /proposal POST and GET
      20             :  * @author Christian Grothoff
      21             :  * @author Marcello Stanisci
      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 Contract Operation Handle
      36             :  */
      37             : struct TALER_MERCHANT_ProposalOperation
      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_ProposalCallback 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             : /**
      72             :  * Structure representing a GET /proposal operation.
      73             :  */
      74             : struct TALER_MERCHANT_ProposalLookupOperation
      75             : {
      76             :   /**
      77             :    * Full URL, includes "/proposal".
      78             :    */
      79             :   char *url;
      80             : 
      81             :   /**
      82             :    * Handle for the request.
      83             :    */
      84             :   struct GNUNET_CURL_Job *job;
      85             : 
      86             :   /**
      87             :    * Function to call with the result.
      88             :    */
      89             :   TALER_MERCHANT_ProposalLookupOperationCallback cb;
      90             : 
      91             :   /**
      92             :    * Closure for @a cb.
      93             :    */
      94             :   void *cb_cls;
      95             : 
      96             :   /**
      97             :    * Reference to the execution context.
      98             :    */
      99             :   struct GNUNET_CURL_Context *ctx;
     100             : 
     101             :   /**
     102             :    * Should we send the lookup operation with a nonce?
     103             :    */
     104             :   int has_nonce;
     105             : 
     106             :   /**
     107             :    * Nonce, only initialized if has_nonce is GNUNET_YES.
     108             :    */
     109             :   struct GNUNET_CRYPTO_EddsaPublicKey nonce;
     110             : 
     111             : };
     112             : 
     113             : 
     114             : /**
     115             :  * Function called when we're done processing the
     116             :  * HTTP POST /proposal request.
     117             :  *
     118             :  * @param cls the `struct TALER_MERCHANT_ProposalOperation`
     119             :  * @param response_code HTTP response code, 0 on error
     120             :  * @param json response body, NULL if not JSON
     121             :  */
     122             : static void
     123          15 : handle_proposal_finished (void *cls,
     124             :                           long response_code,
     125             :                           const json_t *json)
     126             : {
     127          15 :   struct TALER_MERCHANT_ProposalOperation *po = cls;
     128             :   const char *order_id;
     129          15 :   struct GNUNET_JSON_Specification spec[] = {
     130             :     GNUNET_JSON_spec_string ("order_id",
     131             :                              &order_id),
     132             :     GNUNET_JSON_spec_end()
     133             :   };
     134             : 
     135          15 :   po->job = NULL;
     136          15 :   switch (response_code)
     137             :   {
     138             :   case 0:
     139           0 :     po->cb (po->cb_cls,
     140             :             response_code,
     141             :             TALER_EC_INVALID_RESPONSE,
     142             :             json,
     143             :             order_id);
     144           0 :     TALER_MERCHANT_proposal_cancel (po);
     145           0 :     return;
     146             :   case MHD_HTTP_OK:
     147             :     {
     148          15 :       if (GNUNET_OK !=
     149          15 :           GNUNET_JSON_parse (json,
     150             :                              spec,
     151             :                              NULL, NULL))
     152             :       {
     153           0 :         GNUNET_break_op (0);
     154           0 :         response_code = 0;
     155           0 :         break;
     156             :       }
     157             :     }
     158          15 :     break;
     159             :   case MHD_HTTP_BAD_REQUEST:
     160             :       /* This should never happen, either us or the merchant is buggy
     161             :          (or API version conflict); just pass JSON reply to the application */
     162           0 :     break;
     163             :   case MHD_HTTP_FORBIDDEN:
     164           0 :     break;
     165             :   case MHD_HTTP_UNAUTHORIZED:
     166             :     /* Nothing really to verify, merchant says one of the signatures is
     167             :        invalid; as we checked them, this should never happen, we
     168             :        should pass the JSON reply to the application */
     169           0 :     break;
     170             :   case MHD_HTTP_NOT_FOUND:
     171             :     /* Nothing really to verify, this should never
     172             :        happen, we should pass the JSON reply to the application */
     173           0 :     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             :   default:
     179             :     /* unexpected response code */
     180           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     181             :                 "Unexpected response code %u\n",
     182             :                 (unsigned int) response_code);
     183           0 :     GNUNET_break (0);
     184           0 :     response_code = 0;
     185             :   }
     186          15 :   po->cb (po->cb_cls,
     187             :           response_code,
     188             :           TALER_JSON_get_error_code (json),
     189             :           json,
     190             :           order_id);
     191          15 :   GNUNET_JSON_parse_free (spec);
     192          15 :   TALER_MERCHANT_proposal_cancel (po);
     193             : }
     194             : 
     195             : 
     196             : /**
     197             :  * POST an order to the backend and receives the related proposal.
     198             :  *
     199             :  * @param ctx execution context
     200             :  * @param backend_url URL of the backend
     201             :  * @param order basic information about this purchase, to be extended by the
     202             :  * backend
     203             :  * @param proposal_cb the callback to call when a reply for this request is
     204             :  * available
     205             :  * @param proposal_cb_cls closure for @a proposal_cb
     206             :  * @return a handle for this request, NULL on error
     207             :  */
     208             : struct TALER_MERCHANT_ProposalOperation *
     209          15 : TALER_MERCHANT_order_put (struct GNUNET_CURL_Context *ctx,
     210             :                           const char *backend_url,
     211             :                           const json_t *order,
     212             :                           TALER_MERCHANT_ProposalCallback proposal_cb,
     213             :                           void *proposal_cb_cls)
     214             : {
     215             :   struct TALER_MERCHANT_ProposalOperation *po;
     216             :   json_t *req;
     217             :   CURL *eh;
     218             : 
     219          15 :   po = GNUNET_new (struct TALER_MERCHANT_ProposalOperation);
     220          15 :   po->ctx = ctx;
     221          15 :   po->cb = proposal_cb;
     222          15 :   po->cb_cls = proposal_cb_cls;
     223          15 :   po->url = TALER_url_join (backend_url, "/order", NULL);
     224          15 :   req = json_pack ("{s:O}",
     225             :                    "order", (json_t *) order);
     226          15 :   eh = curl_easy_init ();
     227          15 :   po->json_enc = json_dumps (req,
     228             :                              JSON_COMPACT);
     229          15 :   json_decref (req);
     230          15 :   if (NULL == po->json_enc)
     231             :   {
     232           0 :     GNUNET_break (0);
     233           0 :     GNUNET_free (po);
     234           0 :     return NULL;
     235             :   }
     236          15 :   GNUNET_assert (CURLE_OK ==
     237             :                  curl_easy_setopt (eh,
     238             :                                    CURLOPT_URL,
     239             :                                    po->url));
     240          15 :   GNUNET_assert (CURLE_OK ==
     241             :                  curl_easy_setopt (eh,
     242             :                                    CURLOPT_POSTFIELDS,
     243             :                                    po->json_enc));
     244          15 :   GNUNET_assert (CURLE_OK ==
     245             :                  curl_easy_setopt (eh,
     246             :                                    CURLOPT_POSTFIELDSIZE,
     247             :                                    strlen (po->json_enc)));
     248          15 :   po->job = GNUNET_CURL_job_add (ctx,
     249             :                                  eh,
     250             :                                  GNUNET_YES,
     251             :                                  &handle_proposal_finished,
     252             :                                  po);
     253          15 :   return po;
     254             : }
     255             : 
     256             : 
     257             : /**
     258             :  * Function called when we're done processing the GET /proposal request.
     259             :  *
     260             :  * @param cls the `struct TALER_MERCHANT_ProposalLookupOperation`
     261             :  * @param response_code HTTP response code, 0 on error
     262             :  * @param json response body, should be NULL
     263             :  */
     264             : static void
     265          18 : handle_proposal_lookup_finished (void *cls,
     266             :                                  long response_code,
     267             :                                  const json_t *json)
     268             : {
     269          18 :   struct TALER_MERCHANT_ProposalLookupOperation *plo = cls;
     270             :   json_t *contract_terms;
     271             :   struct TALER_MerchantSignatureP sig;
     272             :   struct GNUNET_HashCode hash;
     273          18 :   struct GNUNET_JSON_Specification spec[] = {
     274             :     GNUNET_JSON_spec_json ("contract_terms",
     275             :                            &contract_terms),
     276             :     GNUNET_JSON_spec_fixed_auto ("sig",
     277             :                                  &sig),
     278             :     GNUNET_JSON_spec_end()
     279             :   };
     280          18 :   plo->job = NULL;
     281          18 :   if (MHD_HTTP_OK != response_code)
     282             :   {
     283           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     284             :                 "Proposal lookup failed with HTTP status code %u\n",
     285             :                 (unsigned int) response_code);
     286           0 :     GNUNET_break (0);
     287             : 
     288           0 :     plo->cb (plo->cb_cls,
     289             :              response_code,
     290             :              json,
     291             :              NULL,
     292             :              NULL,
     293             :              NULL);
     294           0 :     TALER_MERCHANT_proposal_lookup_cancel (plo);
     295           0 :     return;
     296             :   }
     297             : 
     298          18 :   if (GNUNET_OK !=
     299          18 :       GNUNET_JSON_parse (json,
     300             :                          spec,
     301             :                          NULL, NULL))
     302             :   {
     303           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     304             :                 "proposal lookup failed to parse JSON\n");
     305           0 :     GNUNET_break_op (0);
     306           0 :     plo->cb (plo->cb_cls,
     307             :              0,
     308             :              json,
     309             :              NULL,
     310             :              NULL,
     311             :              NULL);
     312             : 
     313           0 :     TALER_MERCHANT_proposal_lookup_cancel (plo);
     314           0 :     return;
     315             :   }
     316             : 
     317          18 :   if (GNUNET_OK !=
     318          18 :       TALER_JSON_hash (contract_terms,
     319             :                        &hash))
     320             :   {
     321           0 :     GNUNET_break (0);
     322           0 :     GNUNET_JSON_parse_free (spec);
     323           0 :     plo->cb (plo->cb_cls,
     324             :              0,
     325             :              json,
     326             :              NULL,
     327             :              NULL,
     328             :              NULL);
     329           0 :     TALER_MERCHANT_proposal_lookup_cancel (plo);
     330           0 :     return;
     331             :   }
     332             : 
     333          18 :   plo->job = NULL;
     334             :   /**
     335             :    * As no data is supposed to be extracted from this
     336             :    * call, we just invoke the provided callback.
     337             :    */
     338          18 :   plo->cb (plo->cb_cls,
     339             :            response_code,
     340             :            json,
     341             :            contract_terms,
     342             :            &sig,
     343             :            &hash);
     344          18 :   GNUNET_JSON_parse_free (spec);
     345          18 :   TALER_MERCHANT_proposal_lookup_cancel (plo);
     346             : }
     347             : 
     348             : 
     349             : /**
     350             :  * Calls the GET /proposal API at the backend.  That is,
     351             :  * retrieve a proposal data by providing its transaction id.
     352             :  *
     353             :  * @param ctx execution context
     354             :  * @param backend_url base URL of the merchant backend
     355             :  * @param order_id order id used to perform the lookup
     356             :  * @param nonce nonce used to perform the lookup
     357             :  * @param plo_cb callback which will work the response gotten from the backend
     358             :  * @param plo_cb_cls closure to pass to @a history_cb
     359             :  * @return handle for this operation, NULL upon errors
     360             :  */
     361             : struct TALER_MERCHANT_ProposalLookupOperation *
     362          18 : TALER_MERCHANT_proposal_lookup (struct GNUNET_CURL_Context *ctx,
     363             :                                 const char *backend_url,
     364             :                                 const char *order_id,
     365             :                                 const char *instance,
     366             :                                 const struct GNUNET_CRYPTO_EddsaPublicKey *nonce,
     367             :                                 TALER_MERCHANT_ProposalLookupOperationCallback plo_cb,
     368             :                                 void *plo_cb_cls)
     369             : {
     370             :   struct TALER_MERCHANT_ProposalLookupOperation *plo;
     371             :   CURL *eh;
     372             :   char *base;
     373             : 
     374          18 :   plo = GNUNET_new (struct TALER_MERCHANT_ProposalLookupOperation);
     375          18 :   plo->ctx = ctx;
     376          18 :   plo->cb = plo_cb;
     377          18 :   plo->cb_cls = plo_cb_cls;
     378          18 :   base = TALER_url_join (backend_url, "/public/proposal", NULL);
     379          18 :   if (NULL != nonce)
     380             :   {
     381             :     char *nonce_str;
     382          18 :     plo->has_nonce = GNUNET_YES;
     383          18 :     plo->nonce = *nonce;
     384          18 :     nonce_str = GNUNET_STRINGS_data_to_string_alloc (nonce, sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
     385          18 :     GNUNET_assert (NULL != nonce_str);
     386          18 :     GNUNET_asprintf (&plo->url,
     387             :                      "%s?order_id=%s&instance=%s&nonce=%s",
     388             :                      base,
     389             :                      order_id,
     390             :                      instance,
     391             :                      nonce_str);
     392          18 :     GNUNET_free (nonce_str);
     393             :   }
     394             :   else
     395             :   {
     396           0 :     GNUNET_asprintf (&plo->url,
     397             :                      "%s?order_id=%s&instance=%s",
     398             :                      base,
     399             :                      order_id,
     400             :                      instance);
     401             :   }
     402          18 :   GNUNET_free (base);
     403          18 :   eh = curl_easy_init ();
     404          18 :   if (CURLE_OK != curl_easy_setopt (eh,
     405             :                                     CURLOPT_URL,
     406             :                                     plo->url))
     407             :   {
     408           0 :     GNUNET_break (0);
     409           0 :     return NULL;
     410             :   }
     411             : 
     412          18 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     413             :               "looking up proposal from %s\n",
     414             :               plo->url);
     415             : 
     416          18 :   if (NULL == (plo->job = GNUNET_CURL_job_add (ctx,
     417             :                                                eh,
     418             :                                                GNUNET_YES,
     419             :                                                &handle_proposal_lookup_finished,
     420             :                                                plo)))
     421             :   {
     422           0 :     GNUNET_break (0);
     423           0 :     return NULL;
     424             :   }
     425          18 :   return plo;
     426             : }
     427             : 
     428             : 
     429             : /**
     430             :  * Cancel a POST /proposal request.  This function cannot be used
     431             :  * on a request handle if a response is already served for it.
     432             :  *
     433             :  * @param po the proposal operation request handle
     434             :  */
     435             : void
     436          15 : TALER_MERCHANT_proposal_cancel (struct TALER_MERCHANT_ProposalOperation *po)
     437             : {
     438          15 :   if (NULL != po->job)
     439             :   {
     440           0 :     GNUNET_CURL_job_cancel (po->job);
     441           0 :     po->job = NULL;
     442             :   }
     443          15 :   GNUNET_free (po->url);
     444          15 :   GNUNET_free (po->json_enc);
     445          15 :   GNUNET_free (po);
     446          15 : }
     447             : 
     448             : 
     449             : /**
     450             :  * Cancel a GET /proposal request.
     451             :  *
     452             :  * @param plo handle to the request to be canceled
     453             :  */
     454             : void
     455          18 : TALER_MERCHANT_proposal_lookup_cancel (struct TALER_MERCHANT_ProposalLookupOperation *plo)
     456             : {
     457          18 :   if (NULL != plo->job)
     458             :   {
     459           0 :     GNUNET_CURL_job_cancel (plo->job);
     460           0 :     plo->job = NULL;
     461             :   }
     462          18 :   GNUNET_free (plo->url);
     463          18 :   GNUNET_free (plo);
     464          18 : }
     465             : 
     466             : /* end of merchant_api_contract.c */

Generated by: LCOV version 1.13