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