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