Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2022 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU Affero 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 Affero General Public License for more details.
12 :
13 : You should have received a copy of the GNU Affero General Public License along with
14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file taler-exchange-httpd_reserves_history.c
18 : * @brief Handle /reserves/$RESERVE_PUB/history requests
19 : * @author Florian Dold
20 : * @author Benedikt Mueller
21 : * @author Christian Grothoff
22 : */
23 : #include "platform.h"
24 : #include <gnunet/gnunet_util_lib.h>
25 : #include <jansson.h>
26 : #include "taler_mhd_lib.h"
27 : #include "taler_json_lib.h"
28 : #include "taler_dbevents.h"
29 : #include "taler-exchange-httpd_keys.h"
30 : #include "taler-exchange-httpd_reserves_history.h"
31 : #include "taler-exchange-httpd_responses.h"
32 :
33 :
34 : /**
35 : * How far do we allow a client's time to be off when
36 : * checking the request timestamp?
37 : */
38 : #define TIMESTAMP_TOLERANCE \
39 : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
40 :
41 :
42 : /**
43 : * Closure for #reserve_history_transaction.
44 : */
45 : struct ReserveHistoryContext
46 : {
47 : /**
48 : * Public key of the reserve the inquiry is about.
49 : */
50 : const struct TALER_ReservePublicKeyP *reserve_pub;
51 :
52 : /**
53 : * Timestamp of the request.
54 : */
55 : struct GNUNET_TIME_Timestamp timestamp;
56 :
57 : /**
58 : * Client signature approving the request.
59 : */
60 : struct TALER_ReserveSignatureP reserve_sig;
61 :
62 : /**
63 : * History of the reserve, set in the callback.
64 : */
65 : struct TALER_EXCHANGEDB_ReserveHistory *rh;
66 :
67 : /**
68 : * Global fees applying to the request.
69 : */
70 : const struct TEH_GlobalFee *gf;
71 :
72 : /**
73 : * Current reserve balance.
74 : */
75 : struct TALER_Amount balance;
76 : };
77 :
78 :
79 : /**
80 : * Send reserve history to client.
81 : *
82 : * @param connection connection to the client
83 : * @param rhc reserve history to return
84 : * @return MHD result code
85 : */
86 : static MHD_RESULT
87 0 : reply_reserve_history_success (struct MHD_Connection *connection,
88 : const struct ReserveHistoryContext *rhc)
89 : {
90 0 : const struct TALER_EXCHANGEDB_ReserveHistory *rh = rhc->rh;
91 : json_t *json_history;
92 :
93 0 : json_history = TEH_RESPONSE_compile_reserve_history (rh);
94 0 : if (NULL == json_history)
95 0 : return TALER_MHD_reply_with_error (connection,
96 : MHD_HTTP_INTERNAL_SERVER_ERROR,
97 : TALER_EC_GENERIC_JSON_ALLOCATION_FAILURE,
98 : NULL);
99 0 : return TALER_MHD_REPLY_JSON_PACK (
100 : connection,
101 : MHD_HTTP_OK,
102 : TALER_JSON_pack_amount ("balance",
103 : &rhc->balance),
104 : GNUNET_JSON_pack_array_steal ("history",
105 : json_history));
106 : }
107 :
108 :
109 : /**
110 : * Function implementing /reserves/$RID/history transaction. Given the public
111 : * key of a reserve, return the associated transaction history. Runs the
112 : * transaction logic; IF it returns a non-error code, the transaction logic
113 : * MUST NOT queue a MHD response. IF it returns an hard error, the
114 : * transaction logic MUST queue a MHD response and set @a mhd_ret. IF it
115 : * returns the soft error code, the function MAY be called again to retry and
116 : * MUST not queue a MHD response.
117 : *
118 : * @param cls a `struct ReserveHistoryContext *`
119 : * @param connection MHD request which triggered the transaction
120 : * @param[out] mhd_ret set to MHD response status for @a connection,
121 : * if transaction failed (!); unused
122 : * @return transaction status
123 : */
124 : static enum GNUNET_DB_QueryStatus
125 0 : reserve_history_transaction (void *cls,
126 : struct MHD_Connection *connection,
127 : MHD_RESULT *mhd_ret)
128 : {
129 0 : struct ReserveHistoryContext *rsc = cls;
130 : enum GNUNET_DB_QueryStatus qs;
131 :
132 0 : if (! TALER_amount_is_zero (&rsc->gf->fees.history))
133 : {
134 0 : bool balance_ok = false;
135 0 : bool idempotent = true;
136 :
137 0 : qs = TEH_plugin->insert_history_request (TEH_plugin->cls,
138 : rsc->reserve_pub,
139 0 : &rsc->reserve_sig,
140 : rsc->timestamp,
141 0 : &rsc->gf->fees.history,
142 : &balance_ok,
143 : &idempotent);
144 0 : if (GNUNET_DB_STATUS_HARD_ERROR == qs)
145 : {
146 0 : GNUNET_break (0);
147 : *mhd_ret
148 0 : = TALER_MHD_reply_with_error (connection,
149 : MHD_HTTP_INTERNAL_SERVER_ERROR,
150 : TALER_EC_GENERIC_DB_FETCH_FAILED,
151 : "get_reserve_history");
152 : }
153 0 : if (qs <= 0)
154 : {
155 0 : GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
156 0 : return qs;
157 : }
158 0 : if (! balance_ok)
159 : {
160 0 : return TALER_MHD_reply_with_error (connection,
161 : MHD_HTTP_CONFLICT,
162 : TALER_EC_EXCHANGE_WITHDRAW_HISTORY_ERROR_INSUFFICIENT_FUNDS,
163 : NULL);
164 : }
165 0 : if (idempotent)
166 : {
167 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
168 : "Idempotent /reserves/history request observed. Is caching working?\n");
169 : }
170 : }
171 0 : qs = TEH_plugin->get_reserve_history (TEH_plugin->cls,
172 : rsc->reserve_pub,
173 : &rsc->balance,
174 : &rsc->rh);
175 0 : if (GNUNET_DB_STATUS_HARD_ERROR == qs)
176 : {
177 0 : GNUNET_break (0);
178 : *mhd_ret
179 0 : = TALER_MHD_reply_with_error (connection,
180 : MHD_HTTP_INTERNAL_SERVER_ERROR,
181 : TALER_EC_GENERIC_DB_FETCH_FAILED,
182 : "get_reserve_history");
183 : }
184 0 : return qs;
185 : }
186 :
187 :
188 : MHD_RESULT
189 0 : TEH_handler_reserves_history (struct TEH_RequestContext *rc,
190 : const struct TALER_ReservePublicKeyP *reserve_pub,
191 : const json_t *root)
192 : {
193 : struct ReserveHistoryContext rsc;
194 : MHD_RESULT mhd_ret;
195 : struct GNUNET_JSON_Specification spec[] = {
196 0 : GNUNET_JSON_spec_timestamp ("request_timestamp",
197 : &rsc.timestamp),
198 0 : GNUNET_JSON_spec_fixed_auto ("reserve_sig",
199 : &rsc.reserve_sig),
200 0 : GNUNET_JSON_spec_end ()
201 : };
202 : struct GNUNET_TIME_Timestamp now;
203 :
204 0 : rsc.reserve_pub = reserve_pub;
205 : {
206 : enum GNUNET_GenericReturnValue res;
207 :
208 0 : res = TALER_MHD_parse_json_data (rc->connection,
209 : root,
210 : spec);
211 0 : if (GNUNET_SYSERR == res)
212 : {
213 0 : GNUNET_break (0);
214 0 : return MHD_NO; /* hard failure */
215 : }
216 0 : if (GNUNET_NO == res)
217 : {
218 0 : GNUNET_break_op (0);
219 0 : return MHD_YES; /* failure */
220 : }
221 : }
222 0 : now = GNUNET_TIME_timestamp_get ();
223 0 : if (! GNUNET_TIME_absolute_approx_eq (now.abs_time,
224 : rsc.timestamp.abs_time,
225 : TIMESTAMP_TOLERANCE))
226 : {
227 0 : GNUNET_break_op (0);
228 0 : return TALER_MHD_reply_with_error (rc->connection,
229 : MHD_HTTP_BAD_REQUEST,
230 : TALER_EC_EXCHANGE_GENERIC_CLOCK_SKEW,
231 : NULL);
232 : }
233 : {
234 : struct TEH_KeyStateHandle *keys;
235 :
236 0 : keys = TEH_keys_get_state ();
237 0 : if (NULL == keys)
238 : {
239 0 : GNUNET_break (0);
240 0 : GNUNET_JSON_parse_free (spec);
241 0 : return TALER_MHD_reply_with_error (rc->connection,
242 : MHD_HTTP_INTERNAL_SERVER_ERROR,
243 : TALER_EC_EXCHANGE_GENERIC_KEYS_MISSING,
244 : NULL);
245 : }
246 0 : rsc.gf = TEH_keys_global_fee_by_time (keys,
247 : rsc.timestamp);
248 : }
249 0 : if (NULL == rsc.gf)
250 : {
251 0 : GNUNET_break (0);
252 0 : return TALER_MHD_reply_with_error (rc->connection,
253 : MHD_HTTP_INTERNAL_SERVER_ERROR,
254 : TALER_EC_EXCHANGE_GENERIC_BAD_CONFIGURATION,
255 : NULL);
256 : }
257 0 : if (GNUNET_OK !=
258 0 : TALER_wallet_reserve_history_verify (rsc.timestamp,
259 0 : &rsc.gf->fees.history,
260 : reserve_pub,
261 : &rsc.reserve_sig))
262 : {
263 0 : GNUNET_break_op (0);
264 0 : return TALER_MHD_reply_with_error (rc->connection,
265 : MHD_HTTP_FORBIDDEN,
266 : TALER_EC_EXCHANGE_RESERVES_HISTORY_BAD_SIGNATURE,
267 : NULL);
268 : }
269 0 : rsc.rh = NULL;
270 0 : if (GNUNET_OK !=
271 0 : TEH_DB_run_transaction (rc->connection,
272 : "get reserve history",
273 : TEH_MT_REQUEST_OTHER,
274 : &mhd_ret,
275 : &reserve_history_transaction,
276 : &rsc))
277 : {
278 0 : return mhd_ret;
279 : }
280 0 : if (NULL == rsc.rh)
281 : {
282 0 : return TALER_MHD_reply_with_error (rc->connection,
283 : MHD_HTTP_NOT_FOUND,
284 : TALER_EC_EXCHANGE_RESERVES_STATUS_UNKNOWN,
285 : NULL);
286 : }
287 0 : mhd_ret = reply_reserve_history_success (rc->connection,
288 : &rsc);
289 0 : TEH_plugin->free_reserve_history (TEH_plugin->cls,
290 : rsc.rh);
291 0 : return mhd_ret;
292 : }
293 :
294 :
295 : /* end of taler-exchange-httpd_reserves_history.c */
|