merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

iterate_login_tokens.c (5765B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2025 Taler Systems SA
      4 
      5    TALER is free software; you can redistribute it and/or modify it under the
      6    terms of the GNU General Public License as published by the Free Software
      7    Foundation; either version 3, or (at your option) any later version.
      8 
      9    TALER 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 General Public License for more details.
     12 
     13    You should have received a copy of the GNU General Public License along with
     14    TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 /**
     17  * @file src/backenddb/iterate_login_tokens.c
     18  * @brief Implementation of the iterate_login_tokens function for Postgres
     19  * @author Iván Ávalos
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/iterate_login_tokens.h"
     24 #include "helper.h"
     25 
     26 
     27 /**
     28  * Hard upper bound on the number of records returned by a single
     29  * call, regardless of the limit requested by the client.
     30  */
     31 #define MAX_RECORDS 50000
     32 
     33 
     34 /**
     35  * Context used for TALER_MERCHANTDB_iterate_products().
     36  */
     37 struct LookupLoginTokensContext
     38 {
     39   /**
     40    * Function to call with the results.
     41    */
     42   TALER_MERCHANTDB_LoginTokensCallback cb;
     43 
     44   /**
     45    * Closure for @a cb.
     46    */
     47   void *cb_cls;
     48 
     49   /**
     50    * Did database result extraction fail?
     51    */
     52   bool extract_failed;
     53 };
     54 
     55 
     56 /**
     57  * Function to be called with the results of a SELECT statement
     58  * that has returned @a num_results results about products.
     59  *
     60  * @param[in,out] cls of type `struct LookupProductsContext *`
     61  * @param result the postgres result
     62  * @param num_results the number of results in @a result
     63  */
     64 static void
     65 lookup_login_tokens_cb (void *cls,
     66                         PGresult *result,
     67                         unsigned int num_results)
     68 {
     69   struct LookupLoginTokensContext *plc = cls;
     70 
     71   for (unsigned int i = 0; i < num_results; i++)
     72   {
     73     uint32_t validity_scope;
     74     uint64_t serial;
     75     struct GNUNET_TIME_Timestamp expiration_time;
     76     struct GNUNET_TIME_Timestamp creation_time;
     77     char *description;
     78     struct TALER_MERCHANTDB_LoginTokenP token;
     79     struct GNUNET_PQ_ResultSpec rs[] = {
     80       GNUNET_PQ_result_spec_auto_from_type ("token",
     81                                             &token),
     82       GNUNET_PQ_result_spec_uint32 ("validity_scope",
     83                                     &validity_scope),
     84       GNUNET_PQ_result_spec_string ("description",
     85                                     &description),
     86       GNUNET_PQ_result_spec_timestamp ("creation_time",
     87                                        &creation_time),
     88       GNUNET_PQ_result_spec_timestamp ("expiration_time",
     89                                        &expiration_time),
     90       GNUNET_PQ_result_spec_uint64 ("serial",
     91                                     &serial),
     92       GNUNET_PQ_result_spec_end
     93     };
     94 
     95     if (GNUNET_OK !=
     96         GNUNET_PQ_extract_result (result,
     97                                   rs,
     98                                   i))
     99     {
    100       GNUNET_break (0);
    101       plc->extract_failed = true;
    102       return;
    103     }
    104     plc->cb (plc->cb_cls,
    105              creation_time,
    106              expiration_time,
    107              validity_scope,
    108              description,
    109              serial);
    110     GNUNET_PQ_cleanup_result (rs);
    111   }
    112 }
    113 
    114 
    115 enum GNUNET_DB_QueryStatus
    116 TALER_MERCHANTDB_iterate_login_tokens (
    117   struct TALER_MERCHANTDB_PostgresContext *pg,
    118   const char *instance_id,
    119   uint64_t offset,
    120   int64_t limit,
    121   TALER_MERCHANTDB_LoginTokensCallback cb,
    122   void *cb_cls)
    123 {
    124   struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get ();
    125   uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
    126                                 TALER_MERCHANTDB_abs_limit (limit));
    127   struct LookupLoginTokensContext plc = {
    128     .cb = cb,
    129     .cb_cls = cb_cls,
    130     /* Can be overwritten by the lookup_products_cb */
    131     .extract_failed = false,
    132   };
    133   struct GNUNET_PQ_QueryParam params[] = {
    134     GNUNET_PQ_query_param_timestamp (&now),
    135     GNUNET_PQ_query_param_uint64 (&offset),
    136     GNUNET_PQ_query_param_uint64 (&plimit),
    137     GNUNET_PQ_query_param_end
    138   };
    139   enum GNUNET_DB_QueryStatus qs;
    140 
    141   GNUNET_assert (NULL != pg->current_merchant_id);
    142   GNUNET_assert (0 == strcmp (instance_id,
    143                               pg->current_merchant_id));
    144   if (limit > 0)
    145   {
    146     TMH_PQ_prepare_anon (pg,
    147                          "SELECT"
    148                          " token"
    149                          ",serial"
    150                          ",creation_time"
    151                          ",expiration_time"
    152                          ",validity_scope"
    153                          ",description"
    154                          " FROM merchant_login_tokens"
    155                          " WHERE expiration_time > $1"
    156                          "   AND serial > $2"
    157                          " ORDER BY serial ASC"
    158                          " LIMIT $3");
    159   }
    160   else
    161   {
    162     TMH_PQ_prepare_anon (pg,
    163                          "SELECT"
    164                          " token"
    165                          ",serial"
    166                          ",creation_time"
    167                          ",expiration_time"
    168                          ",validity_scope"
    169                          ",description"
    170                          " FROM merchant_login_tokens"
    171                          " WHERE expiration_time > $1"
    172                          "   AND serial < $2"
    173                          " ORDER BY serial DESC"
    174                          " LIMIT $3");
    175   }
    176   qs = GNUNET_PQ_eval_prepared_multi_select (
    177     pg->conn,
    178     "",
    179     params,
    180     &lookup_login_tokens_cb,
    181     &plc);
    182   /* If there was an error inside lookup_products_cb, return a hard error. */
    183   if (plc.extract_failed)
    184     return GNUNET_DB_STATUS_HARD_ERROR;
    185   return qs;
    186 }