lookup_token_family_key.c (7095B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024 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_token_family_key.c 18 * @brief Implementation of the lookup_token_family_key function for Postgres 19 * @author Christian Blättler 20 */ 21 #include "platform.h" 22 #include <gnunet/gnunet_pq_lib.h> 23 #include <gnunet/gnunet_time_lib.h> 24 #include <string.h> 25 #include <taler/taler_error_codes.h> 26 #include <taler/taler_dbevents.h> 27 #include <taler/taler_pq_lib.h> 28 #include "merchant-database/lookup_token_family_key.h" 29 #include "helper.h" 30 31 32 enum GNUNET_DB_QueryStatus 33 TALER_MERCHANTDB_lookup_token_family_key ( 34 struct TALER_MERCHANTDB_PostgresContext *pg, 35 const char *instance_id, 36 const char *token_family_slug, 37 struct GNUNET_TIME_Timestamp valid_at, 38 struct GNUNET_TIME_Timestamp sign_until, 39 struct TALER_MERCHANTDB_TokenFamilyKeyDetails *details) 40 { 41 struct GNUNET_PQ_QueryParam params[] = { 42 GNUNET_PQ_query_param_string (token_family_slug), 43 GNUNET_PQ_query_param_timestamp (&valid_at), 44 GNUNET_PQ_query_param_timestamp (&sign_until), 45 GNUNET_PQ_query_param_end 46 }; 47 char *kind; 48 struct GNUNET_PQ_ResultSpec rs[] = { 49 GNUNET_PQ_result_spec_allow_null ( 50 GNUNET_PQ_result_spec_blind_sign_pub ("pub", 51 &details->pub.public_key), 52 NULL), 53 GNUNET_PQ_result_spec_allow_null ( 54 GNUNET_PQ_result_spec_blind_sign_priv ("priv", 55 &details->priv.private_key), 56 NULL), 57 GNUNET_PQ_result_spec_allow_null ( 58 GNUNET_PQ_result_spec_timestamp ("signature_validity_start", 59 &details->signature_validity_start), 60 NULL), 61 GNUNET_PQ_result_spec_allow_null ( 62 GNUNET_PQ_result_spec_timestamp ("signature_validity_end", 63 &details->signature_validity_end), 64 NULL), 65 GNUNET_PQ_result_spec_allow_null ( 66 GNUNET_PQ_result_spec_timestamp ("private_key_deleted_at", 67 &details->private_key_deleted_at), 68 NULL), 69 GNUNET_PQ_result_spec_string ("slug", 70 &details->token_family.slug), 71 GNUNET_PQ_result_spec_string ("name", 72 &details->token_family.name), 73 GNUNET_PQ_result_spec_string ("cipher_choice", 74 &details->token_family.cipher_spec), 75 GNUNET_PQ_result_spec_string ("description", 76 &details->token_family.description), 77 TALER_PQ_result_spec_json ("description_i18n", 78 &details->token_family.description_i18n), 79 GNUNET_PQ_result_spec_timestamp ("valid_after", 80 &details->token_family.valid_after), 81 GNUNET_PQ_result_spec_timestamp ("valid_before", 82 &details->token_family.valid_before), 83 GNUNET_PQ_result_spec_relative_time ("duration", 84 &details->token_family.duration), 85 GNUNET_PQ_result_spec_relative_time ("validity_granularity", 86 &details->token_family. 87 validity_granularity), 88 GNUNET_PQ_result_spec_relative_time ("start_offset", 89 &details->token_family.start_offset), 90 GNUNET_PQ_result_spec_string ("kind", 91 &kind), 92 GNUNET_PQ_result_spec_uint64 ("issued", 93 &details->token_family.issued), 94 GNUNET_PQ_result_spec_uint64 ("used", 95 &details->token_family.used), 96 GNUNET_PQ_result_spec_end 97 }; 98 enum GNUNET_DB_QueryStatus qs; 99 100 GNUNET_assert (NULL != pg->current_merchant_id); 101 GNUNET_assert (0 == strcmp (instance_id, 102 pg->current_merchant_id)); 103 check_connection (pg); 104 TMH_PQ_prepare_anon (pg, 105 "SELECT" 106 " h_pub" 107 ",pub" 108 ",priv" 109 ",cipher_choice" 110 ",mtfk.signature_validity_start" 111 ",mtfk.signature_validity_end" 112 ",mtfk.private_key_deleted_at" 113 ",slug" 114 ",name" 115 ",description" 116 ",description_i18n::TEXT" 117 ",mtf.valid_after" 118 ",mtf.valid_before" 119 ",duration" 120 ",validity_granularity" 121 ",start_offset" 122 ",kind" 123 ",issued" 124 ",used" 125 " FROM merchant_token_families mtf" 126 " LEFT JOIN merchant_token_family_keys mtfk" 127 " ON ( (mtf.token_family_serial = mtfk.token_family_serial)" 128 " AND ($2 >= mtfk.signature_validity_start)" 129 " AND ($2 <= mtfk.signature_validity_end)" 130 " AND ($3 <= mtfk.private_key_deleted_at) )" 131 " WHERE slug=$1" 132 " ORDER BY mtfk.signature_validity_start ASC" 133 " LIMIT 1"); 134 memset (details, 135 0, 136 sizeof (*details)); 137 qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 138 "", 139 params, 140 rs); 141 if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs) 142 { 143 if (0 == strcmp (kind, 144 "discount")) 145 { 146 details->token_family.kind = TALER_MERCHANTDB_TFK_Discount; 147 } 148 else if (0 == strcmp (kind, 149 "subscription")) 150 { 151 details->token_family.kind = TALER_MERCHANTDB_TFK_Subscription; 152 } 153 else 154 { 155 GNUNET_free (kind); 156 GNUNET_free (details->token_family.slug); 157 GNUNET_free (details->token_family.name); 158 GNUNET_free (details->token_family.description); 159 json_decref (details->token_family.description_i18n); 160 GNUNET_CRYPTO_blind_sign_pub_decref (details->pub.public_key); 161 GNUNET_CRYPTO_blind_sign_priv_decref (details->priv.private_key); 162 GNUNET_free (details->token_family.cipher_spec); 163 GNUNET_break (0); 164 return GNUNET_DB_STATUS_HARD_ERROR; 165 } 166 GNUNET_free (kind); 167 } 168 return qs; 169 }