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