exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

exchange_api_auditor_add_denomination.c (7010B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2015-2021 Taler Systems SA
      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 lib/exchange_api_auditor_add_denomination.c
     19  * @brief functions for the auditor to add its signature for denomination at the exchange
     20  * @author Christian Grothoff
     21  */
     22 #include "taler/platform.h"
     23 #include "taler/taler_json_lib.h"
     24 #include <gnunet/gnunet_curl_lib.h>
     25 #include <microhttpd.h>
     26 #include "taler/taler_exchange_service.h"
     27 #include "auditor_api_curl_defaults.h"
     28 #include "taler/taler_signatures.h"
     29 #include "taler/taler_curl_lib.h"
     30 #include "taler/taler_json_lib.h"
     31 
     32 
     33 struct TALER_EXCHANGE_AuditorAddDenominationHandle
     34 {
     35 
     36   /**
     37    * The url for this request.
     38    */
     39   char *url;
     40 
     41   /**
     42    * Minor context that holds body and headers.
     43    */
     44   struct TALER_CURL_PostContext post_ctx;
     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_EXCHANGE_AuditorAddDenominationCallback 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 
     68 /**
     69  * Function called when we're done processing the
     70  * HTTP POST /auditor/$AUDITOR_PUB/$H_DENOM_PUB request.
     71  *
     72  * @param cls the `struct TALER_EXCHANGE_AuditorAddDenominationHandle *`
     73  * @param response_code HTTP response code, 0 on error
     74  * @param response response body, NULL if not in JSON
     75  */
     76 static void
     77 handle_auditor_add_denomination_finished (void *cls,
     78                                           long response_code,
     79                                           const void *response)
     80 {
     81   struct TALER_EXCHANGE_AuditorAddDenominationHandle *ah = cls;
     82   const json_t *json = response;
     83   struct TALER_EXCHANGE_AuditorAddDenominationResponse adr = {
     84     .hr.http_status = (unsigned int) response_code,
     85     .hr.reply = json
     86   };
     87 
     88   ah->job = NULL;
     89   switch (response_code)
     90   {
     91   case MHD_HTTP_NO_CONTENT:
     92     break;
     93   case MHD_HTTP_FORBIDDEN:
     94     adr.hr.ec = TALER_JSON_get_error_code (json);
     95     adr.hr.hint = TALER_JSON_get_error_hint (json);
     96     break;
     97   case MHD_HTTP_NOT_FOUND:
     98     adr.hr.ec = TALER_JSON_get_error_code (json);
     99     adr.hr.hint = TALER_JSON_get_error_hint (json);
    100     break;
    101   case MHD_HTTP_GONE:
    102     adr.hr.ec = TALER_JSON_get_error_code (json);
    103     adr.hr.hint = TALER_JSON_get_error_hint (json);
    104     break;
    105   case MHD_HTTP_PRECONDITION_FAILED:
    106     adr.hr.ec = TALER_JSON_get_error_code (json);
    107     adr.hr.hint = TALER_JSON_get_error_hint (json);
    108     break;
    109   default:
    110     /* unexpected response code */
    111     if (NULL != json)
    112     {
    113       adr.hr.ec = TALER_JSON_get_error_code (json);
    114       adr.hr.hint = TALER_JSON_get_error_hint (json);
    115       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    116                   "Unexpected response code %u/%d for exchange auditor-add-denomination at URL `%s'\n",
    117                   (unsigned int) response_code,
    118                   (int) adr.hr.ec,
    119                   ah->url);
    120     }
    121     else
    122     {
    123       adr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    124       adr.hr.hint = NULL;
    125       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    126                   "Unexpected HTTP response code %u (no JSON returned) at URL `%s'\n",
    127                   (unsigned int) response_code,
    128                   ah->url);
    129     }
    130     break;
    131   }
    132   if (NULL != ah->cb)
    133   {
    134     ah->cb (ah->cb_cls,
    135             &adr);
    136     ah->cb = NULL;
    137   }
    138   TALER_EXCHANGE_add_auditor_denomination_cancel (ah);
    139 }
    140 
    141 
    142 struct TALER_EXCHANGE_AuditorAddDenominationHandle *
    143 TALER_EXCHANGE_add_auditor_denomination (
    144   struct GNUNET_CURL_Context *ctx,
    145   const char *url,
    146   const struct TALER_DenominationHashP *h_denom_pub,
    147   const struct TALER_AuditorPublicKeyP *auditor_pub,
    148   const struct TALER_AuditorSignatureP *auditor_sig,
    149   TALER_EXCHANGE_AuditorAddDenominationCallback cb,
    150   void *cb_cls)
    151 {
    152   struct TALER_EXCHANGE_AuditorAddDenominationHandle *ah;
    153   CURL *eh;
    154   json_t *body;
    155 
    156   ah = GNUNET_new (struct TALER_EXCHANGE_AuditorAddDenominationHandle);
    157   ah->cb = cb;
    158   ah->cb_cls = cb_cls;
    159   ah->ctx = ctx;
    160   {
    161     char apub_str[sizeof (*auditor_pub) * 2];
    162     char denom_str[sizeof (*h_denom_pub) * 2];
    163     char arg_str[sizeof (apub_str) + sizeof (denom_str) + 32];
    164     char *end;
    165 
    166     end = GNUNET_STRINGS_data_to_string (auditor_pub,
    167                                          sizeof (*auditor_pub),
    168                                          apub_str,
    169                                          sizeof (apub_str));
    170     *end = '\0';
    171     end = GNUNET_STRINGS_data_to_string (h_denom_pub,
    172                                          sizeof (*h_denom_pub),
    173                                          denom_str,
    174                                          sizeof (denom_str));
    175     *end = '\0';
    176     GNUNET_snprintf (arg_str,
    177                      sizeof (arg_str),
    178                      "auditors/%s/%s",
    179                      apub_str,
    180                      denom_str);
    181     ah->url = TALER_url_join (url,
    182                               arg_str,
    183                               NULL);
    184   }
    185   if (NULL == ah->url)
    186   {
    187     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    188                 "Could not construct request URL.\n");
    189     GNUNET_free (ah);
    190     return NULL;
    191   }
    192   body = GNUNET_JSON_PACK (
    193     GNUNET_JSON_pack_data_auto ("auditor_sig",
    194                                 auditor_sig));
    195   eh = TALER_AUDITOR_curl_easy_get_ (ah->url);
    196   if ( (NULL == eh) ||
    197        (GNUNET_OK !=
    198         TALER_curl_easy_post (&ah->post_ctx,
    199                               eh,
    200                               body)) )
    201   {
    202     GNUNET_break (0);
    203     if (NULL != eh)
    204       curl_easy_cleanup (eh);
    205     json_decref (body);
    206     GNUNET_free (ah->url);
    207     return NULL;
    208   }
    209   json_decref (body);
    210   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    211               "Requesting URL '%s'\n",
    212               ah->url);
    213   ah->job = GNUNET_CURL_job_add2 (ctx,
    214                                   eh,
    215                                   ah->post_ctx.headers,
    216                                   &handle_auditor_add_denomination_finished,
    217                                   ah);
    218   if (NULL == ah->job)
    219   {
    220     TALER_EXCHANGE_add_auditor_denomination_cancel (ah);
    221     return NULL;
    222   }
    223   return ah;
    224 }
    225 
    226 
    227 void
    228 TALER_EXCHANGE_add_auditor_denomination_cancel (
    229   struct TALER_EXCHANGE_AuditorAddDenominationHandle *ah)
    230 {
    231   if (NULL != ah->job)
    232   {
    233     GNUNET_CURL_job_cancel (ah->job);
    234     ah->job = NULL;
    235   }
    236   TALER_curl_easy_post_finished (&ah->post_ctx);
    237   GNUNET_free (ah->url);
    238   GNUNET_free (ah);
    239 }