LCOV - code coverage report
Current view: top level - lib - exchange_api_management_auditor_enable.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 43 65 66.2 %
Date: 2021-08-30 06:43:37 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          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_enable.c
      19             :  * @brief functions to enable 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 "taler_exchange_service.h"
      26             : #include "taler_signatures.h"
      27             : #include "taler_curl_lib.h"
      28             : #include "taler_json_lib.h"
      29             : 
      30             : 
      31             : /**
      32             :  * @brief Handle for a POST /management/auditors request.
      33             :  */
      34             : struct TALER_EXCHANGE_ManagementAuditorEnableHandle
      35             : {
      36             : 
      37             :   /**
      38             :    * The url for this request.
      39             :    */
      40             :   char *url;
      41             : 
      42             :   /**
      43             :    * Minor context that holds body and headers.
      44             :    */
      45             :   struct TALER_CURL_PostContext post_ctx;
      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_EXCHANGE_ManagementAuditorEnableCallback 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             : 
      69             : /**
      70             :  * Function called when we're done processing the
      71             :  * HTTP POST /management/auditors request.
      72             :  *
      73             :  * @param cls the `struct TALER_EXCHANGE_ManagementAuditorEnableHandle *`
      74             :  * @param response_code HTTP response code, 0 on error
      75             :  * @param response response body, NULL if not in JSON
      76             :  */
      77             : static void
      78           9 : handle_auditor_enable_finished (void *cls,
      79             :                                 long response_code,
      80             :                                 const void *response)
      81             : {
      82           9 :   struct TALER_EXCHANGE_ManagementAuditorEnableHandle *ah = cls;
      83           9 :   const json_t *json = response;
      84           9 :   struct TALER_EXCHANGE_HttpResponse hr = {
      85           9 :     .http_status = (unsigned int) response_code,
      86             :     .reply = json
      87             :   };
      88             : 
      89           9 :   ah->job = NULL;
      90           9 :   switch (response_code)
      91             :   {
      92           8 :   case MHD_HTTP_NO_CONTENT:
      93           8 :     break;
      94           1 :   case MHD_HTTP_FORBIDDEN:
      95           1 :     hr.ec = TALER_JSON_get_error_code (json);
      96           1 :     hr.hint = TALER_JSON_get_error_hint (json);
      97           1 :     break;
      98           0 :   case MHD_HTTP_CONFLICT:
      99           0 :     hr.ec = TALER_JSON_get_error_code (json);
     100           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     101           0 :     break;
     102           0 :   default:
     103             :     /* unexpected response code */
     104           0 :     GNUNET_break_op (0);
     105           0 :     hr.ec = TALER_JSON_get_error_code (json);
     106           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     107           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     108             :                 "Unexpected response code %u/%d for exchange management auditor enable\n",
     109             :                 (unsigned int) response_code,
     110             :                 (int) hr.ec);
     111           0 :     break;
     112             :   }
     113           9 :   if (NULL != ah->cb)
     114             :   {
     115           9 :     ah->cb (ah->cb_cls,
     116             :             &hr);
     117           9 :     ah->cb = NULL;
     118             :   }
     119           9 :   TALER_EXCHANGE_management_enable_auditor_cancel (ah);
     120           9 : }
     121             : 
     122             : 
     123             : struct TALER_EXCHANGE_ManagementAuditorEnableHandle *
     124           9 : TALER_EXCHANGE_management_enable_auditor (
     125             :   struct GNUNET_CURL_Context *ctx,
     126             :   const char *url,
     127             :   const struct TALER_AuditorPublicKeyP *auditor_pub,
     128             :   const char *auditor_url,
     129             :   const char *auditor_name,
     130             :   struct GNUNET_TIME_Absolute validity_start,
     131             :   const struct TALER_MasterSignatureP *master_sig,
     132             :   TALER_EXCHANGE_ManagementAuditorEnableCallback cb,
     133             :   void *cb_cls)
     134             : {
     135             :   struct TALER_EXCHANGE_ManagementAuditorEnableHandle *ah;
     136             :   CURL *eh;
     137             :   json_t *body;
     138             : 
     139           9 :   ah = GNUNET_new (struct TALER_EXCHANGE_ManagementAuditorEnableHandle);
     140           9 :   ah->cb = cb;
     141           9 :   ah->cb_cls = cb_cls;
     142           9 :   ah->ctx = ctx;
     143           9 :   ah->url = TALER_url_join (url,
     144             :                             "management/auditors",
     145             :                             NULL);
     146           9 :   if (NULL == ah->url)
     147             :   {
     148           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     149             :                 "Could not construct request URL.\n");
     150           0 :     GNUNET_free (ah);
     151           0 :     return NULL;
     152             :   }
     153           9 :   body = GNUNET_JSON_PACK (
     154             :     GNUNET_JSON_pack_string ("auditor_url",
     155             :                              auditor_url),
     156             :     GNUNET_JSON_pack_string ("auditor_name",
     157             :                              auditor_name),
     158             :     GNUNET_JSON_pack_data_auto ("auditor_pub",
     159             :                                 auditor_pub),
     160             :     GNUNET_JSON_pack_data_auto ("master_sig",
     161             :                                 master_sig),
     162             :     GNUNET_JSON_pack_time_abs ("validity_start",
     163             :                                validity_start));
     164           9 :   eh = curl_easy_init ();
     165           9 :   GNUNET_assert (NULL != eh);
     166           9 :   if (GNUNET_OK !=
     167           9 :       TALER_curl_easy_post (&ah->post_ctx,
     168             :                             eh,
     169             :                             body))
     170             :   {
     171           0 :     GNUNET_break (0);
     172           0 :     json_decref (body);
     173           0 :     GNUNET_free (ah->url);
     174           0 :     GNUNET_free (eh);
     175           0 :     return NULL;
     176             :   }
     177           9 :   json_decref (body);
     178           9 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     179             :               "Requesting URL '%s'\n",
     180             :               ah->url);
     181           9 :   GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     182             :                                                CURLOPT_URL,
     183             :                                                ah->url));
     184          18 :   ah->job = GNUNET_CURL_job_add2 (ctx,
     185             :                                   eh,
     186           9 :                                   ah->post_ctx.headers,
     187             :                                   &handle_auditor_enable_finished,
     188             :                                   ah);
     189           9 :   if (NULL == ah->job)
     190             :   {
     191           0 :     TALER_EXCHANGE_management_enable_auditor_cancel (ah);
     192           0 :     return NULL;
     193             :   }
     194           9 :   return ah;
     195             : }
     196             : 
     197             : 
     198             : void
     199           9 : TALER_EXCHANGE_management_enable_auditor_cancel (
     200             :   struct TALER_EXCHANGE_ManagementAuditorEnableHandle *ah)
     201             : {
     202           9 :   if (NULL != ah->job)
     203             :   {
     204           0 :     GNUNET_CURL_job_cancel (ah->job);
     205           0 :     ah->job = NULL;
     206             :   }
     207           9 :   TALER_curl_easy_post_finished (&ah->post_ctx);
     208           9 :   GNUNET_free (ah->url);
     209           9 :   GNUNET_free (ah);
     210           9 : }

Generated by: LCOV version 1.14