From e6cd3507114e625f89d8d2383fd7a88b21d22b97 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 20 May 2020 17:34:55 +0200 Subject: more work on tipping implementation --- src/backend/taler-merchant-httpd.c | 8 +- .../taler-merchant-httpd_post-tips-ID-pickup.c | 52 ++++----- .../taler-merchant-httpd_post-tips-ID-pickup.h | 48 ++------ .../taler-merchant-httpd_private-get-tips-ID.c | 124 +++++++++++++++++++++ .../taler-merchant-httpd_private-get-tips-ID.h | 41 +++++++ ...-httpd_private-post-reserves-ID-authorize-tip.c | 8 +- ...-httpd_private-post-reserves-ID-authorize-tip.h | 8 +- 7 files changed, 209 insertions(+), 80 deletions(-) create mode 100644 src/backend/taler-merchant-httpd_private-get-tips-ID.c create mode 100644 src/backend/taler-merchant-httpd_private-get-tips-ID.h (limited to 'src/backend') diff --git a/src/backend/taler-merchant-httpd.c b/src/backend/taler-merchant-httpd.c index 7f063909..b2577bd2 100644 --- a/src/backend/taler-merchant-httpd.c +++ b/src/backend/taler-merchant-httpd.c @@ -40,6 +40,7 @@ #include "taler-merchant-httpd_private-get-orders.h" // #include "taler-merchant-httpd_private-get-orders-ID.h" #include "taler-merchant-httpd_private-get-reserves.h" +#include "taler-merchant-httpd_private-get-tips-ID.h" #include "taler-merchant-httpd_private-get-transfers.h" #include "taler-merchant-httpd_private-patch-instances-ID.h" #include "taler-merchant-httpd_private-patch-products-ID.h" @@ -858,12 +859,11 @@ url_handler (void *cls, .method = MHD_HTTP_METHOD_POST, .handler = &TMH_private_post_reserves_ID_authorize_tip }, - /* POST /reserves/authorize-tip: */ + /* POST /tips: */ { - .url_prefix = "/reserves", - .url_suffix = "authorize-tip", + .url_prefix = "/tips", .method = MHD_HTTP_METHOD_POST, - .handler = &TMH_private_post_reserves_authorize_tip + .handler = &TMH_private_post_tips }, /* GET /reserves: */ { diff --git a/src/backend/taler-merchant-httpd_post-tips-ID-pickup.c b/src/backend/taler-merchant-httpd_post-tips-ID-pickup.c index 42066e3c..44258343 100644 --- a/src/backend/taler-merchant-httpd_post-tips-ID-pickup.c +++ b/src/backend/taler-merchant-httpd_post-tips-ID-pickup.c @@ -14,8 +14,8 @@ TALER; see the file COPYING. If not, see */ /** - * @file backend/taler-merchant-httpd_tip-pickup.c - * @brief implementation of /tip-pickup handler + * @file backend/taler-merchant-httpd_post-tips-ID-pickup.c + * @brief implementation of a POST /tips/ID/pickup handler * @author Christian Grothoff */ #include "platform.h" @@ -30,28 +30,30 @@ /** - * Manages a GET /tip-pickup call, checking that the tip is authorized, - * and if so, returning the withdrawal permissions. + * We are shutting down, force resuming all suspended pickup operations. + */ +void +TMH_force_tip_pickup_resume () +{ + // FIXME! +} + + +/** + * Manages a POST /tips/$ID/pickup call, checking that the tip is authorized, + * and if so, returning the blind signatures. * * @param rh context of the handler * @param connection the MHD connection to handle - * @param[in,out] connection_cls the connection's closure (can be updated) - * @param upload_data upload data - * @param[in,out] upload_data_size number of bytes (left) in @a upload_data - * @param mi merchant backend instance, never NULL + * @param[in,out] hc context with further information about the request * @return MHD result code */ MHD_RESULT -MH_handler_tip_pickup_get (struct TMH_RequestHandler *rh, - struct MHD_Connection *connection, - void **connection_cls, - const char *upload_data, - size_t *upload_data_size, - struct MerchantInstance *mi) +TMH_post_tips_ID_pickup (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) { - const char *tip_id_str; char *exchange_url; - json_t *extra; struct GNUNET_HashCode tip_id; struct TALER_Amount tip_amount; struct TALER_Amount tip_amount_left; @@ -60,22 +62,9 @@ MH_handler_tip_pickup_get (struct TMH_RequestHandler *rh, MHD_RESULT ret; enum GNUNET_DB_QueryStatus qs; - tip_id_str = MHD_lookup_connection_value (connection, - MHD_GET_ARGUMENT_KIND, - "tip_id"); - - if (NULL == tip_id_str) - { - /* tip_id is required but missing */ - GNUNET_break_op (0); - return TALER_MHD_reply_with_error (connection, - MHD_HTTP_BAD_REQUEST, - TALER_EC_PARAMETER_MISSING, - "tip_id required"); - } - + GNUNET_assert (NULL != hc->infix); if (GNUNET_OK != - GNUNET_CRYPTO_hash_from_string (tip_id_str, + GNUNET_CRYPTO_hash_from_string (hc->infix, &tip_id)) { /* tip_id has wrong encoding */ @@ -87,6 +76,7 @@ MH_handler_tip_pickup_get (struct TMH_RequestHandler *rh, } db->preflight (db->cls); + // FIXME: logic here is completely bonkers! qs = db->lookup_tip_by_id (db->cls, &tip_id, &exchange_url, diff --git a/src/backend/taler-merchant-httpd_post-tips-ID-pickup.h b/src/backend/taler-merchant-httpd_post-tips-ID-pickup.h index 6fdba31a..f8ac486e 100644 --- a/src/backend/taler-merchant-httpd_post-tips-ID-pickup.h +++ b/src/backend/taler-merchant-httpd_post-tips-ID-pickup.h @@ -14,12 +14,12 @@ TALER; see the file COPYING. If not, see */ /** - * @file backend/taler-merchant-httpd_tip-pickup.h - * @brief headers for /tip-pickup handler + * @file backend/taler-merchant-httpd_post-tips-ID-pickup.h + * @brief headers for POST /tips/ID/pickup handler * @author Christian Grothoff */ -#ifndef TALER_MERCHANT_HTTPD_TIP_PICKUP_H -#define TALER_MERCHANT_HTTPD_TIP_PICKUP_H +#ifndef TALER_MERCHANT_HTTPD_POST_TIPS_ID_PICKUP_H +#define TALER_MERCHANT_HTTPD_POST_TIPS_ID_PICKUP_H #include #include "taler-merchant-httpd.h" @@ -28,48 +28,22 @@ * We are shutting down, force resuming all suspended pickup operations. */ void -MH_force_tip_pickup_resume (void); +TMH_force_tip_pickup_resume (void); /** - * Manages a POST /tip-pickup call, checking that the tip is authorized, - * and if so, returning the withdrawal permissions. + * Manages a POST /tips/$ID/pickup call, checking that the tip is authorized, + * and if so, returning the blind signatures. * * @param rh context of the handler * @param connection the MHD connection to handle - * @param[in,out] connection_cls the connection's closure (can be updated) - * @param upload_data upload data - * @param[in,out] upload_data_size number of bytes (left) in @a upload_data -* @param mi merchant backend instance, never NULL + * @param[in,out] hc context with further information about the request * @return MHD result code */ MHD_RESULT -MH_handler_tip_pickup (struct TMH_RequestHandler *rh, - struct MHD_Connection *connection, - void **connection_cls, - const char *upload_data, - size_t *upload_data_size, - struct MerchantInstance *mi); - - -/** - * Manages a GET /tip-pickup call. - * - * @param rh context of the handler - * @param connection the MHD connection to handle - * @param[in,out] connection_cls the connection's closure (can be updated) - * @param upload_data upload data - * @param[in,out] upload_data_size number of bytes (left) in @a upload_data - * @param mi merchant backend instance, never NULL - * @return MHD result code - */ -MHD_RESULT -MH_handler_tip_pickup_get (struct TMH_RequestHandler *rh, - struct MHD_Connection *connection, - void **connection_cls, - const char *upload_data, - size_t *upload_data_size, - struct MerchantInstance *mi); +TMH_post_tips_ID_pickup (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc); #endif diff --git a/src/backend/taler-merchant-httpd_private-get-tips-ID.c b/src/backend/taler-merchant-httpd_private-get-tips-ID.c new file mode 100644 index 00000000..f186363d --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-get-tips-ID.c @@ -0,0 +1,124 @@ +/* + This file is part of TALER + (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 backend/taler-merchant-httpd_get-tips-ID.c + * @brief implementation of a GET /tips/ID handler + * @author Christian Grothoff + */ +#include "platform.h" +#include +#include +#include +#include +#include "taler-merchant-httpd.h" +#include "taler-merchant-httpd_mhd.h" +#include "taler-merchant-httpd_exchanges.h" +#include "taler-merchant-httpd_tip-pickup.h" + + +/** + * Manages a GET /tips/$ID call, returning the status of a tip. + * + * @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_tips_ID (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) +{ + char *exchange_url; + struct GNUNET_HashCode tip_id; + struct TALER_Amount tip_amount; + struct TALER_Amount tip_amount_left; + struct GNUNET_TIME_Absolute timestamp; + struct GNUNET_TIME_Absolute timestamp_expire; + MHD_RESULT ret; + enum GNUNET_DB_QueryStatus qs; + + GNUNET_assert (NULL != hc->infix); + if (GNUNET_OK != + GNUNET_CRYPTO_hash_from_string (hc->infix, + &tip_id)) + { + /* tip_id has wrong encoding */ + GNUNET_break_op (0); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_PARAMETER_MALFORMED, + "tip_id malformed"); + } + + db->preflight (db->cls); + // FIXME: logic here is completely bonkers! + qs = db->lookup_tip_by_id (db->cls, + &tip_id, + &exchange_url, + &extra, + &tip_amount, + &tip_amount_left, + ×tamp); + + if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs) + { + unsigned int response_code; + enum TALER_ErrorCode ec; + + switch (qs) + { + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + ec = TALER_EC_TIP_PICKUP_TIP_ID_UNKNOWN; + response_code = MHD_HTTP_NOT_FOUND; + break; + case GNUNET_DB_STATUS_SOFT_ERROR: + ec = TALER_EC_TIP_PICKUP_DB_ERROR_SOFT; + response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; + break; + case GNUNET_DB_STATUS_HARD_ERROR: + ec = TALER_EC_TIP_PICKUP_DB_ERROR_HARD; + response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; + break; + default: + GNUNET_break (0); + ec = TALER_EC_INTERNAL_LOGIC_ERROR; + response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; + break; + } + return TALER_MHD_reply_with_error (connection, + response_code, + ec, + "Could not determine exchange URL for the given tip id"); + } + + timestamp_expire = GNUNET_TIME_absolute_add (timestamp, + GNUNET_TIME_UNIT_DAYS); + + ret = TALER_MHD_reply_json_pack ( + connection, + MHD_HTTP_OK, + "{s:s, s:o, s:o, s:o, s:o, s:o?}", + "reason", reason, + "total_authorized", TALER_JSON_from_amount (&tip_amount), + "total_picked_up", TALER_JSON_from_amount (&tip_amount_left), + "reserve_pub", GNUNET_JSON_from_data_auto (&reserve_pub), + "expiration", GNUNET_JSON_from_time_abs (timestamp_expire), + "pickups", pickups); + + GNUNET_free (exchange_url); + return ret; +} diff --git a/src/backend/taler-merchant-httpd_private-get-tips-ID.h b/src/backend/taler-merchant-httpd_private-get-tips-ID.h new file mode 100644 index 00000000..bc594690 --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-get-tips-ID.h @@ -0,0 +1,41 @@ +/* + This file is part of TALER + (C) 2017 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 backend/taler-merchant-httpd_get-tips-ID.h + * @brief headers for GET /tips/ID handler + * @author Christian Grothoff + */ +#ifndef TALER_MERCHANT_HTTPD_GET_TIPS_ID_H +#define TALER_MERCHANT_HTTPD_GET_TIPS_ID_H +#include +#include "taler-merchant-httpd.h" + + +/** + * Manages a GET /tips/$ID call, returning the status of the tip. + * + * @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_tips_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-reserves-ID-authorize-tip.c b/src/backend/taler-merchant-httpd_private-post-reserves-ID-authorize-tip.c index 91936699..0b363cfb 100644 --- a/src/backend/taler-merchant-httpd_private-post-reserves-ID-authorize-tip.c +++ b/src/backend/taler-merchant-httpd_private-post-reserves-ID-authorize-tip.c @@ -199,7 +199,7 @@ TMH_private_post_reserves_ID_authorize_tip (const struct TMH_RequestHandler *rh, /** - * Handle a "/reserves/tip-authorize" request. + * Handle a POST "/tips" request. * Here the client does not specify the reserve public key, so we * are free to pick "any" available reserve. * @@ -209,9 +209,9 @@ TMH_private_post_reserves_ID_authorize_tip (const struct TMH_RequestHandler *rh, * @return MHD result code */ MHD_RESULT -TMH_private_post_reserves_authorize_tip (const struct TMH_RequestHandler *rh, - struct MHD_Connection *connection, - struct TMH_HandlerContext *hc) +TMH_private_post_tips (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) { return authorize_tip (rh, connection, diff --git a/src/backend/taler-merchant-httpd_private-post-reserves-ID-authorize-tip.h b/src/backend/taler-merchant-httpd_private-post-reserves-ID-authorize-tip.h index 2df55295..297a53d1 100644 --- a/src/backend/taler-merchant-httpd_private-post-reserves-ID-authorize-tip.h +++ b/src/backend/taler-merchant-httpd_private-post-reserves-ID-authorize-tip.h @@ -39,7 +39,7 @@ TMH_private_post_reserves_ID_authorize_tip (const struct TMH_RequestHandler *rh, /** - * Handle a "/reserves/tip-authorize" request. + * Handle a POST "/tips" request. * Here the client does not specify the reserve public key, so we * are free to pick "any" available reserve. * @@ -49,9 +49,9 @@ TMH_private_post_reserves_ID_authorize_tip (const struct TMH_RequestHandler *rh, * @return MHD result code */ MHD_RESULT -TMH_private_post_reserves_authorize_tip (const struct TMH_RequestHandler *rh, - struct MHD_Connection *connection, - struct TMH_HandlerContext *hc); +TMH_private_post_tips (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc); #endif -- cgit v1.2.3