taler-merchant-httpd_get-private-accounts-H_WIRE.c (6581B)
1 /* 2 This file is part of TALER 3 (C) 2023, 2026 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-accounts-H_WIRE.c 18 * @brief implement GET /accounts/$ID 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include "taler-merchant-httpd_get-private-accounts-H_WIRE.h" 23 #include <taler/taler_json_lib.h> 24 #include <taler/taler_templating_lib.h> 25 #include "merchant-database/get_account.h" 26 27 28 /** 29 * Handle a GET "/accounts/$ID" request. 30 * 31 * @param rh context of the handler 32 * @param connection the MHD connection to handle 33 * @param[in,out] hc context with further information about the request 34 * @return MHD result code 35 */ 36 enum MHD_Result 37 TMH_private_get_accounts_ID (const struct TMH_RequestHandler *rh, 38 struct MHD_Connection *connection, 39 struct TMH_HandlerContext *hc) 40 { 41 struct TMH_MerchantInstance *mi = hc->instance; 42 const char *h_wire_s = hc->infix; 43 struct TALER_MerchantWireHashP h_wire; 44 struct TALER_MERCHANTDB_AccountDetails tp = { 0 }; 45 enum GNUNET_DB_QueryStatus qs; 46 enum 47 { 48 POF_JSON, 49 POF_TEXT 50 } format; 51 enum MHD_Result ret; 52 53 GNUNET_assert (NULL != mi); 54 GNUNET_assert (NULL != h_wire_s); 55 if (GNUNET_OK != 56 GNUNET_STRINGS_string_to_data (h_wire_s, 57 strlen (h_wire_s), 58 &h_wire, 59 sizeof (h_wire))) 60 { 61 GNUNET_break_op (0); 62 return TALER_MHD_reply_with_error (connection, 63 MHD_HTTP_BAD_REQUEST, 64 TALER_EC_MERCHANT_GENERIC_H_WIRE_MALFORMED, 65 h_wire_s); 66 } 67 qs = TALER_MERCHANTDB_get_account (TMH_db, 68 mi->settings.id, 69 &h_wire, 70 &tp); 71 if (0 > qs) 72 { 73 GNUNET_break (0); 74 return TALER_MHD_reply_with_error (connection, 75 MHD_HTTP_INTERNAL_SERVER_ERROR, 76 TALER_EC_GENERIC_DB_FETCH_FAILED, 77 "get_account_serial"); 78 } 79 if (0 == qs) 80 { 81 return TALER_MHD_reply_with_error (connection, 82 MHD_HTTP_NOT_FOUND, 83 TALER_EC_MERCHANT_GENERIC_ACCOUNT_UNKNOWN, 84 hc->infix); 85 } 86 87 /* Determine desired output format from Accept header */ 88 { 89 const char *mime; 90 91 mime = MHD_lookup_connection_value (connection, 92 MHD_HEADER_KIND, 93 MHD_HTTP_HEADER_ACCEPT); 94 if (NULL == mime) 95 mime = "application/json"; 96 if (0 == strcmp (mime, 97 "*/*")) 98 mime = "application/json"; 99 if (0 == strcmp (mime, 100 "application/json")) 101 { 102 format = POF_JSON; 103 } 104 else if (0 == strcmp (mime, 105 "text/plain")) 106 { 107 format = POF_TEXT; 108 } 109 else 110 { 111 GNUNET_break_op (0); 112 ret = TALER_MHD_REPLY_JSON_PACK ( 113 connection, 114 MHD_HTTP_NOT_ACCEPTABLE, 115 GNUNET_JSON_pack_string ("hint", 116 mime)); 117 goto cleanup; 118 } 119 } 120 121 switch (format) 122 { 123 case POF_JSON: 124 ret = TALER_MHD_REPLY_JSON_PACK ( 125 connection, 126 MHD_HTTP_OK, 127 GNUNET_JSON_pack_bool ("active", 128 tp.active), 129 TALER_JSON_pack_full_payto ("payto_uri", 130 tp.payto_uri), 131 GNUNET_JSON_pack_data_auto ("h_wire", 132 &tp.h_wire), 133 GNUNET_JSON_pack_data_auto ("salt", 134 &tp.salt), 135 GNUNET_JSON_pack_allow_null ( 136 GNUNET_JSON_pack_string ("extra_wire_subject_metadata", 137 tp.extra_wire_subject_metadata)), 138 GNUNET_JSON_pack_allow_null ( 139 GNUNET_JSON_pack_string ("credit_facade_url", 140 tp.credit_facade_url))); 141 /* We do not return the credentials, as they may 142 be sensitive */ 143 break; 144 case POF_TEXT: 145 { 146 struct MHD_Response *resp; 147 json_t *obj; 148 unsigned int response_code = MHD_HTTP_OK; 149 enum GNUNET_GenericReturnValue res; 150 151 obj = GNUNET_JSON_PACK ( 152 GNUNET_JSON_pack_bool ("active", 153 tp.active), 154 TALER_JSON_pack_full_payto ("payto_uri", 155 tp.payto_uri)); 156 res = TALER_TEMPLATING_build (connection, 157 &response_code, 158 "account_status", 159 mi->settings.id, 160 NULL, 161 obj, 162 &resp); 163 json_decref (obj); 164 if (GNUNET_OK != res) 165 { 166 ret = TALER_MHD_REPLY_JSON_PACK ( 167 connection, 168 MHD_HTTP_NOT_ACCEPTABLE, 169 GNUNET_JSON_pack_string ("hint", 170 "text/plain")); 171 break; 172 } 173 TALER_MHD_add_global_headers (resp, 174 false); 175 GNUNET_break (MHD_YES == 176 MHD_add_response_header (resp, 177 MHD_HTTP_HEADER_CONTENT_TYPE, 178 "text/plain")); 179 ret = MHD_queue_response (connection, 180 response_code, 181 resp); 182 MHD_destroy_response (resp); 183 } 184 break; 185 } 186 cleanup: 187 json_decref (tp.credit_facade_credentials); 188 GNUNET_free (tp.extra_wire_subject_metadata); 189 GNUNET_free (tp.payto_uri.full_payto); 190 GNUNET_free (tp.credit_facade_url); 191 return ret; 192 } 193 194 195 /* end of taler-merchant-httpd_get-private-accounts-H_WIRE.c */