merchant

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

lookup_login_tokens.c (5562B)


      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/lookup_login_tokens.c
     18  * @brief Implementation of the lookup_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/lookup_login_tokens.h"
     24 #include "helper.h"
     25 
     26 /**
     27  * Context used for TALER_MERCHANTDB_lookup_products().
     28  */
     29 struct LookupLoginTokensContext
     30 {
     31   /**
     32    * Function to call with the results.
     33    */
     34   TALER_MERCHANTDB_LoginTokensCallback cb;
     35 
     36   /**
     37    * Closure for @a cb.
     38    */
     39   void *cb_cls;
     40 
     41   /**
     42    * Did database result extraction fail?
     43    */
     44   bool extract_failed;
     45 };
     46 
     47 
     48 /**
     49  * Function to be called with the results of a SELECT statement
     50  * that has returned @a num_results results about products.
     51  *
     52  * @param[in,out] cls of type `struct LookupProductsContext *`
     53  * @param result the postgres result
     54  * @param num_results the number of results in @a result
     55  */
     56 static void
     57 lookup_login_tokens_cb (void *cls,
     58                         PGresult *result,
     59                         unsigned int num_results)
     60 {
     61   struct LookupLoginTokensContext *plc = cls;
     62 
     63   for (unsigned int i = 0; i < num_results; i++)
     64   {
     65     uint32_t validity_scope;
     66     uint64_t serial;
     67     struct GNUNET_TIME_Timestamp expiration_time;
     68     struct GNUNET_TIME_Timestamp creation_time;
     69     char *description;
     70     struct TALER_MERCHANTDB_LoginTokenP token;
     71     struct GNUNET_PQ_ResultSpec rs[] = {
     72       GNUNET_PQ_result_spec_auto_from_type ("token",
     73                                             &token),
     74       GNUNET_PQ_result_spec_uint32 ("validity_scope",
     75                                     &validity_scope),
     76       GNUNET_PQ_result_spec_string ("description",
     77                                     &description),
     78       GNUNET_PQ_result_spec_timestamp ("creation_time",
     79                                        &creation_time),
     80       GNUNET_PQ_result_spec_timestamp ("expiration_time",
     81                                        &expiration_time),
     82       GNUNET_PQ_result_spec_uint64 ("serial",
     83                                     &serial),
     84       GNUNET_PQ_result_spec_end
     85     };
     86 
     87     if (GNUNET_OK !=
     88         GNUNET_PQ_extract_result (result,
     89                                   rs,
     90                                   i))
     91     {
     92       GNUNET_break (0);
     93       plc->extract_failed = true;
     94       return;
     95     }
     96     plc->cb (plc->cb_cls,
     97              creation_time,
     98              expiration_time,
     99              validity_scope,
    100              description,
    101              serial);
    102     GNUNET_PQ_cleanup_result (rs);
    103   }
    104 }
    105 
    106 
    107 enum GNUNET_DB_QueryStatus
    108 TALER_MERCHANTDB_lookup_login_tokens (
    109   struct TALER_MERCHANTDB_PostgresContext *pg,
    110   const char *instance_id,
    111   uint64_t offset,
    112   int64_t limit,
    113   TALER_MERCHANTDB_LoginTokensCallback cb,
    114   void *cb_cls)
    115 {
    116   struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get ();
    117   uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
    118   struct LookupLoginTokensContext plc = {
    119     .cb = cb,
    120     .cb_cls = cb_cls,
    121     /* Can be overwritten by the lookup_products_cb */
    122     .extract_failed = false,
    123   };
    124   struct GNUNET_PQ_QueryParam params[] = {
    125     GNUNET_PQ_query_param_timestamp (&now),
    126     GNUNET_PQ_query_param_uint64 (&offset),
    127     GNUNET_PQ_query_param_uint64 (&plimit),
    128     GNUNET_PQ_query_param_end
    129   };
    130   enum GNUNET_DB_QueryStatus qs;
    131 
    132   GNUNET_assert (NULL != pg->current_merchant_id);
    133   GNUNET_assert (0 == strcmp (instance_id,
    134                               pg->current_merchant_id));
    135   check_connection (pg);
    136   if (limit > 0)
    137   {
    138     TMH_PQ_prepare_anon (pg,
    139                          "SELECT"
    140                          " token"
    141                          ",serial"
    142                          ",creation_time"
    143                          ",expiration_time"
    144                          ",validity_scope"
    145                          ",description"
    146                          " FROM merchant_login_tokens"
    147                          " WHERE expiration_time > $1"
    148                          "   AND serial > $2"
    149                          " ORDER BY serial ASC"
    150                          " LIMIT $3");
    151   }
    152   else
    153   {
    154     TMH_PQ_prepare_anon (pg,
    155                          "SELECT"
    156                          " token"
    157                          ",serial"
    158                          ",creation_time"
    159                          ",expiration_time"
    160                          ",validity_scope"
    161                          ",description"
    162                          " FROM merchant_login_tokens"
    163                          " WHERE expiration_time > $1"
    164                          "   AND serial < $2"
    165                          " ORDER BY serial DESC"
    166                          " LIMIT $3");
    167   }
    168   qs = GNUNET_PQ_eval_prepared_multi_select (
    169     pg->conn,
    170     "",
    171     params,
    172     &lookup_login_tokens_cb,
    173     &plc);
    174   /* If there was an error inside lookup_products_cb, return a hard error. */
    175   if (plc.extract_failed)
    176     return GNUNET_DB_STATUS_HARD_ERROR;
    177   return qs;
    178 }