Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2023 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU Lesser General Public License as published by the Free Software
7 : Foundation; either version 2.1, 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 Lesser General Public License for more details.
12 :
13 : You should have received a copy of the GNU Lesser General Public License along with
14 : TALER; see the file COPYING.LGPL. If not, see
15 : <http://www.gnu.org/licenses/>
16 : */
17 : /**
18 : * @file merchant_api_get_instances.c
19 : * @brief Implementation of the GET /instances request of the merchant's HTTP API
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include <curl/curl.h>
24 : #include <jansson.h>
25 : #include <microhttpd.h> /* just for HTTP status codes */
26 : #include <gnunet/gnunet_util_lib.h>
27 : #include <gnunet/gnunet_curl_lib.h>
28 : #include "taler_merchant_service.h"
29 : #include "merchant_api_curl_defaults.h"
30 : #include <taler/taler_json_lib.h>
31 : #include <taler/taler_signatures.h>
32 :
33 :
34 : /**
35 : * Maximum number of instances permitted.
36 : */
37 : #define MAX_INSTANCES 1024
38 :
39 : /**
40 : * Handle for a GET /instances operation.
41 : */
42 : struct TALER_MERCHANT_InstancesGetHandle
43 : {
44 : /**
45 : * The url for this request.
46 : */
47 : char *url;
48 :
49 : /**
50 : * Handle for the request.
51 : */
52 : struct GNUNET_CURL_Job *job;
53 :
54 : /**
55 : * Function to call with the result.
56 : */
57 : TALER_MERCHANT_InstancesGetCallback cb;
58 :
59 : /**
60 : * Closure for @a cb.
61 : */
62 : void *cb_cls;
63 :
64 : /**
65 : * Reference to the execution context.
66 : */
67 : struct GNUNET_CURL_Context *ctx;
68 :
69 : };
70 :
71 :
72 : /**
73 : * Parse instance information from @a ia.
74 : *
75 : * @param json overall reply body
76 : * @param ia JSON array (or NULL!) with instance data
77 : * @param igh operation handle
78 : * @return #GNUNET_OK on success
79 : */
80 : static enum GNUNET_GenericReturnValue
81 4 : parse_instances (const json_t *json,
82 : const json_t *ia,
83 : struct TALER_MERCHANT_InstancesGetHandle *igh)
84 : {
85 4 : unsigned int iis_len = (unsigned int) json_array_size (ia);
86 :
87 4 : if ( (json_array_size (ia) != (size_t) iis_len) ||
88 : (iis_len > MAX_INSTANCES) )
89 : {
90 0 : GNUNET_break (0);
91 0 : return GNUNET_SYSERR;
92 : }
93 4 : {
94 4 : struct TALER_MERCHANT_InstanceInformation iis[GNUNET_NZL (iis_len)];
95 : size_t index;
96 : json_t *value;
97 4 : struct TALER_MERCHANT_InstancesGetResponse igr = {
98 : .hr.http_status = MHD_HTTP_OK,
99 : .hr.reply = json,
100 : .details.ok.iis_length = iis_len,
101 : .details.ok.iis = iis
102 : };
103 :
104 8 : json_array_foreach (ia, index, value) {
105 4 : struct TALER_MERCHANT_InstanceInformation *ii = &iis[index];
106 : const char *uts;
107 : struct GNUNET_JSON_Specification spec[] = {
108 4 : GNUNET_JSON_spec_string ("name",
109 : &ii->name),
110 4 : GNUNET_JSON_spec_string ("user_type",
111 : &uts),
112 4 : GNUNET_JSON_spec_string ("id",
113 : &ii->id),
114 4 : GNUNET_JSON_spec_fixed_auto ("merchant_pub",
115 : &ii->merchant_pub),
116 4 : GNUNET_JSON_spec_array_const ("payment_targets",
117 : &ii->payment_targets),
118 4 : GNUNET_JSON_spec_end ()
119 : };
120 :
121 4 : if (GNUNET_OK !=
122 4 : GNUNET_JSON_parse (value,
123 : spec,
124 : NULL, NULL))
125 : {
126 0 : GNUNET_break_op (0);
127 0 : return GNUNET_SYSERR;
128 : }
129 6 : for (size_t i = 0; i<json_array_size (ii->payment_targets); i++)
130 : {
131 2 : if (! json_is_string (json_array_get (ii->payment_targets,
132 : i)))
133 : {
134 0 : GNUNET_break_op (0);
135 0 : return GNUNET_SYSERR;
136 : }
137 : }
138 : } /* for all instances */
139 4 : igh->cb (igh->cb_cls,
140 : &igr);
141 4 : igh->cb = NULL; /* just to be sure */
142 : }
143 4 : return GNUNET_OK;
144 : }
145 :
146 :
147 : /**
148 : * Function called when we're done processing the
149 : * HTTP /instances request.
150 : *
151 : * @param cls the `struct TALER_MERCHANT_InstancesGetHandle`
152 : * @param response_code HTTP response code, 0 on error
153 : * @param response response body, NULL if not in JSON
154 : */
155 : static void
156 4 : handle_instances_finished (void *cls,
157 : long response_code,
158 : const void *response)
159 : {
160 4 : struct TALER_MERCHANT_InstancesGetHandle *igh = cls;
161 4 : const json_t *json = response;
162 4 : struct TALER_MERCHANT_InstancesGetResponse igr = {
163 4 : .hr.http_status = (unsigned int) response_code,
164 : .hr.reply = json
165 : };
166 :
167 4 : igh->job = NULL;
168 4 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 : "Got /instances response with status code %u\n",
170 : (unsigned int) response_code);
171 4 : switch (response_code)
172 : {
173 4 : case MHD_HTTP_OK:
174 : {
175 : const json_t *instances;
176 : struct GNUNET_JSON_Specification spec[] = {
177 4 : GNUNET_JSON_spec_array_const ("instances",
178 : &instances),
179 4 : GNUNET_JSON_spec_end ()
180 : };
181 :
182 4 : if (GNUNET_OK !=
183 4 : GNUNET_JSON_parse (json,
184 : spec,
185 : NULL, NULL))
186 : {
187 0 : igr.hr.http_status = 0;
188 0 : igr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
189 0 : break;
190 : }
191 4 : if (GNUNET_OK ==
192 4 : parse_instances (json,
193 : instances,
194 : igh))
195 : {
196 4 : TALER_MERCHANT_instances_get_cancel (igh);
197 4 : return;
198 : }
199 0 : igr.hr.http_status = 0;
200 0 : igr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
201 0 : break;
202 : }
203 0 : case MHD_HTTP_UNAUTHORIZED:
204 0 : igr.hr.ec = TALER_JSON_get_error_code (json);
205 0 : igr.hr.hint = TALER_JSON_get_error_hint (json);
206 0 : break;
207 0 : default:
208 : /* unexpected response code */
209 0 : igr.hr.ec = TALER_JSON_get_error_code (json);
210 0 : igr.hr.hint = TALER_JSON_get_error_hint (json);
211 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212 : "Unexpected response code %u/%d\n",
213 : (unsigned int) response_code,
214 : (int) igr.hr.ec);
215 0 : break;
216 : }
217 0 : igh->cb (igh->cb_cls,
218 : &igr);
219 0 : TALER_MERCHANT_instances_get_cancel (igh);
220 : }
221 :
222 :
223 : struct TALER_MERCHANT_InstancesGetHandle *
224 4 : TALER_MERCHANT_instances_get (struct GNUNET_CURL_Context *ctx,
225 : const char *backend_url,
226 : TALER_MERCHANT_InstancesGetCallback instances_cb,
227 : void *instances_cb_cls)
228 : {
229 : struct TALER_MERCHANT_InstancesGetHandle *igh;
230 : CURL *eh;
231 :
232 4 : igh = GNUNET_new (struct TALER_MERCHANT_InstancesGetHandle);
233 4 : igh->ctx = ctx;
234 4 : igh->cb = instances_cb;
235 4 : igh->cb_cls = instances_cb_cls;
236 4 : igh->url = TALER_url_join (backend_url,
237 : "management/instances",
238 : NULL);
239 4 : if (NULL == igh->url)
240 : {
241 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242 : "Could not construct request URL.\n");
243 0 : GNUNET_free (igh);
244 0 : return NULL;
245 : }
246 4 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 : "Requesting URL '%s'\n",
248 : igh->url);
249 4 : eh = TALER_MERCHANT_curl_easy_get_ (igh->url);
250 4 : igh->job = GNUNET_CURL_job_add (ctx,
251 : eh,
252 : &handle_instances_finished,
253 : igh);
254 4 : return igh;
255 : }
256 :
257 :
258 : void
259 4 : TALER_MERCHANT_instances_get_cancel (
260 : struct TALER_MERCHANT_InstancesGetHandle *igh)
261 : {
262 4 : if (NULL != igh->job)
263 0 : GNUNET_CURL_job_cancel (igh->job);
264 4 : GNUNET_free (igh->url);
265 4 : GNUNET_free (igh);
266 4 : }
|