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 "merchant_api_curl_defaults.h"
30 : #include <taler/taler_json_lib.h>
31 : #include <taler/taler_signatures.h>
32 :
33 :
34 : /**
35 : * Handle for a DELETE /reserves/$RESERVE_PUB operation.
36 : */
37 : struct TALER_MERCHANT_ReserveDeleteHandle
38 : {
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_ReserveDeleteCallback 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 DELETE /reserves/$RESERVE_PUB request.
71 : *
72 : * @param cls the `struct TALER_MERCHANT_InstanceDeleteHandle`
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 0 : handle_delete_reserve_finished (void *cls,
78 : long response_code,
79 : const void *response)
80 : {
81 0 : struct TALER_MERCHANT_ReserveDeleteHandle *rdh = cls;
82 0 : const json_t *json = response;
83 0 : struct TALER_MERCHANT_HttpResponse hr = {
84 0 : .http_status = (unsigned int) response_code,
85 : .reply = json
86 : };
87 :
88 0 : rdh->job = NULL;
89 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
90 : "Got /reserves/$ID response with status code %u\n",
91 : (unsigned int) response_code);
92 0 : switch (response_code)
93 : {
94 0 : case MHD_HTTP_NO_CONTENT:
95 0 : break;
96 0 : case MHD_HTTP_UNAUTHORIZED:
97 0 : hr.ec = TALER_JSON_get_error_code (json);
98 0 : hr.hint = TALER_JSON_get_error_hint (json);
99 : /* Nothing really to verify, merchant says we need to authenticate. */
100 0 : break;
101 0 : case MHD_HTTP_NOT_FOUND:
102 0 : hr.ec = TALER_JSON_get_error_code (json);
103 0 : hr.hint = TALER_JSON_get_error_hint (json);
104 0 : break;
105 0 : default:
106 : /* unexpected response code */
107 0 : hr.ec = TALER_JSON_get_error_code (json);
108 0 : hr.hint = TALER_JSON_get_error_hint (json);
109 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
110 : "Unexpected response code %u/%d\n",
111 : (unsigned int) response_code,
112 : (int) hr.ec);
113 0 : break;
114 : }
115 0 : rdh->cb (rdh->cb_cls,
116 : &hr);
117 0 : TALER_MERCHANT_reserve_delete_cancel (rdh);
118 0 : }
119 :
120 :
121 : /**
122 : * Delete the private key of a reserve.
123 : *
124 : * @param ctx the context
125 : * @param backend_url HTTP base URL for the backend
126 : * @param reserve_pub which reserve should be deleted
127 : * @param purge purge instead of just deleting
128 : * @param cb function to call with the backend's return
129 : * @param cb_cls closure for @a config_cb
130 : * @return the instances handle; NULL upon error
131 : */
132 : static struct TALER_MERCHANT_ReserveDeleteHandle *
133 0 : reserve_delete (struct GNUNET_CURL_Context *ctx,
134 : const char *backend_url,
135 : const struct TALER_ReservePublicKeyP *reserve_pub,
136 : bool purge,
137 : TALER_MERCHANT_ReserveDeleteCallback cb,
138 : void *cb_cls)
139 : {
140 : struct TALER_MERCHANT_ReserveDeleteHandle *rdh;
141 :
142 0 : rdh = GNUNET_new (struct TALER_MERCHANT_ReserveDeleteHandle);
143 0 : rdh->ctx = ctx;
144 0 : rdh->cb = cb;
145 0 : rdh->cb_cls = cb_cls;
146 : {
147 : char res_str[sizeof (*reserve_pub) * 2];
148 : char arg_str[sizeof (res_str) + 32];
149 : char *end;
150 :
151 0 : end = GNUNET_STRINGS_data_to_string (reserve_pub,
152 : sizeof (*reserve_pub),
153 : res_str,
154 : sizeof (res_str));
155 0 : *end = '\0';
156 0 : GNUNET_snprintf (arg_str,
157 : sizeof (arg_str),
158 : "private/reserves/%s",
159 : res_str);
160 0 : if (purge)
161 0 : rdh->url = TALER_url_join (backend_url,
162 : arg_str,
163 : "purge",
164 : "yes",
165 : NULL);
166 : else
167 0 : rdh->url = TALER_url_join (backend_url,
168 : arg_str,
169 : NULL);
170 : }
171 0 : if (NULL == rdh->url)
172 : {
173 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
174 : "Could not construct request URL.\n");
175 0 : GNUNET_free (rdh);
176 0 : return NULL;
177 : }
178 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 : "Requesting URL '%s'\n",
180 : rdh->url);
181 : {
182 : CURL *eh;
183 :
184 0 : eh = TALER_MERCHANT_curl_easy_get_ (rdh->url);
185 0 : GNUNET_assert (CURLE_OK ==
186 : curl_easy_setopt (eh,
187 : CURLOPT_CUSTOMREQUEST,
188 : MHD_HTTP_METHOD_DELETE));
189 0 : rdh->job = GNUNET_CURL_job_add (ctx,
190 : eh,
191 : &handle_delete_reserve_finished,
192 : rdh);
193 : }
194 0 : return rdh;
195 : }
196 :
197 :
198 : struct TALER_MERCHANT_ReserveDeleteHandle *
199 0 : TALER_MERCHANT_reserve_delete (
200 : struct GNUNET_CURL_Context *ctx,
201 : const char *backend_url,
202 : const struct TALER_ReservePublicKeyP *reserve_pub,
203 : TALER_MERCHANT_ReserveDeleteCallback cb,
204 : void *cb_cls)
205 : {
206 0 : return reserve_delete (ctx,
207 : backend_url,
208 : reserve_pub,
209 : false,
210 : cb,
211 : cb_cls);
212 : }
213 :
214 :
215 : struct TALER_MERCHANT_ReserveDeleteHandle *
216 0 : TALER_MERCHANT_reserve_purge (struct GNUNET_CURL_Context *ctx,
217 : const char *backend_url,
218 : const struct TALER_ReservePublicKeyP *reserve_pub,
219 : TALER_MERCHANT_ReserveDeleteCallback cb,
220 : void *cb_cls)
221 : {
222 0 : return reserve_delete (ctx,
223 : backend_url,
224 : reserve_pub,
225 : true,
226 : cb,
227 : cb_cls);
228 : }
229 :
230 :
231 : void
232 0 : TALER_MERCHANT_reserve_delete_cancel (
233 : struct TALER_MERCHANT_ReserveDeleteHandle *rdh)
234 : {
235 0 : if (NULL != rdh->job)
236 0 : GNUNET_CURL_job_cancel (rdh->job);
237 0 : GNUNET_free (rdh->url);
238 0 : GNUNET_free (rdh);
239 0 : }
|