From 121bfe3659c988f0aab070b3ae291b7b6f13f8f7 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 7 Nov 2021 13:05:33 +0100 Subject: implement /kyc-proof client API --- src/include/taler_exchange_service.h | 82 +++++++++++++ src/lib/Makefile.am | 1 + src/lib/exchange_api_kyc_proof.c | 217 +++++++++++++++++++++++++++++++++++ 3 files changed, 300 insertions(+) create mode 100644 src/lib/exchange_api_kyc_proof.c diff --git a/src/include/taler_exchange_service.h b/src/include/taler_exchange_service.h index 361956cbe..54fca65cb 100644 --- a/src/include/taler_exchange_service.h +++ b/src/include/taler_exchange_service.h @@ -2160,6 +2160,88 @@ void TALER_EXCHANGE_kyc_check_cancel (struct TALER_EXCHANGE_KycCheckHandle *kyc); +/** + * KYC proof response details. + */ +struct TALER_EXCHANGE_KycProofResponse +{ + /** + * HTTP status code returned by the exchange. + */ + unsigned int http_status; + + /** + * Taler error code, if any. + */ + enum TALER_ErrorCode ec; + + union + { + + /** + * KYC is OK, affirmation returned by the exchange. + */ + struct + { + + /** + * Where to redirect the client next. + */ + const char *redirect_url; + + } found; + + } details; + +}; + +/** + * Function called with the result of a KYC check. + * + * @param cls closure + * @param ks the account's KYC status details + */ +typedef void +(*TALER_EXCHANGE_KycProofCallback)( + void *cls, + const struct TALER_EXCHANGE_KycProofResponse *kpr); + + +/** + * Handle for a /kyc-proof operation. + */ +struct TALER_EXCHANGE_KycProofHandle; + + +/** + * Run interaction with exchange to provide proof of KYC status. + * + * @param eh exchange handle to use + * @param payment_target number identifying the target + * @param code OAuth 2.0 code argument + * @param state OAuth 2.0 state argument + * @param cb function to call with the result + * @param cb_cls closure for @a cb + * @return NULL on error + */ +struct TALER_EXCHANGE_KycProofHandle * +TALER_EXCHANGE_kyc_proof (struct TALER_EXCHANGE_Handle *eh, + uint64_t payment_target, + const char *code, + const char *state, + TALER_EXCHANGE_KycProofCallback cb, + void *cb_cls); + + +/** + * Cancel KYC proof operation. + * + * @param kph handle for operation to cancel + */ +void +TALER_EXCHANGE_kyc_proof_cancel (struct TALER_EXCHANGE_KycProofHandle *kph); + + /** * Handle for a ``/kyc-wallet`` operation. */ diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 518b725a8..2862799df 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -28,6 +28,7 @@ libtalerexchange_la_SOURCES = \ exchange_api_deposit.c \ exchange_api_deposits_get.c \ exchange_api_kyc_check.c \ + exchange_api_kyc_proof.c \ exchange_api_kyc_wallet.c \ exchange_api_link.c \ exchange_api_management_auditor_disable.c \ diff --git a/src/lib/exchange_api_kyc_proof.c b/src/lib/exchange_api_kyc_proof.c new file mode 100644 index 000000000..802152277 --- /dev/null +++ b/src/lib/exchange_api_kyc_proof.c @@ -0,0 +1,217 @@ +/* + This file is part of TALER + Copyright (C) 2021 Taler Systems SA + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + +*/ +/** + * @file lib/exchange_api_kyc_proof.c + * @brief Implementation of the /kyc-proof request + * @author Christian Grothoff + */ +#include "platform.h" +#include /* just for HTTP proof codes */ +#include +#include +#include "taler_exchange_service.h" +#include "taler_json_lib.h" +#include "exchange_api_handle.h" +#include "taler_signatures.h" +#include "exchange_api_curl_defaults.h" + + +/** + * @brief A ``/kyc-proof`` handle + */ +struct TALER_EXCHANGE_KycProofHandle +{ + + /** + * The connection to exchange this request handle will use + */ + struct TALER_EXCHANGE_Handle *exchange; + + /** + * The url for this request. + */ + char *url; + + /** + * Handle to our CURL request. + */ + CURL *eh; + + /** + * Handle for the request. + */ + struct GNUNET_CURL_Job *job; + + /** + * Function to call with the result. + */ + TALER_EXCHANGE_KycProofCallback cb; + + /** + * Closure for @e cb. + */ + void *cb_cls; + +}; + + +/** + * Function called when we're done processing the + * HTTP /kyc-proof request. + * + * @param cls the `struct TALER_EXCHANGE_KycProofHandle` + * @param response_code HTTP response code, 0 on error + * @param response parsed JSON result, NULL on error + */ +static void +handle_kyc_proof_finished (void *cls, + long response_code, + const void *response) +{ + struct TALER_EXCHANGE_KycProofHandle *kph = cls; + const json_t *j = response; + struct TALER_EXCHANGE_KycProofResponse kpr = { + .http_status = (unsigned int) response_code + }; + + kph->job = NULL; + switch (response_code) + { + case 0: + kpr.ec = TALER_EC_GENERIC_INVALID_RESPONSE; + break; + case MHD_HTTP_FOUND: + { + char *redirect_url; + + GNUNET_assert (CURLE_OK == + curl_easy_getinfo (kph->eh, + CURLINFO_REDIRECT_URL, + &redirect_url)); + kpr.details.found.redirect_url = redirect_url; + break; + } + case MHD_HTTP_BAD_REQUEST: + kpr.ec = TALER_JSON_get_error_code (j); + /* This should never happen, either us or the exchange is buggy + (or API version conflict); just pass JSON reply to the application */ + break; + case MHD_HTTP_UNAUTHORIZED: + kpr.ec = TALER_JSON_get_error_code (j); + break; + case MHD_HTTP_NOT_FOUND: + kpr.ec = TALER_JSON_get_error_code (j); + break; + case MHD_HTTP_BAD_GATEWAY: + kpr.ec = TALER_JSON_get_error_code (j); + /* Server had an internal issue; we should retry, but this API + leaves this to the application */ + break; + case MHD_HTTP_GATEWAY_TIMEOUT: + kpr.ec = TALER_JSON_get_error_code (j); + /* Server had an internal issue; we should retry, but this API + leaves this to the application */ + break; + default: + /* unexpected response code */ + GNUNET_break_op (0); + kpr.ec = TALER_JSON_get_error_code (j); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Unexpected response code %u/%d for exchange kyc_proof\n", + (unsigned int) response_code, + (int) kpr.ec); + break; + } + kph->cb (kph->cb_cls, + &kpr); + TALER_EXCHANGE_kyc_proof_cancel (kph); +} + + +struct TALER_EXCHANGE_KycProofHandle * +TALER_EXCHANGE_kyc_proof (struct TALER_EXCHANGE_Handle *exchange, + uint64_t payment_target, + const char *code, + const char *state, + TALER_EXCHANGE_KycProofCallback cb, + void *cb_cls) +{ + struct TALER_EXCHANGE_KycProofHandle *kph; + struct GNUNET_CURL_Context *ctx; + char *arg_str; + + if (GNUNET_YES != + TEAH_handle_is_ready (exchange)) + { + GNUNET_break (0); + return NULL; + } + /* TODO: any escaping of code/state needed??? */ + GNUNET_asprintf (&arg_str, + "/kyc-proof/%llu?code=%s&state=%s", + (unsigned long long) payment_target, + code, + state); + kph = GNUNET_new (struct TALER_EXCHANGE_KycProofHandle); + kph->exchange = exchange; + kph->cb = cb; + kph->cb_cls = cb_cls; + kph->url = TEAH_path_to_url (exchange, + arg_str); + GNUNET_free (arg_str); + if (NULL == kph->url) + { + GNUNET_free (kph); + return NULL; + } + kph->eh = TALER_EXCHANGE_curl_easy_get_ (kph->url); + if (NULL == kph->eh) + { + GNUNET_break (0); + GNUNET_free (kph->url); + GNUNET_free (kph); + return NULL; + } + /* disable location following, we want to learn the + result of a 302 redirect! */ + GNUNET_assert (CURLE_OK == + curl_easy_setopt (kph->eh, + CURLOPT_FOLLOWLOCATION, + 0L)); + ctx = TEAH_handle_to_context (exchange); + kph->job = GNUNET_CURL_job_add_with_ct_json (ctx, + kph->eh, + &handle_kyc_proof_finished, + kph); + return kph; +} + + +void +TALER_EXCHANGE_kyc_proof_cancel (struct TALER_EXCHANGE_KycProofHandle *kph) +{ + if (NULL != kph->job) + { + GNUNET_CURL_job_cancel (kph->job); + kph->job = NULL; + } + GNUNET_free (kph->url); + GNUNET_free (kph); +} + + +/* end of exchange_api_kyc_proof.c */ -- cgit v1.2.3