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_instance.c
19 : * @brief Implementation of the GET /instance/$ID 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_kyclogic_lib.h>
32 : #include <taler/taler_signatures.h>
33 :
34 :
35 : /**
36 : * Handle for a GET /instances/$ID operation.
37 : */
38 : struct TALER_MERCHANT_InstanceGetHandle
39 : {
40 : /**
41 : * The url for this request.
42 : */
43 : char *url;
44 :
45 : /**
46 : * Handle for the request.
47 : */
48 : struct GNUNET_CURL_Job *job;
49 :
50 : /**
51 : * Function to call with the result.
52 : */
53 : TALER_MERCHANT_InstanceGetCallback cb;
54 :
55 : /**
56 : * Closure for @a cb.
57 : */
58 : void *cb_cls;
59 :
60 : /**
61 : * Reference to the execution context.
62 : */
63 : struct GNUNET_CURL_Context *ctx;
64 :
65 : };
66 :
67 :
68 : /**
69 : * Function called when we're done processing the
70 : * HTTP GET /instances/$ID request.
71 : *
72 : * @param cls the `struct TALER_MERCHANT_InstanceGetHandle`
73 : * @param response_code HTTP response code, 0 on error
74 : * @param response response body, NULL if not in JSON
75 : */
76 : static void
77 9 : handle_get_instance_finished (void *cls,
78 : long response_code,
79 : const void *response)
80 : {
81 9 : struct TALER_MERCHANT_InstanceGetHandle *igh = cls;
82 9 : const json_t *json = response;
83 9 : struct TALER_MERCHANT_InstanceGetResponse igr = {
84 9 : .hr.http_status = (unsigned int) response_code,
85 : .hr.reply = json
86 : };
87 :
88 9 : igh->job = NULL;
89 9 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
90 : "Got /instances/$ID response with status code %u\n",
91 : (unsigned int) response_code);
92 9 : switch (response_code)
93 : {
94 5 : case MHD_HTTP_OK:
95 : {
96 : const json_t *address;
97 : const json_t *jurisdiction;
98 : struct GNUNET_JSON_Specification spec[] = {
99 5 : GNUNET_JSON_spec_string (
100 : "name",
101 : &igr.details.ok.details.name),
102 5 : GNUNET_JSON_spec_fixed_auto (
103 : "merchant_pub",
104 : &igr.details.ok.details.merchant_pub),
105 5 : GNUNET_JSON_spec_object_const (
106 : "address",
107 : &address),
108 5 : GNUNET_JSON_spec_object_const (
109 : "jurisdiction",
110 : &jurisdiction),
111 5 : GNUNET_JSON_spec_bool (
112 : "use_stefan",
113 : &igr.details.ok.details.use_stefan),
114 5 : GNUNET_JSON_spec_relative_time (
115 : "default_wire_transfer_delay",
116 : &igr.details.ok.details.default_wire_transfer_delay),
117 5 : GNUNET_JSON_spec_relative_time (
118 : "default_pay_delay",
119 : &igr.details.ok.details.default_pay_delay),
120 5 : GNUNET_JSON_spec_end ()
121 : };
122 :
123 5 : if (GNUNET_OK !=
124 5 : GNUNET_JSON_parse (json,
125 : spec,
126 : NULL, NULL))
127 : {
128 0 : GNUNET_break_op (0);
129 0 : igr.hr.http_status = 0;
130 0 : igr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
131 0 : break;
132 : }
133 5 : igr.details.ok.details.address = address;
134 5 : igr.details.ok.details.jurisdiction = jurisdiction;
135 5 : igh->cb (igh->cb_cls,
136 : &igr);
137 5 : TALER_MERCHANT_instance_get_cancel (igh);
138 5 : return;
139 : }
140 0 : case MHD_HTTP_UNAUTHORIZED:
141 0 : igr.hr.ec = TALER_JSON_get_error_code (json);
142 0 : igr.hr.hint = TALER_JSON_get_error_hint (json);
143 : /* Nothing really to verify, merchant says we need to authenticate. */
144 0 : break;
145 4 : case MHD_HTTP_NOT_FOUND:
146 : /* instance does not exist */
147 4 : igr.hr.ec = TALER_JSON_get_error_code (json);
148 4 : igr.hr.hint = TALER_JSON_get_error_hint (json);
149 4 : break;
150 0 : default:
151 : /* unexpected response code */
152 0 : igr.hr.ec = TALER_JSON_get_error_code (json);
153 0 : igr.hr.hint = TALER_JSON_get_error_hint (json);
154 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155 : "Unexpected response code %u/%d\n",
156 : (unsigned int) response_code,
157 : (int) igr.hr.ec);
158 0 : break;
159 : }
160 4 : igh->cb (igh->cb_cls,
161 : &igr);
162 4 : TALER_MERCHANT_instance_get_cancel (igh);
163 : }
164 :
165 :
166 : struct TALER_MERCHANT_InstanceGetHandle *
167 9 : TALER_MERCHANT_instance_get (struct GNUNET_CURL_Context *ctx,
168 : const char *backend_url,
169 : const char *instance_id,
170 : TALER_MERCHANT_InstanceGetCallback cb,
171 : void *cb_cls)
172 : {
173 : struct TALER_MERCHANT_InstanceGetHandle *igh;
174 : CURL *eh;
175 :
176 9 : igh = GNUNET_new (struct TALER_MERCHANT_InstanceGetHandle);
177 9 : igh->ctx = ctx;
178 9 : igh->cb = cb;
179 9 : igh->cb_cls = cb_cls;
180 9 : if (NULL != instance_id)
181 : {
182 : char *path;
183 :
184 8 : GNUNET_asprintf (&path,
185 : "instances/%s/private",
186 : instance_id);
187 8 : igh->url = TALER_url_join (backend_url,
188 : path,
189 : NULL);
190 8 : GNUNET_free (path);
191 : }
192 : else
193 : {
194 1 : igh->url = TALER_url_join (backend_url,
195 : "private",
196 : NULL);
197 : }
198 9 : if (NULL == igh->url)
199 : {
200 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
201 : "Could not construct request URL.\n");
202 0 : GNUNET_free (igh);
203 0 : return NULL;
204 : }
205 9 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 : "Requesting URL '%s'\n",
207 : igh->url);
208 9 : eh = TALER_MERCHANT_curl_easy_get_ (igh->url);
209 9 : igh->job = GNUNET_CURL_job_add (ctx,
210 : eh,
211 : &handle_get_instance_finished,
212 : igh);
213 9 : return igh;
214 : }
215 :
216 :
217 : void
218 9 : TALER_MERCHANT_instance_get_cancel (
219 : struct TALER_MERCHANT_InstanceGetHandle *igh)
220 : {
221 9 : if (NULL != igh->job)
222 0 : GNUNET_CURL_job_cancel (igh->job);
223 9 : GNUNET_free (igh->url);
224 9 : GNUNET_free (igh);
225 9 : }
|