LCOV - code coverage report
Current view: top level - bank-lib - bank_api_history.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 54 78 69.2 %
Date: 2017-09-17 17:24:28 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2017 GNUnet e.V. & Inria
       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_history.c
      19             :  * @brief Implementation of the /history 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 A /history Handle
      30             :  */
      31             : struct TALER_BANK_HistoryHandle
      32             : {
      33             : 
      34             :   /**
      35             :    * The url for this request.
      36             :    */
      37             :   char *request_url;
      38             : 
      39             :   /**
      40             :    * The base URL of the bank.
      41             :    */
      42             :   char *bank_base_url;
      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_HistoryResultCallback hcb;
      58             : 
      59             :   /**
      60             :    * Closure for @a cb.
      61             :    */
      62             :   void *hcb_cls;
      63             : 
      64             : };
      65             : 
      66             : 
      67             : /**
      68             :  * Parse history given in JSON format and invoke the callback on each item.
      69             :  *
      70             :  * @param hh handle to the account history request
      71             :  * @param history JSON array with the history
      72             :  * @return #GNUNET_OK if history was valid and @a rhistory and @a balance
      73             :  *         were set,
      74             :  *         #GNUNET_SYSERR if there was a protocol violation in @a history
      75             :  */
      76             : static int
      77           4 : parse_account_history (struct TALER_BANK_HistoryHandle *hh,
      78             :                        const json_t *history)
      79             : {
      80             :   json_t *history_array;
      81             : 
      82           4 :   if (NULL == (history_array = json_object_get (history, "data")))
      83             :   {
      84           0 :     GNUNET_break_op (0);
      85           0 :     return GNUNET_SYSERR;
      86             :   }
      87          11 :   for (unsigned int i=0;i<json_array_size (history_array);i++)
      88             :   {
      89             :     struct TALER_BANK_TransferDetails td;
      90             :     const char *sign;
      91             :     uint64_t other_account;
      92             :     uint64_t serial_id;
      93             :     enum TALER_BANK_Direction direction;
      94           7 :     struct GNUNET_JSON_Specification hist_spec[] = {
      95             :       GNUNET_JSON_spec_string ("sign",
      96             :                                &sign),
      97             :       TALER_JSON_spec_amount ("amount",
      98             :                               &td.amount),
      99             :       GNUNET_JSON_spec_absolute_time ("date",
     100             :                                       &td.execution_date),
     101             :       GNUNET_JSON_spec_uint64 ("row_id",
     102             :                                &serial_id),
     103             :       GNUNET_JSON_spec_string ("wt_subject",
     104             :                                (const char **) &td.wire_transfer_subject),
     105             :       GNUNET_JSON_spec_uint64 ("counterpart",
     106             :                                &other_account),
     107             :       GNUNET_JSON_spec_end()
     108             :     };
     109           7 :     json_t *transaction = json_array_get (history_array,
     110             :                                           i);
     111             : 
     112           7 :     if (GNUNET_OK !=
     113           7 :         GNUNET_JSON_parse (transaction,
     114             :                            hist_spec,
     115             :                            NULL, NULL))
     116             :     {
     117           0 :       GNUNET_break_op (0);
     118           0 :       return GNUNET_SYSERR;
     119             :     }
     120             : 
     121           7 :     td.account_details = json_pack ("{s:s, s:s, s:I}",
     122             :                                     "type", "test",
     123             :                                     "bank_uri", hh->bank_base_url,
     124             :                                     "account_number", (json_int_t) other_account);
     125          14 :     direction = (0 == strcasecmp (sign,
     126             :                                   "+"))
     127             :       ? TALER_BANK_DIRECTION_CREDIT
     128           7 :       : TALER_BANK_DIRECTION_DEBIT;
     129           7 :     hh->hcb (hh->hcb_cls,
     130             :              MHD_HTTP_OK,
     131             :              direction,
     132             :              serial_id,
     133             :              &td,
     134             :              transaction);
     135           7 :     GNUNET_JSON_parse_free (hist_spec);
     136           7 :     json_decref (td.account_details);
     137             :   }
     138           4 :   return GNUNET_OK;
     139             : }
     140             : 
     141             : 
     142             : /**
     143             :  * Function called when we're done processing the
     144             :  * HTTP /admin/add/incoming request.
     145             :  *
     146             :  * @param cls the `struct TALER_BANK_HistoryHandle`
     147             :  * @param response_code HTTP response code, 0 on error
     148             :  * @param json parsed JSON result, NULL on error
     149             :  */
     150             : static void
     151           7 : handle_history_finished (void *cls,
     152             :                          long response_code,
     153             :                          const json_t *json)
     154             : {
     155           7 :   struct TALER_BANK_HistoryHandle *hh = cls;
     156             : 
     157           7 :   hh->job = NULL;
     158           7 :   switch (response_code)
     159             :   {
     160             :   case 0:
     161           0 :     break;
     162             :   case MHD_HTTP_OK:
     163           4 :     if (GNUNET_OK !=
     164           4 :         parse_account_history (hh,
     165             :                                json))
     166             :     {
     167           0 :       GNUNET_break_op (0);
     168           0 :       response_code = 0;
     169           0 :       break;
     170             :     }
     171           4 :     response_code = MHD_HTTP_NO_CONTENT; /* signal end of list */
     172           4 :     break;
     173             :   case MHD_HTTP_NO_CONTENT:
     174           3 :     break;
     175             :   case MHD_HTTP_BAD_REQUEST:
     176             :     /* This should never happen, either us or the bank is buggy
     177             :        (or API version conflict); just pass JSON reply to the application */
     178           0 :     break;
     179             :   case MHD_HTTP_FORBIDDEN:
     180             :     /* Access denied */
     181           0 :     break;
     182             :   case MHD_HTTP_UNAUTHORIZED:
     183             :     /* Nothing really to verify, bank says one of the signatures is
     184             :        invalid; as we checked them, this should never happen, we
     185             :        should pass the JSON reply to the application */
     186           0 :     break;
     187             :   case MHD_HTTP_NOT_FOUND:
     188             :     /* Nothing really to verify, this should never
     189             :        happen, we should pass the JSON reply to the application */
     190           0 :     break;
     191             :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     192             :     /* Server had an internal issue; we should retry, but this API
     193             :        leaves this to the application */
     194           0 :     break;
     195             :   default:
     196             :     /* unexpected response code */
     197           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     198             :                 "Unexpected response code %u\n",
     199             :                 (unsigned int) response_code);
     200           0 :     GNUNET_break (0);
     201           0 :     response_code = 0;
     202           0 :     break;
     203             :   }
     204           7 :   hh->hcb (hh->hcb_cls,
     205             :            response_code,
     206             :            TALER_BANK_DIRECTION_NONE,
     207             :            0LLU,
     208             :            NULL,
     209             :            json);
     210           7 :   TALER_BANK_history_cancel (hh);
     211           7 : }
     212             : 
     213             : 
     214             : /**
     215             :  * Request the wire transfer history of a bank account.
     216             :  *
     217             :  * @param ctx curl context for the event loop
     218             :  * @param bank_base_url URL of the bank (used to execute this request)
     219             :  * @param auth authentication data to use
     220             :  * @param account_number which account number should we query
     221             :  * @param direction what kinds of wire transfers should be returned
     222             :  * @param start_row from which row on do we want to get results, use UINT64_MAX for the latest; exclusive
     223             :  * @param num_results how many results do we want; negative numbers to go into the past,
     224             :  *                    positive numbers to go into the future starting at @a start_row;
     225             :  *                    must not be zero.
     226             :  * @param hres_cb the callback to call with the transaction history
     227             :  * @param hres_cb_cls closure for the above callback
     228             :  * @return NULL
     229             :  *         if the inputs are invalid (i.e. zero value for @e num_results).
     230             :  *         In this case, the callback is not called.
     231             :  */
     232             : struct TALER_BANK_HistoryHandle *
     233           7 : TALER_BANK_history (struct GNUNET_CURL_Context *ctx,
     234             :                     const char *bank_base_url,
     235             :                     const struct TALER_BANK_AuthenticationData *auth,
     236             :                     uint64_t account_number,
     237             :                     enum TALER_BANK_Direction direction,
     238             :                     uint64_t start_row,
     239             :                     int64_t num_results,
     240             :                     TALER_BANK_HistoryResultCallback hres_cb,
     241             :                     void *hres_cb_cls)
     242             : {
     243             :   struct TALER_BANK_HistoryHandle *hh;
     244             :   CURL *eh;
     245             :   char *url;
     246             : 
     247           7 :   if (0 == num_results)
     248             :   {
     249           0 :     GNUNET_break (0);
     250           0 :     return NULL;
     251             :   }
     252           7 :   if (TALER_BANK_DIRECTION_NONE == direction)
     253             :   {
     254           0 :     GNUNET_break (0);
     255           0 :     return NULL;
     256             :   }
     257           7 :   if (UINT64_MAX == start_row)
     258             :   {
     259           6 :     if (TALER_BANK_DIRECTION_BOTH == direction)
     260           2 :       GNUNET_asprintf (&url,
     261             :                        "/history?auth=basic&account_number=%llu&delta=%lld",
     262             :                        (unsigned long long) account_number,
     263             :                        (long long) num_results);
     264             :     else
     265           4 :       GNUNET_asprintf (&url,
     266             :                        "/history?auth=basic&account_number=%llu&delta=%lld&direction=%s",
     267             :                        (unsigned long long) account_number,
     268             :                        (long long) num_results,
     269             :                        (TALER_BANK_DIRECTION_CREDIT == direction) ? "credit" : "debit");
     270             : 
     271             :   }
     272             :   else
     273             :   {
     274           1 :     if (TALER_BANK_DIRECTION_BOTH == direction)
     275           1 :       GNUNET_asprintf (&url,
     276             :                        "/history?auth=basic&account_number=%llu&delta=%lld&start=%llu",
     277             :                        (unsigned long long) account_number,
     278             :                        (long long) num_results,
     279             :                        (unsigned long long) start_row);
     280             :     else
     281           0 :       GNUNET_asprintf (&url,
     282             :                        "/history?auth=basic&account_number=%llu&delta=%lld&start=%llu&direction=%s",
     283             :                        (unsigned long long) account_number,
     284             :                        (long long) num_results,
     285             :                        (unsigned long long) start_row,
     286             :                        (TALER_BANK_DIRECTION_CREDIT == direction) ? "credit" : "debit");
     287             :   }
     288             : 
     289           7 :   hh = GNUNET_new (struct TALER_BANK_HistoryHandle);
     290           7 :   hh->hcb = hres_cb;
     291           7 :   hh->hcb_cls = hres_cb_cls;
     292           7 :   hh->bank_base_url = GNUNET_strdup (bank_base_url);
     293           7 :   hh->request_url = TALER_BANK_path_to_url_ (bank_base_url,
     294             :                                              url);
     295           7 :   GNUNET_free (url);
     296           7 :   hh->authh = TALER_BANK_make_auth_header_ (auth);
     297           7 :   eh = curl_easy_init ();
     298           7 :   GNUNET_assert (CURLE_OK ==
     299             :                  curl_easy_setopt (eh,
     300             :                                    CURLOPT_HTTPHEADER,
     301             :                                    hh->authh));
     302           7 :   GNUNET_assert (CURLE_OK ==
     303             :                  curl_easy_setopt (eh,
     304             :                                    CURLOPT_URL,
     305             :                                    hh->request_url));
     306           7 :   hh->job = GNUNET_CURL_job_add (ctx,
     307             :                                  eh,
     308             :                                  GNUNET_NO,
     309             :                                  &handle_history_finished,
     310             :                                  hh);
     311           7 :   return hh;
     312             : }
     313             : 
     314             : 
     315             : /**
     316             :  * Cancel a history request.  This function cannot be used on a request
     317             :  * handle if a response is already served for it.
     318             :  *
     319             :  * @param hh the history request handle
     320             :  */
     321             : void
     322           7 : TALER_BANK_history_cancel (struct TALER_BANK_HistoryHandle *hh)
     323             : {
     324           7 :   if (NULL != hh->job)
     325             :   {
     326           0 :     GNUNET_CURL_job_cancel (hh->job);
     327           0 :     hh->job = NULL;
     328             :   }
     329           7 :   curl_slist_free_all (hh->authh);
     330           7 :   GNUNET_free (hh->request_url);
     331           7 :   GNUNET_free (hh->bank_base_url);
     332           7 :   GNUNET_free (hh);
     333           7 : }
     334             : 
     335             : 
     336             : /* end of bank_api_history.c */

Generated by: LCOV version 1.13