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