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 (5763B)


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