diff options
Diffstat (limited to 'src/authorization/libanastasiseufin/lae_parse.c')
-rw-r--r-- | src/authorization/libanastasiseufin/lae_parse.c | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/src/authorization/libanastasiseufin/lae_parse.c b/src/authorization/libanastasiseufin/lae_parse.c new file mode 100644 index 0000000..6863c3f --- /dev/null +++ b/src/authorization/libanastasiseufin/lae_parse.c | |||
@@ -0,0 +1,156 @@ | |||
1 | /* | ||
2 | This file is part of Anastasis | ||
3 | Copyright (C) 2018-2020 Anastasis SARL | ||
4 | |||
5 | Anastasis 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 | Anastasis 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 | Anastasis; see the file COPYING. If not, see | ||
15 | <http://www.gnu.org/licenses/> | ||
16 | */ | ||
17 | /** | ||
18 | * @file libanastasiseufin/lae_parse.c | ||
19 | * @brief Convenience function to parse authentication configuration | ||
20 | * @author Christian Grothoff | ||
21 | */ | ||
22 | #include "platform.h" | ||
23 | #include "anastasis_eufin_lib.h" | ||
24 | |||
25 | |||
26 | /** | ||
27 | * Parse configuration section with bank authentication data. | ||
28 | * | ||
29 | * @param cfg configuration to parse | ||
30 | * @param section the section with the configuration data | ||
31 | * @param[out] auth set to the configuration data found | ||
32 | * @return #GNUNET_OK on success | ||
33 | */ | ||
34 | int | ||
35 | ANASTASIS_EUFIN_auth_parse_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
36 | const char *section, | ||
37 | struct ANASTASIS_EUFIN_AuthenticationData *auth) | ||
38 | { | ||
39 | const struct | ||
40 | { | ||
41 | const char *m; | ||
42 | enum ANASTASIS_EUFIN_AuthenticationMethod e; | ||
43 | } methods[] = { | ||
44 | { "NONE", ANASTASIS_EUFIN_AUTH_NONE }, | ||
45 | { "BASIC", ANASTASIS_EUFIN_AUTH_BASIC }, | ||
46 | { NULL, ANASTASIS_EUFIN_AUTH_NONE } | ||
47 | }; | ||
48 | char *method; | ||
49 | |||
50 | if (GNUNET_OK != | ||
51 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
52 | section, | ||
53 | "WIRE_GATEWAY_URL", | ||
54 | &auth->wire_gateway_url)) | ||
55 | { | ||
56 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
57 | section, | ||
58 | "WIRE_GATEWAY_URL"); | ||
59 | return GNUNET_SYSERR; | ||
60 | } | ||
61 | |||
62 | if (GNUNET_OK != | ||
63 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
64 | section, | ||
65 | "WIRE_GATEWAY_AUTH_METHOD", | ||
66 | &method)) | ||
67 | { | ||
68 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
69 | section, | ||
70 | "WIRE_GATEWAY_AUTH_METHOD"); | ||
71 | GNUNET_free (auth->wire_gateway_url); | ||
72 | return GNUNET_SYSERR; | ||
73 | } | ||
74 | for (unsigned int i = 0; NULL != methods[i].m; i++) | ||
75 | { | ||
76 | if (0 == strcasecmp (method, | ||
77 | methods[i].m)) | ||
78 | { | ||
79 | switch (methods[i].e) | ||
80 | { | ||
81 | case ANASTASIS_EUFIN_AUTH_NONE: | ||
82 | auth->method = ANASTASIS_EUFIN_AUTH_NONE; | ||
83 | GNUNET_free (method); | ||
84 | return GNUNET_OK; | ||
85 | case ANASTASIS_EUFIN_AUTH_BASIC: | ||
86 | if (GNUNET_OK != | ||
87 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
88 | section, | ||
89 | "USERNAME", | ||
90 | &auth->details.basic.username)) | ||
91 | { | ||
92 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
93 | section, | ||
94 | "USERNAME"); | ||
95 | GNUNET_free (method); | ||
96 | GNUNET_free (auth->wire_gateway_url); | ||
97 | return GNUNET_SYSERR; | ||
98 | } | ||
99 | if (GNUNET_OK != | ||
100 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
101 | section, | ||
102 | "PASSWORD", | ||
103 | &auth->details.basic.password)) | ||
104 | { | ||
105 | GNUNET_free (auth->details.basic.username); | ||
106 | auth->details.basic.username = NULL; | ||
107 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
108 | section, | ||
109 | "PASSWORD"); | ||
110 | GNUNET_free (method); | ||
111 | GNUNET_free (auth->wire_gateway_url); | ||
112 | return GNUNET_SYSERR; | ||
113 | } | ||
114 | auth->method = ANASTASIS_EUFIN_AUTH_BASIC; | ||
115 | GNUNET_free (method); | ||
116 | return GNUNET_OK; | ||
117 | } | ||
118 | } | ||
119 | } | ||
120 | GNUNET_free (method); | ||
121 | return GNUNET_SYSERR; | ||
122 | } | ||
123 | |||
124 | |||
125 | /** | ||
126 | * Free memory inside of @a auth (but not @a auth itself). | ||
127 | * Dual to #ANASTASIS_EUFIN_auth_parse_cfg(). | ||
128 | * | ||
129 | * @param[in] auth authentication data to free | ||
130 | */ | ||
131 | void | ||
132 | ANASTASIS_EUFIN_auth_free (struct ANASTASIS_EUFIN_AuthenticationData *auth) | ||
133 | { | ||
134 | switch (auth->method) | ||
135 | { | ||
136 | case ANASTASIS_EUFIN_AUTH_NONE: | ||
137 | break; | ||
138 | case ANASTASIS_EUFIN_AUTH_BASIC: | ||
139 | if (NULL != auth->details.basic.username) | ||
140 | { | ||
141 | GNUNET_free (auth->details.basic.username); | ||
142 | auth->details.basic.username = NULL; | ||
143 | } | ||
144 | if (NULL != auth->details.basic.password) | ||
145 | { | ||
146 | GNUNET_free (auth->details.basic.password); | ||
147 | auth->details.basic.password = NULL; | ||
148 | } | ||
149 | break; | ||
150 | } | ||
151 | GNUNET_free (auth->wire_gateway_url); | ||
152 | auth->wire_gateway_url = NULL; | ||
153 | } | ||
154 | |||
155 | |||
156 | /* end of lae_parse.c */ | ||