Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022-2026 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU Lesser General Public License as
7 : published by the Free Software Foundation; either version 2.1,
8 : or (at your option) any later version.
9 :
10 : TALER is distributed in the hope that it will be useful,
11 : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : GNU Lesser General Public License for more details.
14 :
15 : You should have received a copy of the GNU Lesser General
16 : Public License along with TALER; see the file COPYING.LGPL.
17 : If not, see <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file merchant_api_post-private-otp-devices-new.c
21 : * @brief Implementation of the POST /private/otp-devices request
22 : * @author Christian Grothoff
23 : */
24 : #include "taler/platform.h"
25 : #include <curl/curl.h>
26 : #include <jansson.h>
27 : #include <microhttpd.h> /* just for HTTP status codes */
28 : #include <gnunet/gnunet_util_lib.h>
29 : #include <gnunet/gnunet_curl_lib.h>
30 : #include <taler/merchant/post-private-otp-devices.h>
31 : #include "merchant_api_curl_defaults.h"
32 : #include "merchant_api_common.h"
33 : #include <taler/taler_json_lib.h>
34 : #include <taler/taler_curl_lib.h>
35 :
36 :
37 : /**
38 : * Handle for a POST /private/otp-devices operation.
39 : */
40 : struct TALER_MERCHANT_PostPrivateOtpDevicesHandle
41 : {
42 : /**
43 : * Base URL of the merchant backend.
44 : */
45 : char *base_url;
46 :
47 : /**
48 : * The full URL for this request.
49 : */
50 : char *url;
51 :
52 : /**
53 : * Handle for the request.
54 : */
55 : struct GNUNET_CURL_Job *job;
56 :
57 : /**
58 : * Function to call with the result.
59 : */
60 : TALER_MERCHANT_PostPrivateOtpDevicesCallback cb;
61 :
62 : /**
63 : * Closure for @a cb.
64 : */
65 : TALER_MERCHANT_POST_PRIVATE_OTP_DEVICES_RESULT_CLOSURE *cb_cls;
66 :
67 : /**
68 : * Reference to the execution context.
69 : */
70 : struct GNUNET_CURL_Context *ctx;
71 :
72 : /**
73 : * Minor context that holds body and headers.
74 : */
75 : struct TALER_CURL_PostContext post_ctx;
76 :
77 : /**
78 : * OTP device identifier.
79 : */
80 : char *otp_device_id;
81 :
82 : /**
83 : * Human-readable description.
84 : */
85 : char *otp_device_description;
86 :
87 : /**
88 : * Base32-encoded OTP secret key (or NULL).
89 : */
90 : char *otp_key;
91 :
92 : /**
93 : * OTP algorithm to use.
94 : */
95 : enum TALER_MerchantConfirmationAlgorithm otp_algorithm;
96 :
97 : /**
98 : * Initial counter value (for HOTP).
99 : */
100 : uint64_t otp_ctr;
101 :
102 : /**
103 : * True if @e otp_ctr was explicitly set via options.
104 : */
105 : bool have_otp_ctr;
106 : };
107 :
108 :
109 : /**
110 : * Function called when we're done processing the
111 : * HTTP POST /private/otp-devices request.
112 : *
113 : * @param cls the `struct TALER_MERCHANT_PostPrivateOtpDevicesHandle`
114 : * @param response_code HTTP response code, 0 on error
115 : * @param response response body, NULL if not in JSON
116 : */
117 : static void
118 0 : handle_post_otp_devices_finished (void *cls,
119 : long response_code,
120 : const void *response)
121 : {
122 0 : struct TALER_MERCHANT_PostPrivateOtpDevicesHandle *ppoh = cls;
123 0 : const json_t *json = response;
124 0 : struct TALER_MERCHANT_PostPrivateOtpDevicesResponse odr = {
125 0 : .hr.http_status = (unsigned int) response_code,
126 : .hr.reply = json
127 : };
128 :
129 0 : ppoh->job = NULL;
130 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
131 : "POST /private/otp-devices completed with response code %u\n",
132 : (unsigned int) response_code);
133 0 : switch (response_code)
134 : {
135 0 : case 0:
136 0 : odr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
137 0 : break;
138 0 : case MHD_HTTP_NO_CONTENT:
139 0 : break;
140 0 : case MHD_HTTP_BAD_REQUEST:
141 0 : odr.hr.ec = TALER_JSON_get_error_code (json);
142 0 : odr.hr.hint = TALER_JSON_get_error_hint (json);
143 0 : break;
144 0 : case MHD_HTTP_UNAUTHORIZED:
145 0 : odr.hr.ec = TALER_JSON_get_error_code (json);
146 0 : odr.hr.hint = TALER_JSON_get_error_hint (json);
147 0 : break;
148 0 : case MHD_HTTP_FORBIDDEN:
149 0 : odr.hr.ec = TALER_JSON_get_error_code (json);
150 0 : odr.hr.hint = TALER_JSON_get_error_hint (json);
151 0 : break;
152 0 : case MHD_HTTP_NOT_FOUND:
153 0 : odr.hr.ec = TALER_JSON_get_error_code (json);
154 0 : odr.hr.hint = TALER_JSON_get_error_hint (json);
155 0 : break;
156 0 : case MHD_HTTP_CONFLICT:
157 0 : odr.hr.ec = TALER_JSON_get_error_code (json);
158 0 : odr.hr.hint = TALER_JSON_get_error_hint (json);
159 0 : break;
160 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
161 0 : odr.hr.ec = TALER_JSON_get_error_code (json);
162 0 : odr.hr.hint = TALER_JSON_get_error_hint (json);
163 0 : break;
164 0 : default:
165 0 : TALER_MERCHANT_parse_error_details_ (json,
166 : response_code,
167 : &odr.hr);
168 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
169 : "Unexpected response code %u/%d\n",
170 : (unsigned int) response_code,
171 : (int) odr.hr.ec);
172 0 : GNUNET_break_op (0);
173 0 : break;
174 : }
175 0 : ppoh->cb (ppoh->cb_cls,
176 : &odr);
177 0 : TALER_MERCHANT_post_private_otp_devices_cancel (ppoh);
178 0 : }
179 :
180 :
181 : struct TALER_MERCHANT_PostPrivateOtpDevicesHandle *
182 0 : TALER_MERCHANT_post_private_otp_devices_create (
183 : struct GNUNET_CURL_Context *ctx,
184 : const char *url,
185 : const char *otp_device_id,
186 : const char *otp_device_description,
187 : const char *otp_key,
188 : enum TALER_MerchantConfirmationAlgorithm otp_algorithm)
189 : {
190 : struct TALER_MERCHANT_PostPrivateOtpDevicesHandle *ppoh;
191 :
192 0 : ppoh = GNUNET_new (struct TALER_MERCHANT_PostPrivateOtpDevicesHandle);
193 0 : ppoh->ctx = ctx;
194 0 : ppoh->base_url = GNUNET_strdup (url);
195 0 : ppoh->otp_device_id = GNUNET_strdup (otp_device_id);
196 0 : ppoh->otp_device_description = GNUNET_strdup (otp_device_description);
197 0 : if (NULL != otp_key)
198 0 : ppoh->otp_key = GNUNET_strdup (otp_key);
199 0 : ppoh->otp_algorithm = otp_algorithm;
200 0 : return ppoh;
201 : }
202 :
203 :
204 : enum GNUNET_GenericReturnValue
205 0 : TALER_MERCHANT_post_private_otp_devices_set_options_ (
206 : struct TALER_MERCHANT_PostPrivateOtpDevicesHandle *ppoh,
207 : unsigned int num_options,
208 : const struct TALER_MERCHANT_PostPrivateOtpDevicesOptionValue *options)
209 : {
210 0 : for (unsigned int i = 0; i < num_options; i++)
211 : {
212 0 : switch (options[i].option)
213 : {
214 0 : case TALER_MERCHANT_POST_PRIVATE_OTP_DEVICES_OPTION_END:
215 0 : return GNUNET_OK;
216 0 : case TALER_MERCHANT_POST_PRIVATE_OTP_DEVICES_OPTION_OTP_CTR:
217 0 : ppoh->otp_ctr = options[i].details.otp_ctr;
218 0 : ppoh->have_otp_ctr = true;
219 0 : break;
220 0 : default:
221 0 : GNUNET_break (0);
222 0 : return GNUNET_SYSERR;
223 : }
224 : }
225 0 : return GNUNET_OK;
226 : }
227 :
228 :
229 : enum TALER_ErrorCode
230 0 : TALER_MERCHANT_post_private_otp_devices_start (
231 : struct TALER_MERCHANT_PostPrivateOtpDevicesHandle *ppoh,
232 : TALER_MERCHANT_PostPrivateOtpDevicesCallback cb,
233 : TALER_MERCHANT_POST_PRIVATE_OTP_DEVICES_RESULT_CLOSURE *cb_cls)
234 : {
235 : json_t *req_obj;
236 : CURL *eh;
237 :
238 0 : ppoh->cb = cb;
239 0 : ppoh->cb_cls = cb_cls;
240 0 : ppoh->url = TALER_url_join (ppoh->base_url,
241 : "private/otp-devices",
242 : NULL);
243 0 : if (NULL == ppoh->url)
244 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
245 0 : req_obj = GNUNET_JSON_PACK (
246 : GNUNET_JSON_pack_string ("otp_device_id",
247 : ppoh->otp_device_id),
248 : GNUNET_JSON_pack_string ("otp_device_description",
249 : ppoh->otp_device_description),
250 : GNUNET_JSON_pack_uint64 ("otp_algorithm",
251 : (uint32_t) ppoh->otp_algorithm),
252 : GNUNET_JSON_pack_allow_null (
253 : GNUNET_JSON_pack_string ("otp_key",
254 : ppoh->otp_key)),
255 : GNUNET_JSON_pack_uint64 ("otp_ctr",
256 : ppoh->otp_ctr));
257 0 : eh = TALER_MERCHANT_curl_easy_get_ (ppoh->url);
258 0 : if ( (NULL == eh) ||
259 : (GNUNET_OK !=
260 0 : TALER_curl_easy_post (&ppoh->post_ctx,
261 : eh,
262 : req_obj)) )
263 : {
264 0 : GNUNET_break (0);
265 0 : json_decref (req_obj);
266 0 : if (NULL != eh)
267 0 : curl_easy_cleanup (eh);
268 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
269 : }
270 0 : json_decref (req_obj);
271 0 : ppoh->job = GNUNET_CURL_job_add2 (ppoh->ctx,
272 : eh,
273 0 : ppoh->post_ctx.headers,
274 : &handle_post_otp_devices_finished,
275 : ppoh);
276 0 : if (NULL == ppoh->job)
277 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
278 0 : return TALER_EC_NONE;
279 : }
280 :
281 :
282 : void
283 0 : TALER_MERCHANT_post_private_otp_devices_cancel (
284 : struct TALER_MERCHANT_PostPrivateOtpDevicesHandle *ppoh)
285 : {
286 0 : if (NULL != ppoh->job)
287 : {
288 0 : GNUNET_CURL_job_cancel (ppoh->job);
289 0 : ppoh->job = NULL;
290 : }
291 0 : TALER_curl_easy_post_finished (&ppoh->post_ctx);
292 0 : GNUNET_free (ppoh->otp_device_id);
293 0 : GNUNET_free (ppoh->otp_device_description);
294 0 : GNUNET_free (ppoh->otp_key);
295 0 : GNUNET_free (ppoh->url);
296 0 : GNUNET_free (ppoh->base_url);
297 0 : GNUNET_free (ppoh);
298 0 : }
299 :
300 :
301 : /* end of merchant_api_post-private-otp-devices-new.c */
|