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_bank_account_token.c (5681B)


      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
      6   under the terms of the GNU General Public License as published by
      7   the Free Software Foundation; either version 3, or (at your
      8   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 GNU
     13   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  * @file testing/testing_api_cmd_bank_account_token.c
     21  * @brief implementation of a bank /account/$ACC/token command
     22  * @author Christian Grothoff
     23  */
     24 #include "taler/taler_json_lib.h"
     25 #include <gnunet/gnunet_curl_lib.h>
     26 #include "taler/taler_bank_service.h"
     27 #include "taler/taler_signatures.h"
     28 #include "taler/taler_testing_lib.h"
     29 
     30 /**
     31  * State for a "bank transfer" CMD.
     32  */
     33 struct AccountTokenState
     34 {
     35 
     36   /**
     37    * Name of the account.
     38    */
     39   const char *account_name;
     40 
     41   /**
     42    * Scope for the requested token.
     43    */
     44   enum TALER_BANK_TokenScope scope;
     45 
     46   /**
     47    * Is the token refreshable?
     48    */
     49   bool refreshable;
     50 
     51   /**
     52    * How long should the token be valid.
     53    */
     54   struct GNUNET_TIME_Relative duration;
     55 
     56   /**
     57    * The access token, set on success.
     58    */
     59   char *access_token;
     60 
     61   /**
     62    * Data to use for authentication of the request.
     63    */
     64   struct TALER_BANK_AuthenticationData auth;
     65 
     66   /**
     67    * Handle to the pending request at the bank.
     68    */
     69   struct TALER_BANK_AccountTokenHandle *ath;
     70 
     71   /**
     72    * Interpreter state.
     73    */
     74   struct TALER_TESTING_Interpreter *is;
     75 
     76   /**
     77    * Expected HTTP status code.
     78    */
     79   unsigned int expected_http_status;
     80 };
     81 
     82 
     83 /**
     84  * This callback will process the bank response to the wire
     85  * transfer.  It just checks whether the HTTP response code is
     86  * acceptable.
     87  *
     88  * @param cls closure with the interpreter state
     89  * @param atr response details
     90  */
     91 static void
     92 token_result_cb (void *cls,
     93                  const struct TALER_BANK_AccountTokenResponse *atr)
     94 {
     95   struct AccountTokenState *fts = cls;
     96   struct TALER_TESTING_Interpreter *is = fts->is;
     97 
     98   fts->ath = NULL;
     99   if (atr->http_status != fts->expected_http_status)
    100   {
    101     TALER_TESTING_unexpected_status (is,
    102                                      atr->http_status,
    103                                      fts->expected_http_status);
    104     return;
    105   }
    106   switch (atr->http_status)
    107   {
    108   case MHD_HTTP_OK:
    109     fts->access_token
    110       = GNUNET_strdup (atr->details.ok.access_token);
    111     break;
    112   default:
    113     break;
    114   }
    115   TALER_TESTING_interpreter_next (is);
    116 }
    117 
    118 
    119 static void
    120 account_token_run (
    121   void *cls,
    122   const struct TALER_TESTING_Command *cmd,
    123   struct TALER_TESTING_Interpreter *is)
    124 {
    125   struct AccountTokenState *fts = cls;
    126 
    127   (void) cmd;
    128   fts->is = is;
    129   fts->ath
    130     = TALER_BANK_account_token (
    131         TALER_TESTING_interpreter_get_context (is),
    132         &fts->auth,
    133         fts->scope,
    134         fts->refreshable,
    135         NULL /* description */,
    136         fts->duration,
    137         &token_result_cb,
    138         fts);
    139   if (NULL == fts->ath)
    140   {
    141     GNUNET_break (0);
    142     TALER_TESTING_interpreter_fail (is);
    143     return;
    144   }
    145 }
    146 
    147 
    148 /**
    149  * Free the state of a "/admin/add-incoming" CMD, and possibly
    150  * cancel a pending operation thereof.
    151  *
    152  * @param cls closure
    153  * @param cmd current CMD being cleaned up.
    154  */
    155 static void
    156 account_token_cleanup (
    157   void *cls,
    158   const struct TALER_TESTING_Command *cmd)
    159 {
    160   struct AccountTokenState *fts = cls;
    161 
    162   if (NULL != fts->ath)
    163   {
    164     TALER_TESTING_command_incomplete (fts->is,
    165                                       cmd->label);
    166     TALER_BANK_account_token_cancel (fts->ath);
    167     fts->ath = NULL;
    168   }
    169   GNUNET_free (fts->access_token);
    170   GNUNET_free (fts);
    171 }
    172 
    173 
    174 /**
    175  * Offer internal data from a "/admin/add-incoming" CMD to other
    176  * commands.
    177  *
    178  * @param cls closure.
    179  * @param[out] ret result
    180  * @param trait name of the trait.
    181  * @param index index number of the object to offer.
    182  * @return #GNUNET_OK on success.
    183  */
    184 static enum GNUNET_GenericReturnValue
    185 account_token_traits (void *cls,
    186                       const void **ret,
    187                       const char *trait,
    188                       unsigned int index)
    189 {
    190   struct AccountTokenState *fts = cls;
    191   struct TALER_TESTING_Trait traits[] = {
    192     TALER_TESTING_make_trait_access_token (fts->access_token),
    193     TALER_TESTING_trait_end ()
    194   };
    195 
    196   if (MHD_HTTP_OK !=
    197       fts->expected_http_status)
    198     return GNUNET_NO; /* requests that failed generate no history */
    199 
    200   return TALER_TESTING_get_trait (traits,
    201                                   ret,
    202                                   trait,
    203                                   index);
    204 }
    205 
    206 
    207 struct TALER_TESTING_Command
    208 TALER_TESTING_cmd_bank_account_token (
    209   const char *label,
    210   const struct TALER_BANK_AuthenticationData *auth,
    211   const char *account_name,
    212   enum TALER_BANK_TokenScope scope,
    213   bool refreshable,
    214   struct GNUNET_TIME_Relative duration,
    215   unsigned int expected_http_status)
    216 {
    217   struct AccountTokenState *fts;
    218 
    219   fts = GNUNET_new (struct AccountTokenState);
    220   fts->account_name = account_name;
    221   fts->scope = scope;
    222   fts->refreshable = refreshable;
    223   fts->duration = duration;
    224   fts->auth = *auth;
    225   fts->expected_http_status = expected_http_status;
    226   {
    227     struct TALER_TESTING_Command cmd = {
    228       .cls = fts,
    229       .label = label,
    230       .run = &account_token_run,
    231       .cleanup = &account_token_cleanup,
    232       .traits = &account_token_traits
    233     };
    234 
    235     return cmd;
    236   }
    237 }
    238 
    239 
    240 /* end of testing_api_cmd_bank_account_token.c */