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_delete_instance.c
19 : * @brief Implementation of the DELETE /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 <taler/taler_json_lib.h>
30 : #include <taler/taler_signatures.h>
31 :
32 :
33 : /**
34 : * Handle for a DELETE /instances/$ID operation.
35 : */
36 : struct TALER_MERCHANT_InstanceDeleteHandle
37 : {
38 : /**
39 : * The url for this request.
40 : */
41 : char *url;
42 :
43 : /**
44 : * Handle for the request.
45 : */
46 : struct GNUNET_CURL_Job *job;
47 :
48 : /**
49 : * Function to call with the result.
50 : */
51 : TALER_MERCHANT_InstanceDeleteCallback cb;
52 :
53 : /**
54 : * Closure for @a cb.
55 : */
56 : void *cb_cls;
57 :
58 : /**
59 : * Reference to the execution context.
60 : */
61 : struct GNUNET_CURL_Context *ctx;
62 :
63 : };
64 :
65 :
66 : /**
67 : * Function called when we're done processing the
68 : * HTTP GET /instances/$ID request.
69 : *
70 : * @param cls the `struct TALER_MERCHANT_InstanceDeleteHandle`
71 : * @param response_code HTTP response code, 0 on error
72 : * @param response response body, NULL if not in JSON
73 : */
74 : static void
75 0 : handle_delete_instance_finished (void *cls,
76 : long response_code,
77 : const void *response)
78 : {
79 0 : struct TALER_MERCHANT_InstanceDeleteHandle *idh = cls;
80 0 : const json_t *json = response;
81 0 : struct TALER_MERCHANT_HttpResponse hr = {
82 0 : .http_status = (unsigned int) response_code,
83 : .reply = json
84 : };
85 :
86 0 : idh->job = NULL;
87 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
88 : "Got /instances/$ID response with status code %u\n",
89 : (unsigned int) response_code);
90 0 : switch (response_code)
91 : {
92 0 : case MHD_HTTP_NO_CONTENT:
93 0 : break;
94 0 : case MHD_HTTP_NOT_FOUND:
95 0 : hr.ec = TALER_JSON_get_error_code (json);
96 0 : hr.hint = TALER_JSON_get_error_hint (json);
97 0 : break;
98 0 : default:
99 : /* unexpected response code */
100 0 : hr.ec = TALER_JSON_get_error_code (json);
101 0 : hr.hint = TALER_JSON_get_error_hint (json);
102 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
103 : "Unexpected response code %u/%d\n",
104 : (unsigned int) response_code,
105 : (int) hr.ec);
106 0 : break;
107 : }
108 0 : idh->cb (idh->cb_cls,
109 : &hr);
110 0 : TALER_MERCHANT_instance_delete_cancel (idh);
111 0 : }
112 :
113 :
114 : /**
115 : * Delete the private key of an instance of a backend, thereby disabling the
116 : * instance for future requests. Will preserve the other instance data
117 : * (i.e. for taxation).
118 : *
119 : * @param ctx the context
120 : * @param backend_url HTTP base URL for the backend
121 : * @param instance_id which instance should be deleted
122 : * @param purge purge instead of just deleting
123 : * @param cb function to call with the
124 : * backend's return
125 : * @param cb_cls closure for @a config_cb
126 : * @return the instances handle; NULL upon error
127 : */
128 : static struct TALER_MERCHANT_InstanceDeleteHandle *
129 0 : instance_delete (struct GNUNET_CURL_Context *ctx,
130 : const char *backend_url,
131 : const char *instance_id,
132 : bool purge,
133 : TALER_MERCHANT_InstanceDeleteCallback cb,
134 : void *cb_cls)
135 : {
136 : struct TALER_MERCHANT_InstanceDeleteHandle *idh;
137 :
138 0 : idh = GNUNET_new (struct TALER_MERCHANT_InstanceDeleteHandle);
139 0 : idh->ctx = ctx;
140 0 : idh->cb = cb;
141 0 : idh->cb_cls = cb_cls;
142 : {
143 : char *path;
144 :
145 0 : GNUNET_asprintf (&path,
146 : "private/instances/%s",
147 : instance_id);
148 0 : if (purge)
149 0 : idh->url = TALER_url_join (backend_url,
150 : path,
151 : "purge",
152 : "yes",
153 : NULL);
154 : else
155 0 : idh->url = TALER_url_join (backend_url,
156 : path,
157 : NULL);
158 0 : GNUNET_free (path);
159 : }
160 0 : if (NULL == idh->url)
161 : {
162 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
163 : "Could not construct request URL.\n");
164 0 : GNUNET_free (idh);
165 0 : return NULL;
166 : }
167 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 : "Requesting URL '%s'\n",
169 : idh->url);
170 : {
171 : CURL *eh;
172 :
173 0 : eh = curl_easy_init ();
174 0 : GNUNET_assert (CURLE_OK ==
175 : curl_easy_setopt (eh,
176 : CURLOPT_URL,
177 : idh->url));
178 0 : GNUNET_assert (CURLE_OK ==
179 : curl_easy_setopt (eh,
180 : CURLOPT_CUSTOMREQUEST,
181 : MHD_HTTP_METHOD_DELETE));
182 0 : idh->job = GNUNET_CURL_job_add (ctx,
183 : eh,
184 : &handle_delete_instance_finished,
185 : idh);
186 : }
187 0 : return idh;
188 : }
189 :
190 :
191 : struct TALER_MERCHANT_InstanceDeleteHandle *
192 0 : TALER_MERCHANT_instance_delete (struct GNUNET_CURL_Context *ctx,
193 : const char *backend_url,
194 : const char *instance_id,
195 : TALER_MERCHANT_InstanceDeleteCallback cb,
196 : void *cb_cls)
197 : {
198 0 : return instance_delete (ctx,
199 : backend_url,
200 : instance_id,
201 : false,
202 : cb,
203 : cb_cls);
204 : }
205 :
206 :
207 : struct TALER_MERCHANT_InstanceDeleteHandle *
208 0 : TALER_MERCHANT_instance_purge (struct GNUNET_CURL_Context *ctx,
209 : const char *backend_url,
210 : const char *instance_id,
211 : TALER_MERCHANT_InstanceDeleteCallback cb,
212 : void *cb_cls)
213 : {
214 0 : return instance_delete (ctx,
215 : backend_url,
216 : instance_id,
217 : true,
218 : cb,
219 : cb_cls);
220 : }
221 :
222 :
223 : void
224 0 : TALER_MERCHANT_instance_delete_cancel (
225 : struct TALER_MERCHANT_InstanceDeleteHandle *idh)
226 : {
227 0 : if (NULL != idh->job)
228 0 : GNUNET_CURL_job_cancel (idh->job);
229 0 : GNUNET_free (idh->url);
230 0 : GNUNET_free (idh);
231 0 : }
|