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


      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/taler_fakebank_lib.h"
     25 #include "taler/taler_bank_service.h"
     26 #include "taler/taler_mhd_lib.h"
     27 #include <gnunet/gnunet_mhd_compat.h>
     28 #include <gnunet/gnunet_mhd_lib.h>
     29 #include "fakebank.h"
     30 #include "fakebank_bank_testing_register.h"
     31 
     32 
     33 MHD_RESULT
     34 TALER_FAKEBANK_bank_testing_register_ (
     35   struct TALER_FAKEBANK_Handle *h,
     36   struct MHD_Connection *connection,
     37   const void *upload_data,
     38   size_t *upload_data_size,
     39   void **con_cls)
     40 {
     41   struct ConnectionContext *cc = *con_cls;
     42   enum GNUNET_MHD_PostResult pr;
     43   json_t *json;
     44   MHD_RESULT res;
     45 
     46   if (NULL == cc)
     47   {
     48     cc = GNUNET_new (struct ConnectionContext);
     49     cc->ctx_cleaner = &GNUNET_MHD_post_parser_cleanup;
     50     *con_cls = cc;
     51   }
     52   pr = GNUNET_MHD_post_parser (REQUEST_BUFFER_MAX,
     53                                connection,
     54                                &cc->ctx,
     55                                upload_data,
     56                                upload_data_size,
     57                                &json);
     58   switch (pr)
     59   {
     60   case GNUNET_MHD_PR_OUT_OF_MEMORY:
     61     GNUNET_break (0);
     62     return MHD_NO;
     63   case GNUNET_MHD_PR_CONTINUE:
     64     return MHD_YES;
     65   case GNUNET_MHD_PR_REQUEST_TOO_LARGE:
     66     GNUNET_break (0);
     67     return MHD_NO;
     68   case GNUNET_MHD_PR_JSON_INVALID:
     69     GNUNET_break (0);
     70     return MHD_NO;
     71   case GNUNET_MHD_PR_SUCCESS:
     72     break;
     73   }
     74 
     75   {
     76     const char *username;
     77     const char *password;
     78     struct GNUNET_JSON_Specification spec[] = {
     79       GNUNET_JSON_spec_string ("username",
     80                                &username),
     81       GNUNET_JSON_spec_string ("password",
     82                                &password),
     83       GNUNET_JSON_spec_end ()
     84     };
     85     enum GNUNET_GenericReturnValue ret;
     86     struct Account *acc;
     87 
     88     if (GNUNET_OK !=
     89         (ret = TALER_MHD_parse_json_data (connection,
     90                                           json,
     91                                           spec)))
     92     {
     93       GNUNET_break_op (0);
     94       json_decref (json);
     95       return (GNUNET_NO == ret) ? MHD_YES : MHD_NO;
     96     }
     97     acc = TALER_FAKEBANK_lookup_account_ (h,
     98                                           username,
     99                                           NULL);
    100     if (NULL != acc)
    101     {
    102       if (0 != strcmp (password,
    103                        acc->password))
    104       {
    105         json_decref (json);
    106         return TALER_MHD_reply_with_error (connection,
    107                                            MHD_HTTP_CONFLICT,
    108                                            TALER_EC_BANK_REGISTER_CONFLICT,
    109                                            "password");
    110       }
    111     }
    112     else
    113     {
    114       acc = TALER_FAKEBANK_lookup_account_ (h,
    115                                             username,
    116                                             username);
    117       GNUNET_assert (NULL != acc);
    118       acc->password = GNUNET_strdup (password);
    119       acc->balance = h->signup_bonus; /* magic money creation! */
    120     }
    121     res = TALER_MHD_REPLY_JSON_PACK (
    122       connection,
    123       MHD_HTTP_OK,
    124       GNUNET_JSON_pack_string ("internal_payto_uri",
    125                                acc->payto_uri));
    126   }
    127   json_decref (json);
    128   return res;
    129 }