LCOV - code coverage report
Current view: top level - lib - merchant_api_tip_authorize.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 126 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 5 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_authorize.c
      19             :  * @brief Implementation of the /tip-authorize 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 tip authorizations.
      37             :  */
      38             : struct TALER_MERCHANT_TipAuthorizeHandle
      39             : {
      40             : 
      41             :   /**
      42             :    * The url for this request.
      43             :    */
      44             :   char *url;
      45             : 
      46             :   /**
      47             :    * Handle for the request.
      48             :    */
      49             :   struct GNUNET_CURL_Job *job;
      50             : 
      51             :   /**
      52             :    * Function to call with the result.
      53             :    */
      54             :   TALER_MERCHANT_TipAuthorizeCallback cb;
      55             : 
      56             :   /**
      57             :    * Closure for @a cb.
      58             :    */
      59             :   void *cb_cls;
      60             : 
      61             :   /**
      62             :    * Reference to the execution context.
      63             :    */
      64             :   struct GNUNET_CURL_Context *ctx;
      65             : 
      66             :   /**
      67             :    * Minor context that holds body and headers.
      68             :    */
      69             :   struct TALER_CURL_PostContext post_ctx;
      70             : };
      71             : 
      72             : 
      73             : /**
      74             :  * We got a 200 response back from the exchange (or the merchant).
      75             :  * Now we need to parse the response and if it is well-formed,
      76             :  * call the callback (and set it to NULL afterwards).
      77             :  *
      78             :  * @param tao handle of the original authorization operation
      79             :  * @param json cryptographic proof returned by the exchange/merchant
      80             :  * @return #GNUNET_OK if response is valid
      81             :  */
      82             : static int
      83           0 : check_ok (struct TALER_MERCHANT_TipAuthorizeHandle *tao,
      84             :           const json_t *json)
      85             : {
      86             :   const char *tip_status_url;
      87             :   const char *taler_tip_uri;
      88             :   struct GNUNET_HashCode tip_id;
      89             :   struct GNUNET_TIME_Absolute expiration_time;
      90             :   struct GNUNET_JSON_Specification spec[] = {
      91           0 :     GNUNET_JSON_spec_string ("tip_status_url", &tip_status_url),
      92           0 :     GNUNET_JSON_spec_string ("taler_tip_uri", &taler_tip_uri),
      93           0 :     TALER_JSON_spec_absolute_time ("tip_expiration", &expiration_time),
      94           0 :     GNUNET_JSON_spec_fixed_auto ("tip_id", &tip_id),
      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             :     char *log;
     108             : 
     109           0 :     GNUNET_break_op (0);
     110           0 :     log = json_dumps (json,
     111             :                       0);
     112           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     113             :                 "JSON %s\n",
     114             :                 log);
     115           0 :     free (log);
     116           0 :     return GNUNET_SYSERR;
     117             :   }
     118           0 :   tao->cb (tao->cb_cls,
     119             :            &hr,
     120             :            &tip_id,
     121             :            taler_tip_uri,
     122             :            expiration_time);
     123           0 :   tao->cb = NULL; /* do not call twice */
     124           0 :   GNUNET_JSON_parse_free (spec);
     125           0 :   return GNUNET_OK;
     126             : }
     127             : 
     128             : 
     129             : /**
     130             :  * Function called when we're done processing the
     131             :  * HTTP /reservers/$TIP_ID/tip-authorize request.
     132             :  *
     133             :  * @param cls the `struct TALER_MERCHANT_TipAuthorizeHandle`
     134             :  * @param response_code HTTP response code, 0 on error
     135             :  * @param json response body, NULL if not in JSON
     136             :  */
     137             : static void
     138           0 : handle_tip_authorize_finished (void *cls,
     139             :                                long response_code,
     140             :                                const void *response)
     141             : {
     142           0 :   struct TALER_MERCHANT_TipAuthorizeHandle *tao = cls;
     143           0 :   const json_t *json = response;
     144           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     145           0 :     .http_status = (unsigned int) response_code,
     146             :     .reply = json
     147             :   };
     148             : 
     149           0 :   tao->job = NULL;
     150           0 :   switch (response_code)
     151             :   {
     152           0 :   case MHD_HTTP_OK:
     153           0 :     if (GNUNET_OK ==
     154           0 :         check_ok (tao,
     155             :                   json))
     156             :     {
     157           0 :       TALER_MERCHANT_tip_authorize_cancel (tao);
     158           0 :       return;
     159             :     }
     160           0 :     GNUNET_break_op (0);
     161           0 :     hr.http_status = 0;
     162           0 :     hr.ec = TALER_EC_INVALID_RESPONSE;
     163           0 :     break;
     164           0 :   case MHD_HTTP_NOT_FOUND:
     165             :     /* Well-defined status code, pass on to application! */
     166           0 :     hr.ec = TALER_JSON_get_error_code (json);
     167           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     168           0 :     break;
     169           0 :   case MHD_HTTP_PRECONDITION_FAILED:
     170             :     /* Well-defined status code, pass on to application! */
     171           0 :     hr.ec = TALER_JSON_get_error_code (json);
     172           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     173           0 :     break;
     174           0 :   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 :     hr.ec = TALER_JSON_get_error_code (json);
     178           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     179           0 :     break;
     180           0 :   case MHD_HTTP_SERVICE_UNAVAILABLE:
     181             :     /* Server had an unclear (internal or external) issue; we should retry,
     182             :        but this API leaves this to the application */
     183           0 :     TALER_MERCHANT_parse_error_details_ (json,
     184             :                                          response_code,
     185             :                                          &hr);
     186           0 :     break;
     187           0 :   default:
     188             :     /* unexpected response code */
     189           0 :     GNUNET_break_op (0);
     190           0 :     TALER_MERCHANT_parse_error_details_ (json,
     191             :                                          response_code,
     192             :                                          &hr);
     193           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     194             :                 "Unexpected response code %u/%d\n",
     195             :                 (unsigned int) response_code,
     196             :                 (int) hr.ec);
     197           0 :     break;
     198             :   }
     199           0 :   if (NULL != tao->cb)
     200           0 :     tao->cb (tao->cb_cls,
     201             :              &hr,
     202             :              NULL,
     203             :              NULL,
     204             :              GNUNET_TIME_UNIT_ZERO_ABS);
     205           0 :   TALER_MERCHANT_tip_authorize_cancel (tao);
     206             : }
     207             : 
     208             : 
     209             : /**
     210             :  * Issue a /tip-authorize request to the backend.  Informs the backend
     211             :  * that a tip should be created.
     212             :  *
     213             :  * @param ctx execution context
     214             :  * @param backend_url base URL of the merchant backend
     215             :  * @param reserve_pub public key of the reserve
     216             :  * @param next_url where the browser should proceed after picking up the tip
     217             :  * @param amount amount to be handed out as a tip
     218             :  * @param justification which justification should be stored (human-readable reason for the tip)
     219             :  * @param authorize_cb callback which will work the response gotten from the backend
     220             :  * @param authorize_cb_cls closure to pass to @a authorize_cb
     221             :  * @return handle for this operation, NULL upon errors
     222             :  */
     223             : struct TALER_MERCHANT_TipAuthorizeHandle *
     224           0 : TALER_MERCHANT_tip_authorize2 (
     225             :   struct GNUNET_CURL_Context *ctx,
     226             :   const char *backend_url,
     227             :   const struct TALER_ReservePublicKeyP *reserve_pub,
     228             :   const char *next_url,
     229             :   const struct TALER_Amount *amount,
     230             :   const char *justification,
     231             :   TALER_MERCHANT_TipAuthorizeCallback authorize_cb,
     232             :   void *authorize_cb_cls)
     233             : {
     234             :   struct TALER_MERCHANT_TipAuthorizeHandle *tao;
     235             :   CURL *eh;
     236             :   json_t *te_obj;
     237             : 
     238           0 :   tao = GNUNET_new (struct TALER_MERCHANT_TipAuthorizeHandle);
     239           0 :   tao->ctx = ctx;
     240           0 :   tao->cb = authorize_cb;
     241           0 :   tao->cb_cls = authorize_cb_cls;
     242             : 
     243             :   {
     244             :     char res_str[sizeof (*reserve_pub) * 2];
     245             :     char arg_str[sizeof (res_str) + 48];
     246             :     char *end;
     247             : 
     248           0 :     end = GNUNET_STRINGS_data_to_string (reserve_pub,
     249             :                                          sizeof (*reserve_pub),
     250             :                                          res_str,
     251             :                                          sizeof (res_str));
     252           0 :     *end = '\0';
     253           0 :     GNUNET_snprintf (arg_str,
     254             :                      sizeof (arg_str),
     255             :                      "private/reserves/%s/authorize-tip",
     256             :                      res_str);
     257           0 :     tao->url = TALER_url_join (backend_url,
     258             :                                arg_str,
     259             :                                NULL);
     260             :   }
     261           0 :   if (NULL == tao->url)
     262             :   {
     263           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     264             :                 "Could not construct request URL.\n");
     265           0 :     GNUNET_free (tao);
     266           0 :     return NULL;
     267             :   }
     268           0 :   te_obj = json_pack ("{"
     269             :                       " s:o," /* amount */
     270             :                       " s:s," /* justification */
     271             :                       " s:s," /* next_url */
     272             :                       "}",
     273             :                       "amount", TALER_JSON_from_amount (amount),
     274             :                       "justification", justification,
     275             :                       "next_url", next_url);
     276           0 :   if (NULL == te_obj)
     277             :   {
     278           0 :     GNUNET_break (0);
     279           0 :     GNUNET_free (tao->url);
     280           0 :     GNUNET_free (tao);
     281           0 :     return NULL;
     282             :   }
     283             : 
     284           0 :   eh = curl_easy_init ();
     285           0 :   if (GNUNET_OK != TALER_curl_easy_post (&tao->post_ctx,
     286             :                                          eh,
     287             :                                          te_obj))
     288             :   {
     289           0 :     GNUNET_break (0);
     290           0 :     json_decref (te_obj);
     291           0 :     GNUNET_free (tao->url);
     292           0 :     GNUNET_free (tao);
     293           0 :     return NULL;
     294             :   }
     295             : 
     296           0 :   json_decref (te_obj);
     297           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     298             :               "Requesting URL '%s'\n",
     299             :               tao->url);
     300           0 :   GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     301             :                                                CURLOPT_URL,
     302             :                                                tao->url));
     303             : 
     304           0 :   tao->job = GNUNET_CURL_job_add2 (ctx,
     305             :                                    eh,
     306           0 :                                    tao->post_ctx.headers,
     307             :                                    &handle_tip_authorize_finished,
     308             :                                    tao);
     309           0 :   return tao;
     310             : }
     311             : 
     312             : 
     313             : /**
     314             :  * Issue a POST /tips request to the backend.  Informs the backend that a tip
     315             :  * should be created. In contrast to #TALER_MERCHANT_tip_authorize2(), the
     316             :  * backend gets to pick the reserve with this API.
     317             :  *
     318             :  * @param ctx execution context
     319             :  * @param backend_url base URL of the merchant backend
     320             :  * @param next_url where the browser should proceed after picking up the tip
     321             :  * @param amount amount to be handed out as a tip
     322             :  * @param justification which justification should be stored (human-readable reason for the tip)
     323             :  * @param authorize_cb callback which will work the response gotten from the backend
     324             :  * @param authorize_cb_cls closure to pass to @a authorize_cb
     325             :  * @return handle for this operation, NULL upon errors
     326             :  */
     327             : struct TALER_MERCHANT_TipAuthorizeHandle *
     328           0 : TALER_MERCHANT_tip_authorize (struct GNUNET_CURL_Context *ctx,
     329             :                               const char *backend_url,
     330             :                               const char *next_url,
     331             :                               const struct TALER_Amount *amount,
     332             :                               const char *justification,
     333             :                               TALER_MERCHANT_TipAuthorizeCallback authorize_cb,
     334             :                               void *authorize_cb_cls)
     335             : {
     336             :   struct TALER_MERCHANT_TipAuthorizeHandle *tao;
     337             :   CURL *eh;
     338             :   json_t *te_obj;
     339             : 
     340           0 :   tao = GNUNET_new (struct TALER_MERCHANT_TipAuthorizeHandle);
     341           0 :   tao->ctx = ctx;
     342           0 :   tao->cb = authorize_cb;
     343           0 :   tao->cb_cls = authorize_cb_cls;
     344             : 
     345           0 :   tao->url = TALER_url_join (backend_url,
     346             :                              "private/tips",
     347             :                              NULL);
     348           0 :   if (NULL == tao->url)
     349             :   {
     350           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     351             :                 "Could not construct request URL.\n");
     352           0 :     GNUNET_free (tao);
     353           0 :     return NULL;
     354             :   }
     355           0 :   te_obj = json_pack ("{"
     356             :                       " s:o," /* amount */
     357             :                       " s:s," /* justification */
     358             :                       " s:s," /* next_url */
     359             :                       "}",
     360             :                       "amount", TALER_JSON_from_amount (amount),
     361             :                       "justification", justification,
     362             :                       "next_url", next_url);
     363           0 :   if (NULL == te_obj)
     364             :   {
     365           0 :     GNUNET_break (0);
     366           0 :     GNUNET_free (tao->url);
     367           0 :     GNUNET_free (tao);
     368           0 :     return NULL;
     369             :   }
     370             : 
     371           0 :   eh = curl_easy_init ();
     372           0 :   if (GNUNET_OK != TALER_curl_easy_post (&tao->post_ctx,
     373             :                                          eh,
     374             :                                          te_obj))
     375             :   {
     376           0 :     GNUNET_break (0);
     377           0 :     json_decref (te_obj);
     378           0 :     GNUNET_free (tao->url);
     379           0 :     GNUNET_free (tao);
     380           0 :     return NULL;
     381             :   }
     382             : 
     383           0 :   json_decref (te_obj);
     384           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     385             :               "Requesting URL '%s'\n",
     386             :               tao->url);
     387           0 :   GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     388             :                                                CURLOPT_URL,
     389             :                                                tao->url));
     390             : 
     391           0 :   tao->job = GNUNET_CURL_job_add2 (ctx,
     392             :                                    eh,
     393           0 :                                    tao->post_ctx.headers,
     394             :                                    &handle_tip_authorize_finished,
     395             :                                    tao);
     396           0 :   return tao;
     397             : }
     398             : 
     399             : 
     400             : /**
     401             :  * Cancel a /track/transaction request.  This function cannot be used
     402             :  * on a request handle if a response is already served for it.
     403             :  *
     404             :  * @param tao handle to the tracking operation being cancelled
     405             :  */
     406             : void
     407           0 : TALER_MERCHANT_tip_authorize_cancel (
     408             :   struct TALER_MERCHANT_TipAuthorizeHandle *tao)
     409             : {
     410           0 :   if (NULL != tao->job)
     411             :   {
     412           0 :     GNUNET_CURL_job_cancel (tao->job);
     413           0 :     tao->job = NULL;
     414             :   }
     415           0 :   TALER_curl_easy_post_finished (&tao->post_ctx);
     416           0 :   GNUNET_free (tao->url);
     417           0 :   GNUNET_free (tao);
     418           0 : }
     419             : 
     420             : 
     421             : /* end of merchant_api_tip_authorize.c */

Generated by: LCOV version 1.14