Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2018, 2020 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_config.c
19 : * @brief Implementation of the /config 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 <taler/taler_json_lib.h>
30 : #include <taler/taler_signatures.h>
31 :
32 : /**
33 : * Which version of the Taler protocol is implemented
34 : * by this library? Used to determine compatibility.
35 : */
36 : #define MERCHANT_PROTOCOL_CURRENT 1
37 :
38 : /**
39 : * How many configs are we backwards compatible with?
40 : */
41 : #define MERCHANT_PROTOCOL_AGE 0
42 :
43 :
44 : /**
45 : * @brief A handle for /config operations
46 : */
47 : struct TALER_MERCHANT_ConfigGetHandle
48 : {
49 : /**
50 : * The url for this request.
51 : */
52 : char *url;
53 :
54 : /**
55 : * Handle for the request.
56 : */
57 : struct GNUNET_CURL_Job *job;
58 :
59 : /**
60 : * Function to call with the result.
61 : */
62 : TALER_MERCHANT_ConfigCallback cb;
63 :
64 : /**
65 : * Closure for @a cb.
66 : */
67 : void *cb_cls;
68 :
69 : /**
70 : * Reference to the execution context.
71 : */
72 : struct GNUNET_CURL_Context *ctx;
73 :
74 : };
75 :
76 :
77 : /**
78 : * Function called when we're done processing the
79 : * HTTP /config request.
80 : *
81 : * @param cls the `struct TALER_MERCHANT_ConfigGetHandle`
82 : * @param response_code HTTP response code, 0 on error
83 : * @param response response body, NULL if not in JSON
84 : */
85 : static void
86 0 : handle_config_finished (void *cls,
87 : long response_code,
88 : const void *response)
89 : {
90 0 : struct TALER_MERCHANT_ConfigGetHandle *vgh = cls;
91 0 : const json_t *json = response;
92 0 : struct TALER_MERCHANT_HttpResponse hr = {
93 0 : .http_status = (unsigned int) response_code,
94 : .reply = json
95 : };
96 :
97 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
98 : "Got /config response with status code %u\n",
99 : (unsigned int) response_code);
100 :
101 0 : vgh->job = NULL;
102 0 : switch (response_code)
103 : {
104 0 : case MHD_HTTP_OK:
105 : {
106 : struct TALER_MERCHANT_ConfigInformation vi;
107 0 : enum TALER_MERCHANT_VersionCompatibility vc =
108 : TALER_MERCHANT_VC_PROTOCOL_ERROR;
109 : struct GNUNET_JSON_Specification spec[] = {
110 0 : GNUNET_JSON_spec_string ("currency",
111 : &vi.currency),
112 0 : GNUNET_JSON_spec_string ("version",
113 : &vi.version),
114 0 : GNUNET_JSON_spec_end ()
115 : };
116 :
117 0 : if (GNUNET_OK !=
118 0 : GNUNET_JSON_parse (json,
119 : spec,
120 : NULL, NULL))
121 : {
122 0 : hr.http_status = 0;
123 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
124 : }
125 : else
126 : {
127 : unsigned int age;
128 : unsigned int revision;
129 : unsigned int current;
130 :
131 0 : if (3 != sscanf (vi.version,
132 : "%u:%u:%u",
133 : ¤t,
134 : &revision,
135 : &age))
136 : {
137 0 : hr.http_status = 0;
138 0 : hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
139 : }
140 : else
141 : {
142 0 : vc = TALER_MERCHANT_VC_MATCH;
143 0 : if (MERCHANT_PROTOCOL_CURRENT < current)
144 : {
145 0 : vc |= TALER_MERCHANT_VC_NEWER;
146 0 : if (MERCHANT_PROTOCOL_CURRENT < current - age)
147 0 : vc |= TALER_MERCHANT_VC_INCOMPATIBLE;
148 : }
149 0 : if (MERCHANT_PROTOCOL_CURRENT > current)
150 : {
151 0 : vc |= TALER_MERCHANT_VC_OLDER;
152 0 : if (MERCHANT_PROTOCOL_CURRENT - MERCHANT_PROTOCOL_AGE > current)
153 0 : vc |= TALER_MERCHANT_VC_INCOMPATIBLE;
154 : }
155 : }
156 : }
157 0 : vgh->cb (vgh->cb_cls,
158 : &hr,
159 : &vi,
160 : vc);
161 0 : TALER_MERCHANT_config_get_cancel (vgh);
162 0 : return;
163 : }
164 0 : default:
165 : /* unexpected response code */
166 0 : hr.ec = TALER_JSON_get_error_code (json);
167 0 : hr.hint = TALER_JSON_get_error_hint (json);
168 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
169 : "Unexpected response code %u/%d\n",
170 : (unsigned int) response_code,
171 : (int) hr.ec);
172 0 : vgh->cb (vgh->cb_cls,
173 : &hr,
174 : NULL,
175 : TALER_MERCHANT_VC_PROTOCOL_ERROR);
176 0 : break;
177 : }
178 0 : TALER_MERCHANT_config_get_cancel (vgh);
179 : }
180 :
181 :
182 : struct TALER_MERCHANT_ConfigGetHandle *
183 0 : TALER_MERCHANT_config_get (struct GNUNET_CURL_Context *ctx,
184 : const char *backend_url,
185 : TALER_MERCHANT_ConfigCallback config_cb,
186 : void *config_cb_cls)
187 : {
188 : struct TALER_MERCHANT_ConfigGetHandle *vgh;
189 : CURL *eh;
190 :
191 0 : vgh = GNUNET_new (struct TALER_MERCHANT_ConfigGetHandle);
192 0 : vgh->ctx = ctx;
193 0 : vgh->cb = config_cb;
194 0 : vgh->cb_cls = config_cb_cls;
195 0 : vgh->url = TALER_url_join (backend_url,
196 : "config",
197 : NULL);
198 0 : if (NULL == vgh->url)
199 : {
200 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
201 : "Could not construct request URL.\n");
202 0 : GNUNET_free (vgh);
203 0 : return NULL;
204 : }
205 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 : "Requesting URL '%s'\n",
207 : vgh->url);
208 0 : eh = curl_easy_init ();
209 0 : GNUNET_assert (CURLE_OK ==
210 : curl_easy_setopt (eh,
211 : CURLOPT_URL,
212 : vgh->url));
213 :
214 0 : vgh->job = GNUNET_CURL_job_add (ctx,
215 : eh,
216 : &handle_config_finished,
217 : vgh);
218 0 : return vgh;
219 : }
220 :
221 :
222 : void
223 0 : TALER_MERCHANT_config_get_cancel (struct TALER_MERCHANT_ConfigGetHandle *vgh)
224 : {
225 0 : if (NULL != vgh->job)
226 : {
227 0 : GNUNET_CURL_job_cancel (vgh->job);
228 0 : vgh->job = NULL;
229 : }
230 0 : GNUNET_free (vgh->url);
231 0 : GNUNET_free (vgh);
232 0 : }
233 :
234 :
235 : /* end of merchant_api_config_get.c */
|