LCOV - code coverage report
Current view: top level - bank-lib - bank_api_admin.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 40 60 66.7 %
Date: 2017-11-25 11:31:41 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2015, 2016, 2017 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 bank-lib/bank_api_admin.c
      19             :  * @brief Implementation of the /admin/ requests of the bank's HTTP API
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include "bank_api_common.h"
      24             : #include <microhttpd.h> /* just for HTTP status codes */
      25             : #include "taler_signatures.h"
      26             : 
      27             : 
      28             : /**
      29             :  * @brief An admin/add/incoming Handle
      30             :  */
      31             : struct TALER_BANK_AdminAddIncomingHandle
      32             : {
      33             : 
      34             :   /**
      35             :    * The url for this request.
      36             :    */
      37             :   char *request_url;
      38             : 
      39             :   /**
      40             :    * JSON encoding of the request to POST.
      41             :    */
      42             :   char *json_enc;
      43             : 
      44             :   /**
      45             :    * Handle for the request.
      46             :    */
      47             :   struct GNUNET_CURL_Job *job;
      48             : 
      49             :   /**
      50             :    * HTTP authentication-related headers for the request.
      51             :    */
      52             :   struct curl_slist *authh;
      53             : 
      54             :   /**
      55             :    * Function to call with the result.
      56             :    */
      57             :   TALER_BANK_AdminAddIncomingResultCallback cb;
      58             : 
      59             :   /**
      60             :    * Closure for @a cb.
      61             :    */
      62             :   void *cb_cls;
      63             : 
      64             : };
      65             : 
      66             : 
      67             : /**
      68             :  * Function called when we're done processing the
      69             :  * HTTP /admin/add/incoming request.
      70             :  *
      71             :  * @param cls the `struct TALER_BANK_AdminAddIncomingHandle`
      72             :  * @param response_code HTTP response code, 0 on error
      73             :  * @param json parsed JSON result, NULL on error
      74             :  */
      75             : static void
      76          21 : handle_admin_add_incoming_finished (void *cls,
      77             :                                     long response_code,
      78             :                                     const json_t *json)
      79             : {
      80          21 :   struct TALER_BANK_AdminAddIncomingHandle *aai = cls;
      81          21 :   uint64_t serial_id = UINT64_MAX;
      82             : 
      83          21 :   aai->job = NULL;
      84          21 :   switch (response_code)
      85             :   {
      86             :   case 0:
      87           0 :     break;
      88             :   case MHD_HTTP_OK:
      89             :     {
      90          21 :       struct GNUNET_JSON_Specification spec[] = {
      91             :         GNUNET_JSON_spec_uint64 ("serial_id",
      92             :                                  &serial_id),
      93             :         GNUNET_JSON_spec_end()
      94             :       };
      95             : 
      96          21 :       if (GNUNET_OK !=
      97          21 :           GNUNET_JSON_parse (json,
      98             :                              spec,
      99             :                              NULL, NULL))
     100             :       {
     101           0 :         GNUNET_break_op (0);
     102           0 :         response_code = 0;
     103           0 :         break;
     104             :       }
     105             :     }
     106          21 :     break;
     107             :   case MHD_HTTP_BAD_REQUEST:
     108             :     /* This should never happen, either us or the bank is buggy
     109             :        (or API version conflict); just pass JSON reply to the application */
     110           0 :     break;
     111             :   case MHD_HTTP_FORBIDDEN:
     112             :     /* Access denied */
     113           0 :     break;
     114             :   case MHD_HTTP_UNAUTHORIZED:
     115             :     /* Nothing really to verify, bank says one of the signatures is
     116             :        invalid; as we checked them, this should never happen, we
     117             :        should pass the JSON reply to the application */
     118           0 :     break;
     119             :   case MHD_HTTP_NOT_FOUND:
     120             :     /* Nothing really to verify, this should never
     121             :        happen, we should pass the JSON reply to the application */
     122           0 :     break;
     123             :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     124             :     /* Server had an internal issue; we should retry, but this API
     125             :        leaves this to the application */
     126           0 :     break;
     127             :   default:
     128             :     /* unexpected response code */
     129           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     130             :                 "Unexpected response code %u\n",
     131             :                 (unsigned int) response_code);
     132           0 :     GNUNET_break (0);
     133           0 :     response_code = 0;
     134           0 :     break;
     135             :   }
     136          21 :   aai->cb (aai->cb_cls,
     137             :            response_code,
     138             :            serial_id,
     139             :            json);
     140          21 :   TALER_BANK_admin_add_incoming_cancel (aai);
     141          21 : }
     142             : 
     143             : 
     144             : /**
     145             :  * Notify the bank that we have received an incoming transaction
     146             :  * which fills a reserve.  Note that this API is an administrative
     147             :  * API and thus not accessible to typical bank clients, but only
     148             :  * to the operators of the bank.
     149             :  *
     150             :  * @param ctx curl context for the event loop
     151             :  * @param bank_base_url URL of the bank (used to execute this request)
     152             :  * @param auth authentication data to send to the bank
     153             :  * @param exchange_base_url base URL of the exchange (for tracking)
     154             :  * @param wtid wire transfer identifier for the transfer
     155             :  * @param amount amount that was deposited
     156             :  * @param debit_account_no account number to withdraw from (53 bits at most)
     157             :  * @param credit_account_no account number to deposit into (53 bits at most)
     158             :  * @param res_cb the callback to call when the final result for this request is available
     159             :  * @param res_cb_cls closure for the above callback
     160             :  * @return NULL
     161             :  *         if the inputs are invalid (i.e. invalid amount).
     162             :  *         In this case, the callback is not called.
     163             :  */
     164             : struct TALER_BANK_AdminAddIncomingHandle *
     165          21 : TALER_BANK_admin_add_incoming (struct GNUNET_CURL_Context *ctx,
     166             :                                const char *bank_base_url,
     167             :                                const struct TALER_BANK_AuthenticationData *auth,
     168             :                                const char *exchange_base_url,
     169             :                                const struct TALER_WireTransferIdentifierRawP *wtid,
     170             :                                const struct TALER_Amount *amount,
     171             :                                uint64_t debit_account_no,
     172             :                                uint64_t credit_account_no,
     173             :                                TALER_BANK_AdminAddIncomingResultCallback res_cb,
     174             :                                void *res_cb_cls)
     175             : {
     176             :   struct TALER_BANK_AdminAddIncomingHandle *aai;
     177             :   json_t *admin_obj;
     178             :   CURL *eh;
     179             : 
     180          21 :   if (NULL == exchange_base_url)
     181             :   {
     182           0 :     GNUNET_break (0);
     183           0 :     return NULL;
     184             :   }
     185          21 :   admin_obj = json_pack ("{s:{s:s}, s:s, s:o, s:o, s:I, s:I}",
     186             :                          "auth", "type", "basic",
     187             :                          "exchange_url", exchange_base_url,
     188             :                          "wtid", GNUNET_JSON_from_data_auto (wtid),
     189             :                          "amount", TALER_JSON_from_amount (amount),
     190             :                          "debit_account", (json_int_t) debit_account_no,
     191             :                          "credit_account", (json_int_t) credit_account_no);
     192          21 :   if (NULL == admin_obj)
     193             :   {
     194           0 :     GNUNET_break (0);
     195           0 :     return NULL;
     196             :   }
     197          21 :   aai = GNUNET_new (struct TALER_BANK_AdminAddIncomingHandle);
     198          21 :   aai->cb = res_cb;
     199          21 :   aai->cb_cls = res_cb_cls;
     200          21 :   aai->request_url = TALER_BANK_path_to_url_ (bank_base_url,
     201             :                                               "/admin/add/incoming");
     202          21 :   aai->authh = TALER_BANK_make_auth_header_ (auth);
     203             :   /* Append content type header here, can't do it in GNUNET_CURL_job_add
     204             :      as that would override the CURLOPT_HTTPHEADER instead of appending. */
     205             :   {
     206             :     struct curl_slist *ext;
     207             : 
     208          21 :     ext = curl_slist_append (aai->authh,
     209             :                              "Content-Type: application/json");
     210          21 :     if (NULL == ext)
     211           0 :       GNUNET_break (0);
     212             :     else
     213          21 :       aai->authh = ext;
     214             :   }
     215          21 :   eh = curl_easy_init ();
     216          21 :   GNUNET_assert (NULL != (aai->json_enc =
     217             :                           json_dumps (admin_obj,
     218             :                                       JSON_COMPACT)));
     219          21 :   json_decref (admin_obj);
     220          21 :   GNUNET_assert (CURLE_OK ==
     221             :                  curl_easy_setopt (eh,
     222             :                                    CURLOPT_HTTPHEADER,
     223             :                                    aai->authh));
     224          21 :   GNUNET_assert (CURLE_OK ==
     225             :                  curl_easy_setopt (eh,
     226             :                                    CURLOPT_URL,
     227             :                                    aai->request_url));
     228          21 :   GNUNET_assert (CURLE_OK ==
     229             :                  curl_easy_setopt (eh,
     230             :                                    CURLOPT_POSTFIELDS,
     231             :                                    aai->json_enc));
     232          21 :   GNUNET_assert (CURLE_OK ==
     233             :                  curl_easy_setopt (eh,
     234             :                                    CURLOPT_POSTFIELDSIZE,
     235             :                                    strlen (aai->json_enc)));
     236          21 :   aai->job = GNUNET_CURL_job_add (ctx,
     237             :                                   eh,
     238             :                                   GNUNET_NO,
     239             :                                   &handle_admin_add_incoming_finished,
     240             :                                   aai);
     241          21 :   return aai;
     242             : }
     243             : 
     244             : 
     245             : /**
     246             :  * Cancel an add incoming.  This function cannot be used on a request
     247             :  * handle if a response is already served for it.
     248             :  *
     249             :  * @param aai the admin add incoming request handle
     250             :  */
     251             : void
     252          21 : TALER_BANK_admin_add_incoming_cancel (struct TALER_BANK_AdminAddIncomingHandle *aai)
     253             : {
     254          21 :   if (NULL != aai->job)
     255             :   {
     256           0 :     GNUNET_CURL_job_cancel (aai->job);
     257           0 :     aai->job = NULL;
     258             :   }
     259          21 :   curl_slist_free_all (aai->authh);
     260          21 :   GNUNET_free (aai->request_url);
     261          21 :   GNUNET_free (aai->json_enc);
     262          21 :   GNUNET_free (aai);
     263          21 : }
     264             : 
     265             : 
     266             : /* end of bank_api_admin.c */

Generated by: LCOV version 1.13