LCOV - code coverage report
Current view: top level - exchange-lib - exchange_api_refund.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 66 87 75.9 %
Date: 2017-11-25 11:31:41 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014, 2015, 2016 GNUnet e.V.
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU General Public License as published by the Free Software
       7             :   Foundation; either version 3, 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 General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file exchange-lib/exchange_api_refund.c
      19             :  * @brief Implementation of the /refund request of the exchange's HTTP API
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <curl/curl.h>
      24             : #include <jansson.h>
      25             : #include <microhttpd.h> /* just for HTTP status codes */
      26             : #include <gnunet/gnunet_util_lib.h>
      27             : #include <gnunet/gnunet_json_lib.h>
      28             : #include <gnunet/gnunet_curl_lib.h>
      29             : #include "taler_json_lib.h"
      30             : #include "taler_exchange_service.h"
      31             : #include "exchange_api_handle.h"
      32             : #include "taler_signatures.h"
      33             : 
      34             : 
      35             : /**
      36             :  * @brief A Refund Handle
      37             :  */
      38             : struct TALER_EXCHANGE_RefundHandle
      39             : {
      40             : 
      41             :   /**
      42             :    * The connection to exchange this request handle will use
      43             :    */
      44             :   struct TALER_EXCHANGE_Handle *exchange;
      45             : 
      46             :   /**
      47             :    * The url for this request.
      48             :    */
      49             :   char *url;
      50             : 
      51             :   /**
      52             :    * JSON encoding of the request to POST.
      53             :    */
      54             :   char *json_enc;
      55             : 
      56             :   /**
      57             :    * Handle for the request.
      58             :    */
      59             :   struct GNUNET_CURL_Job *job;
      60             : 
      61             :   /**
      62             :    * Function to call with the result.
      63             :    */
      64             :   TALER_EXCHANGE_RefundResultCallback cb;
      65             : 
      66             :   /**
      67             :    * Closure for @a cb.
      68             :    */
      69             :   void *cb_cls;
      70             : 
      71             :   /**
      72             :    * Information the exchange should sign in response.
      73             :    */
      74             :   struct TALER_RefundConfirmationPS depconf;
      75             : 
      76             : };
      77             : 
      78             : 
      79             : /**
      80             :  * Verify that the signature on the "200 OK" response
      81             :  * from the exchange is valid.
      82             :  *
      83             :  * @param rh refund handle
      84             :  * @param json json reply with the signature
      85             :  * @param[out] exchange_pub set to the exchange's public key
      86             :  * @return #GNUNET_OK if the signature is valid, #GNUNET_SYSERR if not
      87             :  */
      88             : static int
      89           1 : verify_refund_signature_ok (const struct TALER_EXCHANGE_RefundHandle *rh,
      90             :                             const json_t *json,
      91             :                             struct TALER_ExchangePublicKeyP *exchange_pub)
      92             : {
      93             :   struct TALER_ExchangeSignatureP exchange_sig;
      94             :   const struct TALER_EXCHANGE_Keys *key_state;
      95           1 :   struct GNUNET_JSON_Specification spec[] = {
      96             :     GNUNET_JSON_spec_fixed_auto ("sig", &exchange_sig),
      97             :     GNUNET_JSON_spec_fixed_auto ("pub", exchange_pub),
      98             :     GNUNET_JSON_spec_end()
      99             :   };
     100             : 
     101           1 :   if (GNUNET_OK !=
     102           1 :       GNUNET_JSON_parse (json,
     103             :                          spec,
     104             :                          NULL, NULL))
     105             :   {
     106           0 :     GNUNET_break_op (0);
     107           0 :     return GNUNET_SYSERR;
     108             :   }
     109           1 :   key_state = TALER_EXCHANGE_get_keys (rh->exchange);
     110           1 :   if (GNUNET_OK !=
     111           1 :       TALER_EXCHANGE_test_signing_key (key_state,
     112             :                                        exchange_pub))
     113             :   {
     114           0 :     GNUNET_break_op (0);
     115           0 :     return GNUNET_SYSERR;
     116             :   }
     117           1 :   if (GNUNET_OK !=
     118           1 :       GNUNET_CRYPTO_eddsa_verify (TALER_SIGNATURE_EXCHANGE_CONFIRM_REFUND,
     119             :                                   &rh->depconf.purpose,
     120             :                                   &exchange_sig.eddsa_signature,
     121           1 :                                   &exchange_pub->eddsa_pub))
     122             :   {
     123           0 :     GNUNET_break_op (0);
     124           0 :     return GNUNET_SYSERR;
     125             :   }
     126           1 :   return GNUNET_OK;
     127             : }
     128             : 
     129             : 
     130             : /**
     131             :  * Function called when we're done processing the
     132             :  * HTTP /refund request.
     133             :  *
     134             :  * @param cls the `struct TALER_EXCHANGE_RefundHandle`
     135             :  * @param response_code HTTP response code, 0 on error
     136             :  * @param json parsed JSON result, NULL on error
     137             :  */
     138             : static void
     139           2 : handle_refund_finished (void *cls,
     140             :                         long response_code,
     141             :                         const json_t *json)
     142             : {
     143           2 :   struct TALER_EXCHANGE_RefundHandle *rh = cls;
     144             :   struct TALER_ExchangePublicKeyP exchange_pub;
     145           2 :   struct TALER_ExchangePublicKeyP *ep = NULL;
     146             : 
     147           2 :   rh->job = NULL;
     148           2 :   switch (response_code)
     149             :   {
     150             :   case 0:
     151           0 :     break;
     152             :   case MHD_HTTP_OK:
     153           1 :     if (GNUNET_OK !=
     154           1 :         verify_refund_signature_ok (rh,
     155             :                                     json,
     156             :                                     &exchange_pub))
     157             :     {
     158           0 :       GNUNET_break_op (0);
     159           0 :       response_code = 0;
     160             :     }
     161             :     else
     162             :     {
     163           1 :       ep = &exchange_pub;
     164             :     }
     165           1 :     break;
     166             :   case MHD_HTTP_BAD_REQUEST:
     167             :     /* This should never happen, either us or the exchange is buggy
     168             :        (or API version conflict); just pass JSON reply to the application */
     169           0 :     break;
     170             :   case MHD_HTTP_UNAUTHORIZED:
     171             :     /* Nothing really to verify, exchange says one of the signatures is
     172             :        invalid; as we checked them, this should never happen, we
     173             :        should pass the JSON reply to the application */
     174           0 :     break;
     175             :   case MHD_HTTP_NOT_FOUND:
     176             :     /* Nothing really to verify, this should never
     177             :        happen, we should pass the JSON reply to the application */
     178           0 :     break;
     179             :   case MHD_HTTP_GONE:
     180             :     /* Kind of normal: the money was already sent to the merchant
     181             :        (it was too late for the refund). */
     182           1 :     break;
     183             :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     184             :     /* Server had an internal issue; we should retry, but this API
     185             :        leaves this to the application */
     186           0 :     break;
     187             :   default:
     188             :     /* unexpected response code */
     189           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     190             :                 "Unexpected response code %u\n",
     191             :                 (unsigned int) response_code);
     192           0 :     GNUNET_break (0);
     193           0 :     response_code = 0;
     194           0 :     break;
     195             :   }
     196           2 :   rh->cb (rh->cb_cls,
     197             :           response_code,
     198             :           TALER_JSON_get_error_code (json),
     199             :           ep,
     200             :           json);
     201           2 :   TALER_EXCHANGE_refund_cancel (rh);
     202           2 : }
     203             : 
     204             : 
     205             : /**
     206             :  * Submit a refund request to the exchange and get the exchange's
     207             :  * response.  This API is used by a merchant.  Note that
     208             :  * while we return the response verbatim to the caller for further
     209             :  * processing, we do already verify that the response is well-formed
     210             :  * (i.e. that signatures included in the response are all valid).  If
     211             :  * the exchange's reply is not well-formed, we return an HTTP status code
     212             :  * of zero to @a cb.
     213             :  *
     214             :  * The @a exchange must be ready to operate (i.e.  have
     215             :  * finished processing the /keys reply).  If this check fails, we do
     216             :  * NOT initiate the transaction with the exchange and instead return NULL.
     217             :  *
     218             :  * @param exchange the exchange handle; the exchange must be ready to operate
     219             :  * @param amount the amount to be refunded; must be larger than the refund fee
     220             :  *        (as that fee is still being subtracted), and smaller than the amount
     221             :  *        (with deposit fee) of the original deposit contribution of this coin
     222             :  * @param refund_fee fee applicable to this coin for the refund
     223             :  * @param h_contract_terms hash of the contact of the merchant with the customer that is being refunded
     224             :  * @param coin_pub coin’s public key of the coin from the original deposit operation
     225             :  * @param rtransaction_id transaction id for the transaction between merchant and customer (of refunding operation);
     226             :  *                        this is needed as we may first do a partial refund and later a full refund.  If both
     227             :  *                        refunds are also over the same amount, we need the @a rtransaction_id to make the disjoint
     228             :  *                        refund requests different (as requests are idempotent and otherwise the 2nd refund might not work).
     229             :  * @param merchant_priv the private key of the merchant, used to generate signature for refund request
     230             :  * @param cb the callback to call when a reply for this request is available
     231             :  * @param cb_cls closure for the above callback
     232             :  * @return a handle for this request; NULL if the inputs are invalid (i.e.
     233             :  *         signatures fail to verify).  In this case, the callback is not called.
     234             :  */
     235             : struct TALER_EXCHANGE_RefundHandle *
     236           2 : TALER_EXCHANGE_refund (struct TALER_EXCHANGE_Handle *exchange,
     237             :                        const struct TALER_Amount *amount,
     238             :                        const struct TALER_Amount *refund_fee,
     239             :                        const struct GNUNET_HashCode *h_contract_terms,
     240             :                        const struct TALER_CoinSpendPublicKeyP *coin_pub,
     241             :                        uint64_t rtransaction_id,
     242             :                        const struct TALER_MerchantPrivateKeyP *merchant_priv,
     243             :                        TALER_EXCHANGE_RefundResultCallback cb,
     244             :                        void *cb_cls)
     245             : {
     246             :   struct TALER_EXCHANGE_RefundHandle *rh;
     247             :   struct GNUNET_CURL_Context *ctx;
     248             :   struct TALER_RefundRequestPS rr;
     249             :   struct TALER_MerchantSignatureP merchant_sig;
     250             :   json_t *refund_obj;
     251             :   CURL *eh;
     252             : 
     253           2 :   GNUNET_assert (GNUNET_YES ==
     254             :                  MAH_handle_is_ready (exchange));
     255           2 :   rr.purpose.purpose = htonl (TALER_SIGNATURE_MERCHANT_REFUND);
     256           2 :   rr.purpose.size = htonl (sizeof (struct TALER_RefundRequestPS));
     257           2 :   rr.h_contract_terms = *h_contract_terms;
     258           2 :   rr.coin_pub = *coin_pub;
     259           2 :   GNUNET_CRYPTO_eddsa_key_get_public (&merchant_priv->eddsa_priv,
     260             :                                       &rr.merchant.eddsa_pub);
     261           2 :   rr.rtransaction_id = GNUNET_htonll (rtransaction_id);
     262           2 :   TALER_amount_hton (&rr.refund_amount,
     263             :                      amount);
     264           2 :   TALER_amount_hton (&rr.refund_fee,
     265             :                      refund_fee);
     266           2 :   GNUNET_assert (GNUNET_OK ==
     267             :                  GNUNET_CRYPTO_eddsa_sign (&merchant_priv->eddsa_priv,
     268             :                                            &rr.purpose,
     269             :                                            &merchant_sig.eddsa_sig));
     270           2 :   refund_obj = json_pack ("{s:o, s:o," /* amount/fee */
     271             :                           " s:o, s:o," /* h_contract_terms, coin_pub */
     272             :                           " s:I," /* rtransaction id */
     273             :                           " s:o, s:o}", /* merchant_pub, merchant_sig */
     274             :                           "refund_amount", TALER_JSON_from_amount (amount),
     275             :                           "refund_fee", TALER_JSON_from_amount (refund_fee),
     276             :                           "h_contract_terms", GNUNET_JSON_from_data_auto (h_contract_terms),
     277             :                           "coin_pub", GNUNET_JSON_from_data_auto (coin_pub),
     278             :                           "rtransaction_id", (json_int_t) rtransaction_id,
     279             :                           "merchant_pub", GNUNET_JSON_from_data_auto (&rr.merchant),
     280             :                           "merchant_sig", GNUNET_JSON_from_data_auto (&merchant_sig)
     281             :                           );
     282           2 :   if (NULL == refund_obj)
     283             :   {
     284           0 :     GNUNET_break (0);
     285           0 :     return NULL;
     286             :   }
     287             : 
     288           2 :   rh = GNUNET_new (struct TALER_EXCHANGE_RefundHandle);
     289           2 :   rh->exchange = exchange;
     290           2 :   rh->cb = cb;
     291           2 :   rh->cb_cls = cb_cls;
     292           2 :   rh->url = MAH_path_to_url (exchange, "/refund");
     293           2 :   rh->depconf.purpose.size = htonl (sizeof (struct TALER_RefundConfirmationPS));
     294           2 :   rh->depconf.purpose.purpose = htonl (TALER_SIGNATURE_EXCHANGE_CONFIRM_REFUND);
     295           2 :   rh->depconf.h_contract_terms = *h_contract_terms;
     296           2 :   rh->depconf.coin_pub = *coin_pub;
     297           2 :   rh->depconf.merchant = rr.merchant;
     298           2 :   rh->depconf.rtransaction_id = GNUNET_htonll (rtransaction_id);
     299           2 :   TALER_amount_hton (&rh->depconf.refund_amount,
     300             :                      amount);
     301           2 :   TALER_amount_hton (&rh->depconf.refund_fee,
     302             :                      refund_fee);
     303             : 
     304           2 :   eh = curl_easy_init ();
     305           2 :   GNUNET_assert (NULL != (rh->json_enc =
     306             :                           json_dumps (refund_obj,
     307             :                                       JSON_COMPACT)));
     308           2 :   json_decref (refund_obj);
     309           2 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     310             :               "URL for refund: `%s'\n",
     311             :               rh->url);
     312           2 :   GNUNET_assert (CURLE_OK ==
     313             :                  curl_easy_setopt (eh,
     314             :                                    CURLOPT_URL,
     315             :                                    rh->url));
     316           2 :   GNUNET_assert (CURLE_OK ==
     317             :                  curl_easy_setopt (eh,
     318             :                                    CURLOPT_POSTFIELDS,
     319             :                                    rh->json_enc));
     320           2 :   GNUNET_assert (CURLE_OK ==
     321             :                  curl_easy_setopt (eh,
     322             :                                    CURLOPT_POSTFIELDSIZE,
     323             :                                    strlen (rh->json_enc)));
     324           2 :   ctx = MAH_handle_to_context (exchange);
     325           2 :   rh->job = GNUNET_CURL_job_add (ctx,
     326             :                                  eh,
     327             :                                  GNUNET_YES,
     328             :                                  &handle_refund_finished,
     329             :                                  rh);
     330           2 :   return rh;
     331             : }
     332             : 
     333             : 
     334             : /**
     335             :  * Cancel a refund permission request.  This function cannot be used
     336             :  * on a request handle if a response is already served for it.
     337             :  *
     338             :  * @param refund the refund permission request handle
     339             :  */
     340             : void
     341           2 : TALER_EXCHANGE_refund_cancel (struct TALER_EXCHANGE_RefundHandle *refund)
     342             : {
     343           2 :   if (NULL != refund->job)
     344             :   {
     345           0 :     GNUNET_CURL_job_cancel (refund->job);
     346           0 :     refund->job = NULL;
     347             :   }
     348           2 :   GNUNET_free (refund->url);
     349           2 :   GNUNET_free (refund->json_enc);
     350           2 :   GNUNET_free (refund);
     351           2 : }
     352             : 
     353             : 
     354             : /* end of exchange_api_refund.c */

Generated by: LCOV version 1.13