Line data Source code
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 42 : 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 42 : struct ConnectionContext *cc = *con_cls; 43 : enum GNUNET_MHD_PostResult pr; 44 : json_t *json; 45 : MHD_RESULT res; 46 : 47 42 : if (NULL == cc) 48 : { 49 14 : cc = GNUNET_new (struct ConnectionContext); 50 14 : cc->ctx_cleaner = &GNUNET_MHD_post_parser_cleanup; 51 14 : *con_cls = cc; 52 : } 53 42 : pr = GNUNET_MHD_post_parser (REQUEST_BUFFER_MAX, 54 : connection, 55 : &cc->ctx, 56 : upload_data, 57 : upload_data_size, 58 : &json); 59 42 : switch (pr) 60 : { 61 0 : case GNUNET_MHD_PR_OUT_OF_MEMORY: 62 0 : GNUNET_break (0); 63 0 : return MHD_NO; 64 28 : case GNUNET_MHD_PR_CONTINUE: 65 28 : return MHD_YES; 66 0 : case GNUNET_MHD_PR_REQUEST_TOO_LARGE: 67 0 : GNUNET_break (0); 68 0 : return MHD_NO; 69 0 : case GNUNET_MHD_PR_JSON_INVALID: 70 0 : GNUNET_break (0); 71 0 : return MHD_NO; 72 14 : case GNUNET_MHD_PR_SUCCESS: 73 14 : break; 74 : } 75 : 76 : { 77 : const char *username; 78 : const char *password; 79 : struct GNUNET_JSON_Specification spec[] = { 80 14 : GNUNET_JSON_spec_string ("username", 81 : &username), 82 14 : GNUNET_JSON_spec_string ("password", 83 : &password), 84 14 : GNUNET_JSON_spec_end () 85 : }; 86 : enum GNUNET_GenericReturnValue ret; 87 : struct Account *acc; 88 : 89 14 : if (GNUNET_OK != 90 14 : (ret = TALER_MHD_parse_json_data (connection, 91 : json, 92 : spec))) 93 : { 94 0 : GNUNET_break_op (0); 95 0 : json_decref (json); 96 0 : return (GNUNET_NO == ret) ? MHD_YES : MHD_NO; 97 : } 98 14 : acc = TALER_FAKEBANK_lookup_account_ (h, 99 : username, 100 : NULL); 101 14 : if (NULL != acc) 102 : { 103 0 : if (0 != strcmp (password, 104 0 : acc->password)) 105 : { 106 0 : 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 14 : acc = TALER_FAKEBANK_lookup_account_ (h, 115 : username, 116 : username); 117 14 : GNUNET_assert (NULL != acc); 118 14 : acc->password = GNUNET_strdup (password); 119 14 : acc->balance = h->signup_bonus; /* magic money creation! */ 120 : } 121 14 : 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 14 : json_decref (json); 128 14 : return res; 129 : }