LCOV - code coverage report
Current view: top level - lib - merchant_api_post_order_claim.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 74 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2020 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify
       6             :   it under the terms of the GNU Lesser General Public License as
       7             :   published by the Free Software Foundation; either version 2.1,
       8             :   or (at your option) any later version.
       9             : 
      10             :   TALER is distributed in the hope that it will be useful, but
      11             :   WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :   GNU Lesser General Public License for more details.
      14             : 
      15             :   You should have received a copy of the GNU Lesser General Public
      16             :   License along with TALER; see the file COPYING.LGPL.  If not,
      17             :   see <http://www.gnu.org/licenses/>
      18             : */
      19             : /**
      20             :  * @file lib/merchant_api_post_order_claim.c
      21             :  * @brief Implementation of POST /orders/$ID/claim
      22             :  * @author Christian Grothoff
      23             :  * @author Marcello Stanisci
      24             :  */
      25             : #include "platform.h"
      26             : #include <curl/curl.h>
      27             : #include <jansson.h>
      28             : #include <microhttpd.h> /* just for HTTP status codes */
      29             : #include <gnunet/gnunet_util_lib.h>
      30             : #include <gnunet/gnunet_curl_lib.h>
      31             : #include "taler_merchant_service.h"
      32             : #include <taler/taler_json_lib.h>
      33             : #include <taler/taler_signatures.h>
      34             : #include <taler/taler_curl_lib.h>
      35             : 
      36             : 
      37             : /**
      38             :  * Structure representing a POST /orders/$ID/claim operation.
      39             :  */
      40             : struct TALER_MERCHANT_OrderClaimHandle
      41             : {
      42             :   /**
      43             :    * Full URL, includes "/orders/$ID/claim".
      44             :    */
      45             :   char *url;
      46             : 
      47             :   /**
      48             :    * Handle for the request.
      49             :    */
      50             :   struct GNUNET_CURL_Job *job;
      51             : 
      52             :   /**
      53             :    * Function to call with the result.
      54             :    */
      55             :   TALER_MERCHANT_OrderClaimCallback cb;
      56             : 
      57             :   /**
      58             :    * Closure for @a cb.
      59             :    */
      60             :   void *cb_cls;
      61             : 
      62             :   /**
      63             :    * Reference to the execution context.
      64             :    */
      65             :   struct GNUNET_CURL_Context *ctx;
      66             : 
      67             :   /**
      68             :    * Minor context that holds body and headers.
      69             :    */
      70             :   struct TALER_CURL_PostContext post_ctx;
      71             : };
      72             : 
      73             : 
      74             : /**
      75             :  * Function called when we're done processing the
      76             :  * POST /orders/$ID/claim request.
      77             :  *
      78             :  * @param cls the `struct TALER_MERCHANT_OrderClaimHandle`
      79             :  * @param response_code HTTP response code, 0 on error
      80             :  * @param json response body, should be NULL
      81             :  */
      82             : static void
      83           0 : handle_post_order_claim_finished (void *cls,
      84             :                                   long response_code,
      85             :                                   const void *response)
      86             : {
      87           0 :   struct TALER_MERCHANT_OrderClaimHandle *och = cls;
      88             :   json_t *contract_terms;
      89             :   struct TALER_MerchantSignatureP sig;
      90             :   struct GNUNET_HashCode hash;
      91           0 :   const json_t *json = response;
      92             :   struct GNUNET_JSON_Specification spec[] = {
      93           0 :     GNUNET_JSON_spec_json ("contract_terms",
      94             :                            &contract_terms),
      95           0 :     GNUNET_JSON_spec_fixed_auto ("sig",
      96             :                                  &sig),
      97           0 :     GNUNET_JSON_spec_end ()
      98             :   };
      99           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     100           0 :     .http_status = (unsigned int) response_code,
     101             :     .reply = json
     102             :   };
     103             : 
     104           0 :   och->job = NULL;
     105           0 :   if (MHD_HTTP_OK != response_code)
     106             :   {
     107           0 :     hr.ec = TALER_JSON_get_error_code (json);
     108           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     109           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     110             :                 "Proposal lookup failed with HTTP status code %u/%d\n",
     111             :                 (unsigned int) response_code,
     112             :                 (int) hr.ec);
     113           0 :     och->cb (och->cb_cls,
     114             :              &hr,
     115             :              NULL,
     116             :              NULL,
     117             :              NULL);
     118           0 :     TALER_MERCHANT_order_claim_cancel (och);
     119           0 :     return;
     120             :   }
     121             : 
     122           0 :   if (GNUNET_OK !=
     123           0 :       GNUNET_JSON_parse (json,
     124             :                          spec,
     125             :                          NULL, NULL))
     126             :   {
     127           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     128             :                 "Claiming order failed: could not parse JSON response\n");
     129           0 :     GNUNET_break_op (0);
     130           0 :     hr.ec = TALER_EC_INVALID_RESPONSE;
     131           0 :     hr.http_status = 0;
     132           0 :     och->cb (och->cb_cls,
     133             :              &hr,
     134             :              NULL,
     135             :              NULL,
     136             :              NULL);
     137           0 :     TALER_MERCHANT_order_claim_cancel (och);
     138           0 :     return;
     139             :   }
     140             : 
     141           0 :   if (GNUNET_OK !=
     142           0 :       TALER_JSON_contract_hash (contract_terms,
     143             :                                 &hash))
     144             :   {
     145           0 :     GNUNET_break (0);
     146           0 :     hr.ec = TALER_EC_CLIENT_INTERNAL_FAILURE;
     147           0 :     hr.http_status = 0;
     148           0 :     GNUNET_JSON_parse_free (spec);
     149           0 :     och->cb (och->cb_cls,
     150             :              &hr,
     151             :              NULL,
     152             :              NULL,
     153             :              NULL);
     154           0 :     TALER_MERCHANT_order_claim_cancel (och);
     155           0 :     return;
     156             :   }
     157             : 
     158           0 :   och->cb (och->cb_cls,
     159             :            &hr,
     160             :            contract_terms,
     161             :            &sig,
     162             :            &hash);
     163           0 :   GNUNET_JSON_parse_free (spec);
     164           0 :   TALER_MERCHANT_order_claim_cancel (och);
     165             : }
     166             : 
     167             : 
     168             : /**
     169             :  * Calls the POST /orders/$ID/claim API at the backend.  That is,
     170             :  * retrieve the final contract terms including the client nonce.
     171             :  *
     172             :  * This is a PUBLIC API for wallets.
     173             :  *
     174             :  * @param ctx execution context
     175             :  * @param backend_url base URL of the merchant backend
     176             :  * @param order_id order id used to perform the lookup
     177             :  * @param nonce nonce to use to claim the proposal
     178             :  * @param claim_token the token used to verify the claim
     179             :  * @param cb callback which will work the response gotten from the backend
     180             :  * @param cb_cls closure to pass to @a cb
     181             :  * @return handle for this handle, NULL upon errors
     182             :  */
     183             : struct TALER_MERCHANT_OrderClaimHandle *
     184           0 : TALER_MERCHANT_order_claim (struct GNUNET_CURL_Context *ctx,
     185             :                             const char *backend_url,
     186             :                             const char *order_id,
     187             :                             const struct GNUNET_CRYPTO_EddsaPublicKey *nonce,
     188             :                             const struct TALER_ClaimTokenP *claim_token,
     189             :                             TALER_MERCHANT_OrderClaimCallback cb,
     190             :                             void *cb_cls)
     191             : {
     192             :   struct TALER_MERCHANT_OrderClaimHandle *och;
     193             :   json_t *req_obj;
     194             : 
     195           0 :   req_obj = json_pack ("{s:o}",
     196             :                        "nonce",
     197             :                        GNUNET_JSON_from_data_auto (nonce));
     198           0 :   if (NULL == req_obj)
     199             :   {
     200           0 :     GNUNET_break (0);
     201           0 :     return NULL;
     202             :   }
     203           0 :   if (NULL != claim_token)
     204             :   {
     205           0 :     GNUNET_assert (0 ==
     206             :                    json_object_set_new (req_obj,
     207             :                                         "token",
     208             :                                         GNUNET_JSON_from_data_auto (claim_token)));
     209             :   }
     210           0 :   och = GNUNET_new (struct TALER_MERCHANT_OrderClaimHandle);
     211           0 :   och->ctx = ctx;
     212           0 :   och->cb = cb;
     213           0 :   och->cb_cls = cb_cls;
     214             :   {
     215             :     char *path;
     216             : 
     217           0 :     GNUNET_asprintf (&path,
     218             :                      "orders/%s/claim",
     219             :                      order_id);
     220           0 :     och->url = TALER_url_join (backend_url,
     221             :                                path,
     222             :                                NULL);
     223           0 :     GNUNET_free (path);
     224             :   }
     225           0 :   if (NULL == och->url)
     226             :   {
     227           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     228             :                 "Could not construct request URL.\n");
     229           0 :     json_decref (req_obj);
     230           0 :     GNUNET_free (och);
     231           0 :     return NULL;
     232             :   }
     233           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     234             :               "Claiming order at %s\n",
     235             :               och->url);
     236             :   {
     237             :     CURL *eh;
     238             : 
     239           0 :     eh = curl_easy_init ();
     240           0 :     GNUNET_assert (NULL != eh);
     241           0 :     GNUNET_assert (GNUNET_OK ==
     242             :                    TALER_curl_easy_post (&och->post_ctx,
     243             :                                          eh,
     244             :                                          req_obj));
     245           0 :     json_decref (req_obj);
     246           0 :     GNUNET_assert (CURLE_OK ==
     247             :                    curl_easy_setopt (eh,
     248             :                                      CURLOPT_URL,
     249             :                                      och->url));
     250           0 :     och->job = GNUNET_CURL_job_add2 (ctx,
     251             :                                      eh,
     252           0 :                                      och->post_ctx.headers,
     253             :                                      &handle_post_order_claim_finished,
     254             :                                      och);
     255           0 :     GNUNET_assert (NULL != och->job);
     256             :   }
     257           0 :   return och;
     258             : }
     259             : 
     260             : 
     261             : /**
     262             :  * Cancel a POST /orders/$ID/claim request.
     263             :  *
     264             :  * @param och handle to the request to be canceled
     265             :  */
     266             : void
     267           0 : TALER_MERCHANT_order_claim_cancel (struct TALER_MERCHANT_OrderClaimHandle *och)
     268             : {
     269           0 :   if (NULL != och->job)
     270             :   {
     271           0 :     GNUNET_CURL_job_cancel (och->job);
     272           0 :     och->job = NULL;
     273             :   }
     274           0 :   TALER_curl_easy_post_finished (&och->post_ctx);
     275           0 :   GNUNET_free (och->url);
     276           0 :   GNUNET_free (och);
     277           0 : }
     278             : 
     279             : 
     280             : /* end of merchant_api_post_order_claim.c */

Generated by: LCOV version 1.14