LCOV - code coverage report
Current view: top level - lib - merchant_api_post_instances.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 99 0.0 %
Date: 2022-08-25 06:17:04 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) 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,
      11             :   but 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
      16             :   Public License along with TALER; see the file COPYING.LGPL.
      17             :   If not, see <http://www.gnu.org/licenses/>
      18             : */
      19             : /**
      20             :  * @file merchant_api_post_instances.c
      21             :  * @brief Implementation of the POST /instances request
      22             :  *        of the merchant's HTTP API
      23             :  * @author Christian Grothoff
      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 "taler_merchant_service.h"
      31             : #include "merchant_api_curl_defaults.h"
      32             : #include <taler/taler_json_lib.h>
      33             : #include <taler/taler_curl_lib.h>
      34             : 
      35             : 
      36             : /**
      37             :  * Handle for a POST /instances/$ID operation.
      38             :  */
      39             : struct TALER_MERCHANT_InstancesPostHandle
      40             : {
      41             : 
      42             :   /**
      43             :    * The url for this request.
      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_InstancesPostCallback 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             : /**
      76             :  * Function called when we're done processing the
      77             :  * HTTP POST /instances request.
      78             :  *
      79             :  * @param cls the `struct TALER_MERCHANT_InstancesPostHandle`
      80             :  * @param response_code HTTP response code, 0 on error
      81             :  * @param response response body, NULL if not in JSON
      82             :  */
      83             : static void
      84           0 : handle_post_instances_finished (void *cls,
      85             :                                 long response_code,
      86             :                                 const void *response)
      87             : {
      88           0 :   struct TALER_MERCHANT_InstancesPostHandle *iph = cls;
      89           0 :   const json_t *json = response;
      90           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      91           0 :     .http_status = (unsigned int) response_code,
      92             :     .reply = json
      93             :   };
      94             : 
      95           0 :   iph->job = NULL;
      96           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      97             :               "POST /management/instances completed with response code %u\n",
      98             :               (unsigned int) response_code);
      99           0 :   switch (response_code)
     100             :   {
     101           0 :   case 0:
     102           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     103           0 :     break;
     104           0 :   case MHD_HTTP_NO_CONTENT:
     105           0 :     break;
     106           0 :   case MHD_HTTP_BAD_REQUEST:
     107           0 :     hr.ec = TALER_JSON_get_error_code (json);
     108           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     109             :     /* This should never happen, either us
     110             :      * or the merchant is buggy (or API version conflict);
     111             :      * just pass JSON reply to the application */
     112           0 :     break;
     113           0 :   case MHD_HTTP_UNAUTHORIZED:
     114           0 :     hr.ec = TALER_JSON_get_error_code (json);
     115           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     116             :     /* Nothing really to verify, merchant says we need to authenticate. */
     117           0 :     break;
     118           0 :   case MHD_HTTP_FORBIDDEN:
     119           0 :     hr.ec = TALER_JSON_get_error_code (json);
     120           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     121             :     /* Nothing really to verify, merchant says we tried to abort the payment
     122             :      * after it was successful. We should pass the JSON reply to the
     123             :      * application */
     124           0 :     break;
     125           0 :   case MHD_HTTP_NOT_FOUND:
     126           0 :     hr.ec = TALER_JSON_get_error_code (json);
     127           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     128             :     /* Nothing really to verify, this should never
     129             :        happen, we should pass the JSON reply to the
     130             :        application */
     131           0 :     break;
     132           0 :   case MHD_HTTP_CONFLICT:
     133           0 :     hr.ec = TALER_JSON_get_error_code (json);
     134           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     135           0 :     break;
     136           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     137           0 :     hr.ec = TALER_JSON_get_error_code (json);
     138           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     139             :     /* Server had an internal issue; we should retry,
     140             :        but this API leaves this to the application */
     141           0 :     break;
     142           0 :   default:
     143           0 :     TALER_MERCHANT_parse_error_details_ (json,
     144             :                                          response_code,
     145             :                                          &hr);
     146             :     /* unexpected response code */
     147           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     148             :                 "Unexpected response code %u/%d\n",
     149             :                 (unsigned int) response_code,
     150             :                 (int) hr.ec);
     151           0 :     GNUNET_break_op (0);
     152           0 :     break;
     153             :   }
     154           0 :   iph->cb (iph->cb_cls,
     155             :            &hr);
     156           0 :   TALER_MERCHANT_instances_post_cancel (iph);
     157           0 : }
     158             : 
     159             : 
     160             : struct TALER_MERCHANT_InstancesPostHandle *
     161           0 : TALER_MERCHANT_instances_post (
     162             :   struct GNUNET_CURL_Context *ctx,
     163             :   const char *backend_url,
     164             :   const char *instance_id,
     165             :   unsigned int accounts_length,
     166             :   const char *payto_uris[],
     167             :   const char *name,
     168             :   const json_t *address,
     169             :   const json_t *jurisdiction,
     170             :   const struct TALER_Amount *default_max_wire_fee,
     171             :   uint32_t default_wire_fee_amortization,
     172             :   const struct TALER_Amount *default_max_deposit_fee,
     173             :   struct GNUNET_TIME_Relative default_wire_transfer_delay,
     174             :   struct GNUNET_TIME_Relative default_pay_delay,
     175             :   const char *auth_token,
     176             :   TALER_MERCHANT_InstancesPostCallback cb,
     177             :   void *cb_cls)
     178             : {
     179             :   struct TALER_MERCHANT_InstancesPostHandle *iph;
     180             :   json_t *jpayto_uris;
     181             :   json_t *req_obj;
     182             :   json_t *auth_obj;
     183             : 
     184           0 :   if (NULL != auth_token)
     185             :   {
     186           0 :     if (0 != strncasecmp (RFC_8959_PREFIX,
     187             :                           auth_token,
     188             :                           strlen (RFC_8959_PREFIX)))
     189             :     {
     190           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     191             :                   "Authentication token must start with `%s'\n",
     192             :                   RFC_8959_PREFIX);
     193           0 :       return NULL;
     194             :     }
     195           0 :     auth_obj = GNUNET_JSON_PACK (
     196             :       GNUNET_JSON_pack_string ("method",
     197             :                                "token"),
     198             :       GNUNET_JSON_pack_string ("token",
     199             :                                auth_token));
     200             :   }
     201             :   else
     202             :   {
     203           0 :     auth_obj = GNUNET_JSON_PACK (
     204             :       GNUNET_JSON_pack_string ("method",
     205             :                                "external"));
     206             :   }
     207           0 :   if (NULL == auth_obj)
     208             :   {
     209           0 :     GNUNET_break (0);
     210           0 :     return NULL;
     211             :   }
     212           0 :   jpayto_uris = json_array ();
     213           0 :   if (NULL == jpayto_uris)
     214             :   {
     215           0 :     json_decref (auth_obj);
     216           0 :     GNUNET_break (0);
     217           0 :     return NULL;
     218             :   }
     219           0 :   for (unsigned int i = 0; i<accounts_length; i++)
     220             :   {
     221           0 :     if (0 !=
     222           0 :         json_array_append_new (jpayto_uris,
     223           0 :                                json_string (payto_uris[i])))
     224             :     {
     225           0 :       GNUNET_break (0);
     226           0 :       json_decref (auth_obj);
     227           0 :       json_decref (jpayto_uris);
     228           0 :       return NULL;
     229             :     }
     230             :   }
     231           0 :   req_obj = GNUNET_JSON_PACK (
     232             :     GNUNET_JSON_pack_array_steal ("payto_uris",
     233             :                                   jpayto_uris),
     234             :     GNUNET_JSON_pack_string ("id",
     235             :                              instance_id),
     236             :     GNUNET_JSON_pack_string ("name",
     237             :                              name),
     238             :     GNUNET_JSON_pack_object_incref ("address",
     239             :                                     (json_t *) address),
     240             :     GNUNET_JSON_pack_object_incref ("jurisdiction",
     241             :                                     (json_t *) jurisdiction),
     242             :     TALER_JSON_pack_amount ("default_max_wire_fee",
     243             :                             default_max_wire_fee),
     244             :     GNUNET_JSON_pack_uint64 ("default_wire_fee_amortization",
     245             :                              default_wire_fee_amortization),
     246             :     TALER_JSON_pack_amount ("default_max_deposit_fee",
     247             :                             default_max_deposit_fee),
     248             :     GNUNET_JSON_pack_time_rel ("default_wire_transfer_delay",
     249             :                                default_wire_transfer_delay),
     250             :     GNUNET_JSON_pack_time_rel ("default_pay_delay",
     251             :                                default_pay_delay),
     252             :     GNUNET_JSON_pack_object_steal ("auth",
     253             :                                    auth_obj));
     254           0 :   iph = GNUNET_new (struct TALER_MERCHANT_InstancesPostHandle);
     255           0 :   iph->ctx = ctx;
     256           0 :   iph->cb = cb;
     257           0 :   iph->cb_cls = cb_cls;
     258           0 :   iph->url = TALER_url_join (backend_url,
     259             :                              "management/instances",
     260             :                              NULL);
     261           0 :   if (NULL == iph->url)
     262             :   {
     263           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     264             :                 "Could not construct request URL.\n");
     265           0 :     json_decref (req_obj);
     266           0 :     GNUNET_free (iph);
     267           0 :     return NULL;
     268             :   }
     269             :   {
     270             :     CURL *eh;
     271             : 
     272           0 :     eh = TALER_MERCHANT_curl_easy_get_ (iph->url);
     273           0 :     if (GNUNET_OK !=
     274           0 :         TALER_curl_easy_post (&iph->post_ctx,
     275             :                               eh,
     276             :                               req_obj))
     277             :     {
     278           0 :       GNUNET_break (0);
     279           0 :       curl_easy_cleanup (eh);
     280           0 :       json_decref (req_obj);
     281           0 :       GNUNET_free (iph);
     282           0 :       return NULL;
     283             :     }
     284           0 :     json_decref (req_obj);
     285           0 :     iph->job = GNUNET_CURL_job_add2 (ctx,
     286             :                                      eh,
     287           0 :                                      iph->post_ctx.headers,
     288             :                                      &handle_post_instances_finished,
     289             :                                      iph);
     290             :   }
     291           0 :   return iph;
     292             : }
     293             : 
     294             : 
     295             : void
     296           0 : TALER_MERCHANT_instances_post_cancel (
     297             :   struct TALER_MERCHANT_InstancesPostHandle *iph)
     298             : {
     299           0 :   if (NULL != iph->job)
     300             :   {
     301           0 :     GNUNET_CURL_job_cancel (iph->job);
     302           0 :     iph->job = NULL;
     303             :   }
     304           0 :   TALER_curl_easy_post_finished (&iph->post_ctx);
     305           0 :   GNUNET_free (iph->url);
     306           0 :   GNUNET_free (iph);
     307           0 : }
     308             : 
     309             : 
     310             : /* end of merchant_api_post_instances.c */

Generated by: LCOV version 1.14