cash2ecash

cash2ecash: cash acceptor that issues digital cash (experimental)
Log | Files | Refs | README | LICENSE

api_parse.c (4764B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2018-2020, 2026 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 lib/api_parse.c
     19  * @brief Convenience function to parse authentication configuration
     20  * @author Christian Grothoff
     21  * @author Reto Tellenbach
     22  */
     23 #include "taler/taler_bank_service.h"
     24 #include "../taler/taler_digitizer_service.h"
     25 
     26 
     27 enum GNUNET_GenericReturnValue
     28 DIGITIZER_bank_auth_parse_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg,
     29                               const char *section,
     30                               struct DIGITIZER_BankAuthenticationData *auth)
     31 {
     32   const struct
     33   {
     34     const char *m;
     35     enum TALER_BANK_AuthenticationMethod e;
     36   } methods[] = {
     37     { "NONE",  TALER_BANK_AUTH_NONE    },
     38     { "BASIC", TALER_BANK_AUTH_BASIC   },
     39     { "BEARER", TALER_BANK_AUTH_BEARER },
     40     { NULL, TALER_BANK_AUTH_NONE       }
     41   };
     42   char *method;
     43 
     44   if (GNUNET_OK !=
     45       GNUNET_CONFIGURATION_get_value_string (cfg,
     46                                              section,
     47                                              "AUTH_METHOD",
     48                                              &method))
     49   {
     50     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     51                                section,
     52                                "AUTH_METHOD");
     53     return GNUNET_SYSERR;
     54   }
     55 
     56   for (unsigned int i = 0; NULL != methods[i].m; i++)
     57   {
     58     if (0 == strcasecmp (method,
     59                          methods[i].m))
     60     {
     61       switch (methods[i].e)
     62       {
     63       case TALER_BANK_AUTH_NONE:
     64         auth->method = TALER_BANK_AUTH_NONE;
     65         GNUNET_free (method);
     66         return GNUNET_OK;
     67       case TALER_BANK_AUTH_BASIC:
     68         if (GNUNET_OK !=
     69             GNUNET_CONFIGURATION_get_value_string (
     70               cfg,
     71               section,
     72               "USERNAME",
     73               &auth->details.basic.username))
     74         {
     75           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     76                                      section,
     77                                      "USERNAME");
     78           GNUNET_free (method);
     79           return GNUNET_SYSERR;
     80         }
     81         if (GNUNET_OK !=
     82             GNUNET_CONFIGURATION_get_value_string (
     83               cfg,
     84               section,
     85               "PASSWORD",
     86               &auth->details.basic.password))
     87         {
     88           GNUNET_free (auth->details.basic.username);
     89           auth->details.basic.username = NULL;
     90           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     91                                      section,
     92                                      "PASSWORD");
     93           GNUNET_free (method);
     94           return GNUNET_SYSERR;
     95         }
     96         auth->method = TALER_BANK_AUTH_BASIC;
     97         GNUNET_free (method);
     98         return GNUNET_OK;
     99       case TALER_BANK_AUTH_BEARER:
    100         if (GNUNET_OK !=
    101             GNUNET_CONFIGURATION_get_value_string (
    102               cfg,
    103               section,
    104               "TOKEN",
    105               &auth->details.bearer.token))
    106         {
    107           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
    108                                      section,
    109                                      "TOKEN");
    110           GNUNET_free (method);
    111           return GNUNET_SYSERR;
    112         }
    113         auth->method = TALER_BANK_AUTH_BEARER;
    114         GNUNET_free (method);
    115         return GNUNET_OK;
    116       }
    117     }
    118   }
    119   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    120               "Unknown authentication method `%s'\n",
    121               method);
    122   GNUNET_free (method);
    123   return GNUNET_SYSERR;
    124 }
    125 
    126 
    127 void
    128 DIGITIZER_bank_auth_free (struct DIGITIZER_BankAuthenticationData *auth)
    129 {
    130   switch (auth->method)
    131   {
    132   case TALER_BANK_AUTH_NONE:
    133     break;
    134   case TALER_BANK_AUTH_BASIC:
    135     if (NULL != auth->details.basic.username)
    136     {
    137       GNUNET_free (auth->details.basic.username);
    138       auth->details.basic.username = NULL;
    139     }
    140     if (NULL != auth->details.basic.password)
    141     {
    142       GNUNET_free (auth->details.basic.password);
    143       auth->details.basic.password = NULL;
    144     }
    145     break;
    146   case TALER_BANK_AUTH_BEARER:
    147     if (NULL != auth->details.bearer.token)
    148     {
    149       GNUNET_free (auth->details.bearer.token);
    150       auth->details.bearer.token = NULL;
    151     }
    152     break;
    153   }
    154 }
    155 
    156 
    157 /* end of bank_api_parse.c */