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 (5605B)


      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       return GNUNET_SYSERR;
     58     }
     59   }
     60   if (GNUNET_OK !=
     61       GNUNET_CONFIGURATION_get_value_string (cfg,
     62                                              section,
     63                                              "WIRE_GATEWAY_URL",
     64                                              &auth->wire_gateway_url))
     65   {
     66     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     67                                section,
     68                                "WIRE_GATEWAY_URL");
     69     TALER_BANK_auth_free (auth);
     70     return GNUNET_SYSERR;
     71   }
     72   if (GNUNET_OK !=
     73       GNUNET_CONFIGURATION_get_value_string (cfg,
     74                                              section,
     75                                              "WIRE_GATEWAY_AUTH_METHOD",
     76                                              &method))
     77   {
     78     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     79                                section,
     80                                "WIRE_GATEWAY_AUTH_METHOD");
     81     TALER_BANK_auth_free (auth);
     82     return GNUNET_SYSERR;
     83   }
     84   for (unsigned int i = 0; NULL != methods[i].m; i++)
     85   {
     86     if (0 == strcasecmp (method,
     87                          methods[i].m))
     88     {
     89       switch (methods[i].e)
     90       {
     91       case TALER_BANK_AUTH_NONE:
     92         auth->method = TALER_BANK_AUTH_NONE;
     93         GNUNET_free (method);
     94         return GNUNET_OK;
     95       case TALER_BANK_AUTH_BASIC:
     96         if (GNUNET_OK !=
     97             GNUNET_CONFIGURATION_get_value_string (
     98               cfg,
     99               section,
    100               "USERNAME",
    101               &auth->details.basic.username))
    102         {
    103           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
    104                                      section,
    105                                      "USERNAME");
    106           GNUNET_free (method);
    107           TALER_BANK_auth_free (auth);
    108           return GNUNET_SYSERR;
    109         }
    110         if (GNUNET_OK !=
    111             GNUNET_CONFIGURATION_get_value_string (
    112               cfg,
    113               section,
    114               "PASSWORD",
    115               &auth->details.basic.password))
    116         {
    117           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
    118                                      section,
    119                                      "PASSWORD");
    120           GNUNET_free (method);
    121           TALER_BANK_auth_free (auth);
    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           TALER_BANK_auth_free (auth);
    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     GNUNET_free (auth->details.basic.username);
    165     GNUNET_free (auth->details.basic.password);
    166     break;
    167   case TALER_BANK_AUTH_BEARER:
    168     GNUNET_free (auth->details.bearer.token);
    169     break;
    170   }
    171   GNUNET_free (auth->wire_gateway_url);
    172   GNUNET_free (auth->core_bank_url);
    173 }
    174 
    175 
    176 /* end of bank_api_parse.c */