Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2024-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 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_post-kyc-start-ID.c
19 : * @brief functions to start a KYC process
20 : * @author Christian Grothoff
21 : */
22 : #include "taler/taler_json_lib.h"
23 : #include <microhttpd.h>
24 : #include <gnunet/gnunet_curl_lib.h>
25 : #include "taler/exchange/post-kyc-start-ID.h"
26 : #include "exchange_api_curl_defaults.h"
27 : #include "taler/taler_signatures.h"
28 : #include "taler/taler_curl_lib.h"
29 :
30 :
31 : struct TALER_EXCHANGE_PostKycStartHandle
32 : {
33 :
34 : /**
35 : * The base URL for this request.
36 : */
37 : char *base_url;
38 :
39 : /**
40 : * The full URL for this request.
41 : */
42 : char *url;
43 :
44 : /**
45 : * Minor context that holds body and headers.
46 : */
47 : struct TALER_CURL_PostContext post_ctx;
48 :
49 : /**
50 : * Handle for the request.
51 : */
52 : struct GNUNET_CURL_Job *job;
53 :
54 : /**
55 : * Function to call with the result.
56 : */
57 : TALER_EXCHANGE_PostKycStartCallback cb;
58 :
59 : /**
60 : * Closure for @e cb.
61 : */
62 : TALER_EXCHANGE_POST_KYC_START_RESULT_CLOSURE *cb_cls;
63 :
64 : /**
65 : * Reference to the execution context.
66 : */
67 : struct GNUNET_CURL_Context *ctx;
68 :
69 : /**
70 : * Identifier for the KYC process to start.
71 : */
72 : char *id;
73 :
74 : };
75 :
76 :
77 : /**
78 : * Function called when we're done processing the
79 : * HTTP POST /kyc-start/$ID request.
80 : *
81 : * @param cls the `struct TALER_EXCHANGE_PostKycStartHandle *`
82 : * @param response_code HTTP response code, 0 on error
83 : * @param response response body, NULL if not in JSON
84 : */
85 : static void
86 10 : handle_kyc_start_finished (void *cls,
87 : long response_code,
88 : const void *response)
89 : {
90 10 : struct TALER_EXCHANGE_PostKycStartHandle *pksh = cls;
91 10 : const json_t *json = response;
92 10 : struct TALER_EXCHANGE_PostKycStartResponse adr = {
93 10 : .hr.http_status = (unsigned int) response_code,
94 : .hr.reply = json
95 : };
96 :
97 10 : pksh->job = NULL;
98 10 : switch (response_code)
99 : {
100 0 : case 0:
101 : /* no reply */
102 0 : adr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
103 0 : adr.hr.hint = "server offline?";
104 0 : break;
105 10 : case MHD_HTTP_OK:
106 : {
107 : struct GNUNET_JSON_Specification spec[] = {
108 10 : GNUNET_JSON_spec_string (
109 : "redirect_url",
110 : &adr.details.ok.redirect_url),
111 10 : GNUNET_JSON_spec_end ()
112 : };
113 :
114 10 : if (GNUNET_OK !=
115 10 : GNUNET_JSON_parse (json,
116 : spec,
117 : NULL, NULL))
118 : {
119 0 : GNUNET_break_op (0);
120 0 : adr.hr.http_status = 0;
121 0 : adr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
122 0 : break;
123 : }
124 : }
125 10 : break;
126 0 : case MHD_HTTP_BAD_REQUEST:
127 0 : adr.hr.ec = TALER_JSON_get_error_code (json);
128 0 : adr.hr.hint = TALER_JSON_get_error_hint (json);
129 0 : break;
130 0 : case MHD_HTTP_NOT_FOUND:
131 0 : adr.hr.ec = TALER_JSON_get_error_code (json);
132 0 : adr.hr.hint = TALER_JSON_get_error_hint (json);
133 0 : break;
134 0 : case MHD_HTTP_CONFLICT:
135 0 : adr.hr.ec = TALER_JSON_get_error_code (json);
136 0 : adr.hr.hint = TALER_JSON_get_error_hint (json);
137 0 : break;
138 0 : case MHD_HTTP_INTERNAL_SERVER_ERROR:
139 0 : adr.hr.ec = TALER_JSON_get_error_code (json);
140 0 : adr.hr.hint = TALER_JSON_get_error_hint (json);
141 0 : break;
142 0 : default:
143 : /* unexpected response code */
144 0 : GNUNET_break_op (0);
145 0 : adr.hr.ec = TALER_JSON_get_error_code (json);
146 0 : adr.hr.hint = TALER_JSON_get_error_hint (json);
147 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
148 : "Unexpected response code %u/%d for exchange POST kyc-start\n",
149 : (unsigned int) response_code,
150 : (int) adr.hr.ec);
151 0 : break;
152 : }
153 10 : if (NULL != pksh->cb)
154 : {
155 10 : pksh->cb (pksh->cb_cls,
156 : &adr);
157 10 : pksh->cb = NULL;
158 : }
159 10 : TALER_EXCHANGE_post_kyc_start_cancel (pksh);
160 10 : }
161 :
162 :
163 : struct TALER_EXCHANGE_PostKycStartHandle *
164 10 : TALER_EXCHANGE_post_kyc_start_create (
165 : struct GNUNET_CURL_Context *ctx,
166 : const char *url,
167 : const char *id)
168 : {
169 : struct TALER_EXCHANGE_PostKycStartHandle *pksh;
170 :
171 10 : pksh = GNUNET_new (struct TALER_EXCHANGE_PostKycStartHandle);
172 10 : pksh->ctx = ctx;
173 10 : pksh->base_url = GNUNET_strdup (url);
174 10 : pksh->id = GNUNET_strdup (id);
175 10 : return pksh;
176 : }
177 :
178 :
179 : enum TALER_ErrorCode
180 10 : TALER_EXCHANGE_post_kyc_start_start (
181 : struct TALER_EXCHANGE_PostKycStartHandle *pksh,
182 : TALER_EXCHANGE_PostKycStartCallback cb,
183 : TALER_EXCHANGE_POST_KYC_START_RESULT_CLOSURE *cb_cls)
184 : {
185 : CURL *eh;
186 : json_t *body;
187 : char *path;
188 :
189 10 : pksh->cb = cb;
190 10 : pksh->cb_cls = cb_cls;
191 10 : GNUNET_asprintf (&path,
192 : "kyc-start/%s",
193 : pksh->id);
194 10 : pksh->url = TALER_url_join (pksh->base_url,
195 : path,
196 : NULL);
197 10 : GNUNET_free (path);
198 10 : if (NULL == pksh->url)
199 : {
200 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
201 : "Could not construct request URL.\n");
202 0 : return TALER_EC_GENERIC_CONFIGURATION_INVALID;
203 : }
204 10 : body = json_object (); /* as per spec: empty! */
205 10 : GNUNET_assert (NULL != body);
206 10 : eh = TALER_EXCHANGE_curl_easy_get_ (pksh->url);
207 20 : if ( (NULL == eh) ||
208 : (GNUNET_OK !=
209 10 : TALER_curl_easy_post (&pksh->post_ctx,
210 : eh,
211 : body)) )
212 : {
213 0 : GNUNET_break (0);
214 0 : if (NULL != eh)
215 0 : curl_easy_cleanup (eh);
216 0 : json_decref (body);
217 0 : GNUNET_free (pksh->url);
218 0 : pksh->url = NULL;
219 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
220 : }
221 10 : json_decref (body);
222 10 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223 : "Requesting URL '%s'\n",
224 : pksh->url);
225 20 : pksh->job = GNUNET_CURL_job_add2 (pksh->ctx,
226 : eh,
227 10 : pksh->post_ctx.headers,
228 : &handle_kyc_start_finished,
229 : pksh);
230 10 : if (NULL == pksh->job)
231 : {
232 0 : TALER_curl_easy_post_finished (&pksh->post_ctx);
233 0 : GNUNET_free (pksh->url);
234 0 : pksh->url = NULL;
235 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
236 : }
237 10 : return TALER_EC_NONE;
238 : }
239 :
240 :
241 : void
242 10 : TALER_EXCHANGE_post_kyc_start_cancel (
243 : struct TALER_EXCHANGE_PostKycStartHandle *pksh)
244 : {
245 10 : if (NULL != pksh->job)
246 : {
247 0 : GNUNET_CURL_job_cancel (pksh->job);
248 0 : pksh->job = NULL;
249 : }
250 10 : TALER_curl_easy_post_finished (&pksh->post_ctx);
251 10 : GNUNET_free (pksh->url);
252 10 : GNUNET_free (pksh->base_url);
253 10 : GNUNET_free (pksh->id);
254 10 : GNUNET_free (pksh);
255 10 : }
256 :
257 :
258 : /* end of exchange_api_post-kyc-start-ID.c */
|