LCOV - code coverage report
Current view: top level - lib - exchange_api_kyc_wallet.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 0 80 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_wallet.c
      19             :  * @brief Implementation of the /kyc-wallet request
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <microhttpd.h> /* just for HTTP wallet 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-wallet`` handle
      35             :  */
      36             : struct TALER_EXCHANGE_KycWalletHandle
      37             : {
      38             : 
      39             :   /**
      40             :    * Context for #TEH_curl_easy_post(). Keeps the data that must
      41             :    * persist for Curl to make the upload.
      42             :    */
      43             :   struct TALER_CURL_PostContext ctx;
      44             : 
      45             :   /**
      46             :    * The connection to exchange this request handle will use
      47             :    */
      48             :   struct TALER_EXCHANGE_Handle *exchange;
      49             : 
      50             :   /**
      51             :    * The url for this request.
      52             :    */
      53             :   char *url;
      54             : 
      55             :   /**
      56             :    * Handle for the request.
      57             :    */
      58             :   struct GNUNET_CURL_Job *job;
      59             : 
      60             :   /**
      61             :    * Function to call with the result.
      62             :    */
      63             :   TALER_EXCHANGE_KycWalletCallback cb;
      64             : 
      65             :   /**
      66             :    * Closure for @e cb.
      67             :    */
      68             :   void *cb_cls;
      69             : 
      70             : };
      71             : 
      72             : 
      73             : /**
      74             :  * Function called when we're done processing the
      75             :  * HTTP /kyc-wallet request.
      76             :  *
      77             :  * @param cls the `struct TALER_EXCHANGE_KycWalletHandle`
      78             :  * @param response_code HTTP response code, 0 on error
      79             :  * @param response parsed JSON result, NULL on error
      80             :  */
      81             : static void
      82           0 : handle_kyc_wallet_finished (void *cls,
      83             :                             long response_code,
      84             :                             const void *response)
      85             : {
      86           0 :   struct TALER_EXCHANGE_KycWalletHandle *kwh = cls;
      87           0 :   const json_t *j = response;
      88           0 :   struct TALER_EXCHANGE_WalletKycResponse ks = {
      89           0 :     .http_status = (unsigned int) response_code
      90             :   };
      91             : 
      92           0 :   kwh->job = NULL;
      93           0 :   switch (response_code)
      94             :   {
      95           0 :   case 0:
      96           0 :     ks.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
      97           0 :     break;
      98           0 :   case MHD_HTTP_NO_CONTENT:
      99           0 :     break;
     100           0 :   case MHD_HTTP_BAD_REQUEST:
     101           0 :     ks.ec = TALER_JSON_get_error_code (j);
     102             :     /* This should never happen, either us or the exchange is buggy
     103             :        (or API version conflict); just pass JSON reply to the application */
     104           0 :     break;
     105           0 :   case MHD_HTTP_FORBIDDEN:
     106           0 :     ks.ec = TALER_JSON_get_error_code (j);
     107           0 :     break;
     108           0 :   case MHD_HTTP_NOT_FOUND:
     109           0 :     ks.ec = TALER_JSON_get_error_code (j);
     110           0 :     break;
     111           0 :   case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS:
     112             :     {
     113             :       struct GNUNET_JSON_Specification spec[] = {
     114           0 :         GNUNET_JSON_spec_fixed_auto (
     115             :           "h_payto",
     116             :           &ks.details.unavailable_for_legal_reasons.h_payto),
     117           0 :         GNUNET_JSON_spec_uint64 (
     118             :           "requirement_row",
     119             :           &ks.details.unavailable_for_legal_reasons.requirement_row),
     120           0 :         GNUNET_JSON_spec_end ()
     121             :       };
     122             : 
     123           0 :       if (GNUNET_OK !=
     124           0 :           GNUNET_JSON_parse (j,
     125             :                              spec,
     126             :                              NULL, NULL))
     127             :       {
     128           0 :         GNUNET_break_op (0);
     129           0 :         ks.http_status = 0;
     130           0 :         ks.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     131           0 :         break;
     132             :       }
     133           0 :       break;
     134             :     }
     135           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     136           0 :     ks.ec = TALER_JSON_get_error_code (j);
     137             :     /* Server had an internal issue; we should retry, but this API
     138             :        leaves this to the application */
     139           0 :     break;
     140           0 :   default:
     141             :     /* unexpected response code */
     142           0 :     GNUNET_break_op (0);
     143           0 :     ks.ec = TALER_JSON_get_error_code (j);
     144           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     145             :                 "Unexpected response code %u/%d for exchange /kyc-wallet\n",
     146             :                 (unsigned int) response_code,
     147             :                 (int) ks.ec);
     148           0 :     break;
     149             :   }
     150           0 :   kwh->cb (kwh->cb_cls,
     151             :            &ks);
     152           0 :   TALER_EXCHANGE_kyc_wallet_cancel (kwh);
     153           0 : }
     154             : 
     155             : 
     156             : struct TALER_EXCHANGE_KycWalletHandle *
     157           0 : TALER_EXCHANGE_kyc_wallet (struct TALER_EXCHANGE_Handle *exchange,
     158             :                            const struct TALER_ReservePrivateKeyP *reserve_priv,
     159             :                            const struct TALER_Amount *balance,
     160             :                            TALER_EXCHANGE_KycWalletCallback cb,
     161             :                            void *cb_cls)
     162             : {
     163             :   struct TALER_EXCHANGE_KycWalletHandle *kwh;
     164             :   CURL *eh;
     165             :   json_t *req;
     166             :   struct GNUNET_CURL_Context *ctx;
     167             :   struct TALER_ReservePublicKeyP reserve_pub;
     168             :   struct TALER_ReserveSignatureP reserve_sig;
     169             : 
     170           0 :   GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv->eddsa_priv,
     171             :                                       &reserve_pub.eddsa_pub);
     172           0 :   TALER_wallet_account_setup_sign (reserve_priv,
     173             :                                    &reserve_sig);
     174           0 :   req = GNUNET_JSON_PACK (
     175             :     TALER_JSON_pack_amount ("balance",
     176             :                             balance),
     177             :     GNUNET_JSON_pack_data_auto ("reserve_pub",
     178             :                                 &reserve_pub),
     179             :     GNUNET_JSON_pack_data_auto ("reserve_sig",
     180             :                                 &reserve_sig));
     181           0 :   GNUNET_assert (NULL != req);
     182           0 :   kwh = GNUNET_new (struct TALER_EXCHANGE_KycWalletHandle);
     183           0 :   kwh->exchange = exchange;
     184           0 :   kwh->cb = cb;
     185           0 :   kwh->cb_cls = cb_cls;
     186           0 :   kwh->url = TEAH_path_to_url (exchange,
     187             :                                "/kyc-wallet");
     188           0 :   if (NULL == kwh->url)
     189             :   {
     190           0 :     json_decref (req);
     191           0 :     GNUNET_free (kwh);
     192           0 :     return NULL;
     193             :   }
     194           0 :   ctx = TEAH_handle_to_context (exchange);
     195           0 :   eh = TALER_EXCHANGE_curl_easy_get_ (kwh->url);
     196           0 :   if ( (NULL == eh) ||
     197             :        (GNUNET_OK !=
     198           0 :         TALER_curl_easy_post (&kwh->ctx,
     199             :                               eh,
     200             :                               req)) )
     201             :   {
     202           0 :     GNUNET_break (0);
     203           0 :     if (NULL != eh)
     204           0 :       curl_easy_cleanup (eh);
     205           0 :     json_decref (req);
     206           0 :     GNUNET_free (kwh->url);
     207           0 :     GNUNET_free (kwh);
     208           0 :     return NULL;
     209             :   }
     210           0 :   json_decref (req);
     211           0 :   kwh->job = GNUNET_CURL_job_add2 (ctx,
     212             :                                    eh,
     213           0 :                                    kwh->ctx.headers,
     214             :                                    &handle_kyc_wallet_finished,
     215             :                                    kwh);
     216           0 :   return kwh;
     217             : }
     218             : 
     219             : 
     220             : void
     221           0 : TALER_EXCHANGE_kyc_wallet_cancel (struct TALER_EXCHANGE_KycWalletHandle *kwh)
     222             : {
     223           0 :   if (NULL != kwh->job)
     224             :   {
     225           0 :     GNUNET_CURL_job_cancel (kwh->job);
     226           0 :     kwh->job = NULL;
     227             :   }
     228           0 :   GNUNET_free (kwh->url);
     229           0 :   TALER_curl_easy_post_finished (&kwh->ctx);
     230           0 :   GNUNET_free (kwh);
     231           0 : }
     232             : 
     233             : 
     234             : /* end of exchange_api_kyc_wallet.c */

Generated by: LCOV version 1.14