LCOV - code coverage report
Current view: top level - lib - merchant_api_get_config.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 57.4 % 115 66
Test Date: 2025-10-25 17:15:54 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2014-2025 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 merchant_api_get_config.c
      19              :  * @brief Implementation of the /config request of the merchant'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_curl_lib.h>
      28              : #include "taler_merchant_service.h"
      29              : #include "merchant_api_curl_defaults.h"
      30              : #include <taler/taler_json_lib.h>
      31              : #include <taler/taler_signatures.h>
      32              : 
      33              : /**
      34              :  * Which version of the Taler protocol is implemented
      35              :  * by this library?  Used to determine compatibility.
      36              :  */
      37              : #define MERCHANT_PROTOCOL_CURRENT 22
      38              : 
      39              : /**
      40              :  * How many configs are we backwards-compatible with?
      41              :  */
      42              : #define MERCHANT_PROTOCOL_AGE 10
      43              : 
      44              : /**
      45              :  * How many exchanges do we allow at most per merchant?
      46              :  */
      47              : #define MAX_EXCHANGES 1024
      48              : 
      49              : /**
      50              :  * How many currency specs do we allow at most per merchant?
      51              :  */
      52              : #define MAX_CURRENCIES 1024
      53              : 
      54              : /**
      55              :  * @brief A handle for /config operations
      56              :  */
      57              : struct TALER_MERCHANT_ConfigGetHandle
      58              : {
      59              :   /**
      60              :    * The url for this request.
      61              :    */
      62              :   char *url;
      63              : 
      64              :   /**
      65              :    * Handle for the request.
      66              :    */
      67              :   struct GNUNET_CURL_Job *job;
      68              : 
      69              :   /**
      70              :    * Function to call with the result.
      71              :    */
      72              :   TALER_MERCHANT_ConfigCallback cb;
      73              : 
      74              :   /**
      75              :    * Closure for @a cb.
      76              :    */
      77              :   void *cb_cls;
      78              : 
      79              :   /**
      80              :    * Reference to the execution context.
      81              :    */
      82              :   struct GNUNET_CURL_Context *ctx;
      83              : 
      84              : };
      85              : 
      86              : 
      87              : /**
      88              :  * Function called when we're done processing the
      89              :  * HTTP /config request.
      90              :  *
      91              :  * @param cls the `struct TALER_MERCHANT_ConfigGetHandle`
      92              :  * @param response_code HTTP response code, 0 on error
      93              :  * @param response response body, NULL if not in JSON
      94              :  */
      95              : static void
      96            3 : handle_config_finished (void *cls,
      97              :                         long response_code,
      98              :                         const void *response)
      99              : {
     100            3 :   struct TALER_MERCHANT_ConfigGetHandle *vgh = cls;
     101            3 :   const json_t *json = response;
     102            3 :   struct TALER_MERCHANT_ConfigResponse cr = {
     103            3 :     .hr.http_status = (unsigned int) response_code,
     104              :     .hr.reply = json
     105              :   };
     106              : 
     107            3 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     108              :               "Got /config response with status code %u\n",
     109              :               (unsigned int) response_code);
     110              : 
     111            3 :   vgh->job = NULL;
     112            3 :   switch (response_code)
     113              :   {
     114            3 :   case MHD_HTTP_OK:
     115              :     {
     116              :       const json_t *jcs;
     117            3 :       const json_t *exchanges = NULL;
     118            3 :       struct TALER_MERCHANT_ExchangeConfigInfo *eci = NULL;
     119            3 :       unsigned int num_eci = 0;
     120              :       unsigned int nspec;
     121              :       struct TALER_JSON_ProtocolVersion pv;
     122              :       struct GNUNET_JSON_Specification spec[] = {
     123            3 :         GNUNET_JSON_spec_object_const ("currencies",
     124              :                                        &jcs),
     125            3 :         GNUNET_JSON_spec_array_const ("exchanges",
     126              :                                       &exchanges),
     127            3 :         GNUNET_JSON_spec_string ("currency",
     128              :                                  &cr.details.ok.ci.currency),
     129            3 :         TALER_JSON_spec_version ("version",
     130              :                                  &pv),
     131            3 :         GNUNET_JSON_spec_string ("version",
     132              :                                  &cr.details.ok.ci.version),
     133            3 :         GNUNET_JSON_spec_end ()
     134              :       };
     135              : 
     136            3 :       cr.details.ok.compat = TALER_MERCHANT_VC_PROTOCOL_ERROR;
     137            3 :       if (GNUNET_OK !=
     138            3 :           GNUNET_JSON_parse (json,
     139              :                              spec,
     140              :                              NULL, NULL))
     141              :       {
     142            0 :         GNUNET_break_op (0);
     143            0 :         cr.hr.http_status = 0;
     144            0 :         cr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     145            0 :         break;
     146              :       }
     147            3 :       cr.details.ok.compat = TALER_MERCHANT_VC_MATCH;
     148            3 :       if (MERCHANT_PROTOCOL_CURRENT < pv.current)
     149              :       {
     150            0 :         cr.details.ok.compat |= TALER_MERCHANT_VC_NEWER;
     151            0 :         if (MERCHANT_PROTOCOL_CURRENT < pv.current - pv.age)
     152            0 :           cr.details.ok.compat |= TALER_MERCHANT_VC_INCOMPATIBLE;
     153              :       }
     154            3 :       if (MERCHANT_PROTOCOL_CURRENT > pv.current)
     155              :       {
     156            0 :         cr.details.ok.compat |= TALER_MERCHANT_VC_OLDER;
     157            0 :         if (MERCHANT_PROTOCOL_CURRENT - MERCHANT_PROTOCOL_AGE > pv.current)
     158            0 :           cr.details.ok.compat |= TALER_MERCHANT_VC_INCOMPATIBLE;
     159              :       }
     160              : 
     161            3 :       nspec = (unsigned int) json_object_size (jcs);
     162            3 :       if ( (nspec > MAX_CURRENCIES) ||
     163            3 :            (json_object_size (jcs) != (size_t) nspec) )
     164              :       {
     165            0 :         GNUNET_break_op (0);
     166            0 :         cr.hr.http_status = 0;
     167            0 :         cr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     168            0 :         break;
     169              :       }
     170            3 :       if (NULL != exchanges)
     171              :       {
     172            3 :         num_eci = (unsigned int) json_object_size (exchanges);
     173            3 :         if ( (num_eci > MAX_EXCHANGES) ||
     174            3 :              (json_object_size (exchanges) != (size_t) num_eci) )
     175              :         {
     176            0 :           GNUNET_break_op (0);
     177            0 :           cr.hr.http_status = 0;
     178            0 :           cr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     179            0 :           break;
     180              :         }
     181            3 :         eci = GNUNET_new_array (num_eci,
     182              :                                 struct TALER_MERCHANT_ExchangeConfigInfo);
     183            3 :         for (unsigned int i = 0; i<num_eci; i++)
     184              :         {
     185            0 :           struct TALER_MERCHANT_ExchangeConfigInfo *ei = &eci[i];
     186            0 :           const json_t *ej = json_array_get (exchanges,
     187              :                                              i);
     188              :           struct GNUNET_JSON_Specification ispec[] = {
     189            0 :             GNUNET_JSON_spec_string ("currency",
     190              :                                      &ei->currency),
     191            0 :             GNUNET_JSON_spec_string ("base_url",
     192              :                                      &ei->base_url),
     193            0 :             GNUNET_JSON_spec_fixed_auto ("master_pub",
     194              :                                          &ei->master_pub),
     195            0 :             GNUNET_JSON_spec_end ()
     196              :           };
     197              : 
     198            0 :           if (GNUNET_OK !=
     199            0 :               GNUNET_JSON_parse (ej,
     200              :                                  ispec,
     201              :                                  NULL, NULL))
     202              :           {
     203            0 :             GNUNET_break_op (0);
     204            0 :             cr.hr.http_status = 0;
     205            0 :             cr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     206            0 :             GNUNET_free (eci);
     207            0 :             break;
     208              :           }
     209              :         }
     210              :       }
     211              :       {
     212              :         struct TALER_CurrencySpecification *cspecs;
     213            3 :         unsigned int off = 0;
     214              :         json_t *obj;
     215              :         const char *curr;
     216              : 
     217            3 :         cspecs = GNUNET_new_array (nspec,
     218              :                                    struct TALER_CurrencySpecification);
     219            3 :         cr.details.ok.num_cspecs = nspec;
     220            3 :         cr.details.ok.cspecs = cspecs;
     221            3 :         cr.details.ok.num_exchanges = (unsigned int) num_eci;
     222            3 :         cr.details.ok.exchanges = eci;
     223            6 :         json_object_foreach ((json_t *) jcs, curr, obj)
     224              :         {
     225            3 :           struct TALER_CurrencySpecification *cs = &cspecs[off++];
     226              :           struct GNUNET_JSON_Specification cspec[] = {
     227            3 :             TALER_JSON_spec_currency_specification (curr,
     228              :                                                     curr,
     229              :                                                     cs),
     230            3 :             GNUNET_JSON_spec_end ()
     231              :           };
     232              : 
     233            3 :           if (GNUNET_OK !=
     234            3 :               GNUNET_JSON_parse (jcs,
     235              :                                  cspec,
     236              :                                  NULL, NULL))
     237              :           {
     238            0 :             GNUNET_break_op (0);
     239            0 :             cr.hr.http_status = 0;
     240            0 :             cr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     241            0 :             GNUNET_free (eci);
     242            0 :             TALER_CONFIG_free_currencies (off - 1,
     243              :                                           cspecs);
     244            0 :             break;
     245              :           }
     246              :         }
     247            3 :         vgh->cb (vgh->cb_cls,
     248              :                  &cr);
     249            3 :         GNUNET_free (eci);
     250            3 :         TALER_CONFIG_free_currencies (nspec,
     251              :                                       cspecs);
     252              :       }
     253            3 :       TALER_MERCHANT_config_get_cancel (vgh);
     254            3 :       return;
     255              :     }
     256            0 :   default:
     257              :     /* unexpected response code */
     258            0 :     cr.hr.ec = TALER_JSON_get_error_code (json);
     259            0 :     cr.hr.hint = TALER_JSON_get_error_hint (json);
     260            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     261              :                 "Unexpected response code %u/%d\n",
     262              :                 (unsigned int) response_code,
     263              :                 (int) cr.hr.ec);
     264            0 :     break;
     265              :   }
     266            0 :   vgh->cb (vgh->cb_cls,
     267              :            &cr);
     268            0 :   TALER_MERCHANT_config_get_cancel (vgh);
     269              : }
     270              : 
     271              : 
     272              : struct TALER_MERCHANT_ConfigGetHandle *
     273            3 : TALER_MERCHANT_config_get (struct GNUNET_CURL_Context *ctx,
     274              :                            const char *backend_url,
     275              :                            TALER_MERCHANT_ConfigCallback config_cb,
     276              :                            void *config_cb_cls)
     277              : {
     278              :   struct TALER_MERCHANT_ConfigGetHandle *vgh;
     279              :   CURL *eh;
     280              : 
     281            3 :   vgh = GNUNET_new (struct TALER_MERCHANT_ConfigGetHandle);
     282            3 :   vgh->ctx = ctx;
     283            3 :   vgh->cb = config_cb;
     284            3 :   vgh->cb_cls = config_cb_cls;
     285            3 :   vgh->url = TALER_url_join (backend_url,
     286              :                              "config",
     287              :                              NULL);
     288            3 :   if (NULL == vgh->url)
     289              :   {
     290            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     291              :                 "Could not construct request URL.\n");
     292            0 :     GNUNET_free (vgh);
     293            0 :     return NULL;
     294              :   }
     295            3 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     296              :               "Requesting URL '%s'\n",
     297              :               vgh->url);
     298            3 :   eh = TALER_MERCHANT_curl_easy_get_ (vgh->url);
     299            3 :   vgh->job = GNUNET_CURL_job_add (ctx,
     300              :                                   eh,
     301              :                                   &handle_config_finished,
     302              :                                   vgh);
     303            3 :   return vgh;
     304              : }
     305              : 
     306              : 
     307              : void
     308            3 : TALER_MERCHANT_config_get_cancel (struct TALER_MERCHANT_ConfigGetHandle *vgh)
     309              : {
     310            3 :   if (NULL != vgh->job)
     311              :   {
     312            0 :     GNUNET_CURL_job_cancel (vgh->job);
     313            0 :     vgh->job = NULL;
     314              :   }
     315            3 :   GNUNET_free (vgh->url);
     316            3 :   GNUNET_free (vgh);
     317            3 : }
     318              : 
     319              : 
     320              : /* end of merchant_api_config_get.c */
        

Generated by: LCOV version 2.0-1