merchant

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

lookup_login_tokens.c (5718B)


      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_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "merchant-database/lookup_login_tokens.h"
     26 #include "helper.h"
     27 
     28 /**
     29  * Context used for TALER_MERCHANTDB_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 TALER_MERCHANTDB_lookup_login_tokens (struct TALER_MERCHANTDB_PostgresContext *pg,
    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 GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get ();
    118   uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
    119   struct LookupLoginTokensContext plc = {
    120     .cb = cb,
    121     .cb_cls = cb_cls,
    122     /* Can be overwritten by the lookup_products_cb */
    123     .extract_failed = false,
    124   };
    125   struct GNUNET_PQ_QueryParam params[] = {
    126     GNUNET_PQ_query_param_string (instance_id),
    127     GNUNET_PQ_query_param_timestamp (&now),
    128     GNUNET_PQ_query_param_uint64 (&offset),
    129     GNUNET_PQ_query_param_uint64 (&plimit),
    130     GNUNET_PQ_query_param_end
    131   };
    132   enum GNUNET_DB_QueryStatus qs;
    133 
    134   check_connection (pg);
    135   PREPARE (pg,
    136            "lookup_login_tokens_asc",
    137            "SELECT"
    138            " token"
    139            ",serial"
    140            ",creation_time"
    141            ",expiration_time"
    142            ",validity_scope"
    143            ",description"
    144            " FROM merchant_login_tokens"
    145            " JOIN merchant_instances"
    146            "   USING (merchant_serial)"
    147            " WHERE merchant_instances.merchant_id=$1"
    148            "   AND expiration_time > $2"
    149            "   AND serial > $3"
    150            " ORDER BY serial ASC"
    151            " LIMIT $4");
    152   PREPARE (pg,
    153            "lookup_login_tokens_desc",
    154            "SELECT"
    155            " token"
    156            ",serial"
    157            ",creation_time"
    158            ",expiration_time"
    159            ",validity_scope"
    160            ",description"
    161            " FROM merchant_login_tokens"
    162            " JOIN merchant_instances"
    163            "   USING (merchant_serial)"
    164            " WHERE merchant_instances.merchant_id=$1"
    165            "   AND expiration_time > $2"
    166            "   AND serial < $3"
    167            " ORDER BY serial DESC"
    168            " LIMIT $4");
    169   qs = GNUNET_PQ_eval_prepared_multi_select (
    170     pg->conn,
    171     (limit > 0)
    172     ? "lookup_login_tokens_asc"
    173     : "lookup_login_tokens_desc",
    174     params,
    175     &lookup_login_tokens_cb,
    176     &plc);
    177   /* If there was an error inside lookup_products_cb, return a hard error. */
    178   if (plc.extract_failed)
    179     return GNUNET_DB_STATUS_HARD_ERROR;
    180   return qs;
    181 }