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/platform.h"
23 : #include "taler/taler_bank_service.h"
24 :
25 :
26 : enum GNUNET_GenericReturnValue
27 449 : TALER_BANK_auth_parse_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg,
28 : const char *section,
29 : struct TALER_BANK_AuthenticationData *auth)
30 : {
31 : const struct
32 : {
33 : const char *m;
34 : enum TALER_BANK_AuthenticationMethod e;
35 449 : } methods[] = {
36 : { "NONE", TALER_BANK_AUTH_NONE },
37 : { "BASIC", TALER_BANK_AUTH_BASIC },
38 : { "BEARER", TALER_BANK_AUTH_BEARER },
39 : { NULL, TALER_BANK_AUTH_NONE }
40 : };
41 : char *method;
42 :
43 449 : if (GNUNET_OK !=
44 449 : GNUNET_CONFIGURATION_get_value_string (cfg,
45 : section,
46 : "WIRE_GATEWAY_URL",
47 : &auth->wire_gateway_url))
48 : {
49 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
50 : section,
51 : "WIRE_GATEWAY_URL");
52 0 : return GNUNET_SYSERR;
53 : }
54 :
55 449 : if (GNUNET_OK !=
56 449 : GNUNET_CONFIGURATION_get_value_string (cfg,
57 : section,
58 : "WIRE_GATEWAY_AUTH_METHOD",
59 : &method))
60 : {
61 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
62 : section,
63 : "WIRE_GATEWAY_AUTH_METHOD");
64 0 : GNUNET_free (auth->wire_gateway_url);
65 0 : return GNUNET_SYSERR;
66 : }
67 816 : for (unsigned int i = 0; NULL != methods[i].m; i++)
68 : {
69 816 : if (0 == strcasecmp (method,
70 816 : methods[i].m))
71 : {
72 449 : switch (methods[i].e)
73 : {
74 82 : case TALER_BANK_AUTH_NONE:
75 82 : auth->method = TALER_BANK_AUTH_NONE;
76 82 : GNUNET_free (method);
77 82 : return GNUNET_OK;
78 367 : case TALER_BANK_AUTH_BASIC:
79 367 : if (GNUNET_OK !=
80 367 : GNUNET_CONFIGURATION_get_value_string (
81 : cfg,
82 : section,
83 : "USERNAME",
84 : &auth->details.basic.username))
85 : {
86 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
87 : section,
88 : "USERNAME");
89 0 : GNUNET_free (method);
90 0 : GNUNET_free (auth->wire_gateway_url);
91 0 : return GNUNET_SYSERR;
92 : }
93 367 : if (GNUNET_OK !=
94 367 : GNUNET_CONFIGURATION_get_value_string (
95 : cfg,
96 : section,
97 : "PASSWORD",
98 : &auth->details.basic.password))
99 : {
100 0 : GNUNET_free (auth->details.basic.username);
101 0 : auth->details.basic.username = NULL;
102 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
103 : section,
104 : "PASSWORD");
105 0 : GNUNET_free (method);
106 0 : GNUNET_free (auth->wire_gateway_url);
107 0 : return GNUNET_SYSERR;
108 : }
109 367 : auth->method = TALER_BANK_AUTH_BASIC;
110 367 : GNUNET_free (method);
111 367 : return GNUNET_OK;
112 0 : case TALER_BANK_AUTH_BEARER:
113 0 : if (GNUNET_OK !=
114 0 : GNUNET_CONFIGURATION_get_value_string (
115 : cfg,
116 : section,
117 : "TOKEN",
118 : &auth->details.bearer.token))
119 : {
120 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
121 : section,
122 : "TOKEN");
123 0 : GNUNET_free (method);
124 0 : GNUNET_free (auth->wire_gateway_url);
125 0 : return GNUNET_SYSERR;
126 : }
127 0 : auth->method = TALER_BANK_AUTH_BEARER;
128 0 : GNUNET_free (method);
129 0 : return GNUNET_OK;
130 : }
131 : }
132 : }
133 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
134 : "Unknown authentication method `%s'\n",
135 : method);
136 0 : GNUNET_free (method);
137 0 : return GNUNET_SYSERR;
138 : }
139 :
140 :
141 : void
142 415 : TALER_BANK_auth_free (struct TALER_BANK_AuthenticationData *auth)
143 : {
144 415 : switch (auth->method)
145 : {
146 86 : case TALER_BANK_AUTH_NONE:
147 86 : break;
148 329 : case TALER_BANK_AUTH_BASIC:
149 329 : if (NULL != auth->details.basic.username)
150 : {
151 329 : GNUNET_free (auth->details.basic.username);
152 329 : auth->details.basic.username = NULL;
153 : }
154 329 : if (NULL != auth->details.basic.password)
155 : {
156 329 : GNUNET_free (auth->details.basic.password);
157 329 : auth->details.basic.password = NULL;
158 : }
159 329 : break;
160 0 : case TALER_BANK_AUTH_BEARER:
161 0 : if (NULL != auth->details.bearer.token)
162 : {
163 0 : GNUNET_free (auth->details.bearer.token);
164 0 : auth->details.bearer.token = NULL;
165 : }
166 0 : break;
167 : }
168 :
169 415 : GNUNET_free (auth->wire_gateway_url);
170 415 : auth->wire_gateway_url = NULL;
171 415 : }
172 :
173 :
174 : /* end of bank_api_parse.c */
|