Line data Source code
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/get_token_family_key.c
18 : * @brief Implementation of the get_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/get_token_family_key.h"
29 : #include "helper.h"
30 :
31 :
32 : enum GNUNET_DB_QueryStatus
33 10 : TALER_MERCHANTDB_get_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 10 : struct GNUNET_PQ_QueryParam params[] = {
42 10 : GNUNET_PQ_query_param_string (token_family_slug),
43 10 : GNUNET_PQ_query_param_timestamp (&valid_at),
44 10 : GNUNET_PQ_query_param_timestamp (&sign_until),
45 : GNUNET_PQ_query_param_end
46 : };
47 : char *kind;
48 10 : struct GNUNET_PQ_ResultSpec rs[] = {
49 10 : GNUNET_PQ_result_spec_allow_null (
50 : GNUNET_PQ_result_spec_blind_sign_pub ("pub",
51 : &details->pub.public_key),
52 : NULL),
53 10 : GNUNET_PQ_result_spec_allow_null (
54 : GNUNET_PQ_result_spec_blind_sign_priv ("priv",
55 : &details->priv.private_key),
56 : NULL),
57 10 : GNUNET_PQ_result_spec_allow_null (
58 : GNUNET_PQ_result_spec_timestamp ("signature_validity_start",
59 : &details->signature_validity_start),
60 : NULL),
61 10 : GNUNET_PQ_result_spec_allow_null (
62 : GNUNET_PQ_result_spec_timestamp ("signature_validity_end",
63 : &details->signature_validity_end),
64 : NULL),
65 10 : 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 10 : GNUNET_PQ_result_spec_string ("slug",
70 : &details->token_family.slug),
71 10 : GNUNET_PQ_result_spec_string ("name",
72 : &details->token_family.name),
73 10 : GNUNET_PQ_result_spec_string ("cipher_choice",
74 : &details->token_family.cipher_spec),
75 10 : GNUNET_PQ_result_spec_string ("description",
76 : &details->token_family.description),
77 10 : TALER_PQ_result_spec_json ("description_i18n",
78 : &details->token_family.description_i18n),
79 10 : GNUNET_PQ_result_spec_timestamp ("valid_after",
80 : &details->token_family.valid_after),
81 10 : GNUNET_PQ_result_spec_timestamp ("valid_before",
82 : &details->token_family.valid_before),
83 10 : GNUNET_PQ_result_spec_relative_time ("duration",
84 : &details->token_family.duration),
85 10 : GNUNET_PQ_result_spec_relative_time ("validity_granularity",
86 : &details->token_family.
87 : validity_granularity),
88 10 : GNUNET_PQ_result_spec_relative_time ("start_offset",
89 : &details->token_family.start_offset),
90 10 : GNUNET_PQ_result_spec_string ("kind",
91 : &kind),
92 10 : GNUNET_PQ_result_spec_uint64 ("issued",
93 : &details->token_family.issued),
94 10 : 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 10 : GNUNET_assert (NULL != pg->current_merchant_id);
101 10 : GNUNET_assert (0 == strcmp (instance_id,
102 : pg->current_merchant_id));
103 10 : TMH_PQ_prepare_anon (pg,
104 : "SELECT"
105 : " h_pub"
106 : ",pub"
107 : ",priv"
108 : ",cipher_choice"
109 : ",mtfk.signature_validity_start"
110 : ",mtfk.signature_validity_end"
111 : ",mtfk.private_key_deleted_at"
112 : ",slug"
113 : ",name"
114 : ",description"
115 : ",description_i18n::TEXT"
116 : ",mtf.valid_after"
117 : ",mtf.valid_before"
118 : ",duration"
119 : ",validity_granularity"
120 : ",start_offset"
121 : ",kind"
122 : ",issued"
123 : ",used"
124 : " FROM merchant_token_families mtf"
125 : " LEFT JOIN merchant_token_family_keys mtfk"
126 : " ON ( (mtf.token_family_serial = mtfk.token_family_serial)"
127 : " AND ($2 >= mtfk.signature_validity_start)"
128 : " AND ($2 <= mtfk.signature_validity_end)"
129 : " AND ($3 <= mtfk.private_key_deleted_at) )"
130 : " WHERE slug=$1"
131 : " ORDER BY mtfk.signature_validity_start ASC"
132 : " LIMIT 1");
133 10 : memset (details,
134 : 0,
135 : sizeof (*details));
136 10 : qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
137 : "",
138 : params,
139 : rs);
140 10 : if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
141 : {
142 10 : if (0 == strcmp (kind,
143 : "discount"))
144 : {
145 0 : details->token_family.kind = TALER_MERCHANTDB_TFK_Discount;
146 : }
147 10 : else if (0 == strcmp (kind,
148 : "subscription"))
149 : {
150 10 : details->token_family.kind = TALER_MERCHANTDB_TFK_Subscription;
151 : }
152 : else
153 : {
154 0 : GNUNET_free (kind);
155 0 : GNUNET_free (details->token_family.slug);
156 0 : GNUNET_free (details->token_family.name);
157 0 : GNUNET_free (details->token_family.description);
158 0 : json_decref (details->token_family.description_i18n);
159 0 : GNUNET_CRYPTO_blind_sign_pub_decref (details->pub.public_key);
160 0 : GNUNET_CRYPTO_blind_sign_priv_decref (details->priv.private_key);
161 0 : GNUNET_free (details->token_family.cipher_spec);
162 0 : GNUNET_break (0);
163 0 : return GNUNET_DB_STATUS_HARD_ERROR;
164 : }
165 10 : GNUNET_free (kind);
166 : }
167 10 : return qs;
168 : }
|