Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2023-2026 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-private-accounts-H_WIRE-new.c
19 : * @brief Implementation of the DELETE /private/accounts/$H_WIRE request
20 : * @author Christian Grothoff
21 : */
22 : #include "taler/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/delete-private-accounts-H_WIRE.h>
29 : #include "merchant_api_curl_defaults.h"
30 : #include <taler/taler_json_lib.h>
31 :
32 :
33 : /**
34 : * Handle for a DELETE /private/accounts/$H_WIRE operation.
35 : */
36 : struct TALER_MERCHANT_DeletePrivateAccountHandle
37 : {
38 : /**
39 : * Base URL of the merchant backend.
40 : */
41 : char *base_url;
42 :
43 : /**
44 : * The full URL for this request.
45 : */
46 : char *url;
47 :
48 : /**
49 : * Handle for the request.
50 : */
51 : struct GNUNET_CURL_Job *job;
52 :
53 : /**
54 : * Function to call with the result.
55 : */
56 : TALER_MERCHANT_DeletePrivateAccountCallback cb;
57 :
58 : /**
59 : * Closure for @a cb.
60 : */
61 : TALER_MERCHANT_DELETE_PRIVATE_ACCOUNT_RESULT_CLOSURE *cb_cls;
62 :
63 : /**
64 : * Reference to the execution context.
65 : */
66 : struct GNUNET_CURL_Context *ctx;
67 :
68 : /**
69 : * Hash of the wire details identifying the account to delete.
70 : */
71 : struct TALER_MerchantWireHashP h_wire;
72 : };
73 :
74 :
75 : /**
76 : * Function called when we're done processing the
77 : * HTTP DELETE /private/accounts/$H_WIRE request.
78 : *
79 : * @param cls the `struct TALER_MERCHANT_DeletePrivateAccountHandle`
80 : * @param response_code HTTP response code, 0 on error
81 : * @param response response body, NULL if not in JSON
82 : */
83 : static void
84 0 : handle_delete_account_finished (void *cls,
85 : long response_code,
86 : const void *response)
87 : {
88 0 : struct TALER_MERCHANT_DeletePrivateAccountHandle *dah = cls;
89 0 : const json_t *json = response;
90 0 : struct TALER_MERCHANT_DeletePrivateAccountResponse dar = {
91 0 : .hr.http_status = (unsigned int) response_code,
92 : .hr.reply = json
93 : };
94 :
95 0 : dah->job = NULL;
96 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97 : "Got /private/accounts/$H_WIRE DELETE response with status code %u\n",
98 : (unsigned int) response_code);
99 0 : switch (response_code)
100 : {
101 0 : case MHD_HTTP_NO_CONTENT:
102 0 : break;
103 0 : case MHD_HTTP_UNAUTHORIZED:
104 0 : dar.hr.ec = TALER_JSON_get_error_code (json);
105 0 : dar.hr.hint = TALER_JSON_get_error_hint (json);
106 0 : break;
107 0 : case MHD_HTTP_NOT_FOUND:
108 0 : dar.hr.ec = TALER_JSON_get_error_code (json);
109 0 : dar.hr.hint = TALER_JSON_get_error_hint (json);
110 0 : break;
111 0 : default:
112 0 : dar.hr.ec = TALER_JSON_get_error_code (json);
113 0 : dar.hr.hint = TALER_JSON_get_error_hint (json);
114 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
115 : "Unexpected response code %u/%d for DELETE /private/accounts/$H_WIRE\n",
116 : (unsigned int) response_code,
117 : (int) dar.hr.ec);
118 0 : break;
119 : }
120 0 : dah->cb (dah->cb_cls,
121 : &dar);
122 0 : TALER_MERCHANT_delete_private_account_cancel (dah);
123 0 : }
124 :
125 :
126 : struct TALER_MERCHANT_DeletePrivateAccountHandle *
127 0 : TALER_MERCHANT_delete_private_account_create (
128 : struct GNUNET_CURL_Context *ctx,
129 : const char *url,
130 : const struct TALER_MerchantWireHashP *h_wire)
131 : {
132 : struct TALER_MERCHANT_DeletePrivateAccountHandle *dah;
133 :
134 0 : dah = GNUNET_new (struct TALER_MERCHANT_DeletePrivateAccountHandle);
135 0 : dah->ctx = ctx;
136 0 : dah->base_url = GNUNET_strdup (url);
137 0 : dah->h_wire = *h_wire;
138 0 : return dah;
139 : }
140 :
141 :
142 : enum TALER_ErrorCode
143 0 : TALER_MERCHANT_delete_private_account_start (
144 : struct TALER_MERCHANT_DeletePrivateAccountHandle *dah,
145 : TALER_MERCHANT_DeletePrivateAccountCallback cb,
146 : TALER_MERCHANT_DELETE_PRIVATE_ACCOUNT_RESULT_CLOSURE *cb_cls)
147 : {
148 : CURL *eh;
149 :
150 0 : dah->cb = cb;
151 0 : dah->cb_cls = cb_cls;
152 : {
153 : char h_wire_str[sizeof (dah->h_wire) * 2];
154 : char *path;
155 : char *end;
156 :
157 0 : end = GNUNET_STRINGS_data_to_string (&dah->h_wire,
158 : sizeof (dah->h_wire),
159 : h_wire_str,
160 : sizeof (h_wire_str));
161 0 : *end = '\0';
162 0 : GNUNET_asprintf (&path,
163 : "private/accounts/%s",
164 : h_wire_str);
165 0 : dah->url = TALER_url_join (dah->base_url,
166 : path,
167 : NULL);
168 0 : GNUNET_free (path);
169 : }
170 0 : if (NULL == dah->url)
171 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
172 0 : eh = TALER_MERCHANT_curl_easy_get_ (dah->url);
173 0 : if (NULL == eh)
174 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
175 0 : GNUNET_assert (CURLE_OK ==
176 : curl_easy_setopt (eh,
177 : CURLOPT_CUSTOMREQUEST,
178 : MHD_HTTP_METHOD_DELETE));
179 0 : dah->job = GNUNET_CURL_job_add (dah->ctx,
180 : eh,
181 : &handle_delete_account_finished,
182 : dah);
183 0 : if (NULL == dah->job)
184 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
185 0 : return TALER_EC_NONE;
186 : }
187 :
188 :
189 : void
190 0 : TALER_MERCHANT_delete_private_account_cancel (
191 : struct TALER_MERCHANT_DeletePrivateAccountHandle *dah)
192 : {
193 0 : if (NULL != dah->job)
194 : {
195 0 : GNUNET_CURL_job_cancel (dah->job);
196 0 : dah->job = NULL;
197 : }
198 0 : GNUNET_free (dah->url);
199 0 : GNUNET_free (dah->base_url);
200 0 : GNUNET_free (dah);
201 0 : }
202 :
203 :
204 : /* end of merchant_api_delete-private-accounts-H_WIRE-new.c */
|