taler-merchant-httpd_get-private-otp-devices-DEVICE_ID.c (4137B)
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/select_otp.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 TMH_private_get_otp_devices_ID (const struct TMH_RequestHandler *rh, 37 struct MHD_Connection *connection, 38 struct TMH_HandlerContext *hc) 39 { 40 struct TMH_MerchantInstance *mi = hc->instance; 41 struct TALER_MERCHANTDB_OtpDeviceDetails tp = { 0 }; 42 enum GNUNET_DB_QueryStatus qs; 43 uint64_t faketime_s 44 = GNUNET_TIME_timestamp_to_s (GNUNET_TIME_timestamp_get ()); 45 struct GNUNET_TIME_Timestamp my_time; 46 struct TALER_Amount price; 47 48 TALER_MHD_parse_request_number (connection, 49 "faketime", 50 &faketime_s); 51 memset (&price, 52 0, 53 sizeof (price)); 54 TALER_MHD_parse_request_amount (connection, 55 "price", 56 &price); 57 my_time = GNUNET_TIME_timestamp_from_s (faketime_s); 58 GNUNET_assert (NULL != mi); 59 qs = TALER_MERCHANTDB_select_otp (TMH_db, 60 mi->settings.id, 61 hc->infix, 62 &tp); 63 if (0 > qs) 64 { 65 GNUNET_break (0); 66 return TALER_MHD_reply_with_error (connection, 67 MHD_HTTP_INTERNAL_SERVER_ERROR, 68 TALER_EC_GENERIC_DB_FETCH_FAILED, 69 "select_otp"); 70 } 71 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 72 { 73 return TALER_MHD_reply_with_error (connection, 74 MHD_HTTP_NOT_FOUND, 75 TALER_EC_MERCHANT_GENERIC_OTP_DEVICE_UNKNOWN, 76 hc->infix); 77 } 78 { 79 enum MHD_Result ret; 80 char *pos_confirmation; 81 82 pos_confirmation = (NULL == tp.otp_key) 83 ? NULL 84 : 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 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 GNUNET_free (pos_confirmation); 104 GNUNET_free (tp.otp_description); 105 GNUNET_free (tp.otp_key); 106 return ret; 107 } 108 } 109 110 111 /* end of taler-merchant-httpd_get-private-otp-devices-DEVICE_ID.c */