aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Blättler <blatc2@bfh.ch>2024-06-06 16:18:53 +0200
committerChristian Blättler <blatc2@bfh.ch>2024-06-06 16:18:53 +0200
commit3031310580edd755c1790e664fc3b400a048f154 (patch)
treef21c81d28c50c1f8151afde3a4a2cd81b22ee47d
parentb1a3c1cb80bc63807b33ec9435e7313fefd3ae09 (diff)
store issued tokens in database
-rw-r--r--src/backend/taler-merchant-httpd_post-orders-ID-pay.c32
-rw-r--r--src/backenddb/Makefile.am2
-rw-r--r--src/backenddb/merchant-0007.sql2
-rw-r--r--src/backenddb/merchant-0008.sql47
-rw-r--r--src/backenddb/pg_insert_issued_token.c60
-rw-r--r--src/backenddb/pg_insert_issued_token.h42
-rw-r--r--src/backenddb/plugin_merchantdb_postgres.c4
-rw-r--r--src/include/taler_merchantdb_plugin.h16
8 files changed, 201 insertions, 4 deletions
diff --git a/src/backend/taler-merchant-httpd_post-orders-ID-pay.c b/src/backend/taler-merchant-httpd_post-orders-ID-pay.c
index 948789cb..7fa93404 100644
--- a/src/backend/taler-merchant-httpd_post-orders-ID-pay.c
+++ b/src/backend/taler-merchant-httpd_post-orders-ID-pay.c
@@ -2078,13 +2078,13 @@ phase_execute_pay_transaction (struct PayContext *pc)
pay_end (pc,
TALER_MHD_reply_with_error (pc->connection,
MHD_HTTP_INTERNAL_SERVER_ERROR,
- TALER_EC_GENERIC_DB_FETCH_FAILED,
+ TALER_EC_GENERIC_DB_STORE_FAILED,
"insert used token"));
return;
}
else if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
{
- /* UNIQUE constraint violation, meaining this token was already used. */
+ /* UNIQUE constraint violation, meaning this token was already used. */
pay_end (pc,
TALER_MHD_reply_with_error (pc->connection,
MHD_HTTP_CONFLICT,
@@ -2211,7 +2211,33 @@ phase_execute_pay_transaction (struct PayContext *pc)
}
}
- /* TODO: Store signed output tokens in database. */
+ /* Store signed output tokens in database. */
+ for (size_t i = 0; i<pc->output_tokens_len; i++)
+ {
+ struct SignedOutputToken *output = &pc->output_tokens[i];
+
+ enum GNUNET_DB_QueryStatus qs;
+
+ qs = TMH_db->insert_issued_token (TMH_db->cls,
+ &pc->h_contract_terms,
+ &output->h_issue,
+ &output->sig);
+
+ if (0 >= qs)
+ {
+ TMH_db->rollback (TMH_db->cls);
+ if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
+ return; /* do it again */
+ /* Always report on hard error as well to enable diagnostics */
+ GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
+ pay_end (pc,
+ TALER_MHD_reply_with_error (pc->connection,
+ MHD_HTTP_INTERNAL_SERVER_ERROR,
+ TALER_EC_GENERIC_DB_STORE_FAILED,
+ "insert output token"));
+ return;
+ }
+ }
TMH_notify_order_change (hc->instance,
TMH_OSF_CLAIMED | TMH_OSF_PAID,
diff --git a/src/backenddb/Makefile.am b/src/backenddb/Makefile.am
index a05936da..9612beec 100644
--- a/src/backenddb/Makefile.am
+++ b/src/backenddb/Makefile.am
@@ -24,6 +24,7 @@ sql_DATA = \
merchant-0005.sql \
merchant-0006.sql \
merchant-0007.sql \
+ merchant-0008.sql \
drop.sql
BUILT_SOURCES = \
@@ -178,6 +179,7 @@ libtaler_plugin_merchantdb_postgres_la_SOURCES = \
pg_insert_token_family_key.h pg_insert_token_family_key.c \
pg_lookup_token_family_key.h pg_lookup_token_family_key.c \
pg_insert_spent_token.h pg_insert_spent_token.c \
+ pg_insert_issued_token.h pg_insert_issued_token.c \
plugin_merchantdb_postgres.c \
pg_helper.h pg_helper.c
libtaler_plugin_merchantdb_postgres_la_LIBADD = \
diff --git a/src/backenddb/merchant-0007.sql b/src/backenddb/merchant-0007.sql
index 9018669c..36cd1550 100644
--- a/src/backenddb/merchant-0007.sql
+++ b/src/backenddb/merchant-0007.sql
@@ -14,7 +14,7 @@
-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
--
--- @file merchant-0006.sql
+-- @file merchant-0007.sql
-- @brief alter length check of public key hash in merchant_token_family_keys
-- @author Christian Blättler
diff --git a/src/backenddb/merchant-0008.sql b/src/backenddb/merchant-0008.sql
new file mode 100644
index 00000000..bf50d4d6
--- /dev/null
+++ b/src/backenddb/merchant-0008.sql
@@ -0,0 +1,47 @@
+--
+-- 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 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 <http://www.gnu.org/licenses/>
+--
+
+-- @file merchant-0008.sql
+-- @brief add merchant_issued_tokens table
+-- @author Christian Blättler
+
+-- Everything in one big transaction
+BEGIN;
+
+-- Check patch versioning is in place.
+SELECT _v.register_patch('merchant-0008', NULL, NULL);
+
+SET search_path TO merchant;
+
+
+CREATE TABLE IF NOT EXISTS merchant_issued_tokens
+ (issued_token_serial BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
+ ,h_contract_terms BYTEA NOT NULL CHECK (LENGTH(h_contract_terms)=64)
+ ,token_family_key_serial BIGINT REFERENCES merchant_token_family_keys(token_family_key_serial) ON DELETE CASCADE
+ ,blind_sig BYTEA NOT NULL
+ );
+COMMENT ON TABLE merchant_spent_tokens
+ IS 'Tokens that have been (blindly) issued to customers.';
+COMMENT ON COLUMN merchant_spent_tokens.h_contract_terms
+ IS 'This is no foreign key by design.';
+COMMENT ON COLUMN merchant_spent_tokens.token_family_key_serial
+ IS 'Token family key to which the spent token belongs.';
+COMMENT ON COLUMN merchant_spent_tokens.blind_sig
+ IS 'Blind signature made with token issue key to prove validity of token.';
+
+
+-- Complete transaction
+COMMIT;
diff --git a/src/backenddb/pg_insert_issued_token.c b/src/backenddb/pg_insert_issued_token.c
new file mode 100644
index 00000000..2523d427
--- /dev/null
+++ b/src/backenddb/pg_insert_issued_token.c
@@ -0,0 +1,60 @@
+/*
+ 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 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 <http://www.gnu.org/licenses/>
+ */
+/**
+ * @file backenddb/pg_insert_issued_token.c
+ * @brief Implementation of the insert_issued_token function for Postgres
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include <taler/taler_error_codes.h>
+#include <taler/taler_dbevents.h>
+#include <taler/taler_pq_lib.h>
+#include "pg_insert_issued_token.h"
+#include "pg_helper.h"
+
+enum GNUNET_DB_QueryStatus
+TMH_PG_insert_issued_token (void *cls,
+ const struct TALER_PrivateContractHashP *h_contract_terms,
+ const struct TALER_TokenIssuePublicKeyHashP *h_issue_pub,
+ const struct TALER_TokenIssueBlindSignatureP *blind_sig)
+{
+ struct PostgresClosure *pg = cls;
+
+ struct GNUNET_PQ_QueryParam params[] = {
+ GNUNET_PQ_query_param_auto_from_type (h_issue_pub),
+ GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
+ GNUNET_PQ_query_param_blinded_sig (blind_sig->signature),
+ GNUNET_PQ_query_param_end
+ };
+
+ check_connection (pg);
+ PREPARE (pg,
+ "issued_token_insert",
+ "INSERT INTO merchant_issued_tokens"
+ "(token_family_key_serial"
+ ",h_contract_terms"
+ ",blind_sig)"
+ " SELECT token_family_key_serial, $2, $3"
+ " FROM merchant_token_families"
+ " JOIN merchant_token_family_keys"
+ " USING (token_family_serial)"
+ " WHERE h_pub = $1");
+ return GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "issued_token_insert",
+ params);
+
+
+} \ No newline at end of file
diff --git a/src/backenddb/pg_insert_issued_token.h b/src/backenddb/pg_insert_issued_token.h
new file mode 100644
index 00000000..a65fc16d
--- /dev/null
+++ b/src/backenddb/pg_insert_issued_token.h
@@ -0,0 +1,42 @@
+/*
+ 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 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 <http://www.gnu.org/licenses/>
+ */
+/**
+ * @file backenddb/pg_insert_issued_token.h
+ * @brief implementation of the insert_issued_token function for Postgres
+ * @author Christian Blättler
+ */
+#ifndef PG_INSERT_ISSUED_TOKEN_H
+#define PG_INSERT_ISSUED_TOKEN_H
+
+#include <taler/taler_util.h>
+#include <taler/taler_json_lib.h>
+#include "taler_merchantdb_plugin.h"
+
+
+/**
+ * @param cls closure
+ * @param h_contract_terms hash of the contract the token was issued for
+ * @param h_issue_pub hash of the token issue public key used to sign the issued token
+ * @param blind_sig resulting blind token issue signature
+ * @return database result code
+ */
+enum GNUNET_DB_QueryStatus
+TMH_PG_insert_issued_token (void *cls,
+ const struct TALER_PrivateContractHashP *h_contract_terms,
+ const struct TALER_TokenIssuePublicKeyHashP *h_issue_pub,
+ const struct TALER_TokenIssueBlindSignatureP *blind_sig);
+
+#endif
diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c
index 4016a5fa..16e40ac5 100644
--- a/src/backenddb/plugin_merchantdb_postgres.c
+++ b/src/backenddb/plugin_merchantdb_postgres.c
@@ -141,6 +141,7 @@
#include "pg_insert_token_family_key.h"
#include "pg_lookup_token_family_key.h"
#include "pg_insert_spent_token.h"
+#include "pg_insert_issued_token.h"
/**
@@ -586,6 +587,9 @@ libtaler_plugin_merchantdb_postgres_init (void *cls)
= &TMH_PG_update_deposit_confirmation_status;
plugin->insert_spent_token
= &TMH_PG_insert_spent_token;
+ plugin->insert_issued_token
+ = &TMH_PG_insert_issued_token;
+
diff --git a/src/include/taler_merchantdb_plugin.h b/src/include/taler_merchantdb_plugin.h
index 46b90af3..329975fa 100644
--- a/src/include/taler_merchantdb_plugin.h
+++ b/src/include/taler_merchantdb_plugin.h
@@ -2391,6 +2391,22 @@ struct TALER_MERCHANTDB_Plugin
/**
+ * Insert issued token into the database.
+ *
+ * @param cls closure
+ * @param h_contract_terms hash of the contract the token was issued for
+ * @param h_issue_pub hash of the token issue public key used to sign the issued token
+ * @param blind_sig resulting blind token issue signature
+ * @return database result code
+ */
+ enum GNUNET_DB_QueryStatus
+ (*insert_issued_token) (void *cls,
+ const struct TALER_PrivateContractHashP *h_contract_terms,
+ const struct TALER_TokenIssuePublicKeyHashP *h_issue_pub,
+ const struct TALER_TokenIssueBlindSignatureP *blind_sig);
+
+
+ /**
* Lookup refund proof data.
*
* @param cls closure