[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-donau] 01/03: [donau] work on issue receipts
From: |
gnunet |
Subject: |
[taler-donau] 01/03: [donau] work on issue receipts |
Date: |
Tue, 19 Mar 2024 01:31:04 +0100 |
This is an automated email from the git hooks/post-receive script.
lukas-matyja pushed a commit to branch master
in repository donau.
commit 6ff3a1c769589c9fcbd1c9b2abee18065a54c477
Author: Matyja Lukas Adam <lukas.matyja@students.bfh.ch>
AuthorDate: Tue Mar 19 01:17:10 2024 +0100
[donau] work on issue receipts
---
src/donau/Makefile.am | 3 +-
src/donau/donau-httpd.c | 9 +
...u-httpd_receipt.h => donau-httpd_batch-issue.h} | 14 +-
...ttpd_receipt.h => donau-httpd_issue-receipts.h} | 14 +-
src/donau/donau-httpd_post-batch-issue.c | 269 +++++++++++++++++++++
src/donau/donau-httpd_post-charity.c | 4 +-
...eceipt.c => donau-httpd_post-submit-receipts.c} | 6 +-
...tpd_receipt.h => donau-httpd_submit-receipts.h} | 6 +-
src/lib/donau_api_handle.c | 2 +-
src/testing/.donau-httpd.log.swo | Bin 0 -> 16384 bytes
src/testing/.donau-httpd.log.swp | Bin 0 -> 4096 bytes
src/testing/test_donau_api.c | 10 +-
src/testing/testing_api_cmd_get_donau.c | 2 +-
src/testing/testing_api_cmd_issue_receipts.c | 3 +-
14 files changed, 311 insertions(+), 31 deletions(-)
diff --git a/src/donau/Makefile.am b/src/donau/Makefile.am
index 285f87f..5be55a3 100644
--- a/src/donau/Makefile.am
+++ b/src/donau/Makefile.am
@@ -46,7 +46,8 @@ donau_httpd_SOURCES = \
donau-httpd_charity_delete.c \
donau-httpd_get-charity.c donau-httpd_post-charity.c \
donau-httpd_get-history.c \
- donau-httpd_post-submit-receipt.c donau_httpd_receipt.h \
+ donau-httpd_post-submit-receipts.c donau_httpd_submit-receipts.h \
+ donau-httpd_post-batch-issue.c donau_httpd_batch-issue.h \
donau-httpd_terms.c donau-httpd_terms.h
# Testcases
diff --git a/src/donau/donau-httpd.c b/src/donau/donau-httpd.c
index 2120370..54772fa 100644
--- a/src/donau/donau-httpd.c
+++ b/src/donau/donau-httpd.c
@@ -34,6 +34,7 @@
#include "donau-httpd_config.h"
#include "donau-httpd_keys.h"
#include "donau-httpd_charity.h"
+#include "donau-httpd_batch-issue.h"
#include "donau-httpd_history.h"
#include "donau-httpd_terms.h"
#include "donaudb_plugin.h"
@@ -495,6 +496,14 @@ handle_mhd_request (void *cls,
.handler.delete = &DH_handler_charity_delete,
.nargs = 1,
.nargs_is_upper_bound = true
+ },
+ /* POST batch issue receipts */
+ {
+ .url = "batch-issue",
+ .method = MHD_HTTP_METHOD_POST,
+ .handler.post = &DH_handler_issue_receipts_post,
+ .nargs = 1,
+ .nargs_is_upper_bound = true
},
/**
etc
diff --git a/src/donau/donau-httpd_receipt.h
b/src/donau/donau-httpd_batch-issue.h
similarity index 80%
copy from src/donau/donau-httpd_receipt.h
copy to src/donau/donau-httpd_batch-issue.h
index 5d6238d..053d4bd 100644
--- a/src/donau/donau-httpd_receipt.h
+++ b/src/donau/donau-httpd_batch-issue.h
@@ -14,26 +14,26 @@
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
- * @file donau-httpd_receipt.h
- * @brief Handle /submit requests
- * @author Johannes Casaburi
+ * @file donau-httpd_batch-issue.h
+ * @brief Handle /batch-issue requests
+ * @author Lukas Matyja
*/
-#ifndef DONAU_HTTPD_RECEIPT_H
-#define DONAU_HTTPD_RECEIPT_H
+#ifndef DONAU_HTTPD_ISSUE_RECEIPTS_H
+#define DONAU_HTTPD_ISSUE_RECEIPTS_H
#include <microhttpd.h>
#include "donau-httpd.h"
/**
- * Handle a POST "/submit" request.
+ * Handle a POST "/batch-issue" request.
*
* @param connection the MHD connection to handle
* @param root uploaded JSON data
* @return MHD result code
*/
MHD_RESULT
-DH_handler_submit_receipt_post (
+DH_handler_issue_receipts_post (
struct DH_RequestContext *rc,
const json_t *root,
const char *const args[]);
diff --git a/src/donau/donau-httpd_receipt.h
b/src/donau/donau-httpd_issue-receipts.h
similarity index 80%
copy from src/donau/donau-httpd_receipt.h
copy to src/donau/donau-httpd_issue-receipts.h
index 5d6238d..053d4bd 100644
--- a/src/donau/donau-httpd_receipt.h
+++ b/src/donau/donau-httpd_issue-receipts.h
@@ -14,26 +14,26 @@
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
- * @file donau-httpd_receipt.h
- * @brief Handle /submit requests
- * @author Johannes Casaburi
+ * @file donau-httpd_batch-issue.h
+ * @brief Handle /batch-issue requests
+ * @author Lukas Matyja
*/
-#ifndef DONAU_HTTPD_RECEIPT_H
-#define DONAU_HTTPD_RECEIPT_H
+#ifndef DONAU_HTTPD_ISSUE_RECEIPTS_H
+#define DONAU_HTTPD_ISSUE_RECEIPTS_H
#include <microhttpd.h>
#include "donau-httpd.h"
/**
- * Handle a POST "/submit" request.
+ * Handle a POST "/batch-issue" request.
*
* @param connection the MHD connection to handle
* @param root uploaded JSON data
* @return MHD result code
*/
MHD_RESULT
-DH_handler_submit_receipt_post (
+DH_handler_issue_receipts_post (
struct DH_RequestContext *rc,
const json_t *root,
const char *const args[]);
diff --git a/src/donau/donau-httpd_post-batch-issue.c
b/src/donau/donau-httpd_post-batch-issue.c
new file mode 100644
index 0000000..2d3b8d6
--- /dev/null
+++ b/src/donau/donau-httpd_post-batch-issue.c
@@ -0,0 +1,269 @@
+/*
+ This file is part of TALER
+ Copyright (C) 2024 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 Affero General Public License for more
details.
+
+ You should have received a copy of the GNU Affero General Public License
along with
+ TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+*/
+/**
+ * @file donau-httpd_post-batch-issue.c
+ * @brief Handle request to insert a charity.
+ * @author Lukas Matyja
+ */
+#include <taler/platform.h>
+#include <gnunet/gnunet_util_lib.h>
+#include <gnunet/gnunet_json_lib.h>
+#include <jansson.h>
+#include <microhttpd.h>
+#include <pthread.h>
+#include <taler/taler_json_lib.h>
+#include <taler/taler_mhd_lib.h>
+#include <taler/taler_signatures.h>
+#include "donaudb_plugin.h"
+#include "donau-httpd_charity.h"
+#include "donau-httpd_db.h"
+#include "donau-httpd_metrics.h"
+
+
+/**
+ * Closure for #insert_charity()
+ */
+struct IssueReceiptsContext
+{
+ struct DONAU_CharityPublicKeyP charity_pub;
+ struct DONAU_CharitySignatureP charity_sig;
+ const struct DONAU_BlindedUniqueDonationIdentifierKeyPair *bkp;
+ uint64_t year;
+};
+
+/**
+ * Parse a bkp encoded in JSON.
+ *
+ * @param[out] bkp where to return the result
+ * @param bkp_key_obj json to parse
+ * @return #GNUNET_OK if all is fine, #GNUNET_SYSERR if @a bkp_key_obj
+ * is malformed.
+ */
+static enum GNUNET_GenericReturnValue
+parse_json_bkp (struct DONAU_BlindedUniqueDonationIdentifierKeyPair *bkp,
+ const json_t *bkp_key_obj)
+{
+ struct GNUNET_JSON_Specification spec[] = {
+ GNUNET_JSON_spec_fixed_auto ("h_du_pub",
+ &bkp->h_donation_unit_pub),
+ GNUNET_JSON_spec_fixed_auto ("blinded_udi",
+ &bkp->blinded_udi),
+ GNUNET_JSON_spec_end ()
+ };
+
+ if (GNUNET_OK !=
+ GNUNET_JSON_parse (bkp_key_obj,
+ spec,
+ NULL, NULL))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ return GNUNET_OK;
+}
+
+/**
+ * Function implementing insert charity transaction.
+ *
+ * Runs the transaction logic; IF it returns a non-error code, the
+ * transaction logic MUST NOT queue a MHD response. IF it returns an hard
+ * error, the transaction logic MUST queue a MHD response and set @a mhd_ret.
+ * IF it returns the soft error code, the function MAY be called again to
+ * retry and MUST not queue a MHD response.
+ *
+ * @param cls closure with a `struct InsertCharityContext`
+ * @param connection MHD request which triggered the transaction
+ * @param[out] mhd_ret set to MHD response status for @a connection,
+ * if transaction failed (!)
+ * @return transaction status
+ */
+//static enum GNUNET_DB_QueryStatus
+//issue_receipts (void *cls,
+// struct MHD_Connection *connection,
+// MHD_RESULT *mhd_ret)
+//{
+// struct InsertCharityContext *icc = cls;
+// enum GNUNET_DB_QueryStatus qs;
+//
+// qs = DH_plugin->insert_charity (DH_plugin->cls,
+// &icc->charity_pub,
+// icc->charity_name,
+// icc->charity_url,
+// &icc->max_per_year,
+// &icc->receipts_to_date,
+// &icc->current_year,
+//
&icc->charity_id);
+// if (qs <= 0)
+// {
+// if (GNUNET_DB_STATUS_SOFT_ERROR != qs)
+// {
+// GNUNET_break (0);
+// *mhd_ret = TALER_MHD_reply_with_error (connection,
+// MHD_HTTP_INTERNAL_SERVER_ERROR,
+//
TALER_EC_GENERIC_DB_STORE_FAILED,
+// "insert_charity");
+// return GNUNET_DB_STATUS_HARD_ERROR;
+// }
+// return qs;
+// }
+//
+// return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
+//}
+
+
+MHD_RESULT
+DH_handler_issue_receipts_post (struct DH_RequestContext *rc,
+ const json_t *root,
+ const char
*const args[1])
+{
+ unsigned long long charity_id;
+ char dummy;
+
+ if ( (NULL == args[0]) ||
+ (1 != sscanf (args[0],
+ "%llu%c",
+ &charity_id,
+ &dummy)) )
+ {
+ GNUNET_break_op (0);
+ return TALER_MHD_reply_with_error (rc->connection,
+
MHD_HTTP_BAD_REQUEST,
+
TALER_EC_GENERIC_PARAMETER_MALFORMED,
+
"charity_id");
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ "issue receipts for charity id: %llu\n",
+ charity_id);
+
+ struct IssueReceiptsContext irc;
+ const json_t *budikeypairs;
+
+ struct GNUNET_JSON_Specification spec[] = {
+ GNUNET_JSON_spec_array_const ("budikeypairs",
+
&budikeypairs),
+ GNUNET_JSON_spec_fixed_auto ("charity_sig",
+ &irc.charity_sig),
+ GNUNET_JSON_spec_uint64 ("year",
+ &irc.year),
+ GNUNET_JSON_spec_end ()
+ };
+
+ {
+ enum GNUNET_GenericReturnValue res;
+
+ res = TALER_MHD_parse_json_data (rc->connection,
+ root,
+ spec);
+ if (GNUNET_SYSERR == res)
+ return MHD_NO; /* hard failure */
+ if (GNUNET_NO == res)
+ {
+ GNUNET_break_op (0);
+ return MHD_YES; /* failure */
+ }
+ }
+
+ /* parse the budikeypairs array */
+ const uint64_t num_bkp
+ = json_array_size (budikeypairs);
+ if (0 != num_bkp)
+ {
+ json_t *bkp_obj;
+ unsigned int index;
+
+ struct DONAU_BlindedUniqueDonationIdentifierKeyPair *bkp =
GNUNET_new_array (num_bkp,
+ struct DONAU_BlindedUniqueDonationIdentifierKeyPair);
+ json_array_foreach (budikeypairs, index, bkp_obj) {
+ if (GNUNET_SYSERR ==
+ parse_json_bkp(&bkp[index], bkp_obj)) {
+ return TALER_MHD_reply_with_error (rc->connection,
+ MHD_HTTP_BAD_REQUEST,
+
TALER_EC_GENERIC_PARAMETER_MALFORMED,
+ "budikeypairs");
+ }
+ }
+ irc.bkp = bkp;
+ } else {
+ return TALER_MHD_reply_with_error (rc->connection,
+ MHD_HTTP_BAD_REQUEST,
+
TALER_EC_GENERIC_PARAMETER_MALFORMED,
+ "budikeypairs");
+ }
+
+ /* verify charity signature */
+ //TODO: Get charity pub
+ if (GNUNET_OK !=
+ DONAU_charity_budi_key_pair_verify(num_bkp, &irc.bkp,
+
&irc.charity_pub,
+
&irc.charity_sig))
+ {
+ GNUNET_break_op (0);
+ return TALER_MHD_reply_with_error (
+ rc->connection,
+ MHD_HTTP_FORBIDDEN,
+ DONAU_CHARITY_SIGNATURE_INVALID,
+ NULL);
+ }
+
+ /* request already made? -> idempotent */
+
+
+ /* check annual limit and change it -> Rollback if we could not save the
request*/
+ /* save Request (charity signature, charity id, amount, hash over bkps) and
make it idempotent*/
+ /* sign budis and send the signatures back */
+
+
+// {
+// MHD_RESULT mhd_ret;
+//
+// if (GNUNET_OK !=
+// DH_DB_run_transaction (rc->connection,
+// "issue_receipts",
+// DH_MT_REQUEST_OTHER,
+// &mhd_ret,
+// &issue_receipts,
+// &irc))
+// {
+// return mhd_ret;
+// }
+// }
+//
+// GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+// "receipts were successfully issued\n");
+//
+// result = TALER_MHD_REPLY_JSON_PACK (
+// rc->connection,
+// MHD_HTTP_OK,
+// GNUNET_JSON_pack_data_auto ("charity_pub",
+// &meta.charity_pub),
+// GNUNET_JSON_pack_string ("url",
+// meta.charity_url),
+// GNUNET_JSON_pack_string ("name",
+// meta.charity_name),
+// TALER_JSON_pack_amount ("max_per_year",
+// &meta.max_per_year),
+// TALER_JSON_pack_amount ("receipts_to_date",
+// &meta.receipts_to_date),
+// GNUNET_JSON_pack_uint64 ("current_year",
+// meta.current_year));
+
+
+ MHD_RESULT result;
+ return result;
+}
+
+
+/* end of donau-httpd_post-batch-issue.c */
diff --git a/src/donau/donau-httpd_post-charity.c
b/src/donau/donau-httpd_post-charity.c
index 5bac3ae..a5b50d7 100644
--- a/src/donau/donau-httpd_post-charity.c
+++ b/src/donau/donau-httpd_post-charity.c
@@ -152,8 +152,8 @@ DH_handler_charity_post (struct DH_RequestContext *rc,
}
}
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "generated charity id: %lu\n",
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ "generated charity id: %lu\n",
icc.charity_id);
return TALER_MHD_REPLY_JSON_PACK (
diff --git a/src/donau/donau-httpd_post-submit-receipt.c
b/src/donau/donau-httpd_post-submit-receipts.c
similarity index 97%
rename from src/donau/donau-httpd_post-submit-receipt.c
rename to src/donau/donau-httpd_post-submit-receipts.c
index cfe8f4f..4722235 100644
--- a/src/donau/donau-httpd_post-submit-receipt.c
+++ b/src/donau/donau-httpd_post-submit-receipts.c
@@ -14,7 +14,7 @@
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
- * @file donau-httpd_post-submit-receipt.c
+ * @file donau-httpd_post-submit-receipts.c
* @brief Handle request to insert a submitted receipt.
* @author Johannes Casaburi
*/
@@ -28,7 +28,7 @@
#include "taler/taler_mhd_lib.h"
#include "taler/taler_signatures.h"
#include "donaudb_plugin.h"
-#include "donau-httpd_receipt.h"
+#include "donau-httpd_submit-receipts.h"
#include "donau-httpd_db.h"
#include "donau-httpd_metrics.h"
@@ -152,4 +152,4 @@ DH_handler_submit_receipt_post (struct DH_RequestContext
*rc,
}
-/* end of donau-httpd_post-submit-receipt.c */
+/* end of donau-httpd_post-submit-receipts.c */
diff --git a/src/donau/donau-httpd_receipt.h
b/src/donau/donau-httpd_submit-receipts.h
similarity index 90%
rename from src/donau/donau-httpd_receipt.h
rename to src/donau/donau-httpd_submit-receipts.h
index 5d6238d..8d93f95 100644
--- a/src/donau/donau-httpd_receipt.h
+++ b/src/donau/donau-httpd_submit-receipts.h
@@ -14,12 +14,12 @@
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
- * @file donau-httpd_receipt.h
+ * @file donau-httpd_submit-receipts.h
* @brief Handle /submit requests
* @author Johannes Casaburi
*/
-#ifndef DONAU_HTTPD_RECEIPT_H
-#define DONAU_HTTPD_RECEIPT_H
+#ifndef DONAU_HTTPD_SUBMIT_RECEIPT_H
+#define DONAU_HTTPD_SUBMIT_RECEIPT_H
#include <microhttpd.h>
#include "donau-httpd.h"
diff --git a/src/lib/donau_api_handle.c b/src/lib/donau_api_handle.c
index cc06634..0381c3d 100644
--- a/src/lib/donau_api_handle.c
+++ b/src/lib/donau_api_handle.c
@@ -265,7 +265,7 @@ decode_keys_json (const json_t *resp_obj,
const char *currency;
struct GNUNET_JSON_Specification mspec[] = {
GNUNET_JSON_spec_array_const (
- "sign_keys", // naming convention?
+ "sign_keys",
&sign_keys_array),
GNUNET_JSON_spec_string (
"currency",
diff --git a/src/testing/.donau-httpd.log.swo b/src/testing/.donau-httpd.log.swo
new file mode 100644
index 0000000..2f78513
Binary files /dev/null and b/src/testing/.donau-httpd.log.swo differ
diff --git a/src/testing/.donau-httpd.log.swp b/src/testing/.donau-httpd.log.swp
new file mode 100644
index 0000000..ac7077e
Binary files /dev/null and b/src/testing/.donau-httpd.log.swp differ
diff --git a/src/testing/test_donau_api.c b/src/testing/test_donau_api.c
index e62f313..208da36 100644
--- a/src/testing/test_donau_api.c
+++ b/src/testing/test_donau_api.c
@@ -72,7 +72,7 @@ run (void *cls,
TALER_TESTING_cmd_system_start ("start-donau",
config_file,
"-D",
- NULL),
+ "NULL"),
TALER_TESTING_cmd_get_donau ("get-donau",
cred.cfg,
true),
@@ -91,10 +91,10 @@ run (void *cls,
TALER_TESTING_cmd_charities_get ("get-charities",
&bearer,
MHD_HTTP_OK),
- TALER_TESTING_cmd_issue_receipts ("issue-receipts",
-
"post-charity",
- 2024,
-
MHD_HTTP_CREATED),
+// TALER_TESTING_cmd_issue_receipts ("issue-receipts",
+//
"post-charity",
+// 2024,
+//
MHD_HTTP_CREATED),
TALER_TESTING_cmd_charity_delete("delete-charity",
"post-charity", // cmd trait reference
&bearer,
diff --git a/src/testing/testing_api_cmd_get_donau.c
b/src/testing/testing_api_cmd_get_donau.c
index 17f28da..3d1d736 100644
--- a/src/testing/testing_api_cmd_get_donau.c
+++ b/src/testing/testing_api_cmd_get_donau.c
@@ -167,7 +167,7 @@ get_donau_cleanup (void *cls,
}
DONAU_keys_decref (ges->keys);
ges->keys = NULL;
- //GNUNET_free (ges->donau_url);
+ GNUNET_free (ges->donau_url);
ges->donau_url = NULL;
GNUNET_free (ges);
}
diff --git a/src/testing/testing_api_cmd_issue_receipts.c
b/src/testing/testing_api_cmd_issue_receipts.c
index 7e860a6..a9bf79a 100644
--- a/src/testing/testing_api_cmd_issue_receipts.c
+++ b/src/testing/testing_api_cmd_issue_receipts.c
@@ -148,7 +148,8 @@ status_run (void *cls,
ss->charity_id = (uint64_t) *(charity_id);
}
- // TODO: build bkps with traits from /keys request
+ // TODO: build bkps with traits from /keys request, use the public sign key
from the
+ // DONAU to verify the signatures
ss->birh = DONAU_charity_issue_receipt (
TALER_TESTING_interpreter_get_context (is),
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.