commit 17f0840dd67894d0ff88098814f8cd77cd0beef4 parent ba48fdeace7641e3a3bb46484d7b5fc08de396c4 Author: Christian Grothoff <christian@grothoff.org> Date: Sat, 21 Nov 2026 12:40:45 +0100 A converter may now return a list of attribute sets, one per form; they are stored in their order in one transaction. Diffstat:
33 files changed, 1212 insertions(+), 162 deletions(-)
diff --git a/src/exchange/taler-exchange-httpd_common_kyc.c b/src/exchange/taler-exchange-httpd_common_kyc.c @@ -315,50 +315,79 @@ TEH_kyc_run_measure_cancel (struct TEH_KycMeasureRunContext *kat) } -enum GNUNET_DB_QueryStatus -TEH_kyc_store_attributes ( +/** + * Birthday (for age restriction) of the attribute set @a attributes. + * + * @param provider_name provider that provided them, for logging + * @param attributes an attribute set, can be NULL + * @return days since the epoch, 0 if unknown + */ +static unsigned int +birthday_of (const char *provider_name, + const json_t *attributes) +{ + unsigned int birthday = 0; + const char *birthdate; + + if (! TEH_age_restriction_enabled) + return 0; + birthdate = json_string_value ( + json_object_get (attributes, + TALER_ATTRIBUTE_BIRTHDATE)); + if (NULL == birthdate) + return 0; + if (GNUNET_OK != + TALER_parse_coarse_date (birthdate, + &TEH_age_restriction_mask, + &birthday)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Failed to parse birthdate `%s' from KYC attributes of %s\n", + birthdate, + provider_name); + return 0; + } + return birthday; +} + + +/** + * Store one attribute set of a KYC process. + * + * @param process_row legitimization process the data provided is about + * @param account_id account the the data provided is about + * @param provider_name name of the provider that provided the attributes + * @param provider_user_id user ID at the provider, or NULL + * @param provider_legitimization_id process ID at the provider, or NULL + * @param collection_time when were the attributes collected + * @param expiration until when is the KYC check valid + * @param form_index position of @a attributes among the sets of the + * process, 0 for the first (or only) one + * @param birthday birthday stated by @a attributes, 0 if none + * @param attributes the attribute set, NULL for none + * @return database transaction status + */ +static enum GNUNET_DB_QueryStatus +store_attribute_set ( uint64_t process_row, const struct TALER_NormalizedPaytoHashP *account_id, const char *provider_name, const char *provider_user_id, const char *provider_legitimization_id, + struct GNUNET_TIME_Timestamp collection_time, struct GNUNET_TIME_Absolute expiration, - const json_t *new_attributes) + unsigned int form_index, + unsigned int birthday, + const json_t *attributes) { enum GNUNET_DB_QueryStatus qs; - unsigned int birthday = 0; size_t eas = 0; void *ea = NULL; const char *form_id = NULL; - if (TEH_age_restriction_enabled) - { - const char *birthdate; - - birthdate = json_string_value ( - json_object_get (new_attributes, - TALER_ATTRIBUTE_BIRTHDATE)); - if (NULL != birthdate) - { - enum GNUNET_GenericReturnValue ret; - - ret = TALER_parse_coarse_date (birthdate, - &TEH_age_restriction_mask, - &birthday); - - if (GNUNET_OK != ret) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Failed to parse birthdate `%s' from KYC attributes of %s\n", - birthdate, - provider_name); - } - } - } - - if (NULL != new_attributes) + if (NULL != attributes) { - form_id = json_string_value (json_object_get (new_attributes, + form_id = json_string_value (json_object_get (attributes, "FORM_ID")); if (NULL == form_id) { @@ -367,7 +396,7 @@ TEH_kyc_store_attributes ( return GNUNET_DB_STATUS_HARD_ERROR; } TALER_CRYPTO_kyc_attributes_encrypt (&TEH_attribute_key, - new_attributes, + attributes, &ea, &eas); } @@ -379,11 +408,93 @@ TEH_kyc_store_attributes ( provider_user_id, provider_legitimization_id, birthday, + collection_time, expiration, form_id, + form_index, eas, ea); GNUNET_free (ea); + return qs; +} + + +enum GNUNET_DB_QueryStatus +TEH_kyc_store_attributes ( + uint64_t process_row, + const struct TALER_NormalizedPaytoHashP *account_id, + const char *provider_name, + const char *provider_user_id, + const char *provider_legitimization_id, + struct GNUNET_TIME_Absolute expiration, + const json_t *new_attributes) +{ + struct GNUNET_TIME_Timestamp collection_time + = GNUNET_TIME_timestamp_get (); + enum GNUNET_DB_QueryStatus qs; + + if (json_is_array (new_attributes)) + { + /* Several attribute sets, one per form: stored in one transaction, + numbered in their order, with one collection time. */ + size_t n = json_array_size (new_attributes); + + GNUNET_assert (0 < n); + for (unsigned int retries = 0; retries < 3; retries++) + { + if (GNUNET_OK != + TALER_EXCHANGEDB_start (TEH_pg, + "store_kyc_attribute_sets")) + { + GNUNET_break (0); + return GNUNET_DB_STATUS_HARD_ERROR; + } + qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; + for (size_t i = 0; i < n && 0 < qs; i++) + { + const json_t *set = json_array_get (new_attributes, + i); + + qs = store_attribute_set (process_row, + account_id, + provider_name, + provider_user_id, + provider_legitimization_id, + collection_time, + expiration, + (unsigned int) i, + birthday_of (provider_name, + set), + set); + } + if (0 < qs) + { + qs = TALER_EXCHANGEDB_commit (TEH_pg); + if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) + qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; + } + else + { + TALER_EXCHANGEDB_rollback (TEH_pg); + } + if (GNUNET_DB_STATUS_SOFT_ERROR != qs) + break; + } + } + else + { + qs = store_attribute_set (process_row, + account_id, + provider_name, + provider_user_id, + provider_legitimization_id, + collection_time, + expiration, + 0, + birthday_of (provider_name, + new_attributes), + new_attributes); + } switch (qs) { case GNUNET_DB_STATUS_HARD_ERROR: diff --git a/src/exchange/taler-exchange-httpd_common_kyc.h b/src/exchange/taler-exchange-httpd_common_kyc.h @@ -63,8 +63,12 @@ struct TEH_KycMeasureRunContext; * @param provider_user_id set to user ID at the provider, or NULL if not supported or unknown * @param provider_legitimization_id set to legitimization process ID at the provider, or NULL if not supported or unknown * @param expiration until when is the KYC check valid - * @param new_attributes user attributes returned by the provider - * @return handle to cancel the operation + * @param new_attributes user attributes returned by the provider: an + * attribute set (an object with a "FORM_ID"), or an array of them, + * one per form, of which the first is the primary set (the + * account's current attributes); each set's birthday, if it + * states one, is stored with it + * @return database transaction status */ enum GNUNET_DB_QueryStatus TEH_kyc_store_attributes ( diff --git a/src/exchange/taler-exchange-httpd_get-kyc-proof-PROVIDER_NAME.c b/src/exchange/taler-exchange-httpd_get-kyc-proof-PROVIDER_NAME.c @@ -352,8 +352,7 @@ proof_cb ( GNUNET_log (GNUNET_ERROR_TYPE_INFO, "KYC process #%llu succeeded with KYC provider\n", (unsigned long long) kpc->process_row); - if (! json_is_string (json_object_get (attributes, - "FORM_ID"))) + if (! TALER_kyc_attributes_have_form_ids (attributes)) { /* Success without the mandatory FORM_ID attribute: the provider (or the logic talking to it) misbehaved. */ diff --git a/src/exchange/taler-exchange-httpd_kyc-webhook.c b/src/exchange/taler-exchange-httpd_kyc-webhook.c @@ -240,8 +240,7 @@ webhook_finished_cb ( switch (status) { case TALER_KYCLOGIC_STATUS_SUCCESS: - GNUNET_assert (json_is_string (json_object_get (attributes, - "FORM_ID"))); + GNUNET_assert (TALER_kyc_attributes_have_form_ids (attributes)); qs = TEH_kyc_store_attributes ( process_row, account_id, diff --git a/src/exchangedb/account_history.c b/src/exchangedb/account_history.c @@ -25,7 +25,7 @@ #include "exchange-database/iterate_aml_history.h" #include "exchange-database/iterate_kyc_history.h" #include "exchange-database/get_kyc_rules.h" -#include "exchange-database/iterate_aml_attributes.h" +#include "exchange-database/get_current_kyc_attributes.h" #include "exchange-database/account_history.h" #include <gnunet/gnunet_common.h> @@ -290,76 +290,23 @@ TALER_EXCHANGEDB_current_rule_builder (void *cls) } -/** - * Closure for decrypt_attributes(). - */ -struct DecryptContext -{ - /** - * Overall context. - */ - const struct TALER_EXCHANGEDB_HistoryBuilderContext *hbc; - - /** - * Where to return the attributes. - */ - json_t *attr; -}; - - -/** - * Decrypt and return AML attribute information. - * - * @param cls a `struct DecryptContext *` - * @param row_id current row in kyc_attributes table - * @param collection_time when were the attributes collected - * @param by_aml_officer true if filed by AML officer - * @param officer_name name of the officer, NULL if not @a by_aml_officer - * @param enc_attributes_size size of @a enc_attributes - * @param enc_attributes the encrypted collected attributes - */ -static void -decrypt_attributes ( - void *cls, - uint64_t row_id, - struct GNUNET_TIME_Timestamp collection_time, - bool by_aml_officer, - const char *officer_name, - size_t enc_attributes_size, - const void *enc_attributes) -{ - struct DecryptContext *decon = cls; - - (void) row_id; - (void) collection_time; - (void) officer_name; - decon->attr - = TALER_CRYPTO_kyc_attributes_decrypt (decon->hbc->attribute_key, - enc_attributes, - enc_attributes_size); - GNUNET_break (NULL != decon->attr); -} - - json_t * TALER_EXCHANGEDB_current_attributes_builder (void *cls) { struct TALER_EXCHANGEDB_HistoryBuilderContext *hbc = cls; const struct TALER_NormalizedPaytoHashP *acc = hbc->account; enum GNUNET_DB_QueryStatus qs; - struct DecryptContext decon = { - .hbc = hbc - }; + size_t enc_attributes_size; + void *enc_attributes; + json_t *attr = NULL; - qs = TALER_EXCHANGEDB_iterate_aml_attributes ( + qs = TALER_EXCHANGEDB_get_current_kyc_attributes ( hbc->pg, acc, - INT64_MAX, - -1, /* we only fetch the latest ones */ - &decrypt_attributes, - &decon); + &enc_attributes_size, + &enc_attributes); GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "select_aml_attributes returned %d\n", + "get_current_kyc_attributes returned %d\n", (int) qs); switch (qs) { @@ -368,12 +315,16 @@ TALER_EXCHANGEDB_current_attributes_builder (void *cls) GNUNET_break (0); return NULL; case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - decon.attr = json_object (); - GNUNET_break (NULL != decon.attr); + attr = json_object (); + GNUNET_break (NULL != attr); break; case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: - GNUNET_break (NULL != decon.attr); + attr = TALER_CRYPTO_kyc_attributes_decrypt (hbc->attribute_key, + enc_attributes, + enc_attributes_size); + GNUNET_free (enc_attributes); + GNUNET_break (NULL != attr); break; } - return decon.attr; + return attr; } diff --git a/src/exchangedb/do_insert_kyc_attributes.c b/src/exchangedb/do_insert_kyc_attributes.c @@ -32,13 +32,13 @@ TALER_EXCHANGEDB_do_insert_kyc_attributes ( const char *provider_account_id, const char *provider_legitimization_id, uint32_t birthday, + struct GNUNET_TIME_Timestamp collection_time, struct GNUNET_TIME_Absolute expiration_time, const char *form_name, + uint32_t form_index, size_t enc_attributes_size, const void *enc_attributes) { - struct GNUNET_TIME_Timestamp collection_time - = GNUNET_TIME_timestamp_get (); struct GNUNET_TIME_Timestamp expiration = GNUNET_TIME_absolute_to_timestamp (expiration_time); struct TALER_EXCHANGEDB_KycCompletedEventP rep = { @@ -72,6 +72,7 @@ TALER_EXCHANGEDB_do_insert_kyc_attributes ( (NULL == form_name) ? GNUNET_PQ_query_param_null () : GNUNET_PQ_query_param_string (form_name), + GNUNET_PQ_query_param_uint32 (&form_index), GNUNET_PQ_query_param_end }; bool ok; @@ -91,7 +92,7 @@ TALER_EXCHANGEDB_do_insert_kyc_attributes ( "SELECT " " out_ok" " FROM exchange_do_persist_kyc_attributes " - "($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12);"); + "($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13);"); qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, "do_insert_kyc_attributes", params, diff --git a/src/exchangedb/do_insert_kyc_attributes.sql b/src/exchangedb/do_insert_kyc_attributes.sql @@ -28,6 +28,7 @@ CREATE FUNCTION exchange_do_persist_kyc_attributes( IN in_enc_attributes BYTEA, IN in_kyc_completed_notify_s TEXT, IN in_form_name TEXT, -- can be NULL + IN in_form_index INT4, -- 0 for the first (or only) set of the process OUT out_ok BOOLEAN) -- set to true if we had a legi process matching in_process_row and in_provider_name for this account LANGUAGE plpgsql AS $$ @@ -41,6 +42,7 @@ INSERT INTO kyc_attributes ,by_aml_officer ,encrypted_attributes ,legitimization_serial + ,form_index ) VALUES (in_h_payto ,in_collection_time_ts @@ -48,7 +50,8 @@ INSERT INTO kyc_attributes ,in_form_name ,FALSE ,in_enc_attributes - ,in_process_row); + ,in_process_row + ,in_form_index); -- Wake up taler-exchange-sanctionscheck to check new attributes -- This is value for TALER_DBEVENT_EXCHANGE_NEW_KYC_ATTRIBUTES. @@ -64,9 +67,15 @@ UPDATE legitimization_processes AND provider_name=in_provider_name; out_ok=FOUND; +-- The first set of a process sets the birthday (0 if it has none, as +-- for a process with a single set); a further set only if it has one, +-- so that a form without a birthday does not undo that of another form +-- of the same process. UPDATE reserves SET birthday=in_birthday - WHERE (reserve_pub IN + WHERE ( (0 = in_form_index) OR + (0 <> in_birthday) ) + AND (reserve_pub IN (SELECT reserve_pub FROM reserves_in WHERE wire_source_h_payto IN @@ -95,5 +104,5 @@ INSERT INTO kyc_alerts END $$; -COMMENT ON FUNCTION exchange_do_persist_kyc_attributes(INT8, BYTEA, INT4, TEXT, TEXT, TEXT, INT8, INT8, INT8, BYTEA, TEXT, TEXT) - IS 'Inserts new KYC attributes and updates the status of the legitimization process'; +COMMENT ON FUNCTION exchange_do_persist_kyc_attributes(INT8, BYTEA, INT4, TEXT, TEXT, TEXT, INT8, INT8, INT8, BYTEA, TEXT, TEXT, INT4) + IS 'Inserts one set of new KYC attributes and updates the status of the legitimization process'; diff --git a/src/exchangedb/get_completed_legitimization.c b/src/exchangedb/get_completed_legitimization.c @@ -85,6 +85,8 @@ TALER_EXCHANGEDB_get_completed_legitimization ( " ON (lm.legitimization_measure_serial_id = lp.legitimization_measure_serial_id)" " LEFT JOIN kyc_attributes ka" " ON (ka.legitimization_serial = lp.legitimization_process_serial_id)" + /* the primary set, if the process stored several */ + " AND (0 = ka.form_index)" " WHERE lm.legitimization_measure_serial_id=$1" " AND lp.measure_index=$2;"); return GNUNET_PQ_eval_prepared_singleton_select ( diff --git a/src/exchangedb/get_current_kyc_attributes.c b/src/exchangedb/get_current_kyc_attributes.c @@ -0,0 +1,62 @@ +/* + This file is part of TALER + Copyright (C) 2026 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 exchangedb/get_current_kyc_attributes.c + * @brief Implementation of the get_current_kyc_attributes function for Postgres + */ +#include "taler/taler_pq_lib.h" +#include "exchange-database/get_current_kyc_attributes.h" +#include "helper.h" + + +enum GNUNET_DB_QueryStatus +TALER_EXCHANGEDB_get_current_kyc_attributes ( + struct TALER_EXCHANGEDB_PostgresContext *pg, + const struct TALER_NormalizedPaytoHashP *h_payto, + size_t *enc_attributes_size, + void **enc_attributes) +{ + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (h_payto), + GNUNET_PQ_query_param_end + }; + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_variable_size ( + "encrypted_attributes", + enc_attributes, + enc_attributes_size), + GNUNET_PQ_result_spec_end + }; + + PREPARE (pg, + "get_current_kyc_attributes", + "SELECT" + " ka.encrypted_attributes" + " FROM kyc_attributes ka" + " WHERE ka.h_payto=$1" + " AND ka.form_index=0" + " AND ka.legitimization_serial=" + " (SELECT kn.legitimization_serial" + " FROM kyc_attributes kn" + " WHERE kn.h_payto=$1" + " ORDER BY kn.kyc_attributes_serial_id DESC" + " LIMIT 1);"); + return GNUNET_PQ_eval_prepared_singleton_select ( + pg->conn, + "get_current_kyc_attributes", + params, + rs); +} diff --git a/src/exchangedb/insert_records_by_table.c b/src/exchangedb/insert_records_by_table.c @@ -2018,6 +2018,8 @@ irbt_cb_table_kyc_attributes (struct TALER_EXCHANGEDB_PostgresContext *pg, GNUNET_PQ_query_param_fixed_size ( td->details.kyc_attributes.encrypted_attributes, td->details.kyc_attributes.encrypted_attributes_size), + GNUNET_PQ_query_param_uint32 ( + &td->details.kyc_attributes.form_index), GNUNET_PQ_query_param_end }; @@ -2032,8 +2034,9 @@ irbt_cb_table_kyc_attributes (struct TALER_EXCHANGEDB_PostgresContext *pg, ",form_name" ",by_aml_officer" ",encrypted_attributes" + ",form_index" ") VALUES " - "($1, $2, $3, $4, $5, $6, $7, $8);"); + "($1, $2, $3, $4, $5, $6, $7, $8, $9);"); return GNUNET_PQ_eval_prepared_non_select (pg->conn, "insert_records_by_table_into_table_kyc_attributes", params); diff --git a/src/exchangedb/iterate_kyc_attributes.c b/src/exchangedb/iterate_kyc_attributes.c @@ -142,7 +142,11 @@ TALER_EXCHANGEDB_iterate_kyc_attributes ( " JOIN legitimization_processes lp" " ON (ka.legitimization_serial = lp.legitimization_process_serial_id)" " WHERE ka.h_payto=$1" - " ORDER BY ka.collection_time DESC"); + " ORDER BY ka.collection_time DESC" + /* the sets of one process share the collection time: + keep them together, the primary set first */ + " ,ka.legitimization_serial DESC" + " ,ka.form_index ASC;"); qs = GNUNET_PQ_eval_prepared_multi_select ( pg->conn, "iterate_kyc_attributes", diff --git a/src/exchangedb/iterate_kyc_history.c b/src/exchangedb/iterate_kyc_history.c @@ -173,7 +173,11 @@ TALER_EXCHANGEDB_iterate_kyc_history ( " JOIN legitimization_processes lp" " ON (ka.legitimization_serial = lp.legitimization_process_serial_id)" " WHERE ka.h_payto=$1" - " ORDER BY ka.collection_time DESC;"); + " ORDER BY ka.collection_time DESC" + /* the sets of one process share the collection time: + keep them together, in their order */ + " ,ka.legitimization_serial DESC" + " ,ka.form_index ASC;"); qs = GNUNET_PQ_eval_prepared_multi_select ( pg->conn, diff --git a/src/exchangedb/iterate_records_by_table.c b/src/exchangedb/iterate_records_by_table.c @@ -2847,6 +2847,9 @@ lrbt_cb_table_kyc_attributes (void *cls, "form_name", &td.details.kyc_attributes.form_name), NULL), + GNUNET_PQ_result_spec_uint32 ( + "form_index", + &td.details.kyc_attributes.form_index), GNUNET_PQ_result_spec_bool ( "by_aml_officer", &td.details.kyc_attributes.by_aml_officer), @@ -3693,6 +3696,7 @@ TALER_EXCHANGEDB_iterate_records_by_table ( ",collection_time" ",expiration_time" ",form_name" + ",form_index" ",by_aml_officer" ",encrypted_attributes" " FROM kyc_attributes" diff --git a/src/exchangedb/meson.build b/src/exchangedb/meson.build @@ -161,6 +161,7 @@ libtalerexchangedb = library( 'get_auditor_status.c', 'get_auditor_timestamp.c', 'get_completed_legitimization.c', + 'get_current_kyc_attributes.c', 'get_denomination_meta.c', 'get_global_fee_by_time.c', 'get_h_payto_by_access_token.c', diff --git a/src/exchangedb/sql-schema/exchange-0021.sql b/src/exchangedb/sql-schema/exchange-0021.sql @@ -0,0 +1,86 @@ +-- +-- This file is part of TALER +-- Copyright (C) 2026 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/> +-- + + +BEGIN; + +SELECT _v.register_patch('exchange-0021', NULL, NULL); + +SET search_path TO exchange; + + +-- A KYC process may return several attribute sets, one per form (each +-- with its own FORM_ID), and each set is stored as a row of its own. +-- form_index numbers the sets of one process in the order the provider +-- returned them, so that their order does not depend on row IDs or +-- collection times; the set with index 0 is the primary one. Rows from +-- before this patch are the only set of their process: index 0. The +-- UNIQUE (h_payto, legitimization_serial) constraint of exchange-0004 +-- becomes one over (h_payto, legitimization_serial, form_index): each +-- set of a process is still stored only once. + +CREATE FUNCTION alter_table_kyc_attributes21() +RETURNS void +LANGUAGE plpgsql +AS $$ +BEGIN + ALTER TABLE kyc_attributes + ADD COLUMN form_index INT4 NOT NULL DEFAULT (0); + COMMENT ON COLUMN kyc_attributes.form_index + IS 'position of these attributes in the list of attribute sets the legitimization process returned, starting at 0 (the primary set); 0 for a process that returned a single set'; +END +$$; + +CREATE FUNCTION constrain_table_kyc_attributes21( + IN partition_suffix TEXT +) +RETURNS void +LANGUAGE plpgsql +AS $$ +DECLARE + table_name TEXT DEFAULT 'kyc_attributes'; +BEGIN + table_name = concat_ws('_', table_name, partition_suffix); + EXECUTE FORMAT ( + 'ALTER TABLE ' || table_name || + ' DROP CONSTRAINT ' || table_name || '_legitimization_serial' + ); + EXECUTE FORMAT ( + 'ALTER TABLE ' || table_name || + ' ADD CONSTRAINT ' || table_name || '_legitimization_serial_form_index ' + 'UNIQUE (h_payto,legitimization_serial,form_index)' + ); +END $$; + +INSERT INTO exchange_tables + (name + ,version + ,action + ,partitioned + ,by_range) + VALUES + ('kyc_attributes21' + ,'exchange-0021' + ,'alter' + ,TRUE + ,FALSE), + ('kyc_attributes21' + ,'exchange-0021' + ,'constrain' + ,TRUE + ,FALSE); + +COMMIT; diff --git a/src/exchangedb/sql-schema/meson.build b/src/exchangedb/sql-schema/meson.build @@ -193,6 +193,7 @@ generated_sql = [ ['exchange-0018.sql', ['exchange-0018.sql']], ['exchange-0019.sql', ['exchange-0019.sql']], ['exchange-0020.sql', ['exchange-0020.sql']], + ['exchange-0021.sql', ['exchange-0021.sql']], ['tops-0001.sql', ['tops-0001.sql']], ] diff --git a/src/exchangedb/test_kyc_attributes.c b/src/exchangedb/test_kyc_attributes.c @@ -26,8 +26,11 @@ * #TALER_EXCHANGEDB_update_to_kyc_attributes_unscreened(), * #TALER_EXCHANGEDB_iterate_aml_attributes(), * #TALER_EXCHANGEDB_iterate_kyc_history(), - * #TALER_EXCHANGEDB_kyc_history_builder() and - * #TALER_EXCHANGEDB_current_attributes_builder(). + * #TALER_EXCHANGEDB_kyc_history_builder(), + * #TALER_EXCHANGEDB_current_attributes_builder() (and so + * #TALER_EXCHANGEDB_get_current_kyc_attributes()) and, for a process + * that stored several attribute sets, + * #TALER_EXCHANGEDB_get_completed_legitimization(). * * A `kyc_attributes` row is the (encrypted) personal data one KYC process * collected, so every row needs a `legitimization_processes` row to hang @@ -41,6 +44,7 @@ #include "exchange-database/account_history.h" #include "exchange-database/do_insert_kyc_attributes.h" #include "exchange-database/do_trigger_kyc_rule_for_account.h" +#include "exchange-database/get_completed_legitimization.h" #include "exchange-database/insert_aml_decision.h" #include "exchange-database/insert_aml_officer.h" #include "exchange-database/insert_legitimization_process.h" @@ -83,23 +87,25 @@ static struct TALER_AttributeEncryptionKeyP attribute_key; /** - * Build the attribute set of @a name for storage. + * Build the attribute set of form @a form_id with @a name for storage. * + * @param form_id value of the FORM_ID attribute * @param name value of the NAME attribute * @param[out] enc set to the encrypted attributes, to be freed by the * caller * @param[out] enc_size set to the number of bytes in @a enc */ static void -encrypt_attributes (const char *name, - void **enc, - size_t *enc_size) +encrypt_form_attributes (const char *form_id, + const char *name, + void **enc, + size_t *enc_size) { json_t *attrs; attrs = GNUNET_JSON_PACK ( GNUNET_JSON_pack_string ("FORM_ID", - "test-form"), + form_id), GNUNET_JSON_pack_string ("NAME", name)); GNUNET_assert (NULL != attrs); @@ -112,6 +118,26 @@ encrypt_attributes (const char *name, /** + * Build the attribute set of @a name for storage. + * + * @param name value of the NAME attribute + * @param[out] enc set to the encrypted attributes, to be freed by the + * caller + * @param[out] enc_size set to the number of bytes in @a enc + */ +static void +encrypt_attributes (const char *name, + void **enc, + size_t *enc_size) +{ + encrypt_form_attributes ("test-form", + name, + enc, + enc_size); +} + + +/** * Closure for the various attribute callbacks. */ struct AttributeContext @@ -520,8 +546,10 @@ check_insert (struct TALER_EXCHANGEDB_PostgresContext *pg) "provider-account", "legi-0", 0, + GNUNET_TIME_timestamp_get (), GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_DAYS), "test-form", + 0, enc_size, enc); GNUNET_free (enc); @@ -575,8 +603,10 @@ check_insert_wrong_provider (struct TALER_EXCHANGEDB_PostgresContext *pg) "provider-account", "legi-1", 0, + GNUNET_TIME_timestamp_get (), GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_DAYS), NULL, + 0, enc_size, enc); GNUNET_free (enc); @@ -962,6 +992,314 @@ check_current_attributes (struct TALER_EXCHANGEDB_PostgresContext *pg) /** + * Store one attribute set of the KYC process @a row. + * + * @param pg the database context + * @param row the legitimization process + * @param legi_id provider's ID of the process + * @param collection_time when the process collected its sets + * @param form_index position of the set among those of the process + * @param form_id FORM_ID of the set + * @param name NAME of the set + * @param birthday birthday the set states, 0 for none + * @return database transaction status + */ +static enum GNUNET_DB_QueryStatus +store_set (struct TALER_EXCHANGEDB_PostgresContext *pg, + uint64_t row, + const char *legi_id, + struct GNUNET_TIME_Timestamp collection_time, + uint32_t form_index, + const char *form_id, + const char *name, + uint32_t birthday) +{ + void *enc; + size_t enc_size; + enum GNUNET_DB_QueryStatus qs; + + encrypt_form_attributes (form_id, + name, + &enc, + &enc_size); + qs = TALER_EXCHANGEDB_do_insert_kyc_attributes ( + pg, + row, + &account.h_normalized, + "provider-c", + "provider-account", + legi_id, + birthday, + collection_time, + GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_DAYS), + form_id, + form_index, + enc_size, + enc); + GNUNET_free (enc); + return qs; +} + + +/** + * One KYC process may store several attribute sets, one per form. The + * exchange stores them in their order, numbered by their form index, + * so that the first set (the primary one) is the oldest row of the + * process: the "current attributes" builder and + * #TALER_EXCHANGEDB_get_completed_legitimization() still return it, + * and the KYC history lists the sets in their order. A set is stored + * only once, and a set without a birthday does not undo the birthday + * another set of the process stated. + * + * @param pg the database context + * @return 0 on success + */ +static int +check_several_sets (struct TALER_EXCHANGEDB_PostgresContext *pg) +{ + static const char *const forms[] = { + "form-primary", + "form-second", + "form-third" + }; + static const char *const names[] = { + "Dave", + "Erin", + "Frank" + }; + static const uint32_t birthdays[] = { + 7000, + 0, + 0 + }; + const unsigned int num_sets = 3; + struct TALER_EXCHANGEDB_HistoryBuilderContext hbc = { + .account = &account.h_normalized, + .pg = pg, + .attribute_key = &attribute_key, + .is_wallet = false + }; + struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get (); + struct TALER_ReservePublicKeyP reserve_pub; + char rhex[sizeof (reserve_pub) * 2 + 1]; + uint64_t row; + json_t *j; + + /* a reserve the account wired to, which gets the birthday */ + TDB_reserve_in (pg, + &account, + 42, + "10", + &reserve_pub); + { + char *h = TDB_hex (&reserve_pub, + sizeof (reserve_pub)); + + GNUNET_assert (strlen (h) < sizeof (rhex)); + strcpy (rhex, + h); + GNUNET_free (h); + } + FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != + TALER_EXCHANGEDB_insert_legitimization_process ( + pg, + &account.h_normalized, + 2, + measure_row, + "provider-c", + "provider-account", + "legi-2", + &row)); + for (unsigned int i = 0; i < num_sets; i++) + FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != + store_set (pg, + row, + "legi-2", + now, + i, + forms[i], + names[i], + birthdays[i])); + FAILIF (num_sets != TDB_count (pg, + "FROM kyc_attributes" + " WHERE legitimization_serial=%llu", + (unsigned long long) row)); + for (unsigned int i = 0; i < num_sets; i++) + FAILIF (1 != TDB_count (pg, + "FROM kyc_attributes" + " WHERE legitimization_serial=%llu" + " AND form_name='%s'" + " AND form_index=%u", + (unsigned long long) row, + forms[i], + i)); + FAILIF (1 != TDB_count (pg, + "FROM legitimization_processes" + " WHERE legitimization_process_serial_id=%llu" + " AND finished", + (unsigned long long) row)); + /* the second and third set stated no birthday and kept the first's */ + FAILIF (1 != TDB_count (pg, + "FROM reserves" + " WHERE reserve_pub=decode('%s','hex')" + " AND birthday=%u", + rhex, + (unsigned int) birthdays[0])); + + /* a set is stored once: the same form index again is refused (a + unique violation, which GNUNET_PQ reports as "no results") */ + FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != + store_set (pg, + row, + "legi-2", + now, + 1, + forms[1], + names[1], + 0)); + FAILIF (num_sets != TDB_count (pg, + "FROM kyc_attributes" + " WHERE legitimization_serial=%llu", + (unsigned long long) row)); + + /* AML programs get the primary set as the current attributes, even + though it is the oldest row of the process */ + j = TALER_EXCHANGEDB_current_attributes_builder (&hbc); + FAILIF (NULL == j); + { + const char *name = json_string_value (json_object_get (j, + "NAME")); + const char *form_id = json_string_value (json_object_get (j, + "FORM_ID")); + int bad; + + bad = ( (NULL == name) || + (0 != strcmp (names[0], + name)) || + (NULL == form_id) || + (0 != strcmp (forms[0], + form_id)) ); + json_decref (j); + FAILIF (bad); + } + + /* ... and all sets in the KYC history, newest first: the sets of this + process in their order, then the three older rows */ + j = TALER_EXCHANGEDB_kyc_history_builder (&hbc); + FAILIF (NULL == j); + { + int bad = (num_sets + 3 != json_array_size (j)); + + for (unsigned int i = 0; i < num_sets && ! bad; i++) + { + const json_t *e = json_array_get (j, + i); + const json_t *attrs = json_object_get (e, + "attributes"); + const char *name = json_string_value (json_object_get (attrs, + "NAME")); + const char *provider = json_string_value ( + json_object_get (e, + "provider_name")); + + bad = ( (NULL == name) || + (0 != strcmp (names[i], + name)) || + (NULL == provider) || + (0 != strcmp ("provider-c", + provider)) ); + } + json_decref (j); + FAILIF (bad); + } + + /* the completed legitimization reports the primary set as well, even + though several rows hang off the process */ + { + struct TALER_AccountAccessTokenP access_token; + struct TALER_NormalizedPaytoHashP h_payto; + bool is_wallet; + bool is_finished; + json_t *jmeasures = NULL; + size_t attributes_len; + void *attributes = NULL; + json_t *attrs; + const char *name; + int bad; + + FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != + TALER_EXCHANGEDB_get_completed_legitimization (pg, + measure_row, + 2, + &access_token, + &h_payto, + &is_wallet, + &jmeasures, + &is_finished, + &attributes_len, + &attributes)); + json_decref (jmeasures); + FAILIF (NULL == attributes); + attrs = TALER_CRYPTO_kyc_attributes_decrypt (&attribute_key, + attributes, + attributes_len); + GNUNET_free (attributes); + FAILIF (NULL == attrs); + name = json_string_value (json_object_get (attrs, + "NAME")); + bad = ( (NULL == name) || + (0 != strcmp (names[0], + name)) ); + json_decref (attrs); + FAILIF (bad); + } + + /* A later process whose first set states no birthday clears it, as a + single set does; a further set that states one sets it. */ + FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != + TALER_EXCHANGEDB_insert_legitimization_process ( + pg, + &account.h_normalized, + 3, + measure_row, + "provider-c", + "provider-account", + "legi-3", + &row)); + now = GNUNET_TIME_timestamp_get (); + FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != + store_set (pg, + row, + "legi-3", + now, + 0, + forms[0], + names[0], + 0)); + FAILIF (1 != TDB_count (pg, + "FROM reserves" + " WHERE reserve_pub=decode('%s','hex')" + " AND birthday=0", + rhex)); + FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != + store_set (pg, + row, + "legi-3", + now, + 1, + forms[1], + names[1], + 8000)); + FAILIF (1 != TDB_count (pg, + "FROM reserves" + " WHERE reserve_pub=decode('%s','hex')" + " AND birthday=8000", + rhex)); + return 0; +} + + +/** * The checks to run, in order. */ static const struct TDB_Test tests[] = { @@ -979,6 +1317,8 @@ static const struct TDB_Test tests[] = { &check_aml_attributes }, { "kyc-attributes-current-attributes", &check_current_attributes }, + { "kyc-attributes-several-sets", + &check_several_sets }, { NULL, NULL } }; diff --git a/src/include/exchange-database/account_history.h b/src/include/exchange-database/account_history.h @@ -94,7 +94,9 @@ TALER_EXCHANGEDB_current_rule_builder (void *cls); /** * Function called to obtain the latest KYC attributes - * in JSON for an account on-demand if needed. + * in JSON for an account on-demand if needed: those of the + * legitimization process that stored attributes last, its + * primary set if it stored several. * * Primary test table: `kyc_attributes` (see test_kyc_attributes.c). * diff --git a/src/include/exchange-database/do_insert_kyc_attributes.h b/src/include/exchange-database/do_insert_kyc_attributes.h @@ -29,6 +29,13 @@ /** * Store KYC attribute data. * + * A process that returned several attribute sets stores each with a + * call of its own, in one transaction, with @a form_index 0, 1, ... + * in the order of the sets and the same @a collection_time; the set + * with index 0 is the primary one. The set with index 0 sets the + * birthday of the account's reserves, a further set only if its + * @a birthday is not 0. + * * Primary test table: `kyc_attributes` (see test_kyc_attributes.c). * * @param pg the database context @@ -38,29 +45,30 @@ * @param provider_account_id provider account ID * @param provider_legitimization_id provider legitimization ID * @param birthday birthdate of user, in days after 1990, or 0 if unknown or definitively adult + * @param collection_time when were the attributes collected * @param expiration_time when does the data expire * @param form_name name of the form from which the @a enc_attributes originate, can be NULL + * @param form_index position of the attribute set among those of the + * process, 0 for the first (or only) one * @param enc_attributes_size number of bytes in @a enc_attributes * @param enc_attributes encrypted attribute data * @return database transaction status */ enum GNUNET_DB_QueryStatus -TALER_EXCHANGEDB_do_insert_kyc_attributes (struct - TALER_EXCHANGEDB_PostgresContext *pg, - uint64_t process_row, - const struct - TALER_NormalizedPaytoHashP * - h_payto, - const char *provider_name, - const char *provider_account_id, - const char * - provider_legitimization_id, - uint32_t birthday, - struct GNUNET_TIME_Absolute - expiration_time, - const char *form_name, - size_t enc_attributes_size, - const void *enc_attributes); +TALER_EXCHANGEDB_do_insert_kyc_attributes ( + struct TALER_EXCHANGEDB_PostgresContext *pg, + uint64_t process_row, + const struct TALER_NormalizedPaytoHashP *h_payto, + const char *provider_name, + const char *provider_account_id, + const char *provider_legitimization_id, + uint32_t birthday, + struct GNUNET_TIME_Timestamp collection_time, + struct GNUNET_TIME_Absolute expiration_time, + const char *form_name, + uint32_t form_index, + size_t enc_attributes_size, + const void *enc_attributes); #endif diff --git a/src/include/exchange-database/get_completed_legitimization.h b/src/include/exchange-database/get_completed_legitimization.h @@ -49,7 +49,9 @@ * @a encrypted_attributes * @param[out] encrypted_attributes set to the attributes * obtained for the legitimization process, if it - * succeeded, otherwise set to NULL + * succeeded, otherwise set to NULL; if the process + * stored several attribute sets, the primary one + * (form index 0) * @return database transaction status */ enum GNUNET_DB_QueryStatus diff --git a/src/include/exchange-database/get_current_kyc_attributes.h b/src/include/exchange-database/get_current_kyc_attributes.h @@ -0,0 +1,52 @@ +/* + This file is part of TALER + Copyright (C) 2026 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 get_current_kyc_attributes.h + * @brief implementation of the get_current_kyc_attributes function for Postgres + */ +#ifndef EXCHANGE_DATABASE_GET_CURRENT_KYC_ATTRIBUTES_H +#define EXCHANGE_DATABASE_GET_CURRENT_KYC_ATTRIBUTES_H + +#include "taler/taler_util.h" +#include "taler/taler_json_lib.h" +#include "exchangedb_lib.h" + + +/** + * Lookup the current KYC attributes of an account: those of the + * legitimization process that stored attributes last, and if that + * process stored several attribute sets, its primary one (form index 0). + * + * Primary test table: `kyc_attributes` (see test_kyc_attributes.c). + * + * @param pg the database context + * @param h_payto account to look up + * @param[out] enc_attributes_size set to the number of bytes in + * @a enc_attributes + * @param[out] enc_attributes set to the encrypted attributes, to be + * freed by the caller + * @return database transaction status, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS + * if the account has no attributes + */ +enum GNUNET_DB_QueryStatus +TALER_EXCHANGEDB_get_current_kyc_attributes ( + struct TALER_EXCHANGEDB_PostgresContext *pg, + const struct TALER_NormalizedPaytoHashP *h_payto, + size_t *enc_attributes_size, + void **enc_attributes); + + +#endif diff --git a/src/include/exchange-database/iterate_kyc_history.h b/src/include/exchange-database/iterate_kyc_history.h @@ -102,7 +102,8 @@ typedef void /** * Lookup KYC history for an account identified via - * @a h_payto. + * @a h_payto. Returns one entry per stored attribute set, newest + * first; the sets of one process in their order (form index). * * Primary test table: `kyc_attributes` (see test_kyc_attributes.c). * diff --git a/src/include/exchange-database/iterate_records_by_table.h b/src/include/exchange-database/iterate_records_by_table.h @@ -184,6 +184,7 @@ struct TALER_EXCHANGEDB_TableData struct GNUNET_TIME_Timestamp collection_time; struct GNUNET_TIME_Timestamp expiration_time; char *form_name; /**< NULL if the attributes came from a provider */ + uint32_t form_index; /**< position among the sets of the process */ bool by_aml_officer; void *encrypted_attributes; size_t encrypted_attributes_size; diff --git a/src/include/taler/taler_kyclogic_plugin.h b/src/include/taler/taler_kyclogic_plugin.h @@ -274,7 +274,8 @@ typedef void * @param provider_user_id set to user ID at the provider, or NULL if not supported or unknown * @param provider_legitimization_id set to legitimization process ID at the provider, or NULL if not supported or unknown * @param expiration until when is the KYC check valid - * @param attributes user attributes returned by the provider, MUST include "FORM_ID" of type String, or NULL on error + * @param attributes user attributes returned by the provider, MUST include "FORM_ID" of type String, or NULL on error; + * may also be a non-empty array of such objects, one per form, the first being the primary set * @param http_status HTTP status code of @a response * @param[in] response to return to the HTTP client */ @@ -306,7 +307,8 @@ typedef void * @param provider_legitimization_id set to legitimization process ID at the provider, or NULL if not supported or unknown * @param status KYC status * @param expiration until when is the KYC check valid - * @param attributes user attributes returned by the provider, MUST include "FORM_ID" of type String (or NULL on error) + * @param attributes user attributes returned by the provider, MUST include "FORM_ID" of type String (or NULL on error); + * may also be a non-empty array of such objects, one per form, the first being the primary set * @param http_status HTTP status code of @a response * @param[in] response to return to the HTTP client */ diff --git a/src/include/taler/taler_util.h b/src/include/taler/taler_util.h @@ -564,6 +564,17 @@ TALER_is_session_id (const char *session_id); /** + * Check that a provider's attributes can be stored: an object with a + * string "FORM_ID", or a non-empty array of such objects. + * + * @param attributes attributes returned by a provider + * @return true if they can be stored + */ +bool +TALER_kyc_attributes_have_form_ids (const json_t *attributes); + + +/** * Check if @a lang matches the @a language_pattern, and if so with * which preference. * See also: https://tools.ietf.org/html/rfc7231#section-5.3.1 diff --git a/src/kyclogic/meson.build b/src/kyclogic/meson.build @@ -25,12 +25,14 @@ kyc_bin_SCRIPTS = [ 'taler-exchange-helper-measure-tops-postal-check', 'taler-exchange-helper-measure-tops-sms-check', 'taler-exchange-helper-measure-test-form', + 'taler-exchange-helper-measure-test-forms', 'taler-exchange-helper-measure-test-oauth', 'taler-exchange-helper-measure-update-from-context', 'taler-exchange-helper-measure-validate-accepted-tos', 'taler-exchange-kyc-kycaid-converter.sh', 'taler-exchange-kyc-persona-converter.sh', 'taler-exchange-kyc-oauth2-test-converter.sh', + 'taler-exchange-kyc-oauth2-test-forms-converter.sh', 'taler-exchange-kyc-challenger-email-converter', 'taler-exchange-kyc-challenger-postal-converter', 'taler-exchange-kyc-challenger-sms-converter', @@ -77,22 +79,12 @@ pkg.generate( test_kyclogic = executable( 'test_kyclogic', ['test_kyclogic.c'], - dependencies: [ - libtalerkyclogic_dep, - libtalerutil_dep, - gnunetutil_dep, - ], + dependencies: [libtalerkyclogic_dep, libtalerutil_dep, gnunetutil_dep], include_directories: [incdir, configuration_inc], build_by_default: false, install: false, ) -test( - 'test_kyclogic', - test_kyclogic, - suite: ['kyclogic'], -) - - +test('test_kyclogic', test_kyclogic, suite: ['kyclogic']) executable( 'taler-exchange-helper-sanctions-dummy', diff --git a/src/kyclogic/plugin_kyclogic_oauth2.c b/src/kyclogic/plugin_kyclogic_oauth2.c @@ -1259,6 +1259,25 @@ handle_proof_error (struct TALER_KYCLOGIC_ProofHandle *ph, /** + * Pack the output @a attr of the attribute converter as field + * "attributes" of an error reply. The converter's output is + * parsed without JSON_DECODE_ANY, so it is an object or an array. + * + * @param attr output of the converter, can be NULL + * @return pack specification for @a attr + */ +static struct GNUNET_JSON_PackSpec +pack_converter_output (const json_t *attr) +{ + if (json_is_array (attr)) + return GNUNET_JSON_pack_array_incref ("attributes", + (json_t *) attr); + return GNUNET_JSON_pack_object_incref ("attributes", + (json_t *) attr); +} + + +/** * Type of a callback that receives a JSON @a result. * * @param cls closure with a `struct TALER_KYCLOGIC_ProofHandle *` @@ -1299,8 +1318,7 @@ converted_proof_cb (void *cls, GNUNET_JSON_pack_string ("converter", pd->conversion_binary), GNUNET_JSON_pack_allow_null ( - GNUNET_JSON_pack_object_incref ("attributes", - (json_t *) attr)), + pack_converter_output (attr)), GNUNET_JSON_pack_bool ("debug", ph->pd->debug_mode), GNUNET_JSON_pack_string ("message", @@ -1340,7 +1358,11 @@ converted_proof_cb (void *cls, const char *emsg; unsigned int line; - res = GNUNET_JSON_parse (attr, + /* With several attribute sets, the first carries the id. */ + res = GNUNET_JSON_parse (json_is_array (attr) + ? json_array_get (attr, + 0) + : attr, ispec, &emsg, &line); @@ -1358,8 +1380,7 @@ converted_proof_cb (void *cls, "Unexpected response from KYC attribute converter: returned JSON data must contain 'id' field"), GNUNET_JSON_pack_bool ("debug", ph->pd->debug_mode), - GNUNET_JSON_pack_object_incref ("attributes", - (json_t *) attr), + pack_converter_output (attr), TALER_JSON_pack_ec ( TALER_EC_EXCHANGE_KYC_PROOF_BACKEND_INVALID_RESPONSE)); GNUNET_break ( @@ -1376,8 +1397,7 @@ converted_proof_cb (void *cls, } ph->provider_user_id = GNUNET_strdup (id); } - if (! json_is_string (json_object_get (attr, - "FORM_ID"))) + if (! TALER_kyc_attributes_have_form_ids (attr)) { json_t *body; @@ -1391,8 +1411,7 @@ converted_proof_cb (void *cls, "Missing 'FORM_ID' field in attributes"), GNUNET_JSON_pack_bool ("debug", ph->pd->debug_mode), - GNUNET_JSON_pack_object_incref ("attributes", - (json_t *) attr), + pack_converter_output (attr), TALER_JSON_pack_ec ( TALER_EC_EXCHANGE_KYC_PROOF_BACKEND_INVALID_RESPONSE)); GNUNET_break ( diff --git a/src/kyclogic/taler-exchange-helper-measure-test-forms b/src/kyclogic/taler-exchange-helper-measure-test-forms @@ -0,0 +1,99 @@ +#!/bin/bash +# +# This file is part of TALER +# Copyright (C) 2026 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, If not, see <http://www.gnu.org/license> +# + +# Hard error reporting on. +set -eu + +# Exit, with error message (hard failure) +function exit_fail() { + echo " FAIL: " "$@" >&2 + EXIT_STATUS=1 + exit "$EXIT_STATUS" +} + +CONF="$HOME/.config/taler-exchange.conf" +VERBOSE=0 + +while getopts 'ac:hirvV' OPTION; +do + case "$OPTION" in + a) + # This AML program expects the following inputs + echo "FULL_NAME" + echo "DATE_OF_BIRTH" + exit 0 + ;; + c) + # shellcheck disable=SC2034 + CONF="$OPTARG" + ;; + h) + echo "This is a KYC measure program for tests. It checks that the attribute sets a KYC process returned for its forms test-forms-primary and test-forms-second reached it: the primary set as the current attributes, both in the KYC history in their order. Then it does what taler-exchange-helper-measure-test-oauth does." + echo 'Supported options:' + echo ' -a -- show required attributes' + # shellcheck disable=SC2016 + echo ' -c $CONF -- set configuration' + echo ' -h -- print this help' + echo ' -i -- show required inputs' + echo ' -r -- show required context' + echo ' -v -- show version' + echo ' -V -- be verbose' + exit 0 + ;; + i) + echo "attributes" + echo "kyc_history" + exit 0 + ;; + r) + # No context is required. + exit 0 + ;; + v) + echo "$0 v0.0.0" + exit 0 + ;; + V) + VERBOSE=1 + ;; + ?) + exit_fail "Unrecognized command line option" + ;; + esac +done + +if [ 1 = "$VERBOSE" ] +then + echo "Running $0" 1>&2 +fi + +# See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlProgramInput +# for the full JSON with possible inputs. +INPUT=$(cat) + +# The current attributes are the primary set ... +echo "$INPUT" \ + | jq -e '.attributes.FORM_ID == "test-forms-primary"' > /dev/null \ + || exit_fail "the current attributes are not the primary set" +# ... and the KYC history, newest first, lists the sets of the process +# in the order the provider returned them. +echo "$INPUT" \ + | jq -e '[.kyc_history[0:2][].attributes.FORM_ID] + == ["test-forms-primary", "test-forms-second"]' > /dev/null \ + || exit_fail "the KYC history lacks the sets of the process in their order" + +echo "$INPUT" | exec taler-exchange-helper-measure-test-oauth -c "$CONF" diff --git a/src/kyclogic/taler-exchange-kyc-oauth2-test-forms-converter.sh b/src/kyclogic/taler-exchange-kyc-oauth2-test-forms-converter.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# This file is in the public domain. +# +# Converts the JSON output of the test OAuth 2.0 service like +# taler-exchange-kyc-oauth2-test-converter.sh, but returns it as two +# attribute sets, one per form, as a provider that collects several +# forms in one KYC process does. The first set is the primary one. +# + +# Die if anything goes wrong. +set -eu + +echo "Running $0" 1>&2 + +taler-exchange-kyc-oauth2-test-converter.sh \ + | jq '[ . + {"FORM_ID":"test-forms-primary"}, + {"FORM_ID":"test-forms-second","NOTE":"second form"} ]' +exit 0 diff --git a/src/testing/meson.build b/src/testing/meson.build @@ -904,6 +904,33 @@ test( is_parallel: false, ) +test_kyc_oauth2_forms = executable( + 'test_kyc_oauth2_forms', + ['test_kyc_oauth2_forms.c'], + dependencies: [ + libtalertesting_dep, + libtalerfakebank_dep, + libtalerbank_dep, + libtalerjson_dep, + libtalerexchange_dep, + libtalerutil_dep, + gnunetutil_dep, + gnunetcurl_dep, + json_dep, + gcrypt_dep, + ], + include_directories: [incdir, configuration_inc], + build_by_default: false, + install: false, +) +test( + 'test_kyc_oauth2_forms', + test_kyc_oauth2_forms, + workdir: meson.current_build_dir(), + suite: ['testing', 'integrationtests'], + is_parallel: false, +) + ## Distribution @@ -967,6 +994,7 @@ EXTRA_DIST = [ 'test-taler-exchange-aggregator-postgres.conf', 'test-taler-exchange-wirewatch-postgres.conf', 'test_kyc_api.conf', + 'test_kyc_oauth2_forms.conf', 'test_sanctions.conf', ] diff --git a/src/testing/test_kyc_oauth2_forms.c b/src/testing/test_kyc_oauth2_forms.c @@ -0,0 +1,195 @@ +/* + This file is part of TALER + Copyright (C) 2026 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 testing/test_kyc_oauth2_forms.c + * @brief testcase for an OAuth 2.0 KYC provider whose converter + * returns several attribute sets, one per form + * + * The converter (taler-exchange-kyc-oauth2-test-forms-converter.sh) + * returns two sets. The AML program of the measure + * (taler-exchange-helper-measure-test-forms) raises the limits only if + * the first set is the account's current attributes and the KYC + * history lists both sets in their order; otherwise the account is + * frozen. So the withdrawal after the KYC process succeeds only if the + * exchange stored both sets as it should. + */ +#include "taler/taler_util.h" +#include "taler/taler_json_lib.h" +#include <gnunet/gnunet_util_lib.h> +#include <microhttpd.h> +#include "taler/taler_bank_service.h" +#include "taler/taler_testing_lib.h" + + +/** + * Configuration file we use. One (big) configuration is used + * for the various components for this test. + */ +#define CONFIG_FILE "test_kyc_oauth2_forms.conf" + +/** + * Our credentials. + */ +static struct TALER_TESTING_Credentials cred; + + +/** + * Execute the taler-exchange-wirewatch command with + * our configuration file. + * + * @param label label to use for the command. + */ +#define CMD_EXEC_WIREWATCH(label) \ + TALER_TESTING_cmd_exec_wirewatch2 ( \ + label, \ + CONFIG_FILE, \ + "exchange-account-2") + +/** + * Run wire transfer of funds from some user's account to the + * exchange. + * + * @param label label to use for the command. + * @param amount amount to transfer, i.e. "EUR:1" + */ +#define CMD_TRANSFER_TO_EXCHANGE(label,amount) \ + TALER_TESTING_cmd_admin_add_incoming ( \ + label, \ + amount, \ + &cred.ba, \ + cred.user42_payto) + + +/** + * Main function that will tell the interpreter what commands to + * run. + * + * @param cls closure + * @param is interpreter state + */ +static void +run (void *cls, + struct TALER_TESTING_Interpreter *is) +{ + struct TALER_TESTING_Command withdraw_kyc[] = { + CMD_TRANSFER_TO_EXCHANGE ( + "create-reserve", + "EUR:15.02"), + TALER_TESTING_cmd_check_bank_admin_transfer ( + "check-create-reserve", + "EUR:15.02", + cred.user42_payto, + cred.exchange_payto, + "create-reserve"), + CMD_EXEC_WIREWATCH ("wirewatch"), + TALER_TESTING_cmd_withdraw_amount ( + "withdraw-lacking-kyc", + "create-reserve", + "EUR:10", + 0, /* age restriction off */ + MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS), + TALER_TESTING_cmd_admin_add_kycauth ( + "setup-account-key", + "EUR:0.01", + &cred.ba, + cred.user42_payto, + NULL /* create new key */), + CMD_EXEC_WIREWATCH ("import-kyc-account"), + TALER_TESTING_cmd_check_kyc_get ( + "check-kyc", + "withdraw-lacking-kyc", + "setup-account-key", + TALER_EXCHANGE_KLPT_KYC_AUTH_TRANSFER, + MHD_HTTP_ACCEPTED), + TALER_TESTING_cmd_get_kyc_info ( + "get-kyc-info", + "check-kyc", + MHD_HTTP_OK), + TALER_TESTING_cmd_post_kyc_start ( + "start-kyc-process", + "get-kyc-info", + 0, + MHD_HTTP_OK), + /* An adult: the birthday does not restrict the withdrawal. */ + TALER_TESTING_cmd_oauth_with_birthdate ( + "start-oauth-service", + "1980-00-00", + 6666), + /* The converter returns two attribute sets. */ + TALER_TESTING_cmd_proof_kyc_oauth2 ( + "proof-kyc-oauth2-forms", + "start-kyc-process", + "test-oauth2", + "pass", + MHD_HTTP_SEE_OTHER), + /* The AML program saw both sets and raised the limits. */ + TALER_TESTING_cmd_withdraw_amount ( + "withdraw-with-kyc", + "create-reserve", + "EUR:10", + 0, /* age restriction off */ + MHD_HTTP_OK), + TALER_TESTING_cmd_end () + }; + struct TALER_TESTING_Command commands[] = { + TALER_TESTING_cmd_run_fakebank ( + "run-fakebank", + cred.cfg, + "exchange-account-2"), + TALER_TESTING_cmd_system_start ( + "start-taler", + CONFIG_FILE, + "-e", + NULL), + TALER_TESTING_cmd_get_exchange ( + "get-exchange", + cred.cfg, + NULL, + true, + true), + TALER_TESTING_cmd_batch ( + "withdraw-kyc", + withdraw_kyc), + TALER_TESTING_cmd_end () + }; + + (void) cls; + TALER_TESTING_run (is, + commands); +} + + +int +main (int argc, + char *const *argv) +{ + (void) argc; + return TALER_TESTING_main ( + argv, + "INFO", + CONFIG_FILE, + "exchange-account-2", + TALER_TESTING_BS_FAKEBANK, + &cred, + &run, + NULL); +} + + +/* end of test_kyc_oauth2_forms.c */ diff --git a/src/testing/test_kyc_oauth2_forms.conf b/src/testing/test_kyc_oauth2_forms.conf @@ -0,0 +1,17 @@ +# This file is in the public domain. +# +# test_kyc_api.conf, with an OAuth 2.0 provider that returns one +# attribute set per form. +@INLINE@ test_kyc_api.conf + +[kyc-provider-test-oauth2] +KYC_OAUTH2_CONVERTER_HELPER = taler-exchange-kyc-oauth2-test-forms-converter.sh + +# Run the AML program that checks the sets reached it. +[kyc-measure-run-oauth] +PROGRAM = oauth-forms-check + +[aml-program-oauth-forms-check] +DESCRIPTION = "Checks the attribute sets of the forms of the OAuth2 test provider, then increases all limits to EUR:1000" +COMMAND = taler-exchange-helper-measure-test-forms +FALLBACK = manual-freeze diff --git a/src/util/util.c b/src/util/util.c @@ -579,6 +579,28 @@ TALER_is_session_id (const char *session_id) } +bool +TALER_kyc_attributes_have_form_ids (const json_t *attributes) +{ + size_t idx; + const json_t *set; + + if (json_is_object (attributes)) + return json_is_string (json_object_get (attributes, + "FORM_ID")); + if ( (! json_is_array (attributes)) || + (0 == json_array_size (attributes)) ) + return false; + json_array_foreach ((json_t *) attributes, idx, set) + { + if (! json_is_string (json_object_get (set, + "FORM_ID"))) + return false; + } + return true; +} + + #ifdef __APPLE__ char * strchrnul (const char *s,