anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

iterate_auth_iban_transfers.c (4403B)


      1 /*
      2   This file is part of Anastasis
      3   Copyright (C) 2020-2022 Anastasis SARL
      4 
      5   Anastasis is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file stasis/iterate_auth_iban_transfers.c
     18  * @brief Anastasis database: iterate auth iban transfers
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include "anastasis-db_pg.h"
     23 #include "anastasis/anastasis-database/iterate_auth_iban_transfers.h"
     24 #include "anastasis/anastasis-database/transaction.h"
     25 #include "anastasis/anastasis-database/preflight.h"
     26 #include <taler/taler_pq_lib.h>
     27 
     28 
     29 struct TestIbanContext
     30 {
     31 
     32   /**
     33    * Function to call on each wire transfer found.
     34    */
     35   ANASTASIS_DB_AuthIbanTransferCallback cb;
     36 
     37   /**
     38    * Closure for @a cb.
     39    */
     40   void *cb_cls;
     41 
     42   /**
     43    * Value to return.
     44    */
     45   enum GNUNET_DB_QueryStatus qs;
     46 };
     47 
     48 
     49 /**
     50  * Helper function for #postgres_test_auth_iban_payment().
     51  * To be called with the results of a SELECT statement
     52  * that has returned @a num_results results.
     53  *
     54  * @param cls closure of type `struct TestIbanContext *`
     55  * @param result the postgres result
     56  * @param num_results the number of results in @a result
     57  */
     58 static void
     59 test_auth_cb (void *cls,
     60               PGresult *result,
     61               unsigned int num_results)
     62 {
     63   struct TestIbanContext *tic = cls;
     64 
     65   for (unsigned int i = 0; i<num_results; i++)
     66   {
     67     struct TALER_Amount credit;
     68     char *wire_subject;
     69     struct GNUNET_PQ_ResultSpec rs[] = {
     70       TALER_PQ_result_spec_amount_with_currency ("credit",
     71                                                  &credit),
     72       GNUNET_PQ_result_spec_string ("wire_subject",
     73                                     &wire_subject),
     74       GNUNET_PQ_result_spec_end
     75     };
     76 
     77     if (GNUNET_OK !=
     78         GNUNET_PQ_extract_result (result,
     79                                   rs,
     80                                   i))
     81     {
     82       GNUNET_break (0);
     83       tic->qs = GNUNET_DB_STATUS_HARD_ERROR;
     84       return;
     85     }
     86     if (tic->cb (tic->cb_cls,
     87                  &credit,
     88                  wire_subject))
     89     {
     90       GNUNET_free (wire_subject);
     91       tic->qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     92       return;
     93     }
     94     GNUNET_free (wire_subject);
     95   }
     96 }
     97 
     98 
     99 /**
    100  * Function to check if we are aware of a wire transfer
    101  * that satisfies the IBAN plugin's authentication check.
    102  *
    103  * @param debit_account which debit account to check
    104  * @param earliest_date earliest date to check
    105  * @param cb function to call on all entries found
    106  * @param cb_cls closure for @a cb
    107  * @return transaction status,
    108  *    #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a cb
    109  *      returned 'true' once
    110  *    #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if no
    111  *      wire transfers existed for which @a cb returned true
    112  */enum GNUNET_DB_QueryStatus
    113 ANASTASIS_DB_iterate_auth_iban_transfers (
    114   const char *debit_account,
    115   struct GNUNET_TIME_Timestamp earliest_date,
    116   ANASTASIS_DB_AuthIbanTransferCallback cb,
    117   void *cb_cls)
    118 {
    119   struct TestIbanContext tic = {
    120     .cb = cb,
    121     .cb_cls = cb_cls
    122   };
    123   struct GNUNET_PQ_QueryParam params[] = {
    124     GNUNET_PQ_query_param_string (debit_account),
    125     GNUNET_PQ_query_param_timestamp (&earliest_date),
    126     GNUNET_PQ_query_param_end
    127   };
    128   enum GNUNET_DB_QueryStatus qs;
    129 
    130   PREPARE ("iterate_auth_iban_transfers",
    131            "SELECT"
    132            " credit"
    133            ",wire_subject"
    134            " FROM anastasis_auth_iban_in"
    135            " WHERE debit_account_details=$1"
    136            "  AND execution_date>=$2;");
    137   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    138                                              "iterate_auth_iban_transfers",
    139                                              params,
    140                                              &test_auth_cb,
    141                                              &tic);
    142   if (qs < 0)
    143     return qs;
    144   return tic.qs;
    145 }
    146 
    147 
    148 /* end of iterate_auth_iban_transfers.c */