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