LCOV - code coverage report
Current view: top level - lib - exchange_api_kyc_check.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 0 111 0.0 %
Date: 2022-08-25 06:15:09 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2021 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_check.c
      19             :  * @brief Implementation of the /kyc-check request
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <microhttpd.h> /* just for HTTP check 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-check`` handle
      35             :  */
      36             : struct TALER_EXCHANGE_KycCheckHandle
      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 for the request.
      51             :    */
      52             :   struct GNUNET_CURL_Job *job;
      53             : 
      54             :   /**
      55             :    * Function to call with the result.
      56             :    */
      57             :   TALER_EXCHANGE_KycStatusCallback cb;
      58             : 
      59             :   /**
      60             :    * Closure for @e cb.
      61             :    */
      62             :   void *cb_cls;
      63             : 
      64             :   /**
      65             :    * Hash of the payto:// URL that is being KYC'ed.
      66             :    */
      67             :   struct TALER_PaytoHashP h_payto;
      68             : 
      69             : };
      70             : 
      71             : 
      72             : /**
      73             :  * Function called when we're done processing the
      74             :  * HTTP /kyc-check request.
      75             :  *
      76             :  * @param cls the `struct TALER_EXCHANGE_KycCheckHandle`
      77             :  * @param response_code HTTP response code, 0 on error
      78             :  * @param response parsed JSON result, NULL on error
      79             :  */
      80             : static void
      81           0 : handle_kyc_check_finished (void *cls,
      82             :                            long response_code,
      83             :                            const void *response)
      84             : {
      85           0 :   struct TALER_EXCHANGE_KycCheckHandle *kch = cls;
      86           0 :   const json_t *j = response;
      87           0 :   struct TALER_EXCHANGE_KycStatus ks = {
      88           0 :     .http_status = (unsigned int) response_code
      89             :   };
      90             : 
      91           0 :   kch->job = NULL;
      92           0 :   switch (response_code)
      93             :   {
      94           0 :   case 0:
      95           0 :     ks.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
      96           0 :     break;
      97           0 :   case MHD_HTTP_OK:
      98             :     {
      99             :       struct GNUNET_JSON_Specification spec[] = {
     100           0 :         GNUNET_JSON_spec_fixed_auto ("exchange_sig",
     101             :                                      &ks.details.success.exchange_sig),
     102           0 :         GNUNET_JSON_spec_fixed_auto ("exchange_pub",
     103             :                                      &ks.details.success.exchange_pub),
     104           0 :         GNUNET_JSON_spec_timestamp ("now",
     105             :                                     &ks.details.success.timestamp),
     106           0 :         GNUNET_JSON_spec_end ()
     107             :       };
     108             :       const struct TALER_EXCHANGE_Keys *key_state;
     109             : 
     110           0 :       if (GNUNET_OK !=
     111           0 :           GNUNET_JSON_parse (j,
     112             :                              spec,
     113             :                              NULL, NULL))
     114             :       {
     115           0 :         GNUNET_break_op (0);
     116           0 :         ks.http_status = 0;
     117           0 :         ks.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     118           0 :         break;
     119             :       }
     120           0 :       key_state = TALER_EXCHANGE_get_keys (kch->exchange);
     121           0 :       if (GNUNET_OK !=
     122           0 :           TALER_EXCHANGE_test_signing_key (key_state,
     123             :                                            &ks.details.success.exchange_pub))
     124             :       {
     125           0 :         GNUNET_break_op (0);
     126           0 :         ks.http_status = 0;
     127           0 :         ks.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     128           0 :         GNUNET_JSON_parse_free (spec);
     129           0 :         break;
     130             :       }
     131             : 
     132           0 :       if (GNUNET_OK !=
     133           0 :           TALER_exchange_online_account_setup_success_verify (
     134           0 :             &kch->h_payto,
     135             :             ks.details.success.timestamp,
     136             :             &ks.details.success.exchange_pub,
     137             :             &ks.details.success.exchange_sig))
     138             :       {
     139           0 :         GNUNET_break_op (0);
     140           0 :         ks.http_status = 0;
     141           0 :         ks.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     142           0 :         GNUNET_JSON_parse_free (spec);
     143           0 :         break;
     144             :       }
     145           0 :       kch->cb (kch->cb_cls,
     146             :                &ks);
     147           0 :       GNUNET_JSON_parse_free (spec);
     148           0 :       TALER_EXCHANGE_kyc_check_cancel (kch);
     149           0 :       return;
     150             :     }
     151           0 :   case MHD_HTTP_ACCEPTED:
     152             :     {
     153             :       struct GNUNET_JSON_Specification spec[] = {
     154           0 :         GNUNET_JSON_spec_string ("kyc_url",
     155             :                                  &ks.details.accepted.kyc_url),
     156           0 :         GNUNET_JSON_spec_end ()
     157             :       };
     158             : 
     159           0 :       if (GNUNET_OK !=
     160           0 :           GNUNET_JSON_parse (j,
     161             :                              spec,
     162             :                              NULL, NULL))
     163             :       {
     164           0 :         GNUNET_break_op (0);
     165           0 :         ks.http_status = 0;
     166           0 :         ks.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     167           0 :         break;
     168             :       }
     169           0 :       kch->cb (kch->cb_cls,
     170             :                &ks);
     171           0 :       GNUNET_JSON_parse_free (spec);
     172           0 :       TALER_EXCHANGE_kyc_check_cancel (kch);
     173           0 :       return;
     174             :     }
     175           0 :   case MHD_HTTP_NO_CONTENT:
     176           0 :     break;
     177           0 :   case MHD_HTTP_BAD_REQUEST:
     178           0 :     ks.ec = TALER_JSON_get_error_code (j);
     179             :     /* This should never happen, either us or the exchange is buggy
     180             :        (or API version conflict); just pass JSON reply to the application */
     181           0 :     break;
     182           0 :   case MHD_HTTP_FORBIDDEN:
     183           0 :     ks.ec = TALER_JSON_get_error_code (j);
     184           0 :     break;
     185           0 :   case MHD_HTTP_NOT_FOUND:
     186           0 :     ks.ec = TALER_JSON_get_error_code (j);
     187           0 :     break;
     188           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     189           0 :     ks.ec = TALER_JSON_get_error_code (j);
     190             :     /* Server had an internal issue; we should retry, but this API
     191             :        leaves this to the application */
     192           0 :     break;
     193           0 :   default:
     194             :     /* unexpected response code */
     195           0 :     GNUNET_break_op (0);
     196           0 :     ks.ec = TALER_JSON_get_error_code (j);
     197           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     198             :                 "Unexpected response code %u/%d for exchange kyc_check\n",
     199             :                 (unsigned int) response_code,
     200             :                 (int) ks.ec);
     201           0 :     break;
     202             :   }
     203           0 :   kch->cb (kch->cb_cls,
     204             :            &ks);
     205           0 :   TALER_EXCHANGE_kyc_check_cancel (kch);
     206             : }
     207             : 
     208             : 
     209             : struct TALER_EXCHANGE_KycCheckHandle *
     210           0 : TALER_EXCHANGE_kyc_check (struct TALER_EXCHANGE_Handle *exchange,
     211             :                           uint64_t requirement_row,
     212             :                           const struct TALER_PaytoHashP *h_payto,
     213             :                           enum TALER_KYCLOGIC_KycUserType ut,
     214             :                           struct GNUNET_TIME_Relative timeout,
     215             :                           TALER_EXCHANGE_KycStatusCallback cb,
     216             :                           void *cb_cls)
     217             : {
     218             :   struct TALER_EXCHANGE_KycCheckHandle *kch;
     219             :   CURL *eh;
     220             :   struct GNUNET_CURL_Context *ctx;
     221             :   char *arg_str;
     222             : 
     223           0 :   if (GNUNET_YES !=
     224           0 :       TEAH_handle_is_ready (exchange))
     225             :   {
     226           0 :     GNUNET_break (0);
     227           0 :     return NULL;
     228             :   }
     229             :   {
     230             :     char payto_str[sizeof (*h_payto) * 2];
     231             :     char *end;
     232             :     unsigned long long timeout_ms;
     233             : 
     234           0 :     end = GNUNET_STRINGS_data_to_string (
     235             :       h_payto,
     236             :       sizeof (*h_payto),
     237             :       payto_str,
     238             :       sizeof (payto_str) - 1);
     239           0 :     *end = '\0';
     240           0 :     timeout_ms = timeout.rel_value_us
     241           0 :                  / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us;
     242           0 :     GNUNET_asprintf (&arg_str,
     243             :                      "/kyc-check/%llu/%s/%s?timeout_ms=%llu",
     244             :                      (unsigned long long) requirement_row,
     245             :                      payto_str,
     246             :                      TALER_KYCLOGIC_kyc_user_type2s (ut),
     247             :                      timeout_ms);
     248             :   }
     249           0 :   kch = GNUNET_new (struct TALER_EXCHANGE_KycCheckHandle);
     250           0 :   kch->exchange = exchange;
     251           0 :   kch->h_payto = *h_payto;
     252           0 :   kch->cb = cb;
     253           0 :   kch->cb_cls = cb_cls;
     254           0 :   kch->url = TEAH_path_to_url (exchange,
     255             :                                arg_str);
     256           0 :   GNUNET_free (arg_str);
     257           0 :   if (NULL == kch->url)
     258             :   {
     259           0 :     GNUNET_free (kch);
     260           0 :     return NULL;
     261             :   }
     262           0 :   eh = TALER_EXCHANGE_curl_easy_get_ (kch->url);
     263           0 :   if (NULL == eh)
     264             :   {
     265           0 :     GNUNET_break (0);
     266           0 :     GNUNET_free (kch->url);
     267           0 :     GNUNET_free (kch);
     268           0 :     return NULL;
     269             :   }
     270           0 :   ctx = TEAH_handle_to_context (exchange);
     271           0 :   kch->job = GNUNET_CURL_job_add_with_ct_json (ctx,
     272             :                                                eh,
     273             :                                                &handle_kyc_check_finished,
     274             :                                                kch);
     275           0 :   return kch;
     276             : }
     277             : 
     278             : 
     279             : void
     280           0 : TALER_EXCHANGE_kyc_check_cancel (struct TALER_EXCHANGE_KycCheckHandle *kch)
     281             : {
     282           0 :   if (NULL != kch->job)
     283             :   {
     284           0 :     GNUNET_CURL_job_cancel (kch->job);
     285           0 :     kch->job = NULL;
     286             :   }
     287           0 :   GNUNET_free (kch->url);
     288           0 :   GNUNET_free (kch);
     289           0 : }
     290             : 
     291             : 
     292             : /* end of exchange_api_kyc_check.c */

Generated by: LCOV version 1.14