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 bank/mb_parse.c
19 : * @brief Convenience function to parse authentication configuration
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include "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 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
93 : section,
94 : "USERNAME");
95 0 : GNUNET_free (method);
96 0 : GNUNET_free (auth->wire_gateway_url);
97 0 : return GNUNET_SYSERR;
98 : }
99 0 : if (GNUNET_OK !=
100 0 : GNUNET_CONFIGURATION_get_value_string (cfg,
101 : section,
102 : "PASSWORD",
103 : &auth->details.basic.password))
104 : {
105 0 : GNUNET_free (auth->details.basic.username);
106 0 : auth->details.basic.username = NULL;
107 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
108 : section,
109 : "PASSWORD");
110 0 : GNUNET_free (method);
111 0 : GNUNET_free (auth->wire_gateway_url);
112 0 : return GNUNET_SYSERR;
113 : }
114 0 : auth->method = TALER_MERCHANT_BANK_AUTH_BASIC;
115 0 : GNUNET_free (method);
116 0 : return GNUNET_OK;
117 0 : case TALER_MERCHANT_BANK_AUTH_BEARER:
118 0 : if (GNUNET_OK !=
119 0 : GNUNET_CONFIGURATION_get_value_string (cfg,
120 : section,
121 : "TOKEN",
122 : &auth->details.bearer.token))
123 : {
124 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
125 : section,
126 : "TOKEN");
127 0 : GNUNET_free (method);
128 0 : GNUNET_free (auth->wire_gateway_url);
129 0 : return GNUNET_SYSERR;
130 : }
131 0 : auth->method = TALER_MERCHANT_BANK_AUTH_BEARER;
132 0 : GNUNET_free (method);
133 0 : return GNUNET_OK;
134 : }
135 : }
136 : }
137 0 : GNUNET_free (method);
138 0 : return GNUNET_SYSERR;
139 : }
140 :
141 :
142 : enum GNUNET_GenericReturnValue
143 4 : TALER_MERCHANT_BANK_auth_parse_json (
144 : const json_t *cred,
145 : const char *backend_url,
146 : struct TALER_MERCHANT_BANK_AuthenticationData *auth)
147 : {
148 : const char *method;
149 :
150 4 : if (NULL == backend_url)
151 : {
152 0 : GNUNET_break_op (0);
153 0 : return GNUNET_SYSERR;
154 : }
155 4 : if ( (0 == strlen (backend_url)) ||
156 4 : ('/' != backend_url[strlen (backend_url) - 1]) )
157 : {
158 0 : GNUNET_break_op (0);
159 0 : return GNUNET_SYSERR;
160 : }
161 4 : auth->wire_gateway_url = GNUNET_strdup (backend_url);
162 4 : method = json_string_value (json_object_get (cred,
163 : "type"));
164 4 : if (NULL == method)
165 : {
166 0 : GNUNET_break_op (0);
167 0 : return GNUNET_SYSERR;
168 : }
169 8 : for (unsigned int i = 0; NULL != methods[i].m; i++)
170 : {
171 8 : if (0 == strcasecmp (method,
172 8 : methods[i].m))
173 : {
174 4 : switch (methods[i].e)
175 : {
176 0 : case TALER_MERCHANT_BANK_AUTH_NONE:
177 0 : auth->method = TALER_MERCHANT_BANK_AUTH_NONE;
178 0 : return GNUNET_OK;
179 4 : case TALER_MERCHANT_BANK_AUTH_BASIC:
180 : {
181 : const char *username;
182 : const char *password;
183 : struct GNUNET_JSON_Specification spec[] = {
184 4 : GNUNET_JSON_spec_string ("username",
185 : &username),
186 4 : GNUNET_JSON_spec_string ("password",
187 : &password),
188 4 : GNUNET_JSON_spec_end ()
189 : };
190 : enum GNUNET_GenericReturnValue res;
191 : const char *err;
192 : unsigned int eline;
193 :
194 4 : res = GNUNET_JSON_parse (cred,
195 : spec,
196 : &err,
197 : &eline);
198 4 : if (GNUNET_OK != res)
199 : {
200 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
201 : "Credentials malformed: %s (%u)\n",
202 : err,
203 : eline);
204 0 : GNUNET_free (auth->wire_gateway_url);
205 0 : return GNUNET_SYSERR;
206 : }
207 4 : auth->details.basic.username = GNUNET_strdup (username);
208 4 : auth->details.basic.password = GNUNET_strdup (password);
209 : }
210 4 : auth->method = TALER_MERCHANT_BANK_AUTH_BASIC;
211 4 : return GNUNET_OK;
212 0 : case TALER_MERCHANT_BANK_AUTH_BEARER:
213 : {
214 : const char *token;
215 : struct GNUNET_JSON_Specification spec[] = {
216 0 : GNUNET_JSON_spec_string ("token",
217 : &token),
218 0 : GNUNET_JSON_spec_end ()
219 : };
220 : enum GNUNET_GenericReturnValue res;
221 : const char *err;
222 : unsigned int eline;
223 :
224 0 : res = GNUNET_JSON_parse (cred,
225 : spec,
226 : &err,
227 : &eline);
228 0 : if (GNUNET_OK != res)
229 : {
230 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
231 : "Credentials malformed: %s (%u)\n",
232 : err,
233 : eline);
234 0 : GNUNET_free (auth->wire_gateway_url);
235 0 : return GNUNET_SYSERR;
236 : }
237 0 : auth->details.bearer.token = GNUNET_strdup (token);
238 : }
239 0 : auth->method = TALER_MERCHANT_BANK_AUTH_BEARER;
240 0 : return GNUNET_OK;
241 : }
242 : }
243 : }
244 0 : return GNUNET_SYSERR;
245 : }
246 :
247 :
248 : void
249 4 : TALER_MERCHANT_BANK_auth_free (
250 : struct TALER_MERCHANT_BANK_AuthenticationData *auth)
251 : {
252 4 : switch (auth->method)
253 : {
254 0 : case TALER_MERCHANT_BANK_AUTH_NONE:
255 0 : break;
256 4 : case TALER_MERCHANT_BANK_AUTH_BASIC:
257 4 : GNUNET_free (auth->details.basic.username);
258 4 : GNUNET_free (auth->details.basic.password);
259 4 : break;
260 0 : case TALER_MERCHANT_BANK_AUTH_BEARER:
261 0 : GNUNET_free (auth->details.bearer.token);
262 0 : break;
263 : }
264 4 : GNUNET_free (auth->wire_gateway_url);
265 4 : }
266 :
267 :
268 : /* end of mb_parse.c */
|