Line data Source code
1 : /*
2 : This file is part of Taler
3 : Copyright (C) 2018-2023 Taler Systems SA
4 :
5 : Taler is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 3, or (at your option) any later version.
8 :
9 : Taler is distributed in the hope that it will be useful, but WITHOUT ANY
10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 : A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 :
13 : You should have received a copy of the GNU General Public License along with
14 : Taler; see the file COPYING. If not, see
15 : <http://www.gnu.org/licenses/>
16 : */
17 : /**
18 : * @file src/bank/mb_parse.c
19 : * @brief Convenience function to parse authentication configuration
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include "taler/taler_merchant_bank_lib.h"
24 : #include <gnunet/gnunet_json_lib.h>
25 :
26 :
27 : /**
28 : * Names of authentication methods available.
29 : */
30 : static const struct
31 : {
32 : const char *m;
33 : enum TALER_MERCHANT_BANK_AuthenticationMethod e;
34 : } methods[] = {
35 : { "NONE", TALER_MERCHANT_BANK_AUTH_NONE },
36 : { "BASIC", TALER_MERCHANT_BANK_AUTH_BASIC },
37 : { "BEARER", TALER_MERCHANT_BANK_AUTH_BEARER },
38 : { NULL, TALER_MERCHANT_BANK_AUTH_NONE }
39 : };
40 :
41 :
42 : enum GNUNET_GenericReturnValue
43 0 : TALER_MERCHANT_BANK_auth_parse_cfg (
44 : const struct GNUNET_CONFIGURATION_Handle *cfg,
45 : const char *section,
46 : struct TALER_MERCHANT_BANK_AuthenticationData *auth)
47 : {
48 : char *method;
49 :
50 0 : if (GNUNET_OK !=
51 0 : GNUNET_CONFIGURATION_get_value_string (cfg,
52 : section,
53 : "WIRE_GATEWAY_URL",
54 : &auth->wire_gateway_url))
55 : {
56 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
57 : section,
58 : "WIRE_GATEWAY_URL");
59 0 : return GNUNET_SYSERR;
60 : }
61 :
62 0 : if (GNUNET_OK !=
63 0 : GNUNET_CONFIGURATION_get_value_string (cfg,
64 : section,
65 : "WIRE_GATEWAY_AUTH_METHOD",
66 : &method))
67 : {
68 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
69 : section,
70 : "WIRE_GATEWAY_AUTH_METHOD");
71 0 : GNUNET_free (auth->wire_gateway_url);
72 0 : return GNUNET_SYSERR;
73 : }
74 0 : for (unsigned int i = 0; NULL != methods[i].m; i++)
75 : {
76 0 : if (0 == strcasecmp (method,
77 0 : methods[i].m))
78 : {
79 0 : switch (methods[i].e)
80 : {
81 0 : case TALER_MERCHANT_BANK_AUTH_NONE:
82 0 : auth->method = TALER_MERCHANT_BANK_AUTH_NONE;
83 0 : GNUNET_free (method);
84 0 : return GNUNET_OK;
85 0 : case TALER_MERCHANT_BANK_AUTH_BASIC:
86 0 : if (GNUNET_OK !=
87 0 : GNUNET_CONFIGURATION_get_value_string (cfg,
88 : section,
89 : "USERNAME",
90 : &auth->details.basic.username
91 : ))
92 : {
93 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
94 : section,
95 : "USERNAME");
96 0 : GNUNET_free (method);
97 0 : GNUNET_free (auth->wire_gateway_url);
98 0 : return GNUNET_SYSERR;
99 : }
100 0 : if (GNUNET_OK !=
101 0 : GNUNET_CONFIGURATION_get_value_string (cfg,
102 : section,
103 : "PASSWORD",
104 : &auth->details.basic.password
105 : ))
106 : {
107 0 : GNUNET_free (auth->details.basic.username);
108 0 : auth->details.basic.username = NULL;
109 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
110 : section,
111 : "PASSWORD");
112 0 : GNUNET_free (method);
113 0 : GNUNET_free (auth->wire_gateway_url);
114 0 : return GNUNET_SYSERR;
115 : }
116 0 : auth->method = TALER_MERCHANT_BANK_AUTH_BASIC;
117 0 : GNUNET_free (method);
118 0 : return GNUNET_OK;
119 0 : case TALER_MERCHANT_BANK_AUTH_BEARER:
120 0 : if (GNUNET_OK !=
121 0 : GNUNET_CONFIGURATION_get_value_string (cfg,
122 : section,
123 : "TOKEN",
124 : &auth->details.bearer.token))
125 : {
126 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
127 : section,
128 : "TOKEN");
129 0 : GNUNET_free (method);
130 0 : GNUNET_free (auth->wire_gateway_url);
131 0 : return GNUNET_SYSERR;
132 : }
133 0 : auth->method = TALER_MERCHANT_BANK_AUTH_BEARER;
134 0 : GNUNET_free (method);
135 0 : return GNUNET_OK;
136 : }
137 : }
138 : }
139 0 : GNUNET_free (method);
140 0 : return GNUNET_SYSERR;
141 : }
142 :
143 :
144 : enum GNUNET_GenericReturnValue
145 6 : TALER_MERCHANT_BANK_auth_parse_json (
146 : const json_t *cred,
147 : const char *backend_url,
148 : struct TALER_MERCHANT_BANK_AuthenticationData *auth)
149 : {
150 : const char *method;
151 :
152 6 : if (NULL == backend_url)
153 : {
154 0 : GNUNET_break_op (0);
155 0 : return GNUNET_SYSERR;
156 : }
157 6 : if ( (0 == strlen (backend_url)) ||
158 6 : ('/' != backend_url[strlen (backend_url) - 1]) )
159 : {
160 0 : GNUNET_break_op (0);
161 0 : return GNUNET_SYSERR;
162 : }
163 6 : auth->wire_gateway_url = GNUNET_strdup (backend_url);
164 6 : method = json_string_value (json_object_get (cred,
165 : "type"));
166 6 : if (NULL == method)
167 : {
168 0 : GNUNET_break_op (0);
169 0 : GNUNET_free (auth->wire_gateway_url);
170 0 : return GNUNET_SYSERR;
171 : }
172 12 : for (unsigned int i = 0; NULL != methods[i].m; i++)
173 : {
174 12 : if (0 == strcasecmp (method,
175 12 : methods[i].m))
176 : {
177 6 : switch (methods[i].e)
178 : {
179 0 : case TALER_MERCHANT_BANK_AUTH_NONE:
180 0 : auth->method = TALER_MERCHANT_BANK_AUTH_NONE;
181 0 : return GNUNET_OK;
182 6 : case TALER_MERCHANT_BANK_AUTH_BASIC:
183 : {
184 : const char *username;
185 : const char *password;
186 : struct GNUNET_JSON_Specification spec[] = {
187 6 : GNUNET_JSON_spec_string ("username",
188 : &username),
189 6 : GNUNET_JSON_spec_string ("password",
190 : &password),
191 6 : GNUNET_JSON_spec_end ()
192 : };
193 : enum GNUNET_GenericReturnValue res;
194 : const char *err;
195 : unsigned int eline;
196 :
197 6 : res = GNUNET_JSON_parse (cred,
198 : spec,
199 : &err,
200 : &eline);
201 6 : if (GNUNET_OK != res)
202 : {
203 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
204 : "Credentials malformed: %s (%u)\n",
205 : err,
206 : eline);
207 0 : GNUNET_free (auth->wire_gateway_url);
208 0 : return GNUNET_SYSERR;
209 : }
210 6 : auth->details.basic.username = GNUNET_strdup (username);
211 6 : auth->details.basic.password = GNUNET_strdup (password);
212 : }
213 6 : auth->method = TALER_MERCHANT_BANK_AUTH_BASIC;
214 6 : return GNUNET_OK;
215 0 : case TALER_MERCHANT_BANK_AUTH_BEARER:
216 : {
217 : const char *token;
218 : struct GNUNET_JSON_Specification spec[] = {
219 0 : GNUNET_JSON_spec_string ("token",
220 : &token),
221 0 : GNUNET_JSON_spec_end ()
222 : };
223 : enum GNUNET_GenericReturnValue res;
224 : const char *err;
225 : unsigned int eline;
226 :
227 0 : res = GNUNET_JSON_parse (cred,
228 : spec,
229 : &err,
230 : &eline);
231 0 : if (GNUNET_OK != res)
232 : {
233 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
234 : "Credentials malformed: %s (%u)\n",
235 : err,
236 : eline);
237 0 : GNUNET_free (auth->wire_gateway_url);
238 0 : return GNUNET_SYSERR;
239 : }
240 0 : auth->details.bearer.token = GNUNET_strdup (token);
241 : }
242 0 : auth->method = TALER_MERCHANT_BANK_AUTH_BEARER;
243 0 : return GNUNET_OK;
244 : }
245 : }
246 : }
247 0 : GNUNET_free (auth->wire_gateway_url);
248 0 : return GNUNET_SYSERR;
249 : }
250 :
251 :
252 : void
253 6 : TALER_MERCHANT_BANK_auth_free (
254 : struct TALER_MERCHANT_BANK_AuthenticationData *auth)
255 : {
256 6 : switch (auth->method)
257 : {
258 0 : case TALER_MERCHANT_BANK_AUTH_NONE:
259 0 : break;
260 6 : case TALER_MERCHANT_BANK_AUTH_BASIC:
261 6 : GNUNET_free (auth->details.basic.username);
262 6 : GNUNET_free (auth->details.basic.password);
263 6 : break;
264 0 : case TALER_MERCHANT_BANK_AUTH_BEARER:
265 0 : GNUNET_free (auth->details.bearer.token);
266 0 : break;
267 : }
268 6 : GNUNET_free (auth->wire_gateway_url);
269 6 : }
270 :
271 :
272 : /* end of mb_parse.c */
|