exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

bank_api_parse.c (5640B)


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