summaryrefslogtreecommitdiff
path: root/src/bank-lib
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2020-02-04 21:59:43 +0100
committerChristian Grothoff <christian@grothoff.org>2020-02-04 21:59:43 +0100
commit42bc31744b9810509aef344c54bfee2f4e2a7ccb (patch)
tree92ece6674b23f5e985755992a2ddb840cdd2de7f /src/bank-lib
parente6d6987e5685b35f6c3137f59894a02a63d09766 (diff)
downloadexchange-42bc31744b9810509aef344c54bfee2f4e2a7ccb.tar.gz
exchange-42bc31744b9810509aef344c54bfee2f4e2a7ccb.tar.bz2
exchange-42bc31744b9810509aef344c54bfee2f4e2a7ccb.zip
implement /config in fakebank and taler_bank_lib.h (#6066)
Diffstat (limited to 'src/bank-lib')
-rw-r--r--src/bank-lib/Makefile.am1
-rw-r--r--src/bank-lib/bank_api_config.c255
-rw-r--r--src/bank-lib/fakebank.c63
-rw-r--r--src/bank-lib/taler-fakebank-run.c28
4 files changed, 344 insertions, 3 deletions
diff --git a/src/bank-lib/Makefile.am b/src/bank-lib/Makefile.am
index 729f96e1..7227ad06 100644
--- a/src/bank-lib/Makefile.am
+++ b/src/bank-lib/Makefile.am
@@ -38,6 +38,7 @@ libtalerbank_la_LDFLAGS = \
libtalerbank_la_SOURCES = \
bank_api_admin.c \
bank_api_common.c bank_api_common.h \
+ bank_api_config.c \
bank_api_credit.c \
bank_api_debit.c \
bank_api_transfer.c \
diff --git a/src/bank-lib/bank_api_config.c b/src/bank-lib/bank_api_config.c
new file mode 100644
index 00000000..a84e4ff8
--- /dev/null
+++ b/src/bank-lib/bank_api_config.c
@@ -0,0 +1,255 @@
+/*
+ This file is part of TALER
+ Copyright (C) 2017--2020 Taler Systems SA
+
+ TALER is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 3,
+ or (at your option) any later version.
+
+ TALER is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public
+ License along with TALER; see the file COPYING. If not,
+ see <http://www.gnu.org/licenses/>
+*/
+/**
+ * @file bank-lib/bank_api_config.c
+ * @brief Implementation of the /config request
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "bank_api_common.h"
+#include <microhttpd.h> /* just for HTTP status codes */
+#include "taler_signatures.h"
+
+/**
+ * Protocol version we implement.
+ */
+#define BANK_PROTOCOL_CURRENT 0
+
+/**
+ * How many revisions back are we compatible to.
+ */
+#define BANK_PROTOCOL_AGE 0
+
+
+/**
+ * @brief A /config Handle
+ */
+struct TALER_BANK_ConfigHandle
+{
+
+ /**
+ * The url for this request.
+ */
+ char *request_url;
+
+ /**
+ * Handle for the request.
+ */
+ struct GNUNET_CURL_Job *job;
+
+ /**
+ * Function to call with the result.
+ */
+ TALER_BANK_ConfigCallback hcb;
+
+ /**
+ * Closure for @a cb.
+ */
+ void *hcb_cls;
+};
+
+
+/**
+ * Parse configuration given in JSON format and invoke the callback on each item.
+ *
+ * @param ch handle to the account configuration request
+ * @param config JSON object with the configuration
+ * @return #GNUNET_OK if configuration was valid and @a rconfiguration and @a balance
+ * were set,
+ * #GNUNET_SYSERR if there was a protocol violation in @a configuration
+ */
+static int
+parse_config (struct TALER_BANK_ConfigHandle *ch,
+ const json_t *config)
+{
+ struct TALER_BANK_Configuration cfg;
+ struct GNUNET_JSON_Specification spec[] = {
+ GNUNET_JSON_spec_string ("version",
+ &cfg.version),
+ GNUNET_JSON_spec_string ("currency",
+ &cfg.version),
+ GNUNET_JSON_spec_end ()
+ };
+
+ if (GNUNET_OK !=
+ GNUNET_JSON_parse (config,
+ spec,
+ NULL, NULL))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ ch->hcb (ch->hcb_cls,
+ MHD_HTTP_OK,
+ TALER_EC_NONE,
+ &cfg);
+ GNUNET_JSON_parse_free (spec);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Function called when we're done processing the
+ * HTTP /config request.
+ *
+ * @param cls the `struct TALER_BANK_ConfigHandle`
+ * @param response_code HTTP response code, 0 on error
+ * @param response parsed JSON result, NULL on error
+ */
+static void
+handle_configuration_finished (void *cls,
+ long response_code,
+ const void *response)
+{
+ struct TALER_BANK_ConfigHandle *ch = cls;
+ enum TALER_ErrorCode ec;
+ const json_t *j = response;
+
+ ch->job = NULL;
+ switch (response_code)
+ {
+ case 0:
+ ec = TALER_EC_INVALID_RESPONSE;
+ break;
+ case MHD_HTTP_OK:
+ if (GNUNET_OK !=
+ parse_config (ch,
+ j))
+ {
+ GNUNET_break_op (0);
+ response_code = 0;
+ ec = TALER_EC_INVALID_RESPONSE;
+ break;
+ }
+ response_code = MHD_HTTP_NO_CONTENT; /* signal end of list */
+ ec = TALER_EC_NONE;
+ break;
+ case MHD_HTTP_BAD_REQUEST:
+ /* This should never happen, either us or the bank is buggy
+ (or API version conflict); just pass JSON reply to the application */
+ GNUNET_break_op (0);
+ ec = TALER_JSON_get_error_code (j);
+ break;
+ case MHD_HTTP_UNAUTHORIZED:
+ /* Nothing really to verify, bank says the HTTP Authentication
+ failed. May happen if HTTP authentication is used and the
+ user supplied a wrong username/password combination. */
+ ec = TALER_JSON_get_error_code (j);
+ break;
+ case MHD_HTTP_INTERNAL_SERVER_ERROR:
+ /* Server had an internal issue; we should retry, but this API
+ leaves this to the application */
+ ec = TALER_JSON_get_error_code (j);
+ break;
+ default:
+ /* unexpected response code */
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Unexpected response code %u\n",
+ (unsigned int) response_code);
+ GNUNET_break_op (0);
+ ec = TALER_JSON_get_error_code (j);
+ response_code = 0;
+ break;
+ }
+ ch->hcb (ch->hcb_cls,
+ response_code,
+ ec,
+ NULL);
+ TALER_BANK_configuration_cancel (ch);
+}
+
+
+/**
+ * Request the configuration of the bank.
+ *
+ * @param ctx curl context for the event loop
+ * @param auth authentication data to use
+ * @param hres_cb the callback to call with the
+ * configuration
+ * @param hres_cb_cls closure for the above callback
+ * @return NULL if the inputs are invalid
+ */
+struct TALER_BANK_ConfigHandle *
+TALER_BANK_configuration (struct GNUNET_CURL_Context *ctx,
+ const struct TALER_BANK_AuthenticationData *auth,
+ TALER_BANK_ConfigCallback hres_cb,
+ void *hres_cb_cls)
+{
+ struct TALER_BANK_ConfigHandle *ch;
+ CURL *eh;
+
+ ch = GNUNET_new (struct TALER_BANK_ConfigHandle);
+ ch->hcb = hres_cb;
+ ch->hcb_cls = hres_cb_cls;
+ ch->request_url = TALER_url_join (auth->wire_gateway_url,
+ "config",
+ NULL);
+ if (NULL == ch->request_url)
+ {
+ GNUNET_free (ch);
+ GNUNET_break (0);
+ return NULL;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ "Requesting configuration at `%s'\n",
+ ch->request_url);
+ eh = curl_easy_init ();
+ if ( (GNUNET_OK !=
+ TALER_BANK_setup_auth_ (eh,
+ auth)) ||
+ (CURLE_OK !=
+ curl_easy_setopt (eh,
+ CURLOPT_URL,
+ ch->request_url)) )
+ {
+ GNUNET_break (0);
+ TALER_BANK_configuration_cancel (ch);
+ curl_easy_cleanup (eh);
+ return NULL;
+ }
+ ch->job = GNUNET_CURL_job_add2 (ctx,
+ eh,
+ NULL,
+ &handle_configuration_finished,
+ ch);
+ return ch;
+}
+
+
+/**
+ * Cancel a configuration request. This function cannot be
+ * used on a request handle if a response is already
+ * served for it.
+ *
+ * @param ch the configuration request handle
+ */
+void
+TALER_BANK_configuration_cancel (struct TALER_BANK_ConfigHandle *ch)
+{
+ if (NULL != ch->job)
+ {
+ GNUNET_CURL_job_cancel (ch->job);
+ ch->job = NULL;
+ }
+ GNUNET_free (ch->request_url);
+ GNUNET_free (ch);
+}
+
+
+/* end of bank_api_config.c */
diff --git a/src/bank-lib/fakebank.c b/src/bank-lib/fakebank.c
index abf48687..56af11f8 100644
--- a/src/bank-lib/fakebank.c
+++ b/src/bank-lib/fakebank.c
@@ -27,6 +27,24 @@
#include "taler_mhd_lib.h"
/**
+ * Taler protocol version in the format CURRENT:REVISION:AGE
+ * as used by GNU libtool. See
+ * https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
+ *
+ * Please be very careful when updating and follow
+ * https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html#Updating-version-info
+ * precisely. Note that this version has NOTHING to do with the
+ * release version, and the format is NOT the same that semantic
+ * versioning uses either.
+ *
+ * When changing this version, you likely want to also update
+ * #BANK_PROTOCOL_CURRENT and #BANK_PROTOCOL_AGE in
+ * bank_api_config.c!
+ */
+#define BANK_PROTOCOL_VERSION "0:0:0"
+
+
+/**
* Maximum POST request size (for /admin/add-incoming)
*/
#define REQUEST_BUFFER_MAX (4 * 1024)
@@ -173,6 +191,11 @@ struct TALER_FAKEBANK_Handle
uint64_t serial_counter;
/**
+ * Currency used by the fakebank.
+ */
+ char *currency;
+
+ /**
* BaseURL of the fakebank.
*/
char *my_baseurl;
@@ -527,6 +550,7 @@ TALER_FAKEBANK_stop (struct TALER_FAKEBANK_Handle *h)
h->mhd_bank = NULL;
}
GNUNET_free (h->my_baseurl);
+ GNUNET_free (h->currency);
GNUNET_free (h);
}
@@ -776,7 +800,7 @@ handle_transfer (struct TALER_FAKEBANK_Handle *h,
/**
- * Handle incoming HTTP request for /history
+ * Handle incoming HTTP request for / (home page).
*
* @param h the fakebank handle
* @param connection the connection
@@ -809,6 +833,29 @@ handle_home_page (struct TALER_FAKEBANK_Handle *h,
/**
+ * Handle incoming HTTP request for /config
+ *
+ * @param h the fakebank handle
+ * @param connection the connection
+ * @param con_cls place to store state, not used
+ * @return MHD result code
+ */
+static int
+handle_config (struct TALER_FAKEBANK_Handle *h,
+ struct MHD_Connection *connection,
+ void **con_cls)
+{
+ return TALER_MHD_reply_json_pack (connection,
+ MHD_HTTP_OK,
+ "{s:s, s:s}",
+ "currency",
+ h->currency,
+ "version"
+ BANK_PROTOCOL_VERSION);
+}
+
+
+/**
* This is the "base" structure for both the /history and the
* /history-range API calls.
*/
@@ -1203,6 +1250,13 @@ serve (struct TALER_FAKEBANK_Handle *h,
connection,
con_cls);
if ( (0 == strcmp (url,
+ "/config")) &&
+ (0 == strcasecmp (method,
+ MHD_HTTP_METHOD_GET)) )
+ return handle_config (h,
+ connection,
+ con_cls);
+ if ( (0 == strcmp (url,
"/admin/add-incoming")) &&
(0 == strcasecmp (method,
MHD_HTTP_METHOD_POST)) )
@@ -1433,15 +1487,19 @@ run_mhd (void *cls)
* would have issued the correct wire transfer orders.
*
* @param port port to listen to
+ * @param currency currency the bank uses
* @return NULL on error
*/
struct TALER_FAKEBANK_Handle *
-TALER_FAKEBANK_start (uint16_t port)
+TALER_FAKEBANK_start (uint16_t port,
+ const char *currency)
{
struct TALER_FAKEBANK_Handle *h;
+ GNUNET_assert (strlen (currency) < TALER_CURRENCY_LEN);
h = GNUNET_new (struct TALER_FAKEBANK_Handle);
h->port = port;
+ h->currency = GNUNET_strdup (currency);
GNUNET_asprintf (&h->my_baseurl,
"http://localhost:%u/",
(unsigned int) port);
@@ -1462,6 +1520,7 @@ TALER_FAKEBANK_start (uint16_t port)
MHD_OPTION_END);
if (NULL == h->mhd_bank)
{
+ GNUNET_free (h->currency);
GNUNET_free (h);
return NULL;
}
diff --git a/src/bank-lib/taler-fakebank-run.c b/src/bank-lib/taler-fakebank-run.c
index 55b3da54..588777c9 100644
--- a/src/bank-lib/taler-fakebank-run.c
+++ b/src/bank-lib/taler-fakebank-run.c
@@ -43,12 +43,38 @@ run (void *cls,
const char *cfgfile,
const struct GNUNET_CONFIGURATION_Handle *cfg)
{
+ char *currency_string;
+
(void) cls;
(void) args;
(void) cfgfile;
(void) cfg;
- if (NULL == TALER_FAKEBANK_start (8082))
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_string (cfg,
+ "taler",
+ "CURRENCY",
+ &currency_string))
+ {
+ GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
+ "taler",
+ "CURRENCY");
+ ret = 1;
+ return;
+ }
+ if (strlen (currency_string) >= TALER_CURRENCY_LEN)
+ {
+ GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
+ "taler",
+ "CURRENCY",
+ "Value is too long");
+ GNUNET_free (currency_string);
+ ret = 1;
+ return;
+ }
+ if (NULL == TALER_FAKEBANK_start (8082,
+ currency_string))
ret = 1;
+ GNUNET_free (currency_string);
ret = 0;
}