Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2024 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it
6 : under the terms of the GNU General Public License as published by
7 : the Free Software Foundation; either version 3, or (at your
8 : option) any later version.
9 :
10 : TALER is distributed in the hope that it will be useful, but
11 : WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : 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, see
17 : <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file testing/testing_api_cmd_bank_account_token.c
21 : * @brief implementation of a bank /account/$ACC/token command
22 : * @author Christian Grothoff
23 : */
24 : #include "taler/taler_json_lib.h"
25 : #include <gnunet/gnunet_curl_lib.h>
26 : #include "taler/taler_bank_service.h"
27 : #include "taler/taler_signatures.h"
28 : #include "taler/taler_testing_lib.h"
29 :
30 : /**
31 : * State for a "bank transfer" CMD.
32 : */
33 : struct AccountTokenState
34 : {
35 :
36 : /**
37 : * Name of the account.
38 : */
39 : const char *account_name;
40 :
41 : /**
42 : * Scope for the requested token.
43 : */
44 : enum TALER_BANK_TokenScope scope;
45 :
46 : /**
47 : * Is the token refreshable?
48 : */
49 : bool refreshable;
50 :
51 : /**
52 : * How long should the token be valid.
53 : */
54 : struct GNUNET_TIME_Relative duration;
55 :
56 : /**
57 : * The access token, set on success.
58 : */
59 : char *access_token;
60 :
61 : /**
62 : * Data to use for authentication of the request.
63 : */
64 : struct TALER_BANK_AuthenticationData auth;
65 :
66 : /**
67 : * Handle to the pending request at the bank.
68 : */
69 : struct TALER_BANK_AccountTokenHandle *ath;
70 :
71 : /**
72 : * Interpreter state.
73 : */
74 : struct TALER_TESTING_Interpreter *is;
75 :
76 : /**
77 : * Expected HTTP status code.
78 : */
79 : unsigned int expected_http_status;
80 : };
81 :
82 :
83 : /**
84 : * This callback will process the bank response to the wire
85 : * transfer. It just checks whether the HTTP response code is
86 : * acceptable.
87 : *
88 : * @param cls closure with the interpreter state
89 : * @param atr response details
90 : */
91 : static void
92 0 : token_result_cb (void *cls,
93 : const struct TALER_BANK_AccountTokenResponse *atr)
94 : {
95 0 : struct AccountTokenState *fts = cls;
96 0 : struct TALER_TESTING_Interpreter *is = fts->is;
97 :
98 0 : fts->ath = NULL;
99 0 : if (atr->http_status != fts->expected_http_status)
100 : {
101 0 : TALER_TESTING_unexpected_status (is,
102 : atr->http_status,
103 : fts->expected_http_status);
104 0 : return;
105 : }
106 0 : switch (atr->http_status)
107 : {
108 0 : case MHD_HTTP_OK:
109 : fts->access_token
110 0 : = GNUNET_strdup (atr->details.ok.access_token);
111 0 : break;
112 0 : default:
113 0 : break;
114 : }
115 0 : TALER_TESTING_interpreter_next (is);
116 : }
117 :
118 :
119 : static void
120 0 : account_token_run (
121 : void *cls,
122 : const struct TALER_TESTING_Command *cmd,
123 : struct TALER_TESTING_Interpreter *is)
124 : {
125 0 : struct AccountTokenState *fts = cls;
126 :
127 : (void) cmd;
128 0 : fts->is = is;
129 : fts->ath
130 0 : = TALER_BANK_account_token (
131 : TALER_TESTING_interpreter_get_context (is),
132 0 : &fts->auth,
133 : fts->account_name,
134 : fts->scope,
135 0 : fts->refreshable,
136 : NULL /* description */,
137 : fts->duration,
138 : &token_result_cb,
139 : fts);
140 0 : if (NULL == fts->ath)
141 : {
142 0 : GNUNET_break (0);
143 0 : TALER_TESTING_interpreter_fail (is);
144 0 : return;
145 : }
146 : }
147 :
148 :
149 : /**
150 : * Free the state of a "/admin/add-incoming" CMD, and possibly
151 : * cancel a pending operation thereof.
152 : *
153 : * @param cls closure
154 : * @param cmd current CMD being cleaned up.
155 : */
156 : static void
157 0 : account_token_cleanup (
158 : void *cls,
159 : const struct TALER_TESTING_Command *cmd)
160 : {
161 0 : struct AccountTokenState *fts = cls;
162 :
163 0 : if (NULL != fts->ath)
164 : {
165 0 : TALER_TESTING_command_incomplete (fts->is,
166 : cmd->label);
167 0 : TALER_BANK_account_token_cancel (fts->ath);
168 0 : fts->ath = NULL;
169 : }
170 0 : GNUNET_free (fts->access_token);
171 0 : GNUNET_free (fts);
172 0 : }
173 :
174 :
175 : /**
176 : * Offer internal data from a "/admin/add-incoming" CMD to other
177 : * commands.
178 : *
179 : * @param cls closure.
180 : * @param[out] ret result
181 : * @param trait name of the trait.
182 : * @param index index number of the object to offer.
183 : * @return #GNUNET_OK on success.
184 : */
185 : static enum GNUNET_GenericReturnValue
186 0 : account_token_traits (void *cls,
187 : const void **ret,
188 : const char *trait,
189 : unsigned int index)
190 : {
191 0 : struct AccountTokenState *fts = cls;
192 : struct TALER_TESTING_Trait traits[] = {
193 0 : TALER_TESTING_make_trait_access_token (fts->access_token),
194 0 : TALER_TESTING_trait_end ()
195 : };
196 :
197 0 : if (MHD_HTTP_OK !=
198 0 : fts->expected_http_status)
199 0 : return GNUNET_NO; /* requests that failed generate no history */
200 :
201 0 : return TALER_TESTING_get_trait (traits,
202 : ret,
203 : trait,
204 : index);
205 : }
206 :
207 :
208 : struct TALER_TESTING_Command
209 0 : TALER_TESTING_cmd_bank_account_token (
210 : const char *label,
211 : const struct TALER_BANK_AuthenticationData *auth,
212 : const char *account_name,
213 : enum TALER_BANK_TokenScope scope,
214 : bool refreshable,
215 : struct GNUNET_TIME_Relative duration,
216 : unsigned int expected_http_status)
217 : {
218 : struct AccountTokenState *fts;
219 :
220 0 : fts = GNUNET_new (struct AccountTokenState);
221 0 : fts->account_name = account_name;
222 0 : fts->scope = scope;
223 0 : fts->refreshable = refreshable;
224 0 : fts->duration = duration;
225 0 : fts->auth = *auth;
226 0 : fts->expected_http_status = expected_http_status;
227 : {
228 0 : struct TALER_TESTING_Command cmd = {
229 : .cls = fts,
230 : .label = label,
231 : .run = &account_token_run,
232 : .cleanup = &account_token_cleanup,
233 : .traits = &account_token_traits
234 : };
235 :
236 0 : return cmd;
237 : }
238 : }
239 :
240 :
241 : /* end of testing_api_cmd_bank_account_token.c */
|