Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2025 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_token.c
19 : * @brief Implementation of the DELETE /instance/$ID/private/token request of the merchant's HTTP API
20 : * @author Martin Schanzenbach
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 /instance/$ID/private/token operation.
36 : */
37 : struct TALER_MERCHANT_InstanceTokenDeleteHandle
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_InstanceTokenDeleteCallback 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 /instance/$ID/private/token request.
70 : *
71 : * @param cls the `struct TALER_MERCHANT_TokenDeleteHandle`
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 2 : handle_delete_token_finished (void *cls,
77 : long response_code,
78 : const void *response)
79 : {
80 2 : struct TALER_MERCHANT_InstanceTokenDeleteHandle *tdh = cls;
81 2 : const json_t *json = response;
82 2 : struct TALER_MERCHANT_HttpResponse tdr = {
83 2 : .http_status = (unsigned int) response_code,
84 : .reply = json
85 : };
86 :
87 2 : tdh->job = NULL;
88 2 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
89 : "Got /instances/$ID/private/token response with status code %u\n",
90 : (unsigned int) response_code);
91 2 : switch (response_code)
92 : {
93 2 : case MHD_HTTP_NO_CONTENT:
94 2 : break;
95 0 : case MHD_HTTP_UNAUTHORIZED:
96 0 : tdr.ec = TALER_JSON_get_error_code (json);
97 0 : tdr.hint = TALER_JSON_get_error_hint (json);
98 : /* Nothing really to verify, merchant says we need to authenticate. */
99 0 : break;
100 0 : case MHD_HTTP_NOT_FOUND:
101 0 : break;
102 0 : default:
103 : /* unexpected response code */
104 0 : tdr.ec = TALER_JSON_get_error_code (json);
105 0 : tdr.hint = TALER_JSON_get_error_hint (json);
106 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
107 : "Unexpected response code %u/%d for DELETE /instance/$ID/private/token\n",
108 : (unsigned int) response_code,
109 : (int) tdr.ec);
110 0 : break;
111 : }
112 2 : tdh->cb (tdh->cb_cls,
113 : &tdr);
114 2 : TALER_MERCHANT_instance_token_delete_cancel (tdh);
115 2 : }
116 :
117 :
118 : struct TALER_MERCHANT_InstanceTokenDeleteHandle *
119 2 : TALER_MERCHANT_instance_token_delete (
120 : struct GNUNET_CURL_Context *ctx,
121 : const char *backend_url,
122 : const char *instance_id,
123 : TALER_MERCHANT_InstanceTokenDeleteCallback cb,
124 : void *cb_cls)
125 : {
126 : struct TALER_MERCHANT_InstanceTokenDeleteHandle *tdh;
127 :
128 2 : tdh = GNUNET_new (struct TALER_MERCHANT_InstanceTokenDeleteHandle);
129 2 : tdh->ctx = ctx;
130 2 : tdh->cb = cb;
131 2 : tdh->cb_cls = cb_cls;
132 : {
133 : char *path;
134 :
135 2 : GNUNET_asprintf (&path,
136 : "instances/%s/private/token",
137 : instance_id);
138 2 : tdh->url = TALER_url_join (backend_url,
139 : path,
140 : NULL);
141 2 : GNUNET_free (path);
142 : }
143 2 : if (NULL == tdh->url)
144 : {
145 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146 : "Could not construct request URL.\n");
147 0 : GNUNET_free (tdh);
148 0 : return NULL;
149 : }
150 2 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151 : "Requesting URL '%s'\n",
152 : tdh->url);
153 : {
154 : CURL *eh;
155 :
156 2 : eh = TALER_MERCHANT_curl_easy_get_ (tdh->url);
157 2 : GNUNET_assert (CURLE_OK ==
158 : curl_easy_setopt (eh,
159 : CURLOPT_CUSTOMREQUEST,
160 : MHD_HTTP_METHOD_DELETE));
161 2 : tdh->job = GNUNET_CURL_job_add (ctx,
162 : eh,
163 : &handle_delete_token_finished,
164 : tdh);
165 : }
166 2 : return tdh;
167 : }
168 :
169 :
170 : void
171 2 : TALER_MERCHANT_instance_token_delete_cancel (
172 : struct TALER_MERCHANT_InstanceTokenDeleteHandle *tdh)
173 : {
174 2 : if (NULL != tdh->job)
175 0 : GNUNET_CURL_job_cancel (tdh->job);
176 2 : GNUNET_free (tdh->url);
177 2 : GNUNET_free (tdh);
178 2 : }
|