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


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