Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2021, 2022 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_kyc_proof.c
19 : * @brief Implementation of the /kyc-proof request
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include <microhttpd.h> /* just for HTTP proof codes */
24 : #include <gnunet/gnunet_util_lib.h>
25 : #include <gnunet/gnunet_curl_lib.h>
26 : #include "taler_exchange_service.h"
27 : #include "taler_json_lib.h"
28 : #include "exchange_api_handle.h"
29 : #include "taler_signatures.h"
30 : #include "exchange_api_curl_defaults.h"
31 :
32 :
33 : /**
34 : * @brief A ``/kyc-proof`` handle
35 : */
36 : struct TALER_EXCHANGE_KycProofHandle
37 : {
38 :
39 : /**
40 : * The connection to exchange this request handle will use
41 : */
42 : struct TALER_EXCHANGE_Handle *exchange;
43 :
44 : /**
45 : * The url for this request.
46 : */
47 : char *url;
48 :
49 : /**
50 : * Handle to our CURL request.
51 : */
52 : CURL *eh;
53 :
54 : /**
55 : * Handle for the request.
56 : */
57 : struct GNUNET_CURL_Job *job;
58 :
59 : /**
60 : * Function to call with the result.
61 : */
62 : TALER_EXCHANGE_KycProofCallback cb;
63 :
64 : /**
65 : * Closure for @e cb.
66 : */
67 : void *cb_cls;
68 :
69 : };
70 :
71 :
72 : /**
73 : * Function called when we're done processing the
74 : * HTTP /kyc-proof request.
75 : *
76 : * @param cls the `struct TALER_EXCHANGE_KycProofHandle`
77 : * @param response_code HTTP response code, 0 on error
78 : * @param body response body
79 : * @param body_size number of bytes in @a body
80 : */
81 : static void
82 0 : handle_kyc_proof_finished (void *cls,
83 : long response_code,
84 : const void *body,
85 : size_t body_size)
86 : {
87 0 : struct TALER_EXCHANGE_KycProofHandle *kph = cls;
88 0 : struct TALER_EXCHANGE_KycProofResponse kpr = {
89 0 : .http_status = (unsigned int) response_code
90 : };
91 :
92 : (void) body;
93 : (void) body_size;
94 0 : kph->job = NULL;
95 0 : switch (response_code)
96 : {
97 0 : case 0:
98 0 : break;
99 0 : case MHD_HTTP_SEE_OTHER:
100 : {
101 : char *redirect_url;
102 :
103 0 : GNUNET_assert (CURLE_OK ==
104 : curl_easy_getinfo (kph->eh,
105 : CURLINFO_REDIRECT_URL,
106 : &redirect_url));
107 0 : kpr.details.found.redirect_url = redirect_url;
108 0 : break;
109 : }
110 0 : case MHD_HTTP_BAD_REQUEST:
111 : /* This should never happen, either us or the exchange is buggy
112 : (or API version conflict); just pass JSON reply to the application */
113 0 : break;
114 0 : case MHD_HTTP_UNAUTHORIZED:
115 0 : break;
116 0 : case MHD_HTTP_FORBIDDEN:
117 0 : break;
118 0 : case MHD_HTTP_NOT_FOUND:
119 0 : break;
120 0 : case MHD_HTTP_BAD_GATEWAY:
121 : /* Server had an internal issue; we should retry, but this API
122 : leaves this to the application */
123 0 : break;
124 0 : case MHD_HTTP_GATEWAY_TIMEOUT:
125 : /* Server had an internal issue; we should retry, but this API
126 : leaves this to the application */
127 0 : break;
128 0 : default:
129 : /* unexpected response code */
130 0 : GNUNET_break_op (0);
131 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
132 : "Unexpected response code %u for exchange kyc_proof\n",
133 : (unsigned int) response_code);
134 0 : break;
135 : }
136 0 : kph->cb (kph->cb_cls,
137 : &kpr);
138 0 : TALER_EXCHANGE_kyc_proof_cancel (kph);
139 0 : }
140 :
141 :
142 : struct TALER_EXCHANGE_KycProofHandle *
143 0 : TALER_EXCHANGE_kyc_proof (struct TALER_EXCHANGE_Handle *exchange,
144 : const struct TALER_PaytoHashP *h_payto,
145 : const char *logic,
146 : const char *args,
147 : TALER_EXCHANGE_KycProofCallback cb,
148 : void *cb_cls)
149 : {
150 : struct TALER_EXCHANGE_KycProofHandle *kph;
151 : struct GNUNET_CURL_Context *ctx;
152 : char *arg_str;
153 :
154 0 : if (NULL == args)
155 0 : args = "";
156 : else
157 0 : GNUNET_assert ( (args[0] == '?') ||
158 : (args[0] == '/') );
159 0 : if (GNUNET_YES !=
160 0 : TEAH_handle_is_ready (exchange))
161 : {
162 0 : GNUNET_break (0);
163 0 : return NULL;
164 : }
165 : {
166 : char hstr[sizeof (struct TALER_PaytoHashP) * 2];
167 : char *end;
168 :
169 0 : end = GNUNET_STRINGS_data_to_string (h_payto,
170 : sizeof (*h_payto),
171 : hstr,
172 : sizeof (hstr));
173 0 : *end = '\0';
174 0 : GNUNET_asprintf (&arg_str,
175 : "/kyc-proof/%s/%s%s",
176 : hstr,
177 : logic,
178 : args);
179 : }
180 0 : kph = GNUNET_new (struct TALER_EXCHANGE_KycProofHandle);
181 0 : kph->exchange = exchange;
182 0 : kph->cb = cb;
183 0 : kph->cb_cls = cb_cls;
184 0 : kph->url = TEAH_path_to_url (exchange,
185 : arg_str);
186 0 : GNUNET_free (arg_str);
187 0 : if (NULL == kph->url)
188 : {
189 0 : GNUNET_free (kph);
190 0 : return NULL;
191 : }
192 0 : kph->eh = TALER_EXCHANGE_curl_easy_get_ (kph->url);
193 0 : if (NULL == kph->eh)
194 : {
195 0 : GNUNET_break (0);
196 0 : GNUNET_free (kph->url);
197 0 : GNUNET_free (kph);
198 0 : return NULL;
199 : }
200 : /* disable location following, we want to learn the
201 : result of a 303 redirect! */
202 0 : GNUNET_assert (CURLE_OK ==
203 : curl_easy_setopt (kph->eh,
204 : CURLOPT_FOLLOWLOCATION,
205 : 0L));
206 0 : ctx = TEAH_handle_to_context (exchange);
207 0 : kph->job = GNUNET_CURL_job_add_raw (ctx,
208 : kph->eh,
209 : NULL,
210 : &handle_kyc_proof_finished,
211 : kph);
212 0 : return kph;
213 : }
214 :
215 :
216 : void
217 0 : TALER_EXCHANGE_kyc_proof_cancel (struct TALER_EXCHANGE_KycProofHandle *kph)
218 : {
219 0 : if (NULL != kph->job)
220 : {
221 0 : GNUNET_CURL_job_cancel (kph->job);
222 0 : kph->job = NULL;
223 : }
224 0 : GNUNET_free (kph->url);
225 0 : GNUNET_free (kph);
226 0 : }
227 :
228 :
229 : /* end of exchange_api_kyc_proof.c */
|