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