LCOV - code coverage report
Current view: top level - lib - exchange_api_management_auditor_disable.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 44 71 62.0 %
Date: 2025-06-05 21:03:14 Functions: 3 3 100.0 %

          Line data    Source code
       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_management_auditor_disable.c
      19             :  * @brief functions to disable an auditor
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include "taler_json_lib.h"
      24             : #include <gnunet/gnunet_curl_lib.h>
      25             : #include <microhttpd.h>
      26             : #include "taler_exchange_service.h"
      27             : #include "exchange_api_curl_defaults.h"
      28             : #include "taler_signatures.h"
      29             : #include "taler_curl_lib.h"
      30             : #include "taler_json_lib.h"
      31             : 
      32             : /**
      33             :  * @brief Handle for a POST /management/auditors/$AUDITOR_PUB/disable request.
      34             :  */
      35             : struct TALER_EXCHANGE_ManagementAuditorDisableHandle
      36             : {
      37             : 
      38             :   /**
      39             :    * The url for this request.
      40             :    */
      41             :   char *url;
      42             : 
      43             :   /**
      44             :    * Minor context that holds body and headers.
      45             :    */
      46             :   struct TALER_CURL_PostContext post_ctx;
      47             : 
      48             :   /**
      49             :    * Handle for the request.
      50             :    */
      51             :   struct GNUNET_CURL_Job *job;
      52             : 
      53             :   /**
      54             :    * Function to call with the result.
      55             :    */
      56             :   TALER_EXCHANGE_ManagementAuditorDisableCallback cb;
      57             : 
      58             :   /**
      59             :    * Closure for @a cb.
      60             :    */
      61             :   void *cb_cls;
      62             : 
      63             :   /**
      64             :    * Reference to the execution context.
      65             :    */
      66             :   struct GNUNET_CURL_Context *ctx;
      67             : };
      68             : 
      69             : 
      70             : /**
      71             :  * Function called when we're done processing the
      72             :  * HTTP /management/auditors/%s/disable request.
      73             :  *
      74             :  * @param cls the `struct TALER_EXCHANGE_ManagementAuditorDisableHandle *`
      75             :  * @param response_code HTTP response code, 0 on error
      76             :  * @param response response body, NULL if not in JSON
      77             :  */
      78             : static void
      79           8 : handle_auditor_disable_finished (void *cls,
      80             :                                  long response_code,
      81             :                                  const void *response)
      82             : {
      83           8 :   struct TALER_EXCHANGE_ManagementAuditorDisableHandle *ah = cls;
      84           8 :   const json_t *json = response;
      85           8 :   struct TALER_EXCHANGE_ManagementAuditorDisableResponse adr = {
      86           8 :     .hr.http_status = (unsigned int) response_code,
      87             :     .hr.reply = json
      88             :   };
      89             : 
      90           8 :   ah->job = NULL;
      91           8 :   switch (response_code)
      92             :   {
      93           6 :   case MHD_HTTP_NO_CONTENT:
      94           6 :     break;
      95           2 :   case MHD_HTTP_FORBIDDEN:
      96           2 :     adr.hr.ec = TALER_JSON_get_error_code (json);
      97           2 :     adr.hr.hint = TALER_JSON_get_error_hint (json);
      98           2 :     break;
      99           0 :   case MHD_HTTP_NOT_FOUND:
     100           0 :     adr.hr.ec = TALER_JSON_get_error_code (json);
     101           0 :     adr.hr.hint = TALER_JSON_get_error_hint (json);
     102           0 :     break;
     103           0 :   case MHD_HTTP_CONFLICT:
     104           0 :     adr.hr.ec = TALER_JSON_get_error_code (json);
     105           0 :     adr.hr.hint = TALER_JSON_get_error_hint (json);
     106           0 :     break;
     107           0 :   default:
     108             :     /* unexpected response code */
     109           0 :     GNUNET_break_op (0);
     110           0 :     adr.hr.ec = TALER_JSON_get_error_code (json);
     111           0 :     adr.hr.hint = TALER_JSON_get_error_hint (json);
     112           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     113             :                 "Unexpected response code %u/%d for exchange management auditor disable\n",
     114             :                 (unsigned int) response_code,
     115             :                 (int) adr.hr.ec);
     116           0 :     break;
     117             :   }
     118           8 :   if (NULL != ah->cb)
     119             :   {
     120           8 :     ah->cb (ah->cb_cls,
     121             :             &adr);
     122           8 :     ah->cb = NULL;
     123             :   }
     124           8 :   TALER_EXCHANGE_management_disable_auditor_cancel (ah);
     125           8 : }
     126             : 
     127             : 
     128             : struct TALER_EXCHANGE_ManagementAuditorDisableHandle *
     129           8 : TALER_EXCHANGE_management_disable_auditor (
     130             :   struct GNUNET_CURL_Context *ctx,
     131             :   const char *url,
     132             :   const struct TALER_AuditorPublicKeyP *auditor_pub,
     133             :   struct GNUNET_TIME_Timestamp validity_end,
     134             :   const struct TALER_MasterSignatureP *master_sig,
     135             :   TALER_EXCHANGE_ManagementAuditorDisableCallback cb,
     136             :   void *cb_cls)
     137             : {
     138             :   struct TALER_EXCHANGE_ManagementAuditorDisableHandle *ah;
     139             :   CURL *eh;
     140             :   json_t *body;
     141             : 
     142           8 :   ah = GNUNET_new (struct TALER_EXCHANGE_ManagementAuditorDisableHandle);
     143           8 :   ah->cb = cb;
     144           8 :   ah->cb_cls = cb_cls;
     145           8 :   ah->ctx = ctx;
     146             :   {
     147             :     char epub_str[sizeof (*auditor_pub) * 2];
     148             :     char arg_str[sizeof (epub_str) + 64];
     149             :     char *end;
     150             : 
     151           8 :     end = GNUNET_STRINGS_data_to_string (auditor_pub,
     152             :                                          sizeof (*auditor_pub),
     153             :                                          epub_str,
     154             :                                          sizeof (epub_str));
     155           8 :     *end = '\0';
     156           8 :     GNUNET_snprintf (arg_str,
     157             :                      sizeof (arg_str),
     158             :                      "management/auditors/%s/disable",
     159             :                      epub_str);
     160           8 :     ah->url = TALER_url_join (url,
     161             :                               arg_str,
     162             :                               NULL);
     163             :   }
     164           8 :   if (NULL == ah->url)
     165             :   {
     166           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     167             :                 "Could not construct request URL.\n");
     168           0 :     GNUNET_free (ah);
     169           0 :     return NULL;
     170             :   }
     171           8 :   body = GNUNET_JSON_PACK (
     172             :     GNUNET_JSON_pack_data_auto ("master_sig",
     173             :                                 master_sig),
     174             :     GNUNET_JSON_pack_timestamp ("validity_end",
     175             :                                 validity_end));
     176           8 :   eh = TALER_EXCHANGE_curl_easy_get_ (ah->url);
     177          16 :   if ( (NULL == eh) ||
     178             :        (GNUNET_OK !=
     179           8 :         TALER_curl_easy_post (&ah->post_ctx,
     180             :                               eh,
     181             :                               body)))
     182             :   {
     183           0 :     GNUNET_break (0);
     184           0 :     if (NULL != eh)
     185           0 :       curl_easy_cleanup (eh);
     186           0 :     json_decref (body);
     187           0 :     GNUNET_free (ah->url);
     188           0 :     return NULL;
     189             :   }
     190           8 :   json_decref (body);
     191           8 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     192             :               "Requesting URL '%s'\n",
     193             :               ah->url);
     194          16 :   ah->job = GNUNET_CURL_job_add2 (ctx,
     195             :                                   eh,
     196           8 :                                   ah->post_ctx.headers,
     197             :                                   &handle_auditor_disable_finished,
     198             :                                   ah);
     199           8 :   if (NULL == ah->job)
     200             :   {
     201           0 :     TALER_EXCHANGE_management_disable_auditor_cancel (ah);
     202           0 :     return NULL;
     203             :   }
     204           8 :   return ah;
     205             : }
     206             : 
     207             : 
     208             : void
     209           8 : TALER_EXCHANGE_management_disable_auditor_cancel (
     210             :   struct TALER_EXCHANGE_ManagementAuditorDisableHandle *ah)
     211             : {
     212           8 :   if (NULL != ah->job)
     213             :   {
     214           0 :     GNUNET_CURL_job_cancel (ah->job);
     215           0 :     ah->job = NULL;
     216             :   }
     217           8 :   TALER_curl_easy_post_finished (&ah->post_ctx);
     218           8 :   GNUNET_free (ah->url);
     219           8 :   GNUNET_free (ah);
     220           8 : }

Generated by: LCOV version 1.16