summaryrefslogtreecommitdiff
path: root/src/backenddb/plugin_merchantdb_postgres.c
diff options
context:
space:
mode:
authorChristian Grothoff <grothoff@gnunet.org>2023-09-01 14:27:48 +0200
committerChristian Grothoff <grothoff@gnunet.org>2023-09-01 14:27:48 +0200
commit35dcd4514a93ba0f5353ecd1194fc9b515f2aad4 (patch)
tree399d8f8fbebf5e8ac383fc318b7be08901279a31 /src/backenddb/plugin_merchantdb_postgres.c
parent858e3047b8b595ab693e16ce0bbe0b8983b072ed (diff)
downloadmerchant-35dcd4514a93ba0f5353ecd1194fc9b515f2aad4.tar.gz
merchant-35dcd4514a93ba0f5353ecd1194fc9b515f2aad4.tar.bz2
merchant-35dcd4514a93ba0f5353ecd1194fc9b515f2aad4.zip
new CRUD APIs for OTP devices and merchant accounts (fixes #7929, #7824), one minor test is still failing...
Diffstat (limited to 'src/backenddb/plugin_merchantdb_postgres.c')
-rw-r--r--src/backenddb/plugin_merchantdb_postgres.c377
1 files changed, 39 insertions, 338 deletions
diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c
index 70a3e644..c4074c98 100644
--- a/src/backenddb/plugin_merchantdb_postgres.c
+++ b/src/backenddb/plugin_merchantdb_postgres.c
@@ -31,6 +31,11 @@
#include <taler/taler_mhd_lib.h>
#include "taler_merchantdb_plugin.h"
#include "pg_helper.h"
+#include "pg_insert_otp.h"
+#include "pg_delete_otp.h"
+#include "pg_update_otp.h"
+#include "pg_select_otp.h"
+#include "pg_select_otp_serial.h"
#include "pg_insert_account.h"
#include "pg_update_account.h"
#include "pg_lookup_instances.h"
@@ -43,6 +48,7 @@
#include "pg_insert_exchange_account.h"
#include "pg_lookup_reserves.h"
#include "pg_lookup_instance_auth.h"
+#include "pg_lookup_otp_devices.h"
#include "pg_update_transfer_status.h"
#include "pg_insert_instance.h"
#include "pg_account_kyc_set_status.h"
@@ -72,6 +78,11 @@
#include "pg_insert_contract_terms.h"
#include "pg_update_contract_terms.h"
#include "pg_delete_contract_terms.h"
+#include "pg_delete_template.h"
+#include "pg_insert_template.h"
+#include "pg_update_template.h"
+#include "pg_lookup_templates.h"
+#include "pg_lookup_template.h"
#include "pg_lookup_deposits.h"
#include "pg_insert_exchange_signkey.h"
#include "pg_insert_deposit.h"
@@ -86,6 +97,8 @@
#include "pg_select_exchange_keys.h"
#include "pg_insert_deposit_to_transfer.h"
#include "pg_increase_refund.h"
+#include "pg_select_account.h"
+#include "pg_select_accounts.h"
#include "pg_insert_transfer.h"
#include "pg_insert_transfer_details.h"
#include "pg_store_wire_fee_by_exchange.h"
@@ -2309,283 +2322,6 @@ postgres_insert_pickup_blind_signature (
/**
- * Delete information about a template.
- *
- * @param cls closure
- * @param instance_id instance to delete template of
- * @param template_id template to delete
- * @return DB status code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
- * if template unknown.
- */
-static enum GNUNET_DB_QueryStatus
-postgres_delete_template (void *cls,
- const char *instance_id,
- const char *template_id)
-{
- struct PostgresClosure *pg = cls;
- struct GNUNET_PQ_QueryParam params[] = {
- GNUNET_PQ_query_param_string (instance_id),
- GNUNET_PQ_query_param_string (template_id),
- GNUNET_PQ_query_param_end
- };
-
- check_connection (pg);
- return GNUNET_PQ_eval_prepared_non_select (pg->conn,
- "delete_template",
- params);
-}
-
-
-/**
- * Insert details about a particular template.
- *
- * @param cls closure
- * @param instance_id instance to insert template for
- * @param template_id template identifier of template to insert
- * @param td the template details to insert
- * @return database result code
- */
-static enum GNUNET_DB_QueryStatus
-postgres_insert_template (void *cls,
- const char *instance_id,
- const char *template_id,
- const struct TALER_MERCHANTDB_TemplateDetails *td)
-{
- struct PostgresClosure *pg = cls;
- uint32_t pos32 = (uint32_t) td->pos_algorithm;
- struct GNUNET_PQ_QueryParam params[] = {
- GNUNET_PQ_query_param_string (instance_id),
- GNUNET_PQ_query_param_string (template_id),
- GNUNET_PQ_query_param_string (td->template_description),
- (NULL == td->pos_key)
- ? GNUNET_PQ_query_param_null ()
- : GNUNET_PQ_query_param_string (td->pos_key),
- GNUNET_PQ_query_param_uint32 (&pos32),
- TALER_PQ_query_param_json (td->template_contract),
- GNUNET_PQ_query_param_end
- };
-
- check_connection (pg);
- return GNUNET_PQ_eval_prepared_non_select (pg->conn,
- "insert_template",
- params);
-}
-
-
-/**
- * Update details about a particular template.
- *
- * @param cls closure
- * @param instance_id instance to update template for
- * @param template_id template to update
- * @param td update to the template details on success, can be NULL
- * (in that case we only want to check if the template exists)
- * @return database result code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if the template
- * does not yet exist.
- */
-static enum GNUNET_DB_QueryStatus
-postgres_update_template (void *cls,
- const char *instance_id,
- const char *template_id,
- const struct TALER_MERCHANTDB_TemplateDetails *td)
-{
- struct PostgresClosure *pg = cls;
- uint32_t pos32 = (uint32_t) td->pos_algorithm;
- struct GNUNET_PQ_QueryParam params[] = {
- GNUNET_PQ_query_param_string (instance_id),
- GNUNET_PQ_query_param_string (template_id),
- GNUNET_PQ_query_param_string (td->template_description),
- (NULL == td->pos_key)
- ? GNUNET_PQ_query_param_null ()
- : GNUNET_PQ_query_param_string (td->pos_key),
- GNUNET_PQ_query_param_uint32 (&pos32),
- TALER_PQ_query_param_json (td->template_contract),
- GNUNET_PQ_query_param_end
- };
-
-
- check_connection (pg);
- return GNUNET_PQ_eval_prepared_non_select (pg->conn,
- "update_template",
- params);
-}
-
-
-/**
- * Context used for postgres_lookup_template().
- */
-struct LookupTemplateContext
-{
- /**
- * Function to call with the results.
- */
- TALER_MERCHANTDB_TemplatesCallback cb;
-
- /**
- * Closure for @a cb.
- */
- void *cb_cls;
-
- /**
- * Did database result extraction fail?
- */
- bool extract_failed;
-};
-
-
-/**
- * Function to be called with the results of a SELECT statement
- * that has returned @a num_results results about template.
- *
- * @param[in,out] cls of type `struct LookupTemplateContext *`
- * @param result the postgres result
- * @param num_results the number of results in @a result
- */
-static void
-lookup_templates_cb (void *cls,
- PGresult *result,
- unsigned int num_results)
-{
- struct LookupTemplateContext *tlc = cls;
-
- for (unsigned int i = 0; i < num_results; i++)
- {
- char *template_id;
- char *template_description;
- struct GNUNET_PQ_ResultSpec rs[] = {
- GNUNET_PQ_result_spec_string ("template_id",
- &template_id),
- GNUNET_PQ_result_spec_string ("template_description",
- &template_description),
- GNUNET_PQ_result_spec_end
- };
-
- if (GNUNET_OK !=
- GNUNET_PQ_extract_result (result,
- rs,
- i))
- {
- GNUNET_break (0);
- tlc->extract_failed = true;
- return;
- }
- tlc->cb (tlc->cb_cls,
- template_id,
- template_description);
- GNUNET_PQ_cleanup_result (rs);
- }
-}
-
-
-/**
- * Lookup all of the templates the given instance has configured.
- *
- * @param cls closure
- * @param instance_id instance to lookup template for
- * @param cb function to call on all template found
- * @param cb_cls closure for @a cb
- * @return database result code
- */
-static enum GNUNET_DB_QueryStatus
-postgres_lookup_templates (void *cls,
- const char *instance_id,
- TALER_MERCHANTDB_TemplatesCallback cb,
- void *cb_cls)
-{
- struct PostgresClosure *pg = cls;
- struct LookupTemplateContext tlc = {
- .cb = cb,
- .cb_cls = cb_cls,
- /* Can be overwritten by the lookup_template_cb */
- .extract_failed = false,
- };
- struct GNUNET_PQ_QueryParam params[] = {
- GNUNET_PQ_query_param_string (instance_id),
- GNUNET_PQ_query_param_end
- };
- enum GNUNET_DB_QueryStatus qs;
-
- check_connection (pg);
- qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
- "lookup_templates",
- params,
- &lookup_templates_cb,
- &tlc);
- /* If there was an error inside lookup_template_cb, return a hard error. */
- if (tlc.extract_failed)
- return GNUNET_DB_STATUS_HARD_ERROR;
- return qs;
-}
-
-
-/**
- * Lookup details about a particular template.
- *
- * @param cls closure
- * @param instance_id instance to lookup template for
- * @param template_id template to lookup
- * @param[out] td set to the template details on success, can be NULL
- * (in that case we only want to check if the template exists)
- * @return database result code
- */
-static enum GNUNET_DB_QueryStatus
-postgres_lookup_template (void *cls,
- const char *instance_id,
- const char *template_id,
- struct TALER_MERCHANTDB_TemplateDetails *td)
-{
- struct PostgresClosure *pg = cls;
- struct GNUNET_PQ_QueryParam params[] = {
- GNUNET_PQ_query_param_string (instance_id),
- GNUNET_PQ_query_param_string (template_id),
- GNUNET_PQ_query_param_end
- };
-
- if (NULL == td)
- {
- struct GNUNET_PQ_ResultSpec rs_null[] = {
- GNUNET_PQ_result_spec_end
- };
-
- check_connection (pg);
- return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
- "lookup_template",
- params,
- rs_null);
- }
- else
- {
- uint32_t pos32;
- struct GNUNET_PQ_ResultSpec rs[] = {
- GNUNET_PQ_result_spec_string ("template_description",
- &td->template_description),
- GNUNET_PQ_result_spec_allow_null (
- GNUNET_PQ_result_spec_string ("pos_key",
- &td->pos_key),
- NULL),
- GNUNET_PQ_result_spec_allow_null (
- GNUNET_PQ_result_spec_uint32 ("pos_algorithm",
- &pos32),
- NULL),
- TALER_PQ_result_spec_json ("template_contract",
- &td->template_contract),
- GNUNET_PQ_result_spec_end
- };
- enum GNUNET_DB_QueryStatus qs;
-
- check_connection (pg);
- td->pos_key = NULL;
- qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
- "lookup_template",
- params,
- rs);
- td->pos_algorithm = (enum TALER_MerchantConfirmationAlgorithm) pos32;
- return qs;
- }
-}
-
-
-/**
* Delete information about a webhook.
*
* @param cls closure
@@ -3747,62 +3483,6 @@ postgres_connect (void *cls)
" pickup_serial, $2, $3"
" FROM merchant_reward_pickups"
" WHERE pickup_id=$1"),
- /* for postgres_lookup_templates() */
- GNUNET_PQ_make_prepare ("lookup_templates",
- "SELECT"
- " template_id"
- ",template_description"
- " FROM merchant_template"
- " JOIN merchant_instances"
- " USING (merchant_serial)"
- " WHERE merchant_instances.merchant_id=$1"),
- /* for postgres_lookup_template() */
- GNUNET_PQ_make_prepare ("lookup_template",
- "SELECT"
- " template_description"
- ",pos_key"
- ",pos_algorithm"
- ",template_contract"
- " FROM merchant_template"
- " JOIN merchant_instances"
- " USING (merchant_serial)"
- " WHERE merchant_instances.merchant_id=$1"
- " AND merchant_template.template_id=$2"),
- /* for postgres_delete_template() */
- GNUNET_PQ_make_prepare ("delete_template",
- "DELETE"
- " FROM merchant_template"
- " WHERE merchant_template.merchant_serial="
- " (SELECT merchant_serial "
- " FROM merchant_instances"
- " WHERE merchant_id=$1)"
- " AND merchant_template.template_id=$2"),
- /* for postgres_insert_template() */
- GNUNET_PQ_make_prepare ("insert_template",
- "INSERT INTO merchant_template"
- "(merchant_serial"
- ",template_id"
- ",template_description"
- ",pos_key"
- ",pos_algorithm"
- ",template_contract"
- ")"
- " SELECT merchant_serial,"
- " $2, $3, $4, $5, $6"
- " FROM merchant_instances"
- " WHERE merchant_id=$1"),
- /* for postgres_update_template() */
- GNUNET_PQ_make_prepare ("update_template",
- "UPDATE merchant_template SET"
- " template_description=$3"
- ",pos_key=$4"
- ",pos_algorithm=$5"
- ",template_contract=$6"
- " WHERE merchant_serial="
- " (SELECT merchant_serial"
- " FROM merchant_instances"
- " WHERE merchant_id=$1)"
- " AND template_id=$2"),
/* for postgres_lookup_webhooks() */
GNUNET_PQ_make_prepare ("lookup_webhooks",
"SELECT"
@@ -4010,6 +3690,18 @@ libtaler_plugin_merchantdb_postgres_init (void *cls)
= &TMH_PG_insert_instance;
plugin->insert_account
= &TMH_PG_insert_account;
+ plugin->lookup_otp_devices
+ = &TMH_PG_lookup_otp_devices;
+ plugin->delete_template
+ = &TMH_PG_delete_template;
+ plugin->insert_template
+ = &TMH_PG_insert_template;
+ plugin->update_template
+ = &TMH_PG_update_template;
+ plugin->lookup_templates
+ = &TMH_PG_lookup_templates;
+ plugin->lookup_template
+ = &TMH_PG_lookup_template;
plugin->update_account
= &TMH_PG_update_account;
plugin->account_kyc_set_status
@@ -4040,6 +3732,16 @@ libtaler_plugin_merchantdb_postgres_init (void *cls)
= &TMH_PG_insert_product;
plugin->update_product
= &TMH_PG_update_product;
+ plugin->insert_otp
+ = &TMH_PG_insert_otp;
+ plugin->delete_otp
+ = &TMH_PG_delete_otp;
+ plugin->update_otp
+ = &TMH_PG_update_otp;
+ plugin->select_otp
+ = &TMH_PG_select_otp;
+ plugin->select_otp_serial
+ = &TMH_PG_select_otp_serial;
plugin->lock_product
= &TMH_PG_lock_product;
plugin->expire_locks
@@ -4117,6 +3819,10 @@ libtaler_plugin_merchantdb_postgres_init (void *cls)
= &TMH_PG_update_wirewatch_progress;
plugin->select_wirewatch_accounts
= &TMH_PG_select_wirewatch_accounts;
+ plugin->select_account
+ = &TMH_PG_select_account;
+ plugin->select_accounts
+ = &TMH_PG_select_accounts;
plugin->lookup_reserves
= &TMH_PG_lookup_reserves;
plugin->lookup_pending_reserves = &postgres_lookup_pending_reserves;
@@ -4151,11 +3857,6 @@ libtaler_plugin_merchantdb_postgres_init (void *cls)
= &TMH_PG_authorize_reward;
plugin->insert_pickup
= &TMH_PG_insert_pickup;
- plugin->lookup_templates = &postgres_lookup_templates;
- plugin->lookup_template = &postgres_lookup_template;
- plugin->delete_template = &postgres_delete_template;
- plugin->insert_template = &postgres_insert_template;
- plugin->update_template = &postgres_update_template;
plugin->lookup_webhooks = &postgres_lookup_webhooks;
plugin->lookup_webhook = &postgres_lookup_webhook;
plugin->delete_webhook = &postgres_delete_webhook;