lookup_token_family_key.c (7394B)
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 (instance_id), 43 GNUNET_PQ_query_param_string (token_family_slug), 44 GNUNET_PQ_query_param_timestamp (&valid_at), 45 GNUNET_PQ_query_param_timestamp (&sign_until), 46 GNUNET_PQ_query_param_end 47 }; 48 49 check_connection (pg); 50 PREPARE (pg, 51 "lookup_token_family_key", 52 "SELECT" 53 " h_pub" 54 ",pub" 55 ",priv" 56 ",cipher_choice" 57 ",mtfk.signature_validity_start" 58 ",mtfk.signature_validity_end" 59 ",mtfk.private_key_deleted_at" 60 ",slug" 61 ",name" 62 ",description" 63 ",description_i18n::TEXT" 64 ",mtf.valid_after" 65 ",mtf.valid_before" 66 ",duration" 67 ",validity_granularity" 68 ",start_offset" 69 ",kind" 70 ",issued" 71 ",used" 72 " FROM merchant_token_families mtf" 73 " LEFT JOIN merchant_token_family_keys mtfk" 74 " USING (token_family_serial)" 75 " JOIN merchant_instances mi" 76 " USING (merchant_serial)" 77 " WHERE mi.merchant_id=$1" 78 " AND slug=$2" 79 " AND COALESCE ($3 >= mtfk.signature_validity_start, TRUE)" 80 " AND COALESCE ($3 <= mtfk.signature_validity_end, TRUE)" 81 " AND COALESCE ($4 <= mtfk.private_key_deleted_at, TRUE)" 82 " ORDER BY mtfk.signature_validity_start ASC" 83 " LIMIT 1"); 84 85 if (NULL == details) 86 { 87 struct GNUNET_PQ_ResultSpec rs_null[] = { 88 GNUNET_PQ_result_spec_end 89 }; 90 91 return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 92 "lookup_token_family_key", 93 params, 94 rs_null); 95 } 96 97 { 98 char *kind; 99 enum GNUNET_DB_QueryStatus qs; 100 struct GNUNET_PQ_ResultSpec rs[] = { 101 GNUNET_PQ_result_spec_allow_null ( 102 GNUNET_PQ_result_spec_blind_sign_pub ("pub", 103 &details->pub.public_key), 104 NULL), 105 GNUNET_PQ_result_spec_allow_null ( 106 GNUNET_PQ_result_spec_blind_sign_priv ("priv", 107 &details->priv.private_key), 108 NULL), 109 GNUNET_PQ_result_spec_allow_null ( 110 GNUNET_PQ_result_spec_timestamp ("signature_validity_start", 111 &details->signature_validity_start), 112 NULL), 113 GNUNET_PQ_result_spec_allow_null ( 114 GNUNET_PQ_result_spec_timestamp ("signature_validity_end", 115 &details->signature_validity_end), 116 NULL), 117 GNUNET_PQ_result_spec_allow_null ( 118 GNUNET_PQ_result_spec_timestamp ("private_key_deleted_at", 119 &details->private_key_deleted_at), 120 NULL), 121 GNUNET_PQ_result_spec_string ("slug", 122 &details->token_family.slug), 123 GNUNET_PQ_result_spec_string ("name", 124 &details->token_family.name), 125 GNUNET_PQ_result_spec_string ("cipher_choice", 126 &details->token_family.cipher_spec), 127 GNUNET_PQ_result_spec_string ("description", 128 &details->token_family.description), 129 TALER_PQ_result_spec_json ("description_i18n", 130 &details->token_family.description_i18n), 131 GNUNET_PQ_result_spec_timestamp ("valid_after", 132 &details->token_family.valid_after), 133 GNUNET_PQ_result_spec_timestamp ("valid_before", 134 &details->token_family.valid_before), 135 GNUNET_PQ_result_spec_relative_time ("duration", 136 &details->token_family.duration), 137 GNUNET_PQ_result_spec_relative_time ("validity_granularity", 138 &details->token_family. 139 validity_granularity), 140 GNUNET_PQ_result_spec_relative_time ("start_offset", 141 &details->token_family.start_offset), 142 GNUNET_PQ_result_spec_string ("kind", 143 &kind), 144 GNUNET_PQ_result_spec_uint64 ("issued", 145 &details->token_family.issued), 146 GNUNET_PQ_result_spec_uint64 ("used", 147 &details->token_family.used), 148 GNUNET_PQ_result_spec_end 149 }; 150 151 memset (details, 152 0, 153 sizeof (*details)); 154 qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 155 "lookup_token_family_key", 156 params, 157 rs); 158 if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs) 159 { 160 if (0 == strcmp (kind, 161 "discount")) 162 { 163 details->token_family.kind = TALER_MERCHANTDB_TFK_Discount; 164 } 165 else if (0 == strcmp (kind, 166 "subscription")) 167 { 168 details->token_family.kind = TALER_MERCHANTDB_TFK_Subscription; 169 } 170 else 171 { 172 GNUNET_free (kind); 173 GNUNET_free (details->token_family.slug); 174 GNUNET_free (details->token_family.name); 175 GNUNET_free (details->token_family.description); 176 json_decref (details->token_family.description_i18n); 177 GNUNET_CRYPTO_blind_sign_pub_decref (details->pub.public_key); 178 GNUNET_CRYPTO_blind_sign_priv_decref (details->priv.private_key); 179 GNUNET_free (details->token_family.cipher_spec); 180 GNUNET_break (0); 181 return GNUNET_DB_STATUS_HARD_ERROR; 182 } 183 GNUNET_free (kind); 184 } 185 return qs; 186 } 187 }