Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2015-2021 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 3, 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 General Public License for more details.
12 :
13 : You should have received a copy of the GNU General Public License along with
14 : TALER; see the file COPYING. If not, see
15 : <http://www.gnu.org/licenses/>
16 : */
17 : /**
18 : * @file lib/exchange_api_management_auditor_enable.c
19 : * @brief functions to enable an auditor
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include "taler_json_lib.h"
24 : #include <gnunet/gnunet_curl_lib.h>
25 : #include "taler_exchange_service.h"
26 : #include "exchange_api_curl_defaults.h"
27 : #include "taler_signatures.h"
28 : #include "taler_curl_lib.h"
29 : #include "taler_json_lib.h"
30 :
31 :
32 : /**
33 : * @brief Handle for a POST /management/auditors request.
34 : */
35 : struct TALER_EXCHANGE_ManagementAuditorEnableHandle
36 : {
37 :
38 : /**
39 : * The url for this request.
40 : */
41 : char *url;
42 :
43 : /**
44 : * Minor context that holds body and headers.
45 : */
46 : struct TALER_CURL_PostContext post_ctx;
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_EXCHANGE_ManagementAuditorEnableCallback cb;
57 :
58 : /**
59 : * Closure for @a cb.
60 : */
61 : void *cb_cls;
62 :
63 : /**
64 : * Reference to the execution context.
65 : */
66 : struct GNUNET_CURL_Context *ctx;
67 : };
68 :
69 :
70 : /**
71 : * Function called when we're done processing the
72 : * HTTP POST /management/auditors request.
73 : *
74 : * @param cls the `struct TALER_EXCHANGE_ManagementAuditorEnableHandle *`
75 : * @param response_code HTTP response code, 0 on error
76 : * @param response response body, NULL if not in JSON
77 : */
78 : static void
79 0 : handle_auditor_enable_finished (void *cls,
80 : long response_code,
81 : const void *response)
82 : {
83 0 : struct TALER_EXCHANGE_ManagementAuditorEnableHandle *ah = cls;
84 0 : const json_t *json = response;
85 0 : struct TALER_EXCHANGE_HttpResponse hr = {
86 0 : .http_status = (unsigned int) response_code,
87 : .reply = json
88 : };
89 :
90 0 : ah->job = NULL;
91 0 : switch (response_code)
92 : {
93 0 : case MHD_HTTP_NO_CONTENT:
94 0 : break;
95 0 : case MHD_HTTP_FORBIDDEN:
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 : case MHD_HTTP_CONFLICT:
100 0 : hr.ec = TALER_JSON_get_error_code (json);
101 0 : hr.hint = TALER_JSON_get_error_hint (json);
102 0 : break;
103 0 : default:
104 : /* unexpected response code */
105 0 : GNUNET_break_op (0);
106 0 : hr.ec = TALER_JSON_get_error_code (json);
107 0 : hr.hint = TALER_JSON_get_error_hint (json);
108 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
109 : "Unexpected response code %u/%d for exchange management auditor enable\n",
110 : (unsigned int) response_code,
111 : (int) hr.ec);
112 0 : break;
113 : }
114 0 : if (NULL != ah->cb)
115 : {
116 0 : ah->cb (ah->cb_cls,
117 : &hr);
118 0 : ah->cb = NULL;
119 : }
120 0 : TALER_EXCHANGE_management_enable_auditor_cancel (ah);
121 0 : }
122 :
123 :
124 : struct TALER_EXCHANGE_ManagementAuditorEnableHandle *
125 0 : TALER_EXCHANGE_management_enable_auditor (
126 : struct GNUNET_CURL_Context *ctx,
127 : const char *url,
128 : const struct TALER_AuditorPublicKeyP *auditor_pub,
129 : const char *auditor_url,
130 : const char *auditor_name,
131 : struct GNUNET_TIME_Timestamp validity_start,
132 : const struct TALER_MasterSignatureP *master_sig,
133 : TALER_EXCHANGE_ManagementAuditorEnableCallback cb,
134 : void *cb_cls)
135 : {
136 : struct TALER_EXCHANGE_ManagementAuditorEnableHandle *ah;
137 : CURL *eh;
138 : json_t *body;
139 :
140 0 : ah = GNUNET_new (struct TALER_EXCHANGE_ManagementAuditorEnableHandle);
141 0 : ah->cb = cb;
142 0 : ah->cb_cls = cb_cls;
143 0 : ah->ctx = ctx;
144 0 : ah->url = TALER_url_join (url,
145 : "management/auditors",
146 : NULL);
147 0 : if (NULL == ah->url)
148 : {
149 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
150 : "Could not construct request URL.\n");
151 0 : GNUNET_free (ah);
152 0 : return NULL;
153 : }
154 0 : body = GNUNET_JSON_PACK (
155 : GNUNET_JSON_pack_string ("auditor_url",
156 : auditor_url),
157 : GNUNET_JSON_pack_string ("auditor_name",
158 : auditor_name),
159 : GNUNET_JSON_pack_data_auto ("auditor_pub",
160 : auditor_pub),
161 : GNUNET_JSON_pack_data_auto ("master_sig",
162 : master_sig),
163 : GNUNET_JSON_pack_timestamp ("validity_start",
164 : validity_start));
165 0 : eh = TALER_EXCHANGE_curl_easy_get_ (ah->url);
166 0 : if ( (NULL == eh) ||
167 : (GNUNET_OK !=
168 0 : TALER_curl_easy_post (&ah->post_ctx,
169 : eh,
170 : body)) )
171 : {
172 0 : GNUNET_break (0);
173 0 : json_decref (body);
174 0 : if (NULL != eh)
175 0 : curl_easy_cleanup (eh);
176 0 : GNUNET_free (ah->url);
177 0 : return NULL;
178 : }
179 0 : json_decref (body);
180 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181 : "Requesting URL '%s'\n",
182 : ah->url);
183 0 : ah->job = GNUNET_CURL_job_add2 (ctx,
184 : eh,
185 0 : ah->post_ctx.headers,
186 : &handle_auditor_enable_finished,
187 : ah);
188 0 : if (NULL == ah->job)
189 : {
190 0 : TALER_EXCHANGE_management_enable_auditor_cancel (ah);
191 0 : return NULL;
192 : }
193 0 : return ah;
194 : }
195 :
196 :
197 : void
198 0 : TALER_EXCHANGE_management_enable_auditor_cancel (
199 : struct TALER_EXCHANGE_ManagementAuditorEnableHandle *ah)
200 : {
201 0 : if (NULL != ah->job)
202 : {
203 0 : GNUNET_CURL_job_cancel (ah->job);
204 0 : ah->job = NULL;
205 : }
206 0 : TALER_curl_easy_post_finished (&ah->post_ctx);
207 0 : GNUNET_free (ah->url);
208 0 : GNUNET_free (ah);
209 0 : }
|