LCOV - code coverage report
Current view: top level - lib - exchange_api_management_revoke_denomination_key.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 42 72 58.3 %
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-2020 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_revoke_denomination_key.c
      19             :  * @brief functions to revoke an exchange denomination key
      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/denominations/$H_DENOM_PUB/revoke request.
      33             :  */
      34             : struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle
      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_ManagementRevokeDenominationKeyCallback 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 /management/denominations/$H_DENOM_PUB/revoke request.
      72             :  *
      73             :  * @param cls the `struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *`
      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           8 : handle_revoke_denomination_finished (void *cls,
      79             :                                      long response_code,
      80             :                                      const void *response)
      81             : {
      82           8 :   struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *rh = cls;
      83           8 :   const json_t *json = response;
      84           8 :   struct TALER_EXCHANGE_HttpResponse hr = {
      85           8 :     .http_status = (unsigned int) response_code,
      86             :     .reply = json
      87             :   };
      88             : 
      89           8 :   rh->job = NULL;
      90           8 :   switch (response_code)
      91             :   {
      92           0 :   case 0:
      93             :     /* no reply */
      94           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
      95           0 :     hr.hint = "server offline?";
      96           0 :     break;
      97           8 :   case MHD_HTTP_NO_CONTENT:
      98           8 :     break;
      99           0 :   case MHD_HTTP_FORBIDDEN:
     100           0 :     hr.ec = TALER_JSON_get_error_code (json);
     101           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     102           0 :     break;
     103           0 :   default:
     104             :     /* unexpected response code */
     105           0 :     GNUNET_break_op (0);
     106           0 :     hr.ec = TALER_JSON_get_error_code (json);
     107           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     108           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     109             :                 "Unexpected response code %u/%d for exchange management revoke denomination\n",
     110             :                 (unsigned int) response_code,
     111             :                 (int) hr.ec);
     112           0 :     break;
     113             :   }
     114           8 :   if (NULL != rh->cb)
     115             :   {
     116           8 :     rh->cb (rh->cb_cls,
     117             :             &hr);
     118           8 :     rh->cb = NULL;
     119             :   }
     120           8 :   TALER_EXCHANGE_management_revoke_denomination_key_cancel (rh);
     121           8 : }
     122             : 
     123             : 
     124             : struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *
     125           8 : TALER_EXCHANGE_management_revoke_denomination_key (
     126             :   struct GNUNET_CURL_Context *ctx,
     127             :   const char *url,
     128             :   const struct GNUNET_HashCode *h_denom_pub,
     129             :   const struct TALER_MasterSignatureP *master_sig,
     130             :   TALER_EXCHANGE_ManagementRevokeDenominationKeyCallback cb,
     131             :   void *cb_cls)
     132             : {
     133             :   struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *rh;
     134             :   CURL *eh;
     135             :   json_t *body;
     136             : 
     137           8 :   rh = GNUNET_new (struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle);
     138           8 :   rh->cb = cb;
     139           8 :   rh->cb_cls = cb_cls;
     140           8 :   rh->ctx = ctx;
     141             :   {
     142             :     char epub_str[sizeof (*h_denom_pub) * 2];
     143             :     char arg_str[sizeof (epub_str) + 64];
     144             :     char *end;
     145             : 
     146           8 :     end = GNUNET_STRINGS_data_to_string (h_denom_pub,
     147             :                                          sizeof (*h_denom_pub),
     148             :                                          epub_str,
     149             :                                          sizeof (epub_str));
     150           8 :     *end = '\0';
     151           8 :     GNUNET_snprintf (arg_str,
     152             :                      sizeof (arg_str),
     153             :                      "management/denominations/%s/revoke",
     154             :                      epub_str);
     155           8 :     rh->url = TALER_url_join (url,
     156             :                               arg_str,
     157             :                               NULL);
     158             :   }
     159           8 :   if (NULL == rh->url)
     160             :   {
     161           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     162             :                 "Could not construct request URL.\n");
     163           0 :     GNUNET_free (rh);
     164           0 :     return NULL;
     165             :   }
     166           8 :   body = GNUNET_JSON_PACK (
     167             :     GNUNET_JSON_pack_data_auto ("master_sig",
     168             :                                 master_sig));
     169           8 :   if (NULL == body)
     170             :   {
     171           0 :     GNUNET_break (0);
     172           0 :     GNUNET_free (rh->url);
     173           0 :     GNUNET_free (rh);
     174           0 :     return NULL;
     175             :   }
     176           8 :   eh = curl_easy_init ();
     177           8 :   if (GNUNET_OK !=
     178           8 :       TALER_curl_easy_post (&rh->post_ctx,
     179             :                             eh,
     180             :                             body))
     181             :   {
     182           0 :     GNUNET_break (0);
     183           0 :     json_decref (body);
     184           0 :     GNUNET_free (rh->url);
     185           0 :     GNUNET_free (eh);
     186           0 :     return NULL;
     187             :   }
     188           8 :   json_decref (body);
     189           8 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     190             :               "Requesting URL '%s'\n",
     191             :               rh->url);
     192           8 :   GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     193             :                                                CURLOPT_URL,
     194             :                                                rh->url));
     195          16 :   rh->job = GNUNET_CURL_job_add2 (ctx,
     196             :                                   eh,
     197           8 :                                   rh->post_ctx.headers,
     198             :                                   &handle_revoke_denomination_finished,
     199             :                                   rh);
     200           8 :   if (NULL == rh->job)
     201             :   {
     202           0 :     TALER_EXCHANGE_management_revoke_denomination_key_cancel (rh);
     203           0 :     return NULL;
     204             :   }
     205           8 :   return rh;
     206             : }
     207             : 
     208             : 
     209             : void
     210           8 : TALER_EXCHANGE_management_revoke_denomination_key_cancel (
     211             :   struct TALER_EXCHANGE_ManagementRevokeDenominationKeyHandle *rh)
     212             : {
     213           8 :   if (NULL != rh->job)
     214             :   {
     215           0 :     GNUNET_CURL_job_cancel (rh->job);
     216           0 :     rh->job = NULL;
     217             :   }
     218           8 :   TALER_curl_easy_post_finished (&rh->post_ctx);
     219           8 :   GNUNET_free (rh->url);
     220           8 :   GNUNET_free (rh);
     221           8 : }

Generated by: LCOV version 1.14