merchant

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

pg_lookup_login_tokens.c (5637B)


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