LCOV - code coverage report
Current view: top level - bank-lib - bank_api_admin_add_kycauth.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 46 93 49.5 %
Date: 2025-06-22 12:09:43 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2015--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 bank-lib/bank_api_admin_add_kycauth.c
      19             :  * @brief Implementation of the /admin/add-kycauth requests of the bank's HTTP API
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "taler/platform.h"
      23             : #include "bank_api_common.h"
      24             : #include <microhttpd.h> /* just for HTTP status codes */
      25             : #include "taler/taler_signatures.h"
      26             : #include "taler/taler_curl_lib.h"
      27             : 
      28             : 
      29             : /**
      30             :  * @brief An /admin/add-kycauth Handle
      31             :  */
      32             : struct TALER_BANK_AdminAddKycauthHandle
      33             : {
      34             : 
      35             :   /**
      36             :    * The url for this request.
      37             :    */
      38             :   char *request_url;
      39             : 
      40             :   /**
      41             :    * POST context.
      42             :    */
      43             :   struct TALER_CURL_PostContext post_ctx;
      44             : 
      45             :   /**
      46             :    * Handle for the request.
      47             :    */
      48             :   struct GNUNET_CURL_Job *job;
      49             : 
      50             :   /**
      51             :    * Function to call with the result.
      52             :    */
      53             :   TALER_BANK_AdminAddKycauthCallback cb;
      54             : 
      55             :   /**
      56             :    * Closure for @a cb.
      57             :    */
      58             :   void *cb_cls;
      59             : 
      60             : };
      61             : 
      62             : 
      63             : /**
      64             :  * Function called when we're done processing the
      65             :  * HTTP /admin/add-kycauth request.
      66             :  *
      67             :  * @param cls the `struct TALER_BANK_AdminAddKycauthHandle`
      68             :  * @param response_code HTTP response code, 0 on error
      69             :  * @param response parsed JSON result, NULL on error
      70             :  */
      71             : static void
      72          16 : handle_admin_add_kycauth_finished (void *cls,
      73             :                                    long response_code,
      74             :                                    const void *response)
      75             : {
      76          16 :   struct TALER_BANK_AdminAddKycauthHandle *aai = cls;
      77          16 :   const json_t *j = response;
      78          16 :   struct TALER_BANK_AdminAddKycauthResponse ir = {
      79             :     .http_status = response_code,
      80             :     .response = response
      81             :   };
      82             : 
      83          16 :   aai->job = NULL;
      84          16 :   switch (response_code)
      85             :   {
      86           0 :   case 0:
      87           0 :     ir.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
      88           0 :     break;
      89          16 :   case MHD_HTTP_OK:
      90             :     {
      91             :       struct GNUNET_JSON_Specification spec[] = {
      92          16 :         GNUNET_JSON_spec_uint64 ("row_id",
      93             :                                  &ir.details.ok.serial_id),
      94          16 :         GNUNET_JSON_spec_timestamp ("timestamp",
      95             :                                     &ir.details.ok.timestamp),
      96          16 :         GNUNET_JSON_spec_end ()
      97             :       };
      98             : 
      99          16 :       if (GNUNET_OK !=
     100          16 :           GNUNET_JSON_parse (j,
     101             :                              spec,
     102             :                              NULL, NULL))
     103             :       {
     104           0 :         GNUNET_break_op (0);
     105           0 :         ir.http_status = 0;
     106           0 :         ir.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     107           0 :         break;
     108             :       }
     109             :     }
     110          16 :     break;
     111           0 :   case MHD_HTTP_BAD_REQUEST:
     112             :     /* This should never happen, either us or the bank is buggy
     113             :        (or API version conflict); just pass JSON reply to the application */
     114           0 :     GNUNET_break_op (0);
     115           0 :     ir.ec = TALER_JSON_get_error_code (j);
     116           0 :     break;
     117           0 :   case MHD_HTTP_FORBIDDEN:
     118             :     /* Access denied */
     119           0 :     ir.ec = TALER_JSON_get_error_code (j);
     120           0 :     break;
     121           0 :   case MHD_HTTP_UNAUTHORIZED:
     122             :     /* Nothing really to verify, bank says the password is invalid; we should
     123             :        pass the JSON reply to the application */
     124           0 :     ir.ec = TALER_JSON_get_error_code (j);
     125           0 :     break;
     126           0 :   case MHD_HTTP_NOT_FOUND:
     127             :     /* Nothing really to verify, maybe account really does not exist.
     128             :        We should pass the JSON reply to the application */
     129           0 :     ir.ec = TALER_JSON_get_error_code (j);
     130           0 :     break;
     131           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     132             :     /* Server had an internal issue; we should retry, but this API
     133             :        leaves this to the application */
     134           0 :     ir.ec = TALER_JSON_get_error_code (j);
     135           0 :     break;
     136           0 :   default:
     137             :     /* unexpected response code */
     138           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     139             :                 "Unexpected response code %u\n",
     140             :                 (unsigned int) response_code);
     141           0 :     GNUNET_break (0);
     142           0 :     ir.ec = TALER_JSON_get_error_code (j);
     143           0 :     break;
     144             :   }
     145          16 :   aai->cb (aai->cb_cls,
     146             :            &ir);
     147          16 :   TALER_BANK_admin_add_kycauth_cancel (aai);
     148          16 : }
     149             : 
     150             : 
     151             : struct TALER_BANK_AdminAddKycauthHandle *
     152          16 : TALER_BANK_admin_add_kycauth (
     153             :   struct GNUNET_CURL_Context *ctx,
     154             :   const struct TALER_BANK_AuthenticationData *auth,
     155             :   const union TALER_AccountPublicKeyP *account_pub,
     156             :   const struct TALER_Amount *amount,
     157             :   const struct TALER_FullPayto debit_account,
     158             :   TALER_BANK_AdminAddKycauthCallback res_cb,
     159             :   void *res_cb_cls)
     160             : {
     161             :   struct TALER_BANK_AdminAddKycauthHandle *aai;
     162             :   json_t *admin_obj;
     163             :   CURL *eh;
     164             : 
     165          16 :   if (NULL == debit_account.full_payto)
     166             :   {
     167           0 :     GNUNET_break (0);
     168           0 :     return NULL;
     169             :   }
     170          16 :   if (NULL == account_pub)
     171             :   {
     172           0 :     GNUNET_break (0);
     173           0 :     return NULL;
     174             :   }
     175          16 :   if (NULL == amount)
     176             :   {
     177           0 :     GNUNET_break (0);
     178           0 :     return NULL;
     179             :   }
     180          16 :   admin_obj = GNUNET_JSON_PACK (
     181             :     GNUNET_JSON_pack_data_auto ("account_pub",
     182             :                                 account_pub),
     183             :     TALER_JSON_pack_amount ("amount",
     184             :                             amount),
     185             :     TALER_JSON_pack_full_payto ("debit_account",
     186             :                                 debit_account));
     187          16 :   if (NULL == admin_obj)
     188             :   {
     189           0 :     GNUNET_break (0);
     190           0 :     return NULL;
     191             :   }
     192          16 :   aai = GNUNET_new (struct TALER_BANK_AdminAddKycauthHandle);
     193          16 :   aai->cb = res_cb;
     194          16 :   aai->cb_cls = res_cb_cls;
     195          16 :   aai->request_url = TALER_url_join (auth->wire_gateway_url,
     196             :                                      "admin/add-kycauth",
     197             :                                      NULL);
     198          16 :   if (NULL == aai->request_url)
     199             :   {
     200           0 :     GNUNET_free (aai);
     201           0 :     json_decref (admin_obj);
     202           0 :     return NULL;
     203             :   }
     204          16 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     205             :               "Requesting administrative transaction at `%s' for account %s\n",
     206             :               aai->request_url,
     207             :               TALER_B2S (account_pub));
     208             :   aai->post_ctx.headers
     209          16 :     = curl_slist_append (
     210             :         aai->post_ctx.headers,
     211             :         "Content-Type: application/json");
     212             : 
     213          16 :   eh = curl_easy_init ();
     214          32 :   if ( (NULL == eh) ||
     215             :        (GNUNET_OK !=
     216          16 :         TALER_BANK_setup_auth_ (eh,
     217          16 :                                 auth)) ||
     218             :        (CURLE_OK !=
     219          16 :         curl_easy_setopt (eh,
     220             :                           CURLOPT_URL,
     221          16 :                           aai->request_url)) ||
     222             :        (GNUNET_OK !=
     223          16 :         TALER_curl_easy_post (&aai->post_ctx,
     224             :                               eh,
     225             :                               admin_obj)) )
     226             :   {
     227           0 :     GNUNET_break (0);
     228           0 :     TALER_BANK_admin_add_kycauth_cancel (aai);
     229           0 :     if (NULL != eh)
     230           0 :       curl_easy_cleanup (eh);
     231           0 :     json_decref (admin_obj);
     232           0 :     return NULL;
     233             :   }
     234          16 :   json_decref (admin_obj);
     235             : 
     236          32 :   aai->job = GNUNET_CURL_job_add2 (ctx,
     237             :                                    eh,
     238          16 :                                    aai->post_ctx.headers,
     239             :                                    &handle_admin_add_kycauth_finished,
     240             :                                    aai);
     241          16 :   return aai;
     242             : }
     243             : 
     244             : 
     245             : void
     246          16 : TALER_BANK_admin_add_kycauth_cancel (
     247             :   struct TALER_BANK_AdminAddKycauthHandle *aai)
     248             : {
     249          16 :   if (NULL != aai->job)
     250             :   {
     251           0 :     GNUNET_CURL_job_cancel (aai->job);
     252           0 :     aai->job = NULL;
     253             :   }
     254          16 :   TALER_curl_easy_post_finished (&aai->post_ctx);
     255          16 :   GNUNET_free (aai->request_url);
     256          16 :   GNUNET_free (aai);
     257          16 : }
     258             : 
     259             : 
     260             : /* end of bank_api_admin_add_kycauth.c */

Generated by: LCOV version 1.16