From 99211b460908a829711df078d87044fc302d7aaf Mon Sep 17 00:00:00 2001 From: priscilla Date: Fri, 25 Nov 2022 10:35:17 -0500 Subject: webhook backend --- ...ler-merchant-httpd_private-delete-webhooks-ID.c | 74 +++++++ ...ler-merchant-httpd_private-delete-webhooks-ID.h | 41 ++++ .../taler-merchant-httpd_private-get-webhooks-ID.c | 90 +++++++++ .../taler-merchant-httpd_private-get-webhooks-ID.h | 41 ++++ .../taler-merchant-httpd_private-get-webhooks.c | 77 ++++++++ .../taler-merchant-httpd_private-get-webhooks.h | 41 ++++ ...aler-merchant-httpd_private-patch-webhooks-ID.c | 185 ++++++++++++++++++ ...aler-merchant-httpd_private-patch-webhooks-ID.h | 43 +++++ .../taler-merchant-httpd_private-post-webhooks.c | 214 +++++++++++++++++++++ .../taler-merchant-httpd_private-post-webhooks.h | 43 +++++ src/backenddb/merchant-0001.sql | 2 +- 11 files changed, 850 insertions(+), 1 deletion(-) create mode 100644 src/backend/taler-merchant-httpd_private-delete-webhooks-ID.c create mode 100644 src/backend/taler-merchant-httpd_private-delete-webhooks-ID.h create mode 100644 src/backend/taler-merchant-httpd_private-get-webhooks-ID.c create mode 100644 src/backend/taler-merchant-httpd_private-get-webhooks-ID.h create mode 100644 src/backend/taler-merchant-httpd_private-get-webhooks.c create mode 100644 src/backend/taler-merchant-httpd_private-get-webhooks.h create mode 100644 src/backend/taler-merchant-httpd_private-patch-webhooks-ID.c create mode 100644 src/backend/taler-merchant-httpd_private-patch-webhooks-ID.h create mode 100644 src/backend/taler-merchant-httpd_private-post-webhooks.c create mode 100644 src/backend/taler-merchant-httpd_private-post-webhooks.h diff --git a/src/backend/taler-merchant-httpd_private-delete-webhooks-ID.c b/src/backend/taler-merchant-httpd_private-delete-webhooks-ID.c new file mode 100644 index 00000000..6a8db79c --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-delete-webhooks-ID.c @@ -0,0 +1,74 @@ +/* + This file is part of TALER + (C) 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero 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 taler-merchant-httpd_private-delete-webhooks-ID.c + * @brief implement DELETE /webhooks/$ID + * @author Priscilla HUANG + */ +#include "platform.h" +#include "taler-merchant-httpd_private-delete-webhooks-ID.h" +#include + + +/** + * Handle a DELETE "/webhooks/$ID" request. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_delete_webhooks_ID (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) +{ + struct TMH_MerchantInstance *mi = hc->instance; + enum GNUNET_DB_QueryStatus qs; + + (void) rh; + GNUNET_assert (NULL != mi); + GNUNET_assert (NULL != hc->infix); + qs = TMH_db->delete_webhook (TMH_db->cls, + mi->settings.id, + hc->infix); + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_DB_STORE_FAILED, + "delete_webhook"); + case GNUNET_DB_STATUS_SOFT_ERROR: + GNUNET_break (0); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, + "delete_webhook (soft)"); + + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + return TALER_MHD_reply_static (connection, + MHD_HTTP_NO_CONTENT, + NULL, + NULL, + 0); + } + GNUNET_assert (0); + return MHD_NO; +} + + +/* end of taler-merchant-httpd_private-delete-webhooks-ID.c */ diff --git a/src/backend/taler-merchant-httpd_private-delete-webhooks-ID.h b/src/backend/taler-merchant-httpd_private-delete-webhooks-ID.h new file mode 100644 index 00000000..ba77fb7a --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-delete-webhooks-ID.h @@ -0,0 +1,41 @@ +/* + This file is part of TALER + (C) 2019, 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero 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 taler-merchant-httpd_private-delete-webhooks-ID.h + * @brief implement DELETE /webhooks/$ID/ + * @author Priscilla HUANG + */ +#ifndef TALER_MERCHANT_HTTPD_PRIVATE_DELETE_WEBHOOKS_ID_H +#define TALER_MERCHANT_HTTPD_PRIVATE_DELETE_WEBHOOKS_ID_H + +#include "taler-merchant-httpd.h" + + +/** + * Handle a DELETE "/webhooks/$ID" request. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_delete_webhooks_ID (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc); + +/* end of taler-merchant-httpd_private-delete-webhooks-ID.h */ +#endif diff --git a/src/backend/taler-merchant-httpd_private-get-webhooks-ID.c b/src/backend/taler-merchant-httpd_private-get-webhooks-ID.c new file mode 100644 index 00000000..2780d145 --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-get-webhooks-ID.c @@ -0,0 +1,90 @@ +/* + This file is part of TALER + (C) 2019, 2020, 2021 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero 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 taler-merchant-httpd_private-get-webhooks-ID.c + * @brief implement GET /webhooks/$ID + * @author Priscilla HUANG + */ +#include "platform.h" +#include "taler-merchant-httpd_private-get-webhooks-ID.h" +#include + + +/** + * Handle a GET "/webhooks/$ID" request. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_get_webhooks_ID (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) +{ + struct TMH_MerchantInstance *mi = hc->instance; + struct TALER_MERCHANTDB_WebhookDetails wb = { 0 }; + enum GNUNET_DB_QueryStatus qs; + + GNUNET_assert (NULL != mi); + qs = TMH_db->lookup_webhook (TMH_db->cls, + mi->settings.id, + hc->infix, + &wb); + if (0 > qs) + { + GNUNET_break (0); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_DB_FETCH_FAILED, + "lookup_webhook"); + } + if (0 == qs) + { + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_MERCHANT_GENERIC_WEBHOOK_UNKNOWN, + hc->infix); + } + { + MHD_RESULT ret; + + ret = TALER_MHD_REPLY_JSON_PACK ( + connection, + MHD_HTTP_OK, + GNUNET_JSON_pack_string ("event_type", + wb.event_type), + GNUNET_JSON_pack_string ("url", + wb.url), + GNUNET_JSON_pack_string ("http_method", + wb.http_method), + GNUNET_JSON_pack_string ("header_template", + wb.header_template), + GNUNET_JSON_pack_string ("body_template", + wb.body_template)); + GNUNET_free (wb.event_type); + GNUNET_free (wb.url); + GNUNET_free (wb.http_method); + GNUNET_free (wb.header_template); + GNUNET_free (wb.body_template); + + return ret; + } +} + + +/* end of taler-merchant-httpd_private-get-webhooks-ID.c */ diff --git a/src/backend/taler-merchant-httpd_private-get-webhooks-ID.h b/src/backend/taler-merchant-httpd_private-get-webhooks-ID.h new file mode 100644 index 00000000..261edb41 --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-get-webhooks-ID.h @@ -0,0 +1,41 @@ +/* + This file is part of TALER + (C) 2019, 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero 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 taler-merchant-httpd_private-get-webhooks-ID.h + * @brief implement GET /webhooks/$ID/ + * @author Priscilla HUANG + */ +#ifndef TALER_MERCHANT_HTTPD_PRIVATE_GET_WEBHOOKS_ID_H +#define TALER_MERCHANT_HTTPD_PRIVATE_GET_WEBHOOKS_ID_H + +#include "taler-merchant-httpd.h" + + +/** + * Handle a GET "/webhooks/$ID" request. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_get_webhooks_ID (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc); + +/* end of taler-merchant-httpd_private-get-webhooks-ID.h */ +#endif diff --git a/src/backend/taler-merchant-httpd_private-get-webhooks.c b/src/backend/taler-merchant-httpd_private-get-webhooks.c new file mode 100644 index 00000000..735748e3 --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-get-webhooks.c @@ -0,0 +1,77 @@ +/* + This file is part of TALER + (C) 2019, 2020, 2021 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero 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 taler-merchant-httpd_private-get-webhooks.c + * @brief implement GET /webhooks + * @author Priscilla HUANG + */ +#include "platform.h" +#include "taler-merchant-httpd_private-get-webhooks.h" + + +/** + * Add webhook details to our JSON array. + * + * @param cls a `json_t *` JSON array to build + * @param webhook_id ID of the webhook + */ +static void +add_template (void *cls, + const char *webhook_id, + const char *event_type) +{ + json_t *pa = cls; + + GNUNET_assert (0 == + json_array_append_new ( + pa, + GNUNET_JSON_PACK ( + GNUNET_JSON_pack_string ("webhook_id", + webhook_id)))); +} + + +MHD_RESULT +TMH_private_get_webhooks (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) +{ + json_t *pa; + enum GNUNET_DB_QueryStatus qs; + + pa = json_array (); + GNUNET_assert (NULL != pa); + qs = TMH_db->lookup_webhooks (TMH_db->cls, + hc->instance->settings.id, + &add_webhook, + pa); + if (0 > qs) + { + GNUNET_break (0); + json_decref (pa); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_DB_FETCH_FAILED, + NULL); + } + return TALER_MHD_REPLY_JSON_PACK (connection, + MHD_HTTP_OK, + GNUNET_JSON_pack_array_steal ("webhooks", + pa)); +} + + +/* end of taler-merchant-httpd_private-get-webhooks.c */ diff --git a/src/backend/taler-merchant-httpd_private-get-webhooks.h b/src/backend/taler-merchant-httpd_private-get-webhooks.h new file mode 100644 index 00000000..297e5ace --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-get-webhooks.h @@ -0,0 +1,41 @@ +/* + This file is part of TALER + (C) 2019, 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero 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 taler-merchant-httpd_private-get-webhooks.h + * @brief implement GET /webhooks + * @author Priscilla HUANG + */ +#ifndef TALER_MERCHANT_HTTPD_PRIVATE_GET_WEBHOOKS_H +#define TALER_MERCHANT_HTTPD_PRIVATE_GET_WEBHOOKS_H + +#include "taler-merchant-httpd.h" + + +/** + * Handle a GET "/webhooks" request. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_get_webhooks (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc); + +/* end of taler-merchant-httpd_private-get-webhooks.h */ +#endif diff --git a/src/backend/taler-merchant-httpd_private-patch-webhooks-ID.c b/src/backend/taler-merchant-httpd_private-patch-webhooks-ID.c new file mode 100644 index 00000000..af93e96a --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-patch-webhooks-ID.c @@ -0,0 +1,185 @@ +/* + This file is part of TALER + (C) 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify + it under the terms of the GNU Affero 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 taler-merchant-httpd_private-patch-webhooks-ID.c + * @brief implementing PATCH /webhooks/$ID request handling + * @author Priscilla HUANG + */ +#include "platform.h" +#include "taler-merchant-httpd_private-patch-webhooks-ID.h" +#include "taler-merchant-httpd_helper.h" +#include + + +/** + * How often do we retry the simple INSERT database transaction? + */ +#define MAX_RETRIES 3 + + +/** + * Determine the cause of the PATCH failure in more detail and report. + * + * @param connection connection to report on + * @param instance_id instance we are processing + * @param webhook_id ID of the webhook to patch + * @param wb webhook details we failed to set + */ +static MHD_RESULT +determine_cause (struct MHD_Connection *connection, + const char *instance_id, + const char *webhook_id, + const struct TALER_MERCHANTDB_WebhookDetails *wb) +{ + struct TALER_MERCHANTDB_WebhookDetails wpx; + enum GNUNET_DB_QueryStatus qs; + + qs = TMH_db->lookup_webhook (TMH_db->cls, + instance_id, + webhook_id, + &wpx); + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + GNUNET_break (0); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_DB_FETCH_FAILED, + NULL); + case GNUNET_DB_STATUS_SOFT_ERROR: + GNUNET_break (0); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, + "unexpected serialization problem"); + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_MERCHANT_GENERIC_WEBHOOK_UNKNOWN, + webhook_id); + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + break; /* do below */ + } + + { + enum TALER_ErrorCode ec; + + ec = TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; + TALER_MERCHANTDB_webhook_details_free (&wpx); + GNUNET_break (TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE != ec); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_CONFLICT, + ec, + NULL); + } +} + + +/** + * PATCH configuration of an existing instance, given its configuration. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_patch_webhooks_ID (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) +{ + struct TMH_MerchantInstance *mi = hc->instance; + const char *webhook_id = hc->infix; + struct TALER_MERCHANTDB_WebhookDetails tp = {0}; + enum GNUNET_DB_QueryStatus qs; + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_string ("event_type", + (const char **) &wb.event_type), + GNUNET_JSON_spec_string ("url", + (const char **) &wb.url), + GNUNET_JSON_spec_string ("http_method", + (const char **) &wb.http_method), + GNUNET_JSON_spec_string ("header_template", + (const char **) &wb.header_template), + GNUNET_JSON_spec_string ("body_template", + (const char **) &wb.body_template), + + GNUNET_JSON_spec_end () + }; + + GNUNET_assert (NULL != mi); + GNUNET_assert (NULL != webhook_id); + { + enum GNUNET_GenericReturnValue res; + + res = TALER_MHD_parse_json_data (connection, + hc->request_body, + spec); + if (GNUNET_OK != res) + return (GNUNET_NO == res) + ? MHD_YES + : MHD_NO; + } + + + qs = TMH_db->update_webhook (TMH_db->cls, + mi->settings.id, + webhook_id, + &wb); + { + MHD_RESULT ret = MHD_NO; + + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + GNUNET_break (0); + ret = TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_DB_STORE_FAILED, + NULL); + break; + case GNUNET_DB_STATUS_SOFT_ERROR: + GNUNET_break (0); + ret = TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, + "unexpected serialization problem"); + break; + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + ret = determine_cause (connection, + mi->settings.id, + webhook_id, + &wb); + break; + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + ret = TALER_MHD_reply_static (connection, + MHD_HTTP_NO_CONTENT, + NULL, + NULL, + 0); + break; + } + GNUNET_JSON_parse_free (spec); + return ret; + } +} + + +/* end of taler-merchant-httpd_private-patch-webhooks-ID.c */ diff --git a/src/backend/taler-merchant-httpd_private-patch-webhooks-ID.h b/src/backend/taler-merchant-httpd_private-patch-webhooks-ID.h new file mode 100644 index 00000000..c393b284 --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-patch-webhooks-ID.h @@ -0,0 +1,43 @@ +/* + This file is part of TALER + (C) 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify + it under the terms of the GNU Affero 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 taler-merchant-httpd_private-patch-webhooks-ID.h + * @brief implementing PATCH /webhooks request handling + * @author Priscilla HUANG + */ +#ifndef TALER_MERCHANT_HTTPD_PRIVATE_PATCH_WEBHOOKS_ID_H +#define TALER_MERCHANT_HTTPD_PRIVATE_PATCH_WEBHOOKS_ID_H +#include "taler-merchant-httpd.h" + + +/** + * PATCH configuration of an existing instance, given its configuration. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_patch_webhooks_ID (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc); + +#endif diff --git a/src/backend/taler-merchant-httpd_private-post-webhooks.c b/src/backend/taler-merchant-httpd_private-post-webhooks.c new file mode 100644 index 00000000..ab076924 --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-post-webhooks.c @@ -0,0 +1,214 @@ +/* + This file is part of TALER + (C) 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify + it under the terms of the GNU Affero 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 taler-merchant-httpd_private-post-webhooks.c + * @brief implementing POST /webhooks request handling + * @author Priscilla HUANG + */ +#include "platform.h" +#include "taler-merchant-httpd_private-post-webhooks.h" +#include "taler-merchant-httpd_helper.h" +#include + + +/** + * How often do we retry the simple INSERT database transaction? + */ +#define MAX_RETRIES 3 + + +/** + * Check if the two webhooks are identical. + * + * @param w1 webhook to compare + * @param w2 other webhook to compare + * @return true if they are 'equal', false if not or of payto_uris is not an array + */ +static bool +webhooks_equal (const struct TALER_MERCHANTDB_WebhookDetails *w1, + const struct TALER_MERCHANTDB_WebhookDetails *w2) +{ + return ( (0 == strcmp (w1->event_type, + w2->event_type)) && + (0 == strcmp (w1->url, + w2->url)) && + (0 == strcmp (w1->http_method, + w2->http_method)) && + (0 == strcmp (w1->header_template, + w2->header_template)) && + (0 == strcmp (w1->body_template, + w2->body_template))); +} + + +MHD_RESULT +TMH_private_post_webhooks (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) +{ + struct TMH_MerchantInstance *mi = hc->instance; + struct TALER_MERCHANTDB_WebhookDetails wb = { 0 }; + const char *webhook_id; + enum GNUNET_DB_QueryStatus qs; + struct GNUNET_JSON_Specification spec[] = { + GNUNET_JSON_spec_string ("webhook_id", + &webhook_id), + GNUNET_JSON_spec_string ("event_type", + (const char **) &wb.event_type), + GNUNET_JSON_spec_string ("url", + (const char **) &wb.url), + GNUNET_JSON_spec_string ("http_method", + (const char **) &wb.http_method), + GNUNET_JSON_spec_string ("header_template", + (const char **) &wb.header_template), + GNUNET_JSON_spec_string ("body_template", + (const char **) &wb.body_template), + GNUNET_JSON_spec_end () + }; + + GNUNET_assert (NULL != mi); + { + enum GNUNET_GenericReturnValue res; + + res = TALER_MHD_parse_json_data (connection, + hc->request_body, + spec); + if (GNUNET_OK != res) + { + GNUNET_break_op (0); + return (GNUNET_NO == res) + ? MHD_YES + : MHD_NO; + } + } + + + if (! TMH_url_valid (wb.url)) + { + GNUNET_break_op (0); + GNUNET_JSON_parse_free (spec); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_GENERIC_PARAMETER_MALFORMED, + "url"); + } + + + /* finally, interact with DB until no serialization error */ + for (unsigned int i = 0; istart (TMH_db->cls, + "/post webhooks")) + { + GNUNET_break (0); + GNUNET_JSON_parse_free (spec); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_DB_START_FAILED, + NULL); + } + qs = TMH_db->lookup_webhook (TMH_db->cls, + mi->settings.id, + webhook_id, + &ewb); + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + /* Clean up and fail hard */ + GNUNET_break (0); + TMH_db->rollback (TMH_db->cls); + GNUNET_JSON_parse_free (spec); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GENERIC_DB_FETCH_FAILED, + NULL); + case GNUNET_DB_STATUS_SOFT_ERROR: + /* restart transaction */ + goto retry; + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + /* Good, we can proceed! */ + break; + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + /* idempotency check: is ewb == wb? */ + { + bool eq; + + eq = webhooks_equal (&wb, + &ewb); + TALER_MERCHANTDB_webhook_details_free (&ewb); + TMH_db->rollback (TMH_db->cls); + GNUNET_JSON_parse_free (spec); + return eq + ? TALER_MHD_reply_static (connection, + MHD_HTTP_NO_CONTENT, + NULL, + NULL, + 0) + : TALER_MHD_reply_with_error (connection, + MHD_HTTP_CONFLICT, + TALER_EC_MERCHANT_PRIVATE_POST_WEBHOOKS_CONFLICT_WEBHOOK_EXISTS, + webhook_id); + } + } /* end switch (qs) */ + + qs = TMH_db->insert_webhook (TMH_db->cls, + mi->settings.id, + webhook_id, + &wb); + if (GNUNET_DB_STATUS_HARD_ERROR == qs) + { + TMH_db->rollback (TMH_db->cls); + break; + } + if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs) + { + qs = TMH_db->commit (TMH_db->cls); + if (GNUNET_DB_STATUS_SOFT_ERROR != qs) + break; + } +retry: + GNUNET_assert (GNUNET_DB_STATUS_SOFT_ERROR == qs); + TMH_db->rollback (TMH_db->cls); + } /* for RETRIES loop */ + GNUNET_JSON_parse_free (spec); + if (qs < 0) + { + GNUNET_break (0); + return TALER_MHD_reply_with_error ( + connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + (GNUNET_DB_STATUS_SOFT_ERROR == qs) + ? TALER_EC_GENERIC_DB_SOFT_FAILURE + : TALER_EC_GENERIC_DB_COMMIT_FAILED, + NULL); + } + return TALER_MHD_reply_static (connection, + MHD_HTTP_NO_CONTENT, + NULL, + NULL, + 0); +} + + +/* end of taler-merchant-httpd_private-post-webhooks.c */ diff --git a/src/backend/taler-merchant-httpd_private-post-webhooks.h b/src/backend/taler-merchant-httpd_private-post-webhooks.h new file mode 100644 index 00000000..24a7ccd4 --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-post-webhooks.h @@ -0,0 +1,43 @@ +/* + This file is part of TALER + (C) 2020 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify + it under the terms of the GNU Affero 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 taler-merchant-httpd_private-post-webhooks.h + * @brief implementing POST /webhooks request handling + * @author Priscilla HUANG + */ +#ifndef TALER_MERCHANT_HTTPD_PRIVATE_POST_WEBHOOKS_H +#define TALER_MERCHANT_HTTPD_PRIVATE_POST_WEBHOOKS_H +#include "taler-merchant-httpd.h" + + +/** + * Generate a webhook entry. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_post_webhooks (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc); + +#endif diff --git a/src/backenddb/merchant-0001.sql b/src/backenddb/merchant-0001.sql index a74306d1..cd4ae9f3 100644 --- a/src/backenddb/merchant-0001.sql +++ b/src/backenddb/merchant-0001.sql @@ -175,7 +175,7 @@ COMMENT ON COLUMN merchant_inventory.total_lost COMMENT ON COLUMN merchant_inventory.address IS 'JSON formatted Location of where the product is stocked'; COMMENT ON COLUMN merchant_inventory.next_restock - IS 'GNUnet absolute time indicating when the next restock is expected. 0 for unknown.'; + IS 'GNUnet absolute time i ndicating when the next restock is expected. 0 for unknown.'; COMMENT ON COLUMN merchant_inventory.minimum_age IS 'Minimum age of the customer in years, to be used if an exchange supports the age restriction extension.'; -- cgit v1.2.3