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 : memset (auth,
43 : 0,
44 : sizeof (*auth));
45 301 : if (GNUNET_OK ==
46 301 : GNUNET_CONFIGURATION_get_value_string (cfg,
47 : section,
48 : "CORE_BANK_URL",
49 : &auth->core_bank_url))
50 : {
51 153 : if (! TALER_is_web_url (auth->core_bank_url))
52 : {
53 0 : GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
54 : section,
55 : "CORE_BANK_URL",
56 : "Not a valid URL");
57 0 : TALER_BANK_auth_free (auth);
58 0 : return GNUNET_SYSERR;
59 : }
60 : }
61 301 : if (GNUNET_OK !=
62 301 : GNUNET_CONFIGURATION_get_value_string (cfg,
63 : section,
64 : "WIRE_GATEWAY_URL",
65 : &auth->wire_gateway_url))
66 : {
67 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
68 : section,
69 : "WIRE_GATEWAY_URL");
70 0 : TALER_BANK_auth_free (auth);
71 0 : return GNUNET_SYSERR;
72 : }
73 301 : if (GNUNET_OK !=
74 301 : GNUNET_CONFIGURATION_get_value_string (cfg,
75 : section,
76 : "WIRE_GATEWAY_AUTH_METHOD",
77 : &method))
78 : {
79 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
80 : section,
81 : "WIRE_GATEWAY_AUTH_METHOD");
82 0 : TALER_BANK_auth_free (auth);
83 0 : return GNUNET_SYSERR;
84 : }
85 520 : for (unsigned int i = 0; NULL != methods[i].m; i++)
86 : {
87 520 : if (0 == strcasecmp (method,
88 520 : methods[i].m))
89 : {
90 301 : switch (methods[i].e)
91 : {
92 82 : case TALER_BANK_AUTH_NONE:
93 82 : auth->method = TALER_BANK_AUTH_NONE;
94 82 : GNUNET_free (method);
95 82 : return GNUNET_OK;
96 219 : case TALER_BANK_AUTH_BASIC:
97 219 : if (GNUNET_OK !=
98 219 : GNUNET_CONFIGURATION_get_value_string (
99 : cfg,
100 : section,
101 : "USERNAME",
102 : &auth->details.basic.username))
103 : {
104 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
105 : section,
106 : "USERNAME");
107 0 : GNUNET_free (method);
108 0 : TALER_BANK_auth_free (auth);
109 0 : return GNUNET_SYSERR;
110 : }
111 219 : auth->method = TALER_BANK_AUTH_BASIC;
112 219 : if (GNUNET_OK !=
113 219 : GNUNET_CONFIGURATION_get_value_string (
114 : cfg,
115 : section,
116 : "PASSWORD",
117 : &auth->details.basic.password))
118 : {
119 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
120 : section,
121 : "PASSWORD");
122 0 : GNUNET_free (method);
123 0 : TALER_BANK_auth_free (auth);
124 0 : return GNUNET_SYSERR;
125 : }
126 219 : GNUNET_free (method);
127 219 : return GNUNET_OK;
128 0 : case TALER_BANK_AUTH_BEARER:
129 0 : if (GNUNET_OK !=
130 0 : GNUNET_CONFIGURATION_get_value_string (
131 : cfg,
132 : section,
133 : "TOKEN",
134 : &auth->details.bearer.token))
135 : {
136 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
137 : section,
138 : "TOKEN");
139 0 : GNUNET_free (method);
140 0 : TALER_BANK_auth_free (auth);
141 0 : return GNUNET_SYSERR;
142 : }
143 0 : auth->method = TALER_BANK_AUTH_BEARER;
144 0 : GNUNET_free (method);
145 0 : return GNUNET_OK;
146 : }
147 : }
148 : }
149 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
150 : "Unknown authentication method `%s'\n",
151 : method);
152 0 : GNUNET_free (method);
153 0 : return GNUNET_SYSERR;
154 : }
155 :
156 :
157 : void
158 267 : TALER_BANK_auth_free (struct TALER_BANK_AuthenticationData *auth)
159 : {
160 267 : switch (auth->method)
161 : {
162 82 : case TALER_BANK_AUTH_NONE:
163 82 : break;
164 185 : case TALER_BANK_AUTH_BASIC:
165 185 : GNUNET_free (auth->details.basic.username);
166 185 : GNUNET_free (auth->details.basic.password);
167 185 : break;
168 0 : case TALER_BANK_AUTH_BEARER:
169 0 : GNUNET_free (auth->details.bearer.token);
170 0 : break;
171 : }
172 267 : GNUNET_free (auth->wire_gateway_url);
173 267 : GNUNET_free (auth->core_bank_url);
174 267 : }
175 :
176 :
177 : /* end of bank_api_parse.c */
|