Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2022-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 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 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 taler-merchant-httpd_private-get-otp-devices-ID.c
18 : * @brief implement GET /otp-devices/$ID
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include "taler-merchant-httpd_private-get-otp-devices-ID.h"
23 : #include <taler/taler_json_lib.h>
24 :
25 :
26 : /**
27 : * Handle a GET "/otp-devices/$ID" request.
28 : *
29 : * @param rh context of the handler
30 : * @param connection the MHD connection to handle
31 : * @param[in,out] hc context with further information about the request
32 : * @return MHD result code
33 : */
34 : MHD_RESULT
35 0 : TMH_private_get_otp_devices_ID (const struct TMH_RequestHandler *rh,
36 : struct MHD_Connection *connection,
37 : struct TMH_HandlerContext *hc)
38 : {
39 0 : struct TMH_MerchantInstance *mi = hc->instance;
40 0 : struct TALER_MERCHANTDB_OtpDeviceDetails tp = { 0 };
41 : enum GNUNET_DB_QueryStatus qs;
42 0 : uint64_t faketime_s
43 0 : = GNUNET_TIME_timestamp_to_s (GNUNET_TIME_timestamp_get ());
44 : struct GNUNET_TIME_Timestamp my_time;
45 : struct TALER_Amount price;
46 :
47 0 : TALER_MHD_parse_request_number (connection,
48 : "faketime",
49 : &faketime_s);
50 0 : memset (&price,
51 : 0,
52 : sizeof (price));
53 0 : TALER_MHD_parse_request_amount (connection,
54 : "price",
55 : &price);
56 0 : my_time = GNUNET_TIME_timestamp_from_s (faketime_s);
57 0 : GNUNET_assert (NULL != mi);
58 0 : qs = TMH_db->select_otp (TMH_db->cls,
59 0 : mi->settings.id,
60 0 : hc->infix,
61 : &tp);
62 0 : if (0 > qs)
63 : {
64 0 : GNUNET_break (0);
65 0 : return TALER_MHD_reply_with_error (connection,
66 : MHD_HTTP_INTERNAL_SERVER_ERROR,
67 : TALER_EC_GENERIC_DB_FETCH_FAILED,
68 : "select_otp");
69 : }
70 0 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
71 : {
72 0 : return TALER_MHD_reply_with_error (connection,
73 : MHD_HTTP_NOT_FOUND,
74 : TALER_EC_MERCHANT_GENERIC_OTP_DEVICE_UNKNOWN,
75 0 : hc->infix);
76 : }
77 : {
78 : MHD_RESULT ret;
79 : char *pos_confirmation;
80 :
81 0 : pos_confirmation = (NULL == tp.otp_key)
82 : ? NULL
83 0 : : TALER_build_pos_confirmation (tp.otp_key,
84 : tp.otp_algorithm,
85 : &price,
86 : my_time);
87 : /* Note: we deliberately (by design) do not return the otp_key */
88 0 : ret = TALER_MHD_REPLY_JSON_PACK (
89 : connection,
90 : MHD_HTTP_OK,
91 : GNUNET_JSON_pack_string ("device_description",
92 : tp.otp_description),
93 : GNUNET_JSON_pack_allow_null (
94 : GNUNET_JSON_pack_string ("otp_code",
95 : pos_confirmation)),
96 : GNUNET_JSON_pack_uint64 ("otp_timestamp",
97 : faketime_s),
98 : GNUNET_JSON_pack_uint64 ("otp_algorithm",
99 : tp.otp_algorithm),
100 : GNUNET_JSON_pack_uint64 ("otp_ctr",
101 : tp.otp_ctr));
102 0 : GNUNET_free (pos_confirmation);
103 0 : GNUNET_free (tp.otp_description);
104 0 : GNUNET_free (tp.otp_key);
105 0 : return ret;
106 : }
107 : }
108 :
109 :
110 : /* end of taler-merchant-httpd_private-get-otp-devices-ID.c */
|