LCOV - code coverage report
Current view: top level - exchange-lib - exchange_api_admin.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 35 51 68.6 %
Date: 2017-09-17 17:24:28 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014, 2015, 2016 GNUnet e.V.
       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 exchange-lib/exchange_api_admin.c
      19             :  * @brief Implementation of the /admin/ requests of the exchange's HTTP API
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <curl/curl.h>
      24             : #include <jansson.h>
      25             : #include <microhttpd.h> /* just for HTTP status codes */
      26             : #include <gnunet/gnunet_util_lib.h>
      27             : #include <gnunet/gnunet_json_lib.h>
      28             : #include <gnunet/gnunet_curl_lib.h>
      29             : #include "taler_json_lib.h"
      30             : #include "taler_exchange_service.h"
      31             : #include "exchange_api_handle.h"
      32             : #include "taler_signatures.h"
      33             : 
      34             : 
      35             : /**
      36             :  * @brief An admin/add/incoming Handle
      37             :  */
      38             : struct TALER_EXCHANGE_AdminAddIncomingHandle
      39             : {
      40             : 
      41             :   /**
      42             :    * The connection to exchange this request handle will use
      43             :    */
      44             :   struct TALER_EXCHANGE_Handle *exchange;
      45             : 
      46             :   /**
      47             :    * The url for this request.
      48             :    */
      49             :   char *url;
      50             : 
      51             :   /**
      52             :    * JSON encoding of the request to POST.
      53             :    */
      54             :   char *json_enc;
      55             : 
      56             :   /**
      57             :    * Handle for the request.
      58             :    */
      59             :   struct GNUNET_CURL_Job *job;
      60             : 
      61             :   /**
      62             :    * HTTP headers for the request.
      63             :    */
      64             :   struct curl_slist *headers;
      65             : 
      66             :   /**
      67             :    * Function to call with the result.
      68             :    */
      69             :   TALER_EXCHANGE_AdminAddIncomingResultCallback cb;
      70             : 
      71             :   /**
      72             :    * Closure for @a cb.
      73             :    */
      74             :   void *cb_cls;
      75             : 
      76             : };
      77             : 
      78             : 
      79             : /**
      80             :  * Function called when we're done processing the
      81             :  * HTTP /admin/add/incoming request.
      82             :  *
      83             :  * @param cls the `struct TALER_EXCHANGE_AdminAddIncomingHandle`
      84             :  * @param response_code HTTP response code, 0 on error
      85             :  * @param json parsed JSON result, NULL on error
      86             :  */
      87             : static void
      88           6 : handle_admin_add_incoming_finished (void *cls,
      89             :                                     long response_code,
      90             :                                     const json_t *json)
      91             : {
      92           6 :   struct TALER_EXCHANGE_AdminAddIncomingHandle *aai = cls;
      93             : 
      94           6 :   aai->job = NULL;
      95           6 :   switch (response_code)
      96             :   {
      97             :   case 0:
      98           0 :     break;
      99             :   case MHD_HTTP_OK:
     100           6 :     break;
     101             :   case MHD_HTTP_BAD_REQUEST:
     102             :     /* This should never happen, either us or the exchange is buggy
     103             :        (or API version conflict); just pass JSON reply to the application */
     104           0 :     break;
     105             :   case MHD_HTTP_FORBIDDEN:
     106             :     /* Access denied */
     107           0 :     break;
     108             :   case MHD_HTTP_UNAUTHORIZED:
     109             :     /* Nothing really to verify, exchange says one of the signatures is
     110             :        invalid; as we checked them, this should never happen, we
     111             :        should pass the JSON reply to the application */
     112           0 :     break;
     113             :   case MHD_HTTP_NOT_FOUND:
     114             :     /* Nothing really to verify, this should never
     115             :        happen, we should pass the JSON reply to the application */
     116           0 :     break;
     117             :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     118             :     /* Server had an internal issue; we should retry, but this API
     119             :        leaves this to the application */
     120           0 :     break;
     121             :   default:
     122             :     /* unexpected response code */
     123           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     124             :                 "Unexpected response code %u\n",
     125             :                 (unsigned int) response_code);
     126           0 :     GNUNET_break (0);
     127           0 :     response_code = 0;
     128           0 :     break;
     129             :   }
     130           6 :   aai->cb (aai->cb_cls,
     131             :            response_code,
     132             :            TALER_JSON_get_error_code (json),
     133             :            json);
     134           6 :   TALER_EXCHANGE_admin_add_incoming_cancel (aai);
     135           6 : }
     136             : 
     137             : 
     138             : /**
     139             :  * Notify the exchange that we have received an incoming transaction
     140             :  * which fills a reserve.  Note that this API is an administrative
     141             :  * API and thus not accessible to typical exchange clients, but only
     142             :  * to the operators of the exchange.
     143             :  *
     144             :  * @param exchange the exchange handle; the exchange must be ready to operate
     145             :  * @param admin_url URL of the administrative interface of the exchange
     146             :  * @param reserve_pub public key of the reserve
     147             :  * @param amount amount that was deposited
     148             :  * @param execution_date when did we receive the amount
     149             :  * @param sender_account_details account information of the sender of the money;
     150             :  *        the receiver is always the exchange.
     151             :  * @param transfer_details details that uniquely identify the transfer;
     152             :  *        used to check for duplicate operations by the exchange
     153             :  * @param res_cb the callback to call when the final result for this request is available
     154             :  * @param res_cb_cls closure for the above callback
     155             :  * @return NULL
     156             :  *         if the inputs are invalid (i.e. invalid amount).
     157             :  *         In this case, the callback is not called.
     158             :  */
     159             : struct TALER_EXCHANGE_AdminAddIncomingHandle *
     160           6 : TALER_EXCHANGE_admin_add_incoming (struct TALER_EXCHANGE_Handle *exchange,
     161             :                                    const char *admin_url,
     162             :                                    const struct TALER_ReservePublicKeyP *reserve_pub,
     163             :                                    const struct TALER_Amount *amount,
     164             :                                    struct GNUNET_TIME_Absolute execution_date,
     165             :                                    const json_t *sender_account_details,
     166             :                                    const json_t *transfer_details,
     167             :                                    TALER_EXCHANGE_AdminAddIncomingResultCallback res_cb,
     168             :                                    void *res_cb_cls)
     169             : {
     170             :   struct TALER_EXCHANGE_AdminAddIncomingHandle *aai;
     171             :   struct GNUNET_CURL_Context *ctx;
     172             :   json_t *admin_obj;
     173             :   CURL *eh;
     174             : 
     175           6 :   GNUNET_assert (GNUNET_OK ==
     176             :                  GNUNET_TIME_round_abs (&execution_date));
     177           6 :   if (GNUNET_YES !=
     178           6 :       MAH_handle_is_ready (exchange))
     179             :   {
     180           0 :     GNUNET_break (0);
     181           0 :     return NULL;
     182             :   }
     183           6 :   admin_obj = json_pack ("{s:o, s:o," /* reserve_pub/amount */
     184             :                          " s:o, s:O, s:O}", /* execution_Date/sender/transfer */
     185             :                          "reserve_pub", GNUNET_JSON_from_data_auto (reserve_pub),
     186             :                          "amount", TALER_JSON_from_amount (amount),
     187             :                          "execution_date", GNUNET_JSON_from_time_abs (execution_date),
     188             :                          "sender_account_details", sender_account_details,
     189             :                          "transfer_details", transfer_details);
     190           6 :   if (NULL == admin_obj)
     191             :   {
     192           0 :     GNUNET_break (0);
     193           0 :     return NULL;
     194             :   }
     195           6 :   aai = GNUNET_new (struct TALER_EXCHANGE_AdminAddIncomingHandle);
     196           6 :   aai->exchange = exchange;
     197           6 :   aai->cb = res_cb;
     198           6 :   aai->cb_cls = res_cb_cls;
     199           6 :   aai->url = MAH_path_to_url2 (admin_url,
     200             :                                "/admin/add/incoming");
     201             : 
     202           6 :   eh = curl_easy_init ();
     203           6 :   GNUNET_assert (NULL != (aai->json_enc =
     204             :                           json_dumps (admin_obj,
     205             :                                       JSON_COMPACT)));
     206           6 :   json_decref (admin_obj);
     207           6 :   GNUNET_assert (CURLE_OK ==
     208             :                  curl_easy_setopt (eh,
     209             :                                    CURLOPT_URL,
     210             :                                    aai->url));
     211           6 :   GNUNET_assert (CURLE_OK ==
     212             :                  curl_easy_setopt (eh,
     213             :                                    CURLOPT_POSTFIELDS,
     214             :                                    aai->json_enc));
     215           6 :   GNUNET_assert (CURLE_OK ==
     216             :                  curl_easy_setopt (eh,
     217             :                                    CURLOPT_POSTFIELDSIZE,
     218             :                                    strlen (aai->json_enc)));
     219           6 :   ctx = MAH_handle_to_context (exchange);
     220           6 :   aai->job = GNUNET_CURL_job_add (ctx,
     221             :                                   eh,
     222             :                                   GNUNET_YES,
     223             :                                   &handle_admin_add_incoming_finished,
     224             :                                   aai);
     225           6 :   return aai;
     226             : }
     227             : 
     228             : 
     229             : /**
     230             :  * Cancel an add incoming.  This function cannot be used on a request
     231             :  * handle if a response is already served for it.
     232             :  *
     233             :  * @param aai the admin add incoming request handle
     234             :  */
     235             : void
     236           6 : TALER_EXCHANGE_admin_add_incoming_cancel (struct TALER_EXCHANGE_AdminAddIncomingHandle *aai)
     237             : {
     238           6 :   if (NULL != aai->job)
     239             :   {
     240           0 :     GNUNET_CURL_job_cancel (aai->job);
     241           0 :     aai->job = NULL;
     242             :   }
     243           6 :   curl_slist_free_all (aai->headers);
     244           6 :   GNUNET_free (aai->url);
     245           6 :   GNUNET_free (aai->json_enc);
     246           6 :   GNUNET_free (aai);
     247           6 : }
     248             : 
     249             : 
     250             : /* end of exchange_api_admin.c */

Generated by: LCOV version 1.13