From 282e4f59ee849c6c84f753e8fae5691aaf8e9303 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 26 Jan 2020 17:48:13 +0100 Subject: address bank_api_credit comment/FIXME from Florian, also adapt debit logic accordingly --- src/bank-lib/bank_api_credit.c | 23 +++++++---------------- src/bank-lib/bank_api_debit.c | 30 ++++++++++++++---------------- 2 files changed, 21 insertions(+), 32 deletions(-) (limited to 'src/bank-lib') diff --git a/src/bank-lib/bank_api_credit.c b/src/bank-lib/bank_api_credit.c index 50725a4ed..66e128da1 100644 --- a/src/bank-lib/bank_api_credit.c +++ b/src/bank-lib/bank_api_credit.c @@ -30,7 +30,7 @@ /** - * @brief A /history Handle + * @brief A /history/incoming Handle */ struct TALER_BANK_CreditHistoryHandle { @@ -173,30 +173,21 @@ handle_credit_history_finished (void *cls, GNUNET_break_op (0); ec = TALER_JSON_get_error_code (j); break; - case MHD_HTTP_FORBIDDEN: - /* Access denied */ - GNUNET_break_op (0); - ec = TALER_JSON_get_error_code (j); - break; case MHD_HTTP_UNAUTHORIZED: - /* FIXME(dold): I don't get this comment below. What signatures would the - bank even verify?! */ - /* Nothing really to verify, bank says one of the signatures is - invalid; as we checked them, this should never happen, we - should pass the JSON reply to the application */ - GNUNET_break_op (0); + /* 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_NOT_FOUND: - /* Nothing really to verify, this should never - happen, we should pass the JSON reply to the application */ - GNUNET_break_op (0); + /* Nothing really to verify: the bank is either unaware + of the endpoint (not a bank), or of the account. + We should pass the JSON (?) reply to the application */ 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 */ - GNUNET_break_op (0); ec = TALER_JSON_get_error_code (j); break; default: diff --git a/src/bank-lib/bank_api_debit.c b/src/bank-lib/bank_api_debit.c index 0e218eb48..58f6ae6d4 100644 --- a/src/bank-lib/bank_api_debit.c +++ b/src/bank-lib/bank_api_debit.c @@ -30,7 +30,7 @@ /** - * @brief A /history Handle + * @brief A /history/outgoing Handle */ struct TALER_BANK_DebitHistoryHandle { @@ -129,16 +129,16 @@ parse_account_history (struct TALER_BANK_DebitHistoryHandle *hh, /** * Function called when we're done processing the - * HTTP /history request. + * HTTP /history/outgoing request. * * @param cls the `struct TALER_BANK_DebitHistoryHandle` * @param response_code HTTP response code, 0 on error * @param response parsed JSON result, NULL on error */ static void -handle_history_finished (void *cls, - long response_code, - const void *response) +handle_debit_history_finished (void *cls, + long response_code, + const void *response) { struct TALER_BANK_DebitHistoryHandle *hh = cls; enum TALER_ErrorCode ec; @@ -169,21 +169,19 @@ handle_history_finished (void *cls, 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 */ - ec = TALER_JSON_get_error_code (j); - break; - case MHD_HTTP_FORBIDDEN: - /* Access denied */ + GNUNET_break_op (0); ec = TALER_JSON_get_error_code (j); break; case MHD_HTTP_UNAUTHORIZED: - /* Nothing really to verify, bank says one of the signatures is - invalid; as we checked them, this should never happen, we - should pass the JSON reply to the application */ + /* 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_NOT_FOUND: - /* Nothing really to verify, this should never - happen, we should pass the JSON reply to the application */ + /* Nothing really to verify: the bank is either unaware + of the endpoint (not a bank), or of the account. + We should pass the JSON (?) reply to the application */ ec = TALER_JSON_get_error_code (j); break; case MHD_HTTP_INTERNAL_SERVER_ERROR: @@ -196,7 +194,7 @@ handle_history_finished (void *cls, GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unexpected response code %u\n", (unsigned int) response_code); - GNUNET_break (0); + GNUNET_break_op (0); ec = TALER_JSON_get_error_code (j); response_code = 0; break; @@ -292,7 +290,7 @@ TALER_BANK_debit_history (struct GNUNET_CURL_Context *ctx, hh->job = GNUNET_CURL_job_add2 (ctx, eh, NULL, - &handle_history_finished, + &handle_debit_history_finished, hh); return hh; } -- cgit v1.2.3 From 42bc31744b9810509aef344c54bfee2f4e2a7ccb Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 4 Feb 2020 21:59:43 +0100 Subject: implement /config in fakebank and taler_bank_lib.h (#6066) --- src/bank-lib/Makefile.am | 1 + src/bank-lib/bank_api_config.c | 255 ++++++++++++++++++++++++++++++++++++++ src/bank-lib/fakebank.c | 63 +++++++++- src/bank-lib/taler-fakebank-run.c | 28 ++++- 4 files changed, 344 insertions(+), 3 deletions(-) create mode 100644 src/bank-lib/bank_api_config.c (limited to 'src/bank-lib') diff --git a/src/bank-lib/Makefile.am b/src/bank-lib/Makefile.am index 729f96e15..7227ad068 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 000000000..a84e4ff85 --- /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 +*/ +/** + * @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 /* 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 abf486876..56af11f8a 100644 --- a/src/bank-lib/fakebank.c +++ b/src/bank-lib/fakebank.c @@ -26,6 +26,24 @@ #include "taler_bank_service.h" #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) */ @@ -172,6 +190,11 @@ struct TALER_FAKEBANK_Handle */ uint64_t serial_counter; + /** + * Currency used by the fakebank. + */ + char *currency; + /** * BaseURL of the fakebank. */ @@ -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 @@ -808,6 +832,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. @@ -1202,6 +1249,13 @@ serve (struct TALER_FAKEBANK_Handle *h, return handle_home_page (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, @@ -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 55b3da54f..588777c94 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", + ¤cy_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; } -- cgit v1.2.3