exchange

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

testing_api_cmd_kyc_proof.c (6244B)


      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
      6   it under the terms of the GNU General Public License as
      7   published by the Free Software Foundation; either version 3, or
      8   (at your option) any later version.
      9 
     10   TALER is distributed in the hope that it will be useful, but
     11   WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13   GNU General Public License for more details.
     14 
     15   You should have received a copy of the GNU General Public
     16   License along with TALER; see the file COPYING.  If not, see
     17   <http://www.gnu.org/licenses/>
     18 */
     19 
     20 /**
     21  * @file testing/testing_api_cmd_kyc_proof.c
     22  * @brief Implement the testing CMDs for the /kyc-proof/ operation.
     23  * @author Christian Grothoff
     24  */
     25 #include "taler/platform.h"
     26 #include "taler/taler_json_lib.h"
     27 #include <gnunet/gnunet_curl_lib.h>
     28 #include "taler/taler_testing_lib.h"
     29 
     30 /**
     31  * State for a "track transaction" CMD.
     32  */
     33 struct KycProofGetState
     34 {
     35 
     36   /**
     37    * Command to get a reserve private key from.
     38    */
     39   const char *payment_target_reference;
     40 
     41   /**
     42    * Code to pass.
     43    */
     44   const char *code;
     45 
     46   /**
     47    * Logic section name to pass to `/kyc-proof/` handler.
     48    */
     49   const char *logic;
     50 
     51   /**
     52    * Expected HTTP response code.
     53    */
     54   unsigned int expected_response_code;
     55 
     56   /**
     57    * Set to the KYC REDIRECT *if* the exchange replied with
     58    * success (#MHD_HTTP_OK).
     59    */
     60   char *redirect_url;
     61 
     62   /**
     63    * Handle to the "track transaction" pending operation.
     64    */
     65   struct TALER_EXCHANGE_KycProofHandle *kph;
     66 
     67   /**
     68    * Interpreter state.
     69    */
     70   struct TALER_TESTING_Interpreter *is;
     71 };
     72 
     73 
     74 /**
     75  * Handle response to the command.
     76  *
     77  * @param cls closure.
     78  * @param kpr KYC proof response details
     79  */
     80 static void
     81 proof_kyc_cb (void *cls,
     82               const struct TALER_EXCHANGE_KycProofResponse *kpr)
     83 {
     84   struct KycProofGetState *kcg = cls;
     85   struct TALER_TESTING_Interpreter *is = kcg->is;
     86 
     87   kcg->kph = NULL;
     88   if (kcg->expected_response_code != kpr->hr.http_status)
     89   {
     90     TALER_TESTING_unexpected_status (is,
     91                                      kpr->hr.http_status,
     92                                      kcg->expected_response_code);
     93     return;
     94   }
     95   switch (kpr->hr.http_status)
     96   {
     97   case MHD_HTTP_SEE_OTHER:
     98     kcg->redirect_url = GNUNET_strdup (kpr->details.found.redirect_url);
     99     break;
    100   case MHD_HTTP_FORBIDDEN:
    101     break;
    102   case MHD_HTTP_BAD_GATEWAY:
    103     break;
    104   default:
    105     GNUNET_break (0);
    106     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    107                 "Unexpected response code %u to /kyc-proof\n",
    108                 kpr->hr.http_status);
    109     break;
    110   }
    111   TALER_TESTING_interpreter_next (kcg->is);
    112 }
    113 
    114 
    115 /**
    116  * Run the command.
    117  *
    118  * @param cls closure.
    119  * @param cmd the command to execute.
    120  * @param is the interpreter state.
    121  */
    122 static void
    123 proof_kyc_run (void *cls,
    124                const struct TALER_TESTING_Command *cmd,
    125                struct TALER_TESTING_Interpreter *is)
    126 {
    127   struct KycProofGetState *kps = cls;
    128   const struct TALER_TESTING_Command *res_cmd;
    129   const struct TALER_NormalizedPaytoHashP *h_payto;
    130   char *uargs;
    131   const char *exchange_url;
    132 
    133   (void) cmd;
    134   kps->is = is;
    135   exchange_url = TALER_TESTING_get_exchange_url (is);
    136   if (NULL == exchange_url)
    137   {
    138     GNUNET_break (0);
    139     return;
    140   }
    141   res_cmd = TALER_TESTING_interpreter_lookup_command (
    142     kps->is,
    143     kps->payment_target_reference);
    144   if (NULL == res_cmd)
    145   {
    146     GNUNET_break (0);
    147     TALER_TESTING_interpreter_fail (kps->is);
    148     return;
    149   }
    150   if (GNUNET_OK !=
    151       TALER_TESTING_get_trait_h_normalized_payto (res_cmd,
    152                                                   &h_payto))
    153   {
    154     GNUNET_break (0);
    155     TALER_TESTING_interpreter_fail (kps->is);
    156     return;
    157   }
    158   if (NULL == kps->code)
    159     uargs = NULL;
    160   else
    161     GNUNET_asprintf (&uargs,
    162                      "&code=%s",
    163                      kps->code);
    164   kps->kph = TALER_EXCHANGE_kyc_proof (
    165     TALER_TESTING_interpreter_get_context (is),
    166     exchange_url,
    167     h_payto,
    168     kps->logic,
    169     uargs,
    170     &proof_kyc_cb,
    171     kps);
    172   GNUNET_free (uargs);
    173   GNUNET_assert (NULL != kps->kph);
    174 }
    175 
    176 
    177 /**
    178  * Cleanup the state from a "kyc proof" CMD, and possibly
    179  * cancel a operation thereof.
    180  *
    181  * @param cls closure.
    182  * @param cmd the command which is being cleaned up.
    183  */
    184 static void
    185 proof_kyc_cleanup (void *cls,
    186                    const struct TALER_TESTING_Command *cmd)
    187 {
    188   struct KycProofGetState *kps = cls;
    189 
    190   if (NULL != kps->kph)
    191   {
    192     TALER_TESTING_command_incomplete (kps->is,
    193                                       cmd->label);
    194     TALER_EXCHANGE_kyc_proof_cancel (kps->kph);
    195     kps->kph = NULL;
    196   }
    197   GNUNET_free (kps->redirect_url);
    198   GNUNET_free (kps);
    199 }
    200 
    201 
    202 /**
    203  * Offer internal data from a "proof KYC" CMD.
    204  *
    205  * @param cls closure.
    206  * @param[out] ret result (could be anything).
    207  * @param trait name of the trait.
    208  * @param index index number of the object to offer.
    209  * @return #GNUNET_OK on success.
    210  */
    211 static enum GNUNET_GenericReturnValue
    212 proof_kyc_traits (void *cls,
    213                   const void **ret,
    214                   const char *trait,
    215                   unsigned int index)
    216 {
    217   struct KycProofGetState *kps = cls;
    218   struct TALER_TESTING_Trait traits[] = {
    219     TALER_TESTING_make_trait_web_url (kps->redirect_url),
    220     TALER_TESTING_trait_end ()
    221   };
    222 
    223   return TALER_TESTING_get_trait (traits,
    224                                   ret,
    225                                   trait,
    226                                   index);
    227 }
    228 
    229 
    230 struct TALER_TESTING_Command
    231 TALER_TESTING_cmd_proof_kyc_oauth2 (
    232   const char *label,
    233   const char *payment_target_reference,
    234   const char *logic_section,
    235   const char *code,
    236   unsigned int expected_response_code)
    237 {
    238   struct KycProofGetState *kps;
    239 
    240   kps = GNUNET_new (struct KycProofGetState);
    241   kps->code = code;
    242   kps->logic = logic_section;
    243   kps->payment_target_reference = payment_target_reference;
    244   kps->expected_response_code = expected_response_code;
    245   {
    246     struct TALER_TESTING_Command cmd = {
    247       .cls = kps,
    248       .label = label,
    249       .run = &proof_kyc_run,
    250       .cleanup = &proof_kyc_cleanup,
    251       .traits = &proof_kyc_traits
    252     };
    253 
    254     return cmd;
    255   }
    256 }
    257 
    258 
    259 /* end of testing_api_cmd_kyc_proof.c */