Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2016-2024 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_twg_admin_add_incoming.c
21 : * @brief library that fakes being a Taler bank for testcases
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_common_make_admin_transfer.h"
31 : #include "fakebank_twg_admin_add_incoming.h"
32 :
33 : MHD_RESULT
34 183 : TALER_FAKEBANK_twg_admin_add_incoming_ (
35 : struct TALER_FAKEBANK_Handle *h,
36 : struct MHD_Connection *connection,
37 : const char *account,
38 : const char *upload_data,
39 : size_t *upload_data_size,
40 : void **con_cls)
41 : {
42 183 : struct ConnectionContext *cc = *con_cls;
43 : enum GNUNET_MHD_PostResult pr;
44 : json_t *json;
45 : uint64_t row_id;
46 : struct GNUNET_TIME_Timestamp timestamp;
47 :
48 183 : if (NULL == cc)
49 : {
50 61 : cc = GNUNET_new (struct ConnectionContext);
51 61 : cc->ctx_cleaner = &GNUNET_MHD_post_parser_cleanup;
52 61 : *con_cls = cc;
53 : }
54 183 : pr = GNUNET_MHD_post_parser (REQUEST_BUFFER_MAX,
55 : connection,
56 : &cc->ctx,
57 : upload_data,
58 : upload_data_size,
59 : &json);
60 183 : switch (pr)
61 : {
62 0 : case GNUNET_MHD_PR_OUT_OF_MEMORY:
63 0 : GNUNET_break (0);
64 0 : return MHD_NO;
65 122 : case GNUNET_MHD_PR_CONTINUE:
66 122 : return MHD_YES;
67 0 : case GNUNET_MHD_PR_REQUEST_TOO_LARGE:
68 0 : GNUNET_break (0);
69 0 : return MHD_NO;
70 0 : case GNUNET_MHD_PR_JSON_INVALID:
71 0 : GNUNET_break (0);
72 0 : return MHD_NO;
73 61 : case GNUNET_MHD_PR_SUCCESS:
74 61 : break;
75 : }
76 : {
77 : struct TALER_FullPayto debit_account;
78 : struct TALER_Amount amount;
79 : struct TALER_ReservePublicKeyP reserve_pub;
80 : char *debit;
81 : enum GNUNET_GenericReturnValue ret;
82 : struct GNUNET_JSON_Specification spec[] = {
83 61 : GNUNET_JSON_spec_fixed_auto ("reserve_pub",
84 : &reserve_pub),
85 61 : TALER_JSON_spec_full_payto_uri ("debit_account",
86 : &debit_account),
87 61 : TALER_JSON_spec_amount ("amount",
88 61 : h->currency,
89 : &amount),
90 61 : GNUNET_JSON_spec_end ()
91 : };
92 :
93 61 : if (GNUNET_OK !=
94 61 : (ret = TALER_MHD_parse_json_data (connection,
95 : json,
96 : spec)))
97 : {
98 0 : GNUNET_break_op (0);
99 0 : json_decref (json);
100 2 : return (GNUNET_NO == ret) ? MHD_YES : MHD_NO;
101 : }
102 61 : if (0 != strcasecmp (amount.currency,
103 61 : h->currency))
104 : {
105 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
106 : "Currency `%s' does not match our configuration\n",
107 : amount.currency);
108 0 : json_decref (json);
109 0 : return TALER_MHD_reply_with_error (
110 : connection,
111 : MHD_HTTP_CONFLICT,
112 : TALER_EC_GENERIC_CURRENCY_MISMATCH,
113 : NULL);
114 : }
115 61 : debit = TALER_xtalerbank_account_from_payto (debit_account);
116 61 : if (NULL == debit)
117 : {
118 0 : GNUNET_break_op (0);
119 0 : return TALER_MHD_reply_with_error (
120 : connection,
121 : MHD_HTTP_BAD_REQUEST,
122 : TALER_EC_GENERIC_PAYTO_URI_MALFORMED,
123 0 : debit_account.full_payto);
124 : }
125 61 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
126 : "Receiving incoming wire transfer: %s->%s, subject: %s, amount: %s\n",
127 : debit,
128 : account,
129 : TALER_B2S (&reserve_pub),
130 : TALER_amount2s (&amount));
131 61 : ret = TALER_FAKEBANK_make_admin_transfer_ (h,
132 : debit,
133 : account,
134 : &amount,
135 : &reserve_pub,
136 : &row_id,
137 : ×tamp);
138 61 : GNUNET_free (debit);
139 61 : if (GNUNET_OK != ret)
140 : {
141 2 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
142 : "Reserve public key not unique\n");
143 2 : json_decref (json);
144 2 : return TALER_MHD_reply_with_error (
145 : connection,
146 : MHD_HTTP_CONFLICT,
147 : TALER_EC_BANK_DUPLICATE_RESERVE_PUB_SUBJECT,
148 : NULL);
149 : }
150 : }
151 59 : json_decref (json);
152 :
153 : /* Finally build response object */
154 59 : return TALER_MHD_REPLY_JSON_PACK (connection,
155 : MHD_HTTP_OK,
156 : GNUNET_JSON_pack_uint64 ("row_id",
157 : row_id),
158 : GNUNET_JSON_pack_timestamp ("timestamp",
159 : timestamp));
160 : }
|