exchange

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

fakebank_bank_testing_register.c (3983B)


      1 /*
      2   This file is part of TALER
      3   (C) 2016-2023 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or
      6   modify it under the terms of the GNU General Public License
      7   as published by the Free Software Foundation; either version 3,
      8   or (at your option) any later version.
      9 
     10   TALER is distributed in the hope that it will be useful,
     11   but 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,
     17   see <http://www.gnu.org/licenses/>
     18 */
     19 /**
     20  * @file bank-lib/fakebank_bank_testing_register.c
     21  * @brief implementation of /testing/register endpoint for the bank API
     22  * @author Christian Grothoff <christian@grothoff.org>
     23  */
     24 #include "taler/platform.h"
     25 #include "taler/taler_fakebank_lib.h"
     26 #include "taler/taler_bank_service.h"
     27 #include "taler/taler_mhd_lib.h"
     28 #include <gnunet/gnunet_mhd_compat.h>
     29 #include <gnunet/gnunet_mhd_lib.h>
     30 #include "fakebank.h"
     31 #include "fakebank_bank_testing_register.h"
     32 
     33 
     34 MHD_RESULT
     35 TALER_FAKEBANK_bank_testing_register_ (
     36   struct TALER_FAKEBANK_Handle *h,
     37   struct MHD_Connection *connection,
     38   const void *upload_data,
     39   size_t *upload_data_size,
     40   void **con_cls)
     41 {
     42   struct ConnectionContext *cc = *con_cls;
     43   enum GNUNET_MHD_PostResult pr;
     44   json_t *json;
     45   MHD_RESULT res;
     46 
     47   if (NULL == cc)
     48   {
     49     cc = GNUNET_new (struct ConnectionContext);
     50     cc->ctx_cleaner = &GNUNET_MHD_post_parser_cleanup;
     51     *con_cls = cc;
     52   }
     53   pr = GNUNET_MHD_post_parser (REQUEST_BUFFER_MAX,
     54                                connection,
     55                                &cc->ctx,
     56                                upload_data,
     57                                upload_data_size,
     58                                &json);
     59   switch (pr)
     60   {
     61   case GNUNET_MHD_PR_OUT_OF_MEMORY:
     62     GNUNET_break (0);
     63     return MHD_NO;
     64   case GNUNET_MHD_PR_CONTINUE:
     65     return MHD_YES;
     66   case GNUNET_MHD_PR_REQUEST_TOO_LARGE:
     67     GNUNET_break (0);
     68     return MHD_NO;
     69   case GNUNET_MHD_PR_JSON_INVALID:
     70     GNUNET_break (0);
     71     return MHD_NO;
     72   case GNUNET_MHD_PR_SUCCESS:
     73     break;
     74   }
     75 
     76   {
     77     const char *username;
     78     const char *password;
     79     struct GNUNET_JSON_Specification spec[] = {
     80       GNUNET_JSON_spec_string ("username",
     81                                &username),
     82       GNUNET_JSON_spec_string ("password",
     83                                &password),
     84       GNUNET_JSON_spec_end ()
     85     };
     86     enum GNUNET_GenericReturnValue ret;
     87     struct Account *acc;
     88 
     89     if (GNUNET_OK !=
     90         (ret = TALER_MHD_parse_json_data (connection,
     91                                           json,
     92                                           spec)))
     93     {
     94       GNUNET_break_op (0);
     95       json_decref (json);
     96       return (GNUNET_NO == ret) ? MHD_YES : MHD_NO;
     97     }
     98     acc = TALER_FAKEBANK_lookup_account_ (h,
     99                                           username,
    100                                           NULL);
    101     if (NULL != acc)
    102     {
    103       if (0 != strcmp (password,
    104                        acc->password))
    105       {
    106         json_decref (json);
    107         return TALER_MHD_reply_with_error (connection,
    108                                            MHD_HTTP_CONFLICT,
    109                                            TALER_EC_BANK_REGISTER_CONFLICT,
    110                                            "password");
    111       }
    112     }
    113     else
    114     {
    115       acc = TALER_FAKEBANK_lookup_account_ (h,
    116                                             username,
    117                                             username);
    118       GNUNET_assert (NULL != acc);
    119       acc->password = GNUNET_strdup (password);
    120       acc->balance = h->signup_bonus; /* magic money creation! */
    121     }
    122     res = TALER_MHD_REPLY_JSON_PACK (
    123       connection,
    124       MHD_HTTP_OK,
    125       GNUNET_JSON_pack_string ("internal_payto_uri",
    126                                acc->payto_uri));
    127   }
    128   json_decref (json);
    129   return res;
    130 }