exchange

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

exchange_api_post-management-auditors-AUDITOR_PUB-disable.c (7586B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2015-2026 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_post-management-auditors-AUDITOR_PUB-disable.c
     19  * @brief functions to disable an auditor
     20  * @author Christian Grothoff
     21  */
     22 #include "taler/taler_json_lib.h"
     23 #include <gnunet/gnunet_curl_lib.h>
     24 #include <microhttpd.h>
     25 #include "taler/exchange/post-management-auditors-AUDITOR_PUB-disable.h"
     26 #include "exchange_api_curl_defaults.h"
     27 #include "taler/taler_curl_lib.h"
     28 
     29 
     30 /**
     31  * @brief Handle for a POST /management/auditors/$AUDITOR_PUB/disable request.
     32  */
     33 struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle
     34 {
     35 
     36   /**
     37    * The base URL for this request.
     38    */
     39   char *base_url;
     40 
     41   /**
     42    * The full URL for this request, set during _start.
     43    */
     44   char *url;
     45 
     46   /**
     47    * Minor context that holds body and headers.
     48    */
     49   struct TALER_CURL_PostContext post_ctx;
     50 
     51   /**
     52    * Handle for the request.
     53    */
     54   struct GNUNET_CURL_Job *job;
     55 
     56   /**
     57    * Function to call with the result.
     58    */
     59   TALER_EXCHANGE_PostManagementAuditorsDisableCallback cb;
     60 
     61   /**
     62    * Closure for @a cb.
     63    */
     64   TALER_EXCHANGE_POST_MANAGEMENT_AUDITORS_DISABLE_RESULT_CLOSURE *cb_cls;
     65 
     66   /**
     67    * Reference to the execution context.
     68    */
     69   struct GNUNET_CURL_Context *ctx;
     70 
     71   /**
     72    * Public signing key of the auditor.
     73    */
     74   struct TALER_AuditorPublicKeyP auditor_pub;
     75 
     76   /**
     77    * When was this decided?
     78    */
     79   struct GNUNET_TIME_Timestamp validity_end;
     80 
     81   /**
     82    * Signature affirming the auditor disablement.
     83    */
     84   struct TALER_MasterSignatureP master_sig;
     85 
     86 };
     87 
     88 
     89 /**
     90  * Function called when we're done processing the
     91  * HTTP POST /management/auditors/$AUDITOR_PUB/disable request.
     92  *
     93  * @param cls the `struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle`
     94  * @param response_code HTTP response code, 0 on error
     95  * @param response response body, NULL if not in JSON
     96  */
     97 static void
     98 handle_auditors_disable_finished (void *cls,
     99                                   long response_code,
    100                                   const void *response)
    101 {
    102   struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle *pmadh = cls;
    103   const json_t *json = response;
    104   struct TALER_EXCHANGE_PostManagementAuditorsDisableResponse res = {
    105     .hr.http_status = (unsigned int) response_code,
    106     .hr.reply = json
    107   };
    108 
    109   pmadh->job = NULL;
    110   switch (response_code)
    111   {
    112   case MHD_HTTP_NO_CONTENT:
    113     break;
    114   case MHD_HTTP_FORBIDDEN:
    115     res.hr.ec = TALER_JSON_get_error_code (json);
    116     res.hr.hint = TALER_JSON_get_error_hint (json);
    117     break;
    118   case MHD_HTTP_NOT_FOUND:
    119     res.hr.ec = TALER_JSON_get_error_code (json);
    120     res.hr.hint = TALER_JSON_get_error_hint (json);
    121     break;
    122   case MHD_HTTP_CONFLICT:
    123     res.hr.ec = TALER_JSON_get_error_code (json);
    124     res.hr.hint = TALER_JSON_get_error_hint (json);
    125     break;
    126   default:
    127     /* unexpected response code */
    128     GNUNET_break_op (0);
    129     res.hr.ec = TALER_JSON_get_error_code (json);
    130     res.hr.hint = TALER_JSON_get_error_hint (json);
    131     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    132                 "Unexpected response code %u/%d for exchange management auditor disable\n",
    133                 (unsigned int) response_code,
    134                 (int) res.hr.ec);
    135     break;
    136   }
    137   if (NULL != pmadh->cb)
    138   {
    139     pmadh->cb (pmadh->cb_cls,
    140                &res);
    141     pmadh->cb = NULL;
    142   }
    143   TALER_EXCHANGE_post_management_auditors_disable_cancel (pmadh);
    144 }
    145 
    146 
    147 struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle *
    148 TALER_EXCHANGE_post_management_auditors_disable_create (
    149   struct GNUNET_CURL_Context *ctx,
    150   const char *url,
    151   const struct TALER_AuditorPublicKeyP *auditor_pub,
    152   struct GNUNET_TIME_Timestamp validity_end,
    153   const struct TALER_MasterSignatureP *master_sig)
    154 {
    155   struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle *pmadh;
    156 
    157   pmadh = GNUNET_new (
    158     struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle);
    159   pmadh->ctx = ctx;
    160   pmadh->base_url = GNUNET_strdup (url);
    161   pmadh->auditor_pub = *auditor_pub;
    162   pmadh->validity_end = validity_end;
    163   pmadh->master_sig = *master_sig;
    164   return pmadh;
    165 }
    166 
    167 
    168 enum TALER_ErrorCode
    169 TALER_EXCHANGE_post_management_auditors_disable_start (
    170   struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle *pmadh,
    171   TALER_EXCHANGE_PostManagementAuditorsDisableCallback cb,
    172   TALER_EXCHANGE_POST_MANAGEMENT_AUDITORS_DISABLE_RESULT_CLOSURE *cb_cls)
    173 {
    174   CURL *eh;
    175   json_t *body;
    176 
    177   pmadh->cb = cb;
    178   pmadh->cb_cls = cb_cls;
    179   {
    180     char epub_str[sizeof (pmadh->auditor_pub) * 2];
    181     char arg_str[sizeof (epub_str) + 64];
    182     char *end;
    183 
    184     end = GNUNET_STRINGS_data_to_string (&pmadh->auditor_pub,
    185                                          sizeof (pmadh->auditor_pub),
    186                                          epub_str,
    187                                          sizeof (epub_str));
    188     *end = '\0';
    189     GNUNET_snprintf (arg_str,
    190                      sizeof (arg_str),
    191                      "management/auditors/%s/disable",
    192                      epub_str);
    193     pmadh->url = TALER_url_join (pmadh->base_url,
    194                                  arg_str,
    195                                  NULL);
    196   }
    197   if (NULL == pmadh->url)
    198   {
    199     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    200                 "Could not construct request URL.\n");
    201     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
    202   }
    203   body = GNUNET_JSON_PACK (
    204     GNUNET_JSON_pack_data_auto ("master_sig",
    205                                 &pmadh->master_sig),
    206     GNUNET_JSON_pack_timestamp ("validity_end",
    207                                 pmadh->validity_end));
    208   eh = TALER_EXCHANGE_curl_easy_get_ (pmadh->url);
    209   if ( (NULL == eh) ||
    210        (GNUNET_OK !=
    211         TALER_curl_easy_post (&pmadh->post_ctx,
    212                               eh,
    213                               body)) )
    214   {
    215     GNUNET_break (0);
    216     if (NULL != eh)
    217       curl_easy_cleanup (eh);
    218     json_decref (body);
    219     GNUNET_free (pmadh->url);
    220     pmadh->url = NULL;
    221     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    222   }
    223   json_decref (body);
    224   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    225               "Requesting URL '%s'\n",
    226               pmadh->url);
    227   pmadh->job = GNUNET_CURL_job_add2 (pmadh->ctx,
    228                                      eh,
    229                                      pmadh->post_ctx.headers,
    230                                      &handle_auditors_disable_finished,
    231                                      pmadh);
    232   if (NULL == pmadh->job)
    233   {
    234     TALER_curl_easy_post_finished (&pmadh->post_ctx);
    235     GNUNET_free (pmadh->url);
    236     pmadh->url = NULL;
    237     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
    238   }
    239   return TALER_EC_NONE;
    240 }
    241 
    242 
    243 void
    244 TALER_EXCHANGE_post_management_auditors_disable_cancel (
    245   struct TALER_EXCHANGE_PostManagementAuditorsDisableHandle *pmadh)
    246 {
    247   if (NULL != pmadh->job)
    248   {
    249     GNUNET_CURL_job_cancel (pmadh->job);
    250     pmadh->job = NULL;
    251   }
    252   TALER_curl_easy_post_finished (&pmadh->post_ctx);
    253   GNUNET_free (pmadh->url);
    254   GNUNET_free (pmadh->base_url);
    255   GNUNET_free (pmadh);
    256 }
    257 
    258 
    259 /* end of exchange_api_post-management-auditors-AUDITOR_PUB-disable.c */