LCOV - code coverage report
Current view: top level - lib - merchant_api_get_kyc.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 103 0.0 %
Date: 2022-06-30 06:15:34 Functions: 0 6 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 Lesser General Public License as published by the Free Software
       7             :   Foundation; either version 2.1, 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 Lesser General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Lesser General Public License along with
      14             :   TALER; see the file COPYING.LGPL.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file merchant_api_get_kyc.c
      19             :  * @brief Implementation of the GET /kyc request of the merchant's HTTP API
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <curl/curl.h>
      24             : #include <jansson.h>
      25             : #include <microhttpd.h> /* just for HTTP status codes */
      26             : #include <gnunet/gnunet_util_lib.h>
      27             : #include <gnunet/gnunet_curl_lib.h>
      28             : #include "taler_merchant_service.h"
      29             : #include "merchant_api_curl_defaults.h"
      30             : #include <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : 
      33             : 
      34             : /**
      35             :  * Handle for a GET /kyc operation.
      36             :  */
      37             : struct TALER_MERCHANT_KycGetHandle
      38             : {
      39             :   /**
      40             :    * The url for this request.
      41             :    */
      42             :   char *url;
      43             : 
      44             :   /**
      45             :    * Handle for the request.
      46             :    */
      47             :   struct GNUNET_CURL_Job *job;
      48             : 
      49             :   /**
      50             :    * Function to call with the result.
      51             :    */
      52             :   TALER_MERCHANT_KycGetCallback cb;
      53             : 
      54             :   /**
      55             :    * Closure for @a cb.
      56             :    */
      57             :   void *cb_cls;
      58             : 
      59             :   /**
      60             :    * Reference to the execution context.
      61             :    */
      62             :   struct GNUNET_CURL_Context *ctx;
      63             : 
      64             : };
      65             : 
      66             : 
      67             : /**
      68             :  * Parse @a kyc response and call the continuation on success.
      69             :  *
      70             :  * @param kyc operation handle
      71             :  * @param[in,out] kr response details
      72             :  * @param pends pending_kycs array from the reply
      73             :  * @param touts timeout_kycs array from the reply
      74             :  * @return #GNUNET_OK on success (callback was called)
      75             :  */
      76             : static enum GNUNET_GenericReturnValue
      77           0 : parse_kyc (struct TALER_MERCHANT_KycGetHandle *kyc,
      78             :            struct TALER_MERCHANT_KycResponse *kr,
      79             :            json_t *pends,
      80             :            json_t *touts)
      81           0 : {
      82           0 :   unsigned int num_pends = json_array_size (pends);
      83           0 :   unsigned int num_touts = json_array_size (touts);
      84           0 :   struct TALER_MERCHANT_AccountKycRedirectDetail pending_kycs[GNUNET_NZL (
      85             :                                                                 num_pends)];
      86           0 :   struct TALER_MERCHANT_ExchangeKycFailureDetail timeout_kycs[GNUNET_NZL (
      87             :                                                                 num_touts)];
      88             : 
      89           0 :   for (unsigned int i = 0; i<num_pends; i++)
      90             :   {
      91             :     struct GNUNET_JSON_Specification spec[] = {
      92           0 :       GNUNET_JSON_spec_string ("kyc_url",
      93             :                                &pending_kycs[i].kyc_url),
      94           0 :       GNUNET_JSON_spec_string ("exchange_url",
      95             :                                &pending_kycs[i].exchange_url),
      96           0 :       GNUNET_JSON_spec_string ("payto_uri",
      97             :                                &pending_kycs[i].payto_uri),
      98           0 :       GNUNET_JSON_spec_end ()
      99             :     };
     100             : 
     101           0 :     if (GNUNET_OK !=
     102           0 :         GNUNET_JSON_parse (json_array_get (pends,
     103             :                                            i),
     104             :                            spec,
     105             :                            NULL, NULL))
     106             :     {
     107           0 :       GNUNET_break (0);
     108           0 :       return GNUNET_SYSERR;
     109             :     }
     110             :   }
     111           0 :   for (unsigned int i = 0; i<num_touts; i++)
     112             :   {
     113             :     uint32_t hs;
     114             :     uint32_t ec;
     115             :     struct GNUNET_JSON_Specification spec[] = {
     116           0 :       GNUNET_JSON_spec_string ("exchange_url",
     117             :                                &timeout_kycs[i].exchange_url),
     118           0 :       GNUNET_JSON_spec_uint32 ("exchange_code",
     119             :                                &ec),
     120           0 :       GNUNET_JSON_spec_uint32 ("exchange_http_status",
     121             :                                &hs),
     122           0 :       GNUNET_JSON_spec_end ()
     123             :     };
     124             : 
     125           0 :     if (GNUNET_OK !=
     126           0 :         GNUNET_JSON_parse (json_array_get (touts,
     127             :                                            i),
     128             :                            spec,
     129             :                            NULL, NULL))
     130             :     {
     131           0 :       GNUNET_break (0);
     132           0 :       return GNUNET_SYSERR;
     133             :     }
     134           0 :     timeout_kycs[i].exchange_http_status = (unsigned int) hs;
     135           0 :     timeout_kycs[i].exchange_code = (enum TALER_ErrorCode) ec;
     136             :   }
     137           0 :   kr->details.kyc_status.pending_kycs = pending_kycs;
     138           0 :   kr->details.kyc_status.timeout_kycs = timeout_kycs;
     139           0 :   kr->details.kyc_status.pending_kycs_length = num_pends;
     140           0 :   kr->details.kyc_status.timeout_kycs_length = num_touts;
     141           0 :   kyc->cb (kyc->cb_cls,
     142             :            kr);
     143           0 :   return GNUNET_OK;
     144             : }
     145             : 
     146             : 
     147             : /**
     148             :  * Function called when we're done processing the
     149             :  * HTTP /kyc request.
     150             :  *
     151             :  * @param cls the `struct TALER_MERCHANT_KycGetHandle`
     152             :  * @param response_code HTTP response code, 0 on error
     153             :  * @param response response body, NULL if not in JSON
     154             :  */
     155             : static void
     156           0 : handle_get_kyc_finished (void *cls,
     157             :                          long response_code,
     158             :                          const void *response)
     159             : {
     160           0 :   struct TALER_MERCHANT_KycGetHandle *kyc = cls;
     161           0 :   const json_t *json = response;
     162           0 :   struct TALER_MERCHANT_KycResponse kr = {
     163           0 :     .hr.http_status = (unsigned int) response_code,
     164             :     .hr.reply = json
     165             :   };
     166             : 
     167           0 :   kyc->job = NULL;
     168           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     169             :               "Got /kyc response with status code %u\n",
     170             :               (unsigned int) response_code);
     171           0 :   switch (response_code)
     172             :   {
     173           0 :   case MHD_HTTP_NO_CONTENT:
     174           0 :     break;
     175           0 :   case MHD_HTTP_ACCEPTED:
     176             :   case MHD_HTTP_BAD_GATEWAY:
     177             :   case MHD_HTTP_GATEWAY_TIMEOUT:
     178             :     {
     179             :       json_t *pends;
     180             :       json_t *touts;
     181             :       struct GNUNET_JSON_Specification spec[] = {
     182           0 :         GNUNET_JSON_spec_json ("pending_kycs",
     183             :                                &pends),
     184           0 :         GNUNET_JSON_spec_json ("timeout_kycs",
     185             :                                &touts),
     186           0 :         GNUNET_JSON_spec_end ()
     187             :       };
     188             : 
     189           0 :       if (GNUNET_OK !=
     190           0 :           GNUNET_JSON_parse (json,
     191             :                              spec,
     192             :                              NULL, NULL))
     193             :       {
     194           0 :         kr.hr.http_status = 0;
     195           0 :         kr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     196           0 :         break;
     197             :       }
     198           0 :       if ( (! json_is_array (pends)) ||
     199           0 :            (! json_is_array (touts)) ||
     200             :            (GNUNET_OK !=
     201           0 :             parse_kyc (kyc,
     202             :                        &kr,
     203             :                        pends,
     204             :                        touts)) )
     205             :       {
     206           0 :         kr.hr.http_status = 0;
     207           0 :         kr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     208           0 :         break;
     209             :       }
     210             :       /* parse_kyc called the continuation already */
     211           0 :       GNUNET_JSON_parse_free (spec);
     212           0 :       TALER_MERCHANT_kyc_get_cancel (kyc);
     213           0 :       return;
     214             :     }
     215           0 :   case MHD_HTTP_UNAUTHORIZED:
     216           0 :     kr.hr.ec = TALER_JSON_get_error_code (json);
     217           0 :     kr.hr.hint = TALER_JSON_get_error_hint (json);
     218             :     /* Nothing really to verify, merchant says we need to authenticate. */
     219           0 :     break;
     220           0 :   default:
     221             :     /* unexpected response code */
     222           0 :     kr.hr.ec = TALER_JSON_get_error_code (json);
     223           0 :     kr.hr.hint = TALER_JSON_get_error_hint (json);
     224           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     225             :                 "Unexpected response code %u/%d\n",
     226             :                 (unsigned int) response_code,
     227             :                 (int) kr.hr.ec);
     228           0 :     break;
     229             :   }
     230           0 :   kyc->cb (kyc->cb_cls,
     231             :            &kr);
     232           0 :   TALER_MERCHANT_kyc_get_cancel (kyc);
     233             : }
     234             : 
     235             : 
     236             : /**
     237             :  * Issue a GET KYC request to the backend.
     238             :  * Returns KYC status of bank accounts.
     239             :  *
     240             :  * @param ctx execution context
     241             :  * @param[in] url URL to use for the request, consumed!
     242             :  * @param h_wire which bank account to query, NULL for all
     243             :  * @param exchange_url which exchange to query, NULL for all
     244             :  * @param timeout how long to wait for a (positive) reply
     245             :  * @param cb function to call with the result
     246             :  * @param cb_cls closure for @a cb
     247             :  * @return handle for this operation, NULL upon errors
     248             :  */
     249             : static struct TALER_MERCHANT_KycGetHandle *
     250           0 : kyc_get (struct GNUNET_CURL_Context *ctx,
     251             :          char *url,
     252             :          const struct TALER_MerchantWireHashP *h_wire,
     253             :          const char *exchange_url,
     254             :          struct GNUNET_TIME_Relative timeout,
     255             :          TALER_MERCHANT_KycGetCallback cb,
     256             :          void *cb_cls)
     257             : {
     258             :   struct TALER_MERCHANT_KycGetHandle *kyc;
     259             :   CURL *eh;
     260             :   char timeout_ms[32];
     261             : 
     262           0 :   kyc = GNUNET_new (struct TALER_MERCHANT_KycGetHandle);
     263           0 :   kyc->ctx = ctx;
     264           0 :   kyc->cb = cb;
     265           0 :   kyc->cb_cls = cb_cls;
     266           0 :   GNUNET_snprintf (timeout_ms,
     267             :                    sizeof (timeout_ms),
     268             :                    "%llu",
     269           0 :                    (unsigned long long) (timeout.rel_value_us
     270           0 :                                          / GNUNET_TIME_UNIT_MILLISECONDS.
     271             :                                          rel_value_us));
     272           0 :   kyc->url = TALER_url_join (url,
     273             :                              "kyc",
     274             :                              "h_wire",
     275             :                              NULL == h_wire
     276             :                              ? NULL
     277           0 :                              : GNUNET_h2s_full (&h_wire->hash),
     278             :                              "exchange_url",
     279             :                              NULL == exchange_url
     280             :                              ? NULL
     281             :                              : exchange_url,
     282             :                              "timeout_ms",
     283           0 :                              GNUNET_TIME_relative_is_zero (timeout)
     284             :                              ? NULL
     285             :                              : timeout_ms,
     286             :                              NULL);
     287           0 :   GNUNET_free (url);
     288           0 :   if (NULL == kyc->url)
     289             :   {
     290           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     291             :                 "Could not construct request URL.\n");
     292           0 :     GNUNET_free (kyc);
     293           0 :     return NULL;
     294             :   }
     295           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     296             :               "Requesting URL '%s'\n",
     297             :               kyc->url);
     298           0 :   eh = TALER_MERCHANT_curl_easy_get_ (kyc->url);
     299           0 :   kyc->job = GNUNET_CURL_job_add (ctx,
     300             :                                   eh,
     301             :                                   &handle_get_kyc_finished,
     302             :                                   kyc);
     303           0 :   return kyc;
     304             : }
     305             : 
     306             : 
     307             : struct TALER_MERCHANT_KycGetHandle *
     308           0 : TALER_MERCHANT_kyc_get (struct GNUNET_CURL_Context *ctx,
     309             :                         const char *backend_url,
     310             :                         const struct TALER_MerchantWireHashP *h_wire,
     311             :                         const char *exchange_url,
     312             :                         struct GNUNET_TIME_Relative timeout,
     313             :                         TALER_MERCHANT_KycGetCallback cb,
     314             :                         void *cb_cls)
     315             : {
     316             :   char *url;
     317             : 
     318           0 :   GNUNET_asprintf (&url,
     319             :                    "%sprivate/",
     320             :                    backend_url);
     321           0 :   return kyc_get (ctx,
     322             :                   url,
     323             :                   h_wire,
     324             :                   exchange_url,
     325             :                   timeout,
     326             :                   cb,
     327             :                   cb_cls);
     328             : }
     329             : 
     330             : 
     331             : struct TALER_MERCHANT_KycGetHandle *
     332           0 : TALER_MERCHANT_management_kyc_get (struct GNUNET_CURL_Context *ctx,
     333             :                                    const char *backend_url,
     334             :                                    const char *instance_id,
     335             :                                    const struct TALER_MerchantWireHashP *h_wire,
     336             :                                    const char *exchange_url,
     337             :                                    struct GNUNET_TIME_Relative timeout,
     338             :                                    TALER_MERCHANT_KycGetCallback cb,
     339             :                                    void *cb_cls)
     340             : {
     341             :   char *url;
     342             : 
     343           0 :   GNUNET_asprintf (&url,
     344             :                    "%smanagement/instances/%s/",
     345             :                    backend_url,
     346             :                    instance_id);
     347           0 :   return kyc_get (ctx,
     348             :                   url,
     349             :                   h_wire,
     350             :                   exchange_url,
     351             :                   timeout,
     352             :                   cb,
     353             :                   cb_cls);
     354             : }
     355             : 
     356             : 
     357             : void
     358           0 : TALER_MERCHANT_kyc_get_cancel (
     359             :   struct TALER_MERCHANT_KycGetHandle *kyc)
     360             : {
     361           0 :   if (NULL != kyc->job)
     362           0 :     GNUNET_CURL_job_cancel (kyc->job);
     363           0 :   GNUNET_free (kyc->url);
     364           0 :   GNUNET_free (kyc);
     365           0 : }

Generated by: LCOV version 1.14