taler-merchant-httpd_patch-private-otp-devices-DEVICE_ID.c (4108B)
1 /* 2 This file is part of TALER 3 (C) 2022 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify 6 it under the terms of the GNU Affero General Public License as 7 published by the Free Software Foundation; either version 3, 8 or (at your option) any later version. 9 10 TALER is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public 16 License along with TALER; see the file COPYING. If not, 17 see <http://www.gnu.org/licenses/> 18 */ 19 20 /** 21 * @file src/backend/taler-merchant-httpd_patch-private-otp-devices-DEVICE_ID.c 22 * @brief implementing PATCH /otp-devices/$ID request handling 23 * @author Christian Grothoff 24 */ 25 #include "platform.h" 26 #include "taler-merchant-httpd_patch-private-otp-devices-DEVICE_ID.h" 27 #include "taler-merchant-httpd_helper.h" 28 #include <taler/taler_json_lib.h> 29 #include "merchant-database/update_otp.h" 30 31 32 enum MHD_Result 33 TMH_private_patch_otp_devices_ID (const struct TMH_RequestHandler *rh, 34 struct MHD_Connection *connection, 35 struct TMH_HandlerContext *hc) 36 { 37 struct TMH_MerchantInstance *mi = hc->instance; 38 const char *device_id = hc->infix; 39 struct TALER_MERCHANTDB_OtpDeviceDetails tp = {0}; 40 enum GNUNET_DB_QueryStatus qs; 41 struct GNUNET_JSON_Specification spec[] = { 42 GNUNET_JSON_spec_string ("otp_device_description", 43 (const char **) &tp.otp_description), 44 TALER_JSON_spec_otp_type ("otp_algorithm", 45 &tp.otp_algorithm), 46 GNUNET_JSON_spec_mark_optional ( 47 GNUNET_JSON_spec_uint64 ("otp_ctr", 48 &tp.otp_ctr), 49 NULL), 50 GNUNET_JSON_spec_mark_optional ( 51 52 TALER_JSON_spec_otp_key ("otp_key", 53 (const char **) &tp.otp_key), 54 NULL), 55 GNUNET_JSON_spec_end () 56 }; 57 58 GNUNET_assert (NULL != mi); 59 GNUNET_assert (NULL != device_id); 60 { 61 enum GNUNET_GenericReturnValue res; 62 63 res = TALER_MHD_parse_json_data (connection, 64 hc->request_body, 65 spec); 66 if (GNUNET_OK != res) 67 return (GNUNET_NO == res) 68 ? MHD_YES 69 : MHD_NO; 70 } 71 72 qs = TALER_MERCHANTDB_update_otp (TMH_db, 73 mi->settings.id, 74 device_id, 75 &tp); 76 { 77 enum MHD_Result ret = MHD_NO; 78 79 switch (qs) 80 { 81 case GNUNET_DB_STATUS_HARD_ERROR: 82 GNUNET_break (0); 83 ret = TALER_MHD_reply_with_error (connection, 84 MHD_HTTP_INTERNAL_SERVER_ERROR, 85 TALER_EC_GENERIC_DB_STORE_FAILED, 86 "update_pos"); 87 break; 88 case GNUNET_DB_STATUS_SOFT_ERROR: 89 GNUNET_break (0); 90 ret = TALER_MHD_reply_with_error (connection, 91 MHD_HTTP_INTERNAL_SERVER_ERROR, 92 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 93 "unexpected serialization problem"); 94 break; 95 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 96 ret = TALER_MHD_reply_with_error (connection, 97 MHD_HTTP_NOT_FOUND, 98 TALER_EC_MERCHANT_GENERIC_OTP_DEVICE_UNKNOWN, 99 device_id); 100 break; 101 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 102 ret = TALER_MHD_reply_static (connection, 103 MHD_HTTP_NO_CONTENT, 104 NULL, 105 NULL, 106 0); 107 break; 108 } 109 GNUNET_JSON_parse_free (spec); 110 return ret; 111 } 112 } 113 114 115 /* end of taler-merchant-httpd_patch-private-otp-devices-DEVICE_ID.c */