exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

bank_api_admin_add_kycauth.c (7181B)


      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 handle_admin_add_kycauth_finished (void *cls,
     73                                    long response_code,
     74                                    const void *response)
     75 {
     76   struct TALER_BANK_AdminAddKycauthHandle *aai = cls;
     77   const json_t *j = response;
     78   struct TALER_BANK_AdminAddKycauthResponse ir = {
     79     .http_status = response_code,
     80     .response = response
     81   };
     82 
     83   aai->job = NULL;
     84   switch (response_code)
     85   {
     86   case 0:
     87     ir.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     88     break;
     89   case MHD_HTTP_OK:
     90     {
     91       struct GNUNET_JSON_Specification spec[] = {
     92         GNUNET_JSON_spec_uint64 ("row_id",
     93                                  &ir.details.ok.serial_id),
     94         GNUNET_JSON_spec_timestamp ("timestamp",
     95                                     &ir.details.ok.timestamp),
     96         GNUNET_JSON_spec_end ()
     97       };
     98 
     99       if (GNUNET_OK !=
    100           GNUNET_JSON_parse (j,
    101                              spec,
    102                              NULL, NULL))
    103       {
    104         GNUNET_break_op (0);
    105         ir.http_status = 0;
    106         ir.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
    107         break;
    108       }
    109     }
    110     break;
    111   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     GNUNET_break_op (0);
    115     ir.ec = TALER_JSON_get_error_code (j);
    116     break;
    117   case MHD_HTTP_FORBIDDEN:
    118     /* Access denied */
    119     ir.ec = TALER_JSON_get_error_code (j);
    120     break;
    121   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     ir.ec = TALER_JSON_get_error_code (j);
    125     break;
    126   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     ir.ec = TALER_JSON_get_error_code (j);
    130     break;
    131   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     ir.ec = TALER_JSON_get_error_code (j);
    135     break;
    136   default:
    137     /* unexpected response code */
    138     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    139                 "Unexpected response code %u\n",
    140                 (unsigned int) response_code);
    141     GNUNET_break (0);
    142     ir.ec = TALER_JSON_get_error_code (j);
    143     break;
    144   }
    145   aai->cb (aai->cb_cls,
    146            &ir);
    147   TALER_BANK_admin_add_kycauth_cancel (aai);
    148 }
    149 
    150 
    151 struct TALER_BANK_AdminAddKycauthHandle *
    152 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   if (NULL == debit_account.full_payto)
    166   {
    167     GNUNET_break (0);
    168     return NULL;
    169   }
    170   if (NULL == account_pub)
    171   {
    172     GNUNET_break (0);
    173     return NULL;
    174   }
    175   if (NULL == amount)
    176   {
    177     GNUNET_break (0);
    178     return NULL;
    179   }
    180   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   if (NULL == admin_obj)
    188   {
    189     GNUNET_break (0);
    190     return NULL;
    191   }
    192   aai = GNUNET_new (struct TALER_BANK_AdminAddKycauthHandle);
    193   aai->cb = res_cb;
    194   aai->cb_cls = res_cb_cls;
    195   aai->request_url = TALER_url_join (auth->wire_gateway_url,
    196                                      "admin/add-kycauth",
    197                                      NULL);
    198   if (NULL == aai->request_url)
    199   {
    200     GNUNET_free (aai);
    201     json_decref (admin_obj);
    202     return NULL;
    203   }
    204   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     = curl_slist_append (
    210         aai->post_ctx.headers,
    211         "Content-Type: application/json");
    212 
    213   eh = curl_easy_init ();
    214   if ( (NULL == eh) ||
    215        (GNUNET_OK !=
    216         TALER_BANK_setup_auth_ (eh,
    217                                 auth)) ||
    218        (CURLE_OK !=
    219         curl_easy_setopt (eh,
    220                           CURLOPT_URL,
    221                           aai->request_url)) ||
    222        (GNUNET_OK !=
    223         TALER_curl_easy_post (&aai->post_ctx,
    224                               eh,
    225                               admin_obj)) )
    226   {
    227     GNUNET_break (0);
    228     TALER_BANK_admin_add_kycauth_cancel (aai);
    229     if (NULL != eh)
    230       curl_easy_cleanup (eh);
    231     json_decref (admin_obj);
    232     return NULL;
    233   }
    234   json_decref (admin_obj);
    235 
    236   aai->job = GNUNET_CURL_job_add2 (ctx,
    237                                    eh,
    238                                    aai->post_ctx.headers,
    239                                    &handle_admin_add_kycauth_finished,
    240                                    aai);
    241   return aai;
    242 }
    243 
    244 
    245 void
    246 TALER_BANK_admin_add_kycauth_cancel (
    247   struct TALER_BANK_AdminAddKycauthHandle *aai)
    248 {
    249   if (NULL != aai->job)
    250   {
    251     GNUNET_CURL_job_cancel (aai->job);
    252     aai->job = NULL;
    253   }
    254   TALER_curl_easy_post_finished (&aai->post_ctx);
    255   GNUNET_free (aai->request_url);
    256   GNUNET_free (aai);
    257 }
    258 
    259 
    260 /* end of bank_api_admin_add_kycauth.c */