LCOV - code coverage report
Current view: top level - lib - exchange_api_kyc_start.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 60.3 % 73 44
Test Date: 2025-12-28 14:06:02 Functions: 100.0 % 3 3

            Line data    Source code
       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           10 : handle_kyc_start_finished (void *cls,
      78              :                            long response_code,
      79              :                            const void *response)
      80              : {
      81           10 :   struct TALER_EXCHANGE_KycStartHandle *wh = cls;
      82           10 :   const json_t *json = response;
      83           10 :   struct TALER_EXCHANGE_KycStartResponse adr = {
      84           10 :     .hr.http_status = (unsigned int) response_code,
      85              :     .hr.reply = json
      86              :   };
      87              : 
      88           10 :   wh->job = NULL;
      89           10 :   switch (response_code)
      90              :   {
      91            0 :   case 0:
      92              :     /* no reply */
      93            0 :     adr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
      94            0 :     adr.hr.hint = "server offline?";
      95            0 :     break;
      96           10 :   case MHD_HTTP_OK:
      97              :     {
      98              :       struct GNUNET_JSON_Specification spec[] = {
      99           10 :         GNUNET_JSON_spec_string (
     100              :           "redirect_url",
     101              :           &adr.details.ok.redirect_url),
     102           10 :         GNUNET_JSON_spec_end ()
     103              :       };
     104              : 
     105           10 :       if (GNUNET_OK !=
     106           10 :           GNUNET_JSON_parse (json,
     107              :                              spec,
     108              :                              NULL, NULL))
     109              :       {
     110            0 :         GNUNET_break_op (0);
     111            0 :         adr.hr.http_status = 0;
     112            0 :         adr.hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     113            0 :         break;
     114              :       }
     115              :     }
     116           10 :     break;
     117            0 :   case MHD_HTTP_NOT_FOUND:
     118            0 :     break;
     119            0 :   default:
     120              :     /* unexpected response code */
     121            0 :     GNUNET_break_op (0);
     122            0 :     adr.hr.ec = TALER_JSON_get_error_code (json);
     123            0 :     adr.hr.hint = TALER_JSON_get_error_hint (json);
     124            0 :     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            0 :     break;
     129              :   }
     130           10 :   if (NULL != wh->cb)
     131              :   {
     132           10 :     wh->cb (wh->cb_cls,
     133              :             &adr);
     134           10 :     wh->cb = NULL;
     135              :   }
     136           10 :   TALER_EXCHANGE_kyc_start_cancel (wh);
     137           10 : }
     138              : 
     139              : 
     140              : struct TALER_EXCHANGE_KycStartHandle *
     141           10 : 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           10 :   wh = GNUNET_new (struct TALER_EXCHANGE_KycStartHandle);
     153           10 :   wh->cb = cb;
     154           10 :   wh->cb_cls = cb_cls;
     155           10 :   wh->ctx = ctx;
     156              :   {
     157              :     char *path;
     158              : 
     159           10 :     GNUNET_asprintf (&path,
     160              :                      "kyc-start/%s",
     161              :                      id);
     162           10 :     wh->url = TALER_url_join (url,
     163              :                               path,
     164              :                               NULL);
     165           10 :     GNUNET_free (path);
     166              :   }
     167           10 :   if (NULL == wh->url)
     168              :   {
     169            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     170              :                 "Could not construct request URL.\n");
     171            0 :     GNUNET_free (wh);
     172            0 :     return NULL;
     173              :   }
     174           10 :   body = json_object (); /* as per spec: empty! */
     175           10 :   GNUNET_assert (NULL != body);
     176           10 :   eh = TALER_EXCHANGE_curl_easy_get_ (wh->url);
     177           20 :   if ( (NULL == eh) ||
     178              :        (GNUNET_OK !=
     179           10 :         TALER_curl_easy_post (&wh->post_ctx,
     180              :                               eh,
     181              :                               body)) )
     182              :   {
     183            0 :     GNUNET_break (0);
     184            0 :     if (NULL != eh)
     185            0 :       curl_easy_cleanup (eh);
     186            0 :     json_decref (body);
     187            0 :     GNUNET_free (wh->url);
     188            0 :     return NULL;
     189              :   }
     190           10 :   json_decref (body);
     191           10 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     192              :               "Requesting URL '%s'\n",
     193              :               wh->url);
     194           20 :   wh->job = GNUNET_CURL_job_add2 (ctx,
     195              :                                   eh,
     196           10 :                                   wh->post_ctx.headers,
     197              :                                   &handle_kyc_start_finished,
     198              :                                   wh);
     199           10 :   if (NULL == wh->job)
     200              :   {
     201            0 :     TALER_EXCHANGE_kyc_start_cancel (wh);
     202            0 :     return NULL;
     203              :   }
     204           10 :   return wh;
     205              : }
     206              : 
     207              : 
     208              : void
     209           10 : TALER_EXCHANGE_kyc_start_cancel (
     210              :   struct TALER_EXCHANGE_KycStartHandle *wh)
     211              : {
     212           10 :   if (NULL != wh->job)
     213              :   {
     214            0 :     GNUNET_CURL_job_cancel (wh->job);
     215            0 :     wh->job = NULL;
     216              :   }
     217           10 :   TALER_curl_easy_post_finished (&wh->post_ctx);
     218           10 :   GNUNET_free (wh->url);
     219           10 :   GNUNET_free (wh);
     220           10 : }
        

Generated by: LCOV version 2.0-1