Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2018-2020 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-lib/bank_api_parse.c
19 : * @brief Convenience function to parse authentication configuration
20 : * @author Christian Grothoff
21 : */
22 : #include "taler/taler_bank_service.h"
23 :
24 :
25 : enum GNUNET_GenericReturnValue
26 301 : TALER_BANK_auth_parse_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg,
27 : const char *section,
28 : struct TALER_BANK_AuthenticationData *auth)
29 : {
30 : const struct
31 : {
32 : const char *m;
33 : enum TALER_BANK_AuthenticationMethod e;
34 301 : } methods[] = {
35 : { "NONE", TALER_BANK_AUTH_NONE },
36 : { "BASIC", TALER_BANK_AUTH_BASIC },
37 : { "BEARER", TALER_BANK_AUTH_BEARER },
38 : { NULL, TALER_BANK_AUTH_NONE }
39 : };
40 : char *method;
41 :
42 301 : auth->core_bank_url = NULL;
43 301 : if (GNUNET_OK ==
44 301 : GNUNET_CONFIGURATION_get_value_string (cfg,
45 : section,
46 : "CORE_BANK_URL",
47 : &auth->core_bank_url))
48 : {
49 153 : if (! TALER_is_web_url (auth->core_bank_url))
50 : {
51 0 : GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
52 : section,
53 : "CORE_BANK_URL",
54 : "Not a valid URL");
55 0 : return GNUNET_SYSERR;
56 : }
57 : }
58 301 : if (GNUNET_OK !=
59 301 : GNUNET_CONFIGURATION_get_value_string (cfg,
60 : section,
61 : "WIRE_GATEWAY_URL",
62 : &auth->wire_gateway_url))
63 : {
64 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
65 : section,
66 : "WIRE_GATEWAY_URL");
67 0 : return GNUNET_SYSERR;
68 : }
69 301 : if (GNUNET_OK !=
70 301 : GNUNET_CONFIGURATION_get_value_string (cfg,
71 : section,
72 : "WIRE_GATEWAY_AUTH_METHOD",
73 : &method))
74 : {
75 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
76 : section,
77 : "WIRE_GATEWAY_AUTH_METHOD");
78 0 : GNUNET_free (auth->wire_gateway_url);
79 0 : return GNUNET_SYSERR;
80 : }
81 520 : for (unsigned int i = 0; NULL != methods[i].m; i++)
82 : {
83 520 : if (0 == strcasecmp (method,
84 520 : methods[i].m))
85 : {
86 301 : switch (methods[i].e)
87 : {
88 82 : case TALER_BANK_AUTH_NONE:
89 82 : auth->method = TALER_BANK_AUTH_NONE;
90 82 : GNUNET_free (method);
91 82 : return GNUNET_OK;
92 219 : case TALER_BANK_AUTH_BASIC:
93 219 : if (GNUNET_OK !=
94 219 : GNUNET_CONFIGURATION_get_value_string (
95 : cfg,
96 : section,
97 : "USERNAME",
98 : &auth->details.basic.username))
99 : {
100 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
101 : section,
102 : "USERNAME");
103 0 : GNUNET_free (method);
104 0 : GNUNET_free (auth->wire_gateway_url);
105 0 : return GNUNET_SYSERR;
106 : }
107 219 : if (GNUNET_OK !=
108 219 : GNUNET_CONFIGURATION_get_value_string (
109 : cfg,
110 : section,
111 : "PASSWORD",
112 : &auth->details.basic.password))
113 : {
114 0 : GNUNET_free (auth->details.basic.username);
115 0 : auth->details.basic.username = NULL;
116 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
117 : section,
118 : "PASSWORD");
119 0 : GNUNET_free (method);
120 0 : GNUNET_free (auth->wire_gateway_url);
121 0 : return GNUNET_SYSERR;
122 : }
123 219 : auth->method = TALER_BANK_AUTH_BASIC;
124 219 : GNUNET_free (method);
125 219 : return GNUNET_OK;
126 0 : case TALER_BANK_AUTH_BEARER:
127 0 : if (GNUNET_OK !=
128 0 : GNUNET_CONFIGURATION_get_value_string (
129 : cfg,
130 : section,
131 : "TOKEN",
132 : &auth->details.bearer.token))
133 : {
134 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
135 : section,
136 : "TOKEN");
137 0 : GNUNET_free (method);
138 0 : GNUNET_free (auth->wire_gateway_url);
139 0 : return GNUNET_SYSERR;
140 : }
141 0 : auth->method = TALER_BANK_AUTH_BEARER;
142 0 : GNUNET_free (method);
143 0 : return GNUNET_OK;
144 : }
145 : }
146 : }
147 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
148 : "Unknown authentication method `%s'\n",
149 : method);
150 0 : GNUNET_free (method);
151 0 : return GNUNET_SYSERR;
152 : }
153 :
154 :
155 : void
156 267 : TALER_BANK_auth_free (struct TALER_BANK_AuthenticationData *auth)
157 : {
158 267 : switch (auth->method)
159 : {
160 86 : case TALER_BANK_AUTH_NONE:
161 86 : break;
162 181 : case TALER_BANK_AUTH_BASIC:
163 181 : if (NULL != auth->details.basic.username)
164 : {
165 181 : GNUNET_free (auth->details.basic.username);
166 181 : auth->details.basic.username = NULL;
167 : }
168 181 : if (NULL != auth->details.basic.password)
169 : {
170 181 : GNUNET_free (auth->details.basic.password);
171 181 : auth->details.basic.password = NULL;
172 : }
173 181 : break;
174 0 : case TALER_BANK_AUTH_BEARER:
175 0 : if (NULL != auth->details.bearer.token)
176 : {
177 0 : GNUNET_free (auth->details.bearer.token);
178 0 : auth->details.bearer.token = NULL;
179 : }
180 0 : break;
181 : }
182 :
183 267 : GNUNET_free (auth->wire_gateway_url);
184 267 : auth->wire_gateway_url = NULL;
185 267 : }
186 :
187 :
188 : /* end of bank_api_parse.c */
|