From 0eb10b52c3a65770617d391ebdf0596e845718ce Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 28 May 2023 23:00:37 +0200 Subject: implement new account api in libtalermerchant (for #7824) --- src/include/taler_merchant_service.h | 124 +++++++++++++++++- src/lib/Makefile.am | 4 +- src/lib/merchant_api_delete_account.c | 185 +++++++++++++++++++++++++++ src/lib/merchant_api_post_account.c | 228 ++++++++++++++++++++++++++++++++++ src/lib/merchant_api_post_instances.c | 2 +- 5 files changed, 540 insertions(+), 3 deletions(-) create mode 100644 src/lib/merchant_api_delete_account.c create mode 100644 src/lib/merchant_api_post_account.c (limited to 'src') diff --git a/src/include/taler_merchant_service.h b/src/include/taler_merchant_service.h index 33ef033f..114811be 100644 --- a/src/include/taler_merchant_service.h +++ b/src/include/taler_merchant_service.h @@ -480,7 +480,7 @@ struct TALER_MERCHANT_InstancesPostHandle; /** - * Function called with the result of the GET /instances/$ID operation. + * Function called with the result of the POST /instances/$ID operation. * * @param cls closure * @param hr HTTP response data @@ -570,6 +570,128 @@ TALER_MERCHANT_instances_post_cancel ( struct TALER_MERCHANT_InstancesPostHandle *iph); +/** + * Handle for a POST /instances/$ID/account operation. + */ +struct TALER_MERCHANT_AccountPostHandle; + + +/** + * Response for a POST /instances/$ID/account operation. + */ +struct TALER_MERCHANT_AccountPostResponse +{ + /** + * HTTP response data + */ + struct TALER_MERCHANT_HttpResponse hr; +}; + + +/** + * Function called with the result of the POST /instances/$ID/account operation. + * + * @param cls closure + * @param par response data + */ +typedef void +(*TALER_MERCHANT_AccountPostCallback)( + void *cls, + const struct TALER_MERCHANT_AccountPostResponse *par); + + +/** + * Setup an new account for an instance in the backend. + * + * @param ctx the context + * @param backend_url HTTP base URL for the backend + * @param account the bank accounts to add to the merchant instance + * @param auth_token authentication token to use for access control, NULL for external auth; MUST follow RFC 8959 + * @param cb function to call with the response + * @param cb_cls closure for @a config_cb + * @return the instances handle; NULL upon error + */ +struct TALER_MERCHANT_AccountPostHandle * +TALER_MERCHANT_account_post ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const struct TALER_MERCHANT_AccountConfig *account, + TALER_MERCHANT_AccountPostCallback cb, + void *cb_cls); + + +/** + * Cancel /account request. Must not be called by clients after + * the callback was invoked. + * + * @param pah request to cancel. + */ +void +TALER_MERCHANT_account_post_cancel ( + struct TALER_MERCHANT_AccountPostHandle *pah); + + +/** + * Handle for a DELETE /instances/$ID/account/$H_WIRE operation. + */ +struct TALER_MERCHANT_AccountDeleteHandle; + + +/** + * Response for a DELETE /instances/$ID/account operation. + */ +struct TALER_MERCHANT_AccountDeleteResponse +{ + /** + * HTTP response data + */ + struct TALER_MERCHANT_HttpResponse hr; +}; + + +/** + * Function called with the result of the DELETE /instances/$ID/account/$H_WIRE operation. + * + * @param cls closure + * @param par response data + */ +typedef void +(*TALER_MERCHANT_AccountDeleteCallback)( + void *cls, + const struct TALER_MERCHANT_AccountDeleteResponse *par); + + +/** + * Remove bank account from an instance in the backend. + * + * @param ctx the context + * @param backend_url HTTP base URL for the backend + * @param h_wire wire hash of the bank accounts to delete + * @param auth_token authentication token to use for access control, NULL for external auth; MUST follow RFC 8959 + * @param cb function to call with the response + * @param cb_cls closure for @a config_cb + * @return the instances handle; NULL upon error + */ +struct TALER_MERCHANT_AccountDeleteHandle * +TALER_MERCHANT_account_delete ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const struct TALER_MerchantWireHashP *h_wire, + TALER_MERCHANT_AccountDeleteCallback cb, + void *cb_cls); + + +/** + * Cancel /account request. Must not be called by clients after + * the callback was invoked. + * + * @param pah request to cancel. + */ +void +TALER_MERCHANT_account_delete_cancel ( + struct TALER_MERCHANT_AccountDeleteHandle *pah); + + /** * Handle for a PATCH /instances/$ID operation. */ diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 6f1a41fe..e59bd432 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -16,12 +16,13 @@ libtalermerchant_la_LDFLAGS = \ libtalermerchant_la_SOURCES = \ merchant_api_curl_defaults.c merchant_api_curl_defaults.h \ merchant_api_common.c merchant_api_common.h \ + merchant_api_delete_account.c \ merchant_api_delete_instance.c \ merchant_api_delete_order.c \ merchant_api_delete_product.c \ merchant_api_delete_reserve.c \ - merchant_api_delete_transfer.c \ merchant_api_delete_template.c \ + merchant_api_delete_transfer.c \ merchant_api_delete_webhook.c \ merchant_api_get_config.c \ merchant_api_get_instance.c \ @@ -46,6 +47,7 @@ libtalermerchant_la_SOURCES = \ merchant_api_patch_product.c \ merchant_api_patch_template.c \ merchant_api_patch_webhook.c \ + merchant_api_post_account.c \ merchant_api_post_instance_auth.c \ merchant_api_post_instances.c \ merchant_api_post_orders.c \ diff --git a/src/lib/merchant_api_delete_account.c b/src/lib/merchant_api_delete_account.c new file mode 100644 index 00000000..e47833b7 --- /dev/null +++ b/src/lib/merchant_api_delete_account.c @@ -0,0 +1,185 @@ +/* + This file is part of TALER + Copyright (C) 2023 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Lesser General Public License as published by the Free Software + Foundation; either version 2.1, 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License along with + TALER; see the file COPYING.LGPL. If not, see + +*/ +/** + * @file merchant_api_delete_account.c + * @brief Implementation of the DELETE /private/account/$H_WIRE request of the merchant's HTTP API + * @author Christian Grothoff + */ +#include "platform.h" +#include +#include +#include /* just for HTTP status codes */ +#include +#include +#include "taler_merchant_service.h" +#include "merchant_api_curl_defaults.h" +#include +#include + + +/** + * Handle for a DELETE /accounts/$ID operation. + */ +struct TALER_MERCHANT_AccountDeleteHandle +{ + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_AccountDeleteCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Reference to the execution context. + */ + struct GNUNET_CURL_Context *ctx; + +}; + + +/** + * Function called when we're done processing the + * HTTP DELETE /accounts/$H_WIRE request. + * + * @param cls the `struct TALER_MERCHANT_AccountDeleteHandle` + * @param response_code HTTP response code, 0 on error + * @param response response body, NULL if not in JSON + */ +static void +handle_delete_account_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_AccountDeleteHandle *adh = cls; + const json_t *json = response; + struct TALER_MERCHANT_AccountDeleteResponse adr = { + .hr.http_status = (unsigned int) response_code, + .hr.reply = json + }; + + adh->job = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got /accounts/$H_WIRE response with status code %u\n", + (unsigned int) response_code); + switch (response_code) + { + case MHD_HTTP_NO_CONTENT: + break; + case MHD_HTTP_UNAUTHORIZED: + adr.hr.ec = TALER_JSON_get_error_code (json); + adr.hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, merchant says we need to authenticate. */ + break; + case MHD_HTTP_NOT_FOUND: + break; + default: + /* unexpected response code */ + adr.hr.ec = TALER_JSON_get_error_code (json); + adr.hr.hint = TALER_JSON_get_error_hint (json); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u/%d for DELETE /account/ID\n", + (unsigned int) response_code, + (int) adr.hr.ec); + break; + } + adh->cb (adh->cb_cls, + &adr); + TALER_MERCHANT_account_delete_cancel (adh); +} + + +struct TALER_MERCHANT_AccountDeleteHandle * +TALER_MERCHANT_account_delete ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const struct TALER_MerchantWireHashP *h_wire, + TALER_MERCHANT_AccountDeleteCallback cb, + void *cb_cls) +{ + struct TALER_MERCHANT_AccountDeleteHandle *adh; + + adh = GNUNET_new (struct TALER_MERCHANT_AccountDeleteHandle); + adh->ctx = ctx; + adh->cb = cb; + adh->cb_cls = cb_cls; + { + char h_wire_str[sizeof (*h_wire) * 2]; + char *path; + char *end; + + end = GNUNET_STRINGS_data_to_string (h_wire, + sizeof (*h_wire), + h_wire_str, + sizeof (h_wire_str)); + *end = '\0'; + GNUNET_asprintf (&path, + "private/account/%s", + h_wire_str); + adh->url = TALER_url_join (backend_url, + path, + NULL); + GNUNET_free (path); + } + if (NULL == adh->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + GNUNET_free (adh); + return NULL; + } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Requesting URL '%s'\n", + adh->url); + { + CURL *eh; + + eh = TALER_MERCHANT_curl_easy_get_ (adh->url); + GNUNET_assert (CURLE_OK == + curl_easy_setopt (eh, + CURLOPT_CUSTOMREQUEST, + MHD_HTTP_METHOD_DELETE)); + adh->job = GNUNET_CURL_job_add (ctx, + eh, + &handle_delete_account_finished, + adh); + } + return adh; +} + + +void +TALER_MERCHANT_account_delete_cancel ( + struct TALER_MERCHANT_AccountDeleteHandle *adh) +{ + if (NULL != adh->job) + GNUNET_CURL_job_cancel (adh->job); + GNUNET_free (adh->url); + GNUNET_free (adh); +} diff --git a/src/lib/merchant_api_post_account.c b/src/lib/merchant_api_post_account.c new file mode 100644 index 00000000..68be6afd --- /dev/null +++ b/src/lib/merchant_api_post_account.c @@ -0,0 +1,228 @@ +/* + This file is part of TALER + Copyright (C) 2023 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1, + 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with TALER; see the file COPYING.LGPL. + If not, see +*/ +/** + * @file merchant_api_post_account.c + * @brief Implementation of the POST /account request + * of the merchant's HTTP API + * @author Christian Grothoff + */ +#include "platform.h" +#include +#include +#include /* just for HTTP status codes */ +#include +#include "taler_merchant_service.h" +#include "merchant_api_curl_defaults.h" +#include "merchant_api_common.h" +#include +#include + + +/** + * Handle for a POST /private/account operation. + */ +struct TALER_MERCHANT_AccountPostHandle +{ + + /** + * The url for this request. + */ + char *url; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_MERCHANT_AccountPostCallback cb; + + /** + * Closure for @a cb. + */ + void *cb_cls; + + /** + * Reference to the execution context. + */ + struct GNUNET_CURL_Context *ctx; + + /** + * Minor context that holds body and headers. + */ + struct TALER_CURL_PostContext post_ctx; + +}; + + +/** + * Function called when we're done processing the + * HTTP POST /account request. + * + * @param cls the `struct TALER_MERCHANT_AccountPostHandle` + * @param response_code HTTP response code, 0 on error + * @param response response body, NULL if not in JSON + */ +static void +handle_post_account_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_MERCHANT_AccountPostHandle *aph = cls; + const json_t *json = response; + struct TALER_MERCHANT_AccountPostResponse apr = { + .hr.http_status = (unsigned int) response_code, + .hr.reply = json + }; + + aph->job = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "POST /account completed with response code %u\n", + (unsigned int) response_code); + switch (response_code) + { + case 0: + apr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + break; + case MHD_HTTP_NO_CONTENT: + break; + case MHD_HTTP_BAD_REQUEST: + GNUNET_break_op (0); + apr.hr.ec = TALER_JSON_get_error_code (json); + apr.hr.hint = TALER_JSON_get_error_hint (json); + /* This should never happen, either us + * or the merchant is buggy (or API version conflict); + * just pass JSON reply to the application */ + break; + case MHD_HTTP_FORBIDDEN: + apr.hr.ec = TALER_JSON_get_error_code (json); + apr.hr.hint = TALER_JSON_get_error_hint (json); + break; + case MHD_HTTP_NOT_FOUND: + apr.hr.ec = TALER_JSON_get_error_code (json); + apr.hr.hint = TALER_JSON_get_error_hint (json); + /* Nothing really to verify, this should never + happen, we should pass the JSON reply to the + application */ + break; + case MHD_HTTP_CONFLICT: + apr.hr.ec = TALER_JSON_get_error_code (json); + apr.hr.hint = TALER_JSON_get_error_hint (json); + break; + case MHD_HTTP_INTERNAL_SERVER_ERROR: + apr.hr.ec = TALER_JSON_get_error_code (json); + apr.hr.hint = TALER_JSON_get_error_hint (json); + /* Server had an internal issue; we should retry, + but this API leaves this to the application */ + break; + default: + TALER_MERCHANT_parse_error_details_ (json, + response_code, + &apr.hr); + /* unexpected response code */ + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u/%d\n", + (unsigned int) response_code, + (int) apr.hr.ec); + GNUNET_break_op (0); + break; + } + aph->cb (aph->cb_cls, + &apr); + TALER_MERCHANT_account_post_cancel (aph); +} + + +struct TALER_MERCHANT_AccountPostHandle * +TALER_MERCHANT_account_post ( + struct GNUNET_CURL_Context *ctx, + const char *backend_url, + const struct TALER_MERCHANT_AccountConfig *account, + TALER_MERCHANT_AccountPostCallback cb, + void *cb_cls) +{ + struct TALER_MERCHANT_AccountPostHandle *aph; + json_t *req_obj; + + req_obj = GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ( + "payto_uri", + account->payto_uri), + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_string ( + "credit_facade_url", + account->credit_facade_url)), + GNUNET_JSON_pack_allow_null ( + GNUNET_JSON_pack_object_incref ( + "credit_facade_credentials", + account->credit_facade_credentials)) + ); + aph = GNUNET_new (struct TALER_MERCHANT_AccountPostHandle); + aph->ctx = ctx; + aph->cb = cb; + aph->cb_cls = cb_cls; + aph->url = TALER_url_join (backend_url, + "private/account", + NULL); + if (NULL == aph->url) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not construct request URL.\n"); + json_decref (req_obj); + GNUNET_free (aph); + return NULL; + } + { + CURL *eh; + + eh = TALER_MERCHANT_curl_easy_get_ (aph->url); + GNUNET_assert (GNUNET_OK == + TALER_curl_easy_post (&aph->post_ctx, + eh, + req_obj)); + json_decref (req_obj); + aph->job = GNUNET_CURL_job_add2 (ctx, + eh, + aph->post_ctx.headers, + &handle_post_account_finished, + aph); + GNUNET_assert (NULL != aph->job); + } + return aph; +} + + +void +TALER_MERCHANT_account_post_cancel ( + struct TALER_MERCHANT_AccountPostHandle *aph) +{ + if (NULL != aph->job) + { + GNUNET_CURL_job_cancel (aph->job); + aph->job = NULL; + } + TALER_curl_easy_post_finished (&aph->post_ctx); + GNUNET_free (aph->url); + GNUNET_free (aph); +} + + +/* end of merchant_api_post_account.c */ diff --git a/src/lib/merchant_api_post_instances.c b/src/lib/merchant_api_post_instances.c index 88ef3a06..77168e2a 100644 --- a/src/lib/merchant_api_post_instances.c +++ b/src/lib/merchant_api_post_instances.c @@ -241,7 +241,7 @@ TALER_MERCHANT_instances_post ( GNUNET_JSON_pack_allow_null ( GNUNET_JSON_pack_object_incref ( "credit_facade_credentials", - accounts->credit_facade_credentials)) + account->credit_facade_credentials)) ); if (0 != -- cgit v1.2.3