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