taler-merchant-httpd_get-private-otp-devices-DEVICE_ID.c (5150B)
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 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_get_otp_device (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 "get_otp_device"); 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 switch (tp.otp_algorithm) 83 { 84 case TALER_MCA_ECDSA_CHALLENGE: 85 case TALER_MCA_EDDSA_CHALLENGE: 86 /* the confirmation is a signature over the challenge of a 87 specific order, so there is no per-device code to compute 88 here; the device is identified by otp_device_pub below */ 89 pos_confirmation = NULL; 90 break; 91 case TALER_MCA_NONE: 92 case TALER_MCA_WITHOUT_PRICE: 93 case TALER_MCA_WITH_PRICE: 94 pos_confirmation = (NULL == tp.otp_key) 95 ? NULL 96 : TALER_build_pos_confirmation (tp.otp_key, 97 tp.otp_algorithm, 98 &price, 99 my_time); 100 /* Note: we deliberately (by design) do not return the otp_key */ 101 break; 102 default: 103 GNUNET_break (0); 104 GNUNET_free (tp.otp_description); 105 GNUNET_free (tp.otp_key); 106 GNUNET_free (tp.otp_device_pub); 107 return TALER_MHD_reply_with_error ( 108 connection, 109 MHD_HTTP_INTERNAL_SERVER_ERROR, 110 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 111 "unknown OTP algorithm"); 112 } 113 ret = TALER_MHD_REPLY_JSON_PACK ( 114 connection, 115 MHD_HTTP_OK, 116 GNUNET_JSON_pack_string ("device_description", 117 tp.otp_description), 118 GNUNET_JSON_pack_allow_null ( 119 GNUNET_JSON_pack_string ("otp_code", 120 pos_confirmation)), 121 GNUNET_JSON_pack_uint64 ("otp_timestamp", 122 faketime_s), 123 GNUNET_JSON_pack_uint64 ("otp_algorithm", 124 tp.otp_algorithm), 125 GNUNET_JSON_pack_uint64 ("otp_ctr", 126 tp.otp_ctr), 127 GNUNET_JSON_pack_allow_null ( 128 GNUNET_JSON_pack_string ("otp_device_pub", 129 tp.otp_device_pub))); 130 GNUNET_free (pos_confirmation); 131 GNUNET_free (tp.otp_description); 132 GNUNET_free (tp.otp_key); 133 GNUNET_free (tp.otp_device_pub); 134 return ret; 135 } 136 } 137 138 139 /* end of taler-merchant-httpd_get-private-otp-devices-DEVICE_ID.c */