taler-merchant-httpd_get-management-instances-INSTANCE.c (8907B)
1 /* 2 This file is part of TALER 3 (C) 2019-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-management-instances-INSTANCE.c 18 * @brief implement GET /instances/$ID 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include "taler-merchant-httpd_get-management-instances-INSTANCE.h" 23 #include "taler-merchant-httpd_helper.h" 24 #include <taler/taler_json_lib.h> 25 #include <taler/taler_templating_lib.h> 26 27 28 /** 29 * Reply to a GET "/instances/$ID" request with the details a 30 * merchant needs to log in, as human-readable text. This is what 31 * the sign-up e-mail queued by the database contains. 32 * 33 * @param mi instance to return information about 34 * @param connection the MHD connection to handle 35 * @return MHD result code 36 */ 37 static enum MHD_Result 38 get_instances_ID_text (struct TMH_MerchantInstance *mi, 39 struct MHD_Connection *connection) 40 { 41 struct GNUNET_Buffer buf; 42 char *base_url; 43 struct MHD_Response *resp; 44 unsigned int response_code = MHD_HTTP_OK; 45 json_t *obj; 46 enum GNUNET_GenericReturnValue res; 47 enum MHD_Result ret; 48 49 /* The login page is at the top-level URL, whatever the instance. */ 50 if (GNUNET_OK != 51 TMH_base_url_by_connection (connection, 52 "admin", 53 &buf)) 54 { 55 GNUNET_break_op (0); 56 return TALER_MHD_reply_with_error (connection, 57 MHD_HTTP_BAD_REQUEST, 58 TALER_EC_GENERIC_PARAMETER_MISSING, 59 MHD_HTTP_HEADER_HOST); 60 } 61 GNUNET_buffer_write_path (&buf, 62 ""); 63 base_url = GNUNET_buffer_reap_str (&buf); 64 obj = GNUNET_JSON_PACK ( 65 GNUNET_JSON_pack_string ("id", 66 mi->settings.id), 67 GNUNET_JSON_pack_string ("name", 68 mi->settings.name), 69 GNUNET_JSON_pack_string ("base_url", 70 base_url)); 71 GNUNET_free (base_url); 72 res = TALER_TEMPLATING_build (connection, 73 &response_code, 74 "instance_details", 75 mi->settings.id, 76 NULL, 77 obj, 78 &resp); 79 json_decref (obj); 80 if (GNUNET_SYSERR == res) 81 { 82 GNUNET_break (0); 83 return MHD_NO; 84 } 85 if (GNUNET_OK == res) 86 { 87 TALER_MHD_add_global_headers (resp, 88 false); 89 GNUNET_break (MHD_YES == 90 MHD_add_response_header (resp, 91 MHD_HTTP_HEADER_CONTENT_TYPE, 92 "text/plain")); 93 } 94 ret = MHD_queue_response (connection, 95 response_code, 96 resp); 97 MHD_destroy_response (resp); 98 return ret; 99 } 100 101 102 /** 103 * Handle a GET "/instances/$ID" request. 104 * 105 * @param mi instance to return information about 106 * @param connection the MHD connection to handle 107 * @return MHD result code 108 */ 109 static enum MHD_Result 110 get_instances_ID (struct TMH_MerchantInstance *mi, 111 struct MHD_Connection *connection) 112 { 113 const char *mime; 114 json_t *ja; 115 json_t *auth; 116 117 GNUNET_assert (NULL != mi); 118 mime = MHD_lookup_connection_value (connection, 119 MHD_HEADER_KIND, 120 MHD_HTTP_HEADER_ACCEPT); 121 if ( (NULL == mime) || 122 (0 == strcmp (mime, 123 "*/*")) ) 124 mime = "application/json"; 125 if (0 == strcmp (mime, 126 "text/plain")) 127 return get_instances_ID_text (mi, 128 connection); 129 if (0 != strcmp (mime, 130 "application/json")) 131 { 132 GNUNET_break_op (0); 133 return TALER_MHD_REPLY_JSON_PACK ( 134 connection, 135 MHD_HTTP_NOT_ACCEPTABLE, 136 GNUNET_JSON_pack_string ("hint", 137 mime)); 138 } 139 ja = json_array (); 140 GNUNET_assert (NULL != ja); 141 for (struct TMH_WireMethod *wm = mi->wm_head; 142 NULL != wm; 143 wm = wm->next) 144 { 145 GNUNET_assert ( 146 0 == 147 json_array_append_new ( 148 ja, 149 GNUNET_JSON_PACK ( 150 TALER_JSON_pack_full_payto ( 151 "payto_uri", 152 wm->payto_uri), 153 GNUNET_JSON_pack_allow_null ( 154 GNUNET_JSON_pack_string ( 155 "credit_facade_url", 156 wm->credit_facade_url)), 157 GNUNET_JSON_pack_data_auto ("h_wire", 158 &wm->h_wire), 159 GNUNET_JSON_pack_data_auto ( 160 "salt", 161 &wm->wire_salt), 162 GNUNET_JSON_pack_bool ("active", 163 wm->active)))); 164 } 165 if (GNUNET_YES == TMH_strict_v19) 166 { 167 // When pre v19 is deprecated this if guard can be removed 168 // and the code below should never return "external" 169 GNUNET_assert (! GNUNET_is_zero (&mi->auth.auth_hash)); 170 } 171 auth = GNUNET_JSON_PACK ( 172 GNUNET_JSON_pack_string ("method", 173 GNUNET_is_zero (&mi->auth.auth_hash) 174 ? "external" 175 : "token")); 176 return TALER_MHD_REPLY_JSON_PACK ( 177 connection, 178 MHD_HTTP_OK, 179 GNUNET_JSON_pack_array_steal ("accounts", 180 ja), 181 GNUNET_JSON_pack_string ("name", 182 mi->settings.name), 183 GNUNET_JSON_pack_allow_null ( 184 GNUNET_JSON_pack_string ("website", 185 mi->settings.website)), 186 GNUNET_JSON_pack_allow_null ( 187 GNUNET_JSON_pack_string ("email", 188 mi->settings.email)), 189 GNUNET_JSON_pack_bool ("email_validated", 190 mi->settings.email_validated), 191 GNUNET_JSON_pack_allow_null ( 192 GNUNET_JSON_pack_string ("phone_number", 193 mi->settings.phone)), 194 GNUNET_JSON_pack_bool ("phone_validated", 195 mi->settings.phone_validated), 196 GNUNET_JSON_pack_allow_null ( 197 GNUNET_JSON_pack_string ("logo", 198 mi->settings.logo)), 199 GNUNET_JSON_pack_data_auto ("merchant_pub", 200 &mi->merchant_pub), 201 GNUNET_JSON_pack_object_incref ("address", 202 mi->settings.address), 203 GNUNET_JSON_pack_object_incref ("jurisdiction", 204 mi->settings.jurisdiction), 205 GNUNET_JSON_pack_bool ("use_stefan", 206 mi->settings.use_stefan), 207 GNUNET_JSON_pack_time_rel ("default_wire_transfer_delay", 208 mi->settings.default_wire_transfer_delay), 209 GNUNET_JSON_pack_time_rel ("default_pay_delay", 210 mi->settings.default_pay_delay), 211 GNUNET_JSON_pack_time_rel ("default_refund_delay", 212 mi->settings.default_refund_delay), 213 GNUNET_JSON_pack_time_rounder_interval ( 214 "default_wire_transfer_rounding_interval", 215 mi->settings.default_wire_transfer_rounding_interval), 216 GNUNET_JSON_pack_object_steal ("auth", 217 auth)); 218 } 219 220 221 enum MHD_Result 222 TMH_private_get_instances_ID (const struct TMH_RequestHandler *rh, 223 struct MHD_Connection *connection, 224 struct TMH_HandlerContext *hc) 225 { 226 struct TMH_MerchantInstance *mi = hc->instance; 227 228 return get_instances_ID (mi, 229 connection); 230 } 231 232 233 enum MHD_Result 234 TMH_private_get_instances_default_ID (const struct TMH_RequestHandler *rh, 235 struct MHD_Connection *connection, 236 struct TMH_HandlerContext *hc) 237 { 238 struct TMH_MerchantInstance *mi; 239 240 mi = TMH_lookup_instance (hc->infix); 241 if ( (NULL == mi) || 242 (mi->deleted) ) 243 { 244 return TALER_MHD_reply_with_error (connection, 245 MHD_HTTP_NOT_FOUND, 246 TALER_EC_MERCHANT_GENERIC_INSTANCE_UNKNOWN, 247 hc->infix); 248 } 249 return get_instances_ID (mi, 250 connection); 251 } 252 253 254 /* end of taler-merchant-httpd_get-management-instances-INSTANCE.c */