merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

mb_parse.c (8699B)


      1 /*
      2   This file is part of Taler
      3   Copyright (C) 2018-2023 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/mb_parse.c
     19  * @brief Convenience function to parse authentication configuration
     20  * @author Christian Grothoff
     21  */
     22 #include "platform.h"
     23 #include "taler_merchant_bank_lib.h"
     24 #include <gnunet/gnunet_json_lib.h>
     25 
     26 
     27 /**
     28  * Names of authentication methods available.
     29  */
     30 static const struct
     31 {
     32   const char *m;
     33   enum TALER_MERCHANT_BANK_AuthenticationMethod e;
     34 } methods[] = {
     35   { "NONE",   TALER_MERCHANT_BANK_AUTH_NONE   },
     36   { "BASIC",  TALER_MERCHANT_BANK_AUTH_BASIC  },
     37   { "BEARER", TALER_MERCHANT_BANK_AUTH_BEARER },
     38   { NULL, TALER_MERCHANT_BANK_AUTH_NONE     }
     39 };
     40 
     41 
     42 enum GNUNET_GenericReturnValue
     43 TALER_MERCHANT_BANK_auth_parse_cfg (
     44   const struct GNUNET_CONFIGURATION_Handle *cfg,
     45   const char *section,
     46   struct TALER_MERCHANT_BANK_AuthenticationData *auth)
     47 {
     48   char *method;
     49 
     50   if (GNUNET_OK !=
     51       GNUNET_CONFIGURATION_get_value_string (cfg,
     52                                              section,
     53                                              "WIRE_GATEWAY_URL",
     54                                              &auth->wire_gateway_url))
     55   {
     56     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     57                                section,
     58                                "WIRE_GATEWAY_URL");
     59     return GNUNET_SYSERR;
     60   }
     61 
     62   if (GNUNET_OK !=
     63       GNUNET_CONFIGURATION_get_value_string (cfg,
     64                                              section,
     65                                              "WIRE_GATEWAY_AUTH_METHOD",
     66                                              &method))
     67   {
     68     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     69                                section,
     70                                "WIRE_GATEWAY_AUTH_METHOD");
     71     GNUNET_free (auth->wire_gateway_url);
     72     return GNUNET_SYSERR;
     73   }
     74   for (unsigned int i = 0; NULL != methods[i].m; i++)
     75   {
     76     if (0 == strcasecmp (method,
     77                          methods[i].m))
     78     {
     79       switch (methods[i].e)
     80       {
     81       case TALER_MERCHANT_BANK_AUTH_NONE:
     82         auth->method = TALER_MERCHANT_BANK_AUTH_NONE;
     83         GNUNET_free (method);
     84         return GNUNET_OK;
     85       case TALER_MERCHANT_BANK_AUTH_BASIC:
     86         if (GNUNET_OK !=
     87             GNUNET_CONFIGURATION_get_value_string (cfg,
     88                                                    section,
     89                                                    "USERNAME",
     90                                                    &auth->details.basic.username))
     91         {
     92           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     93                                      section,
     94                                      "USERNAME");
     95           GNUNET_free (method);
     96           GNUNET_free (auth->wire_gateway_url);
     97           return GNUNET_SYSERR;
     98         }
     99         if (GNUNET_OK !=
    100             GNUNET_CONFIGURATION_get_value_string (cfg,
    101                                                    section,
    102                                                    "PASSWORD",
    103                                                    &auth->details.basic.password))
    104         {
    105           GNUNET_free (auth->details.basic.username);
    106           auth->details.basic.username = NULL;
    107           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
    108                                      section,
    109                                      "PASSWORD");
    110           GNUNET_free (method);
    111           GNUNET_free (auth->wire_gateway_url);
    112           return GNUNET_SYSERR;
    113         }
    114         auth->method = TALER_MERCHANT_BANK_AUTH_BASIC;
    115         GNUNET_free (method);
    116         return GNUNET_OK;
    117       case TALER_MERCHANT_BANK_AUTH_BEARER:
    118         if (GNUNET_OK !=
    119             GNUNET_CONFIGURATION_get_value_string (cfg,
    120                                                    section,
    121                                                    "TOKEN",
    122                                                    &auth->details.bearer.token))
    123         {
    124           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
    125                                      section,
    126                                      "TOKEN");
    127           GNUNET_free (method);
    128           GNUNET_free (auth->wire_gateway_url);
    129           return GNUNET_SYSERR;
    130         }
    131         auth->method = TALER_MERCHANT_BANK_AUTH_BEARER;
    132         GNUNET_free (method);
    133         return GNUNET_OK;
    134       }
    135     }
    136   }
    137   GNUNET_free (method);
    138   return GNUNET_SYSERR;
    139 }
    140 
    141 
    142 enum GNUNET_GenericReturnValue
    143 TALER_MERCHANT_BANK_auth_parse_json (
    144   const json_t *cred,
    145   const char *backend_url,
    146   struct TALER_MERCHANT_BANK_AuthenticationData *auth)
    147 {
    148   const char *method;
    149 
    150   if (NULL == backend_url)
    151   {
    152     GNUNET_break_op (0);
    153     return GNUNET_SYSERR;
    154   }
    155   if ( (0 == strlen (backend_url)) ||
    156        ('/' != backend_url[strlen (backend_url) - 1]) )
    157   {
    158     GNUNET_break_op (0);
    159     return GNUNET_SYSERR;
    160   }
    161   auth->wire_gateway_url = GNUNET_strdup (backend_url);
    162   method = json_string_value (json_object_get (cred,
    163                                                "type"));
    164   if (NULL == method)
    165   {
    166     GNUNET_break_op (0);
    167     return GNUNET_SYSERR;
    168   }
    169   for (unsigned int i = 0; NULL != methods[i].m; i++)
    170   {
    171     if (0 == strcasecmp (method,
    172                          methods[i].m))
    173     {
    174       switch (methods[i].e)
    175       {
    176       case TALER_MERCHANT_BANK_AUTH_NONE:
    177         auth->method = TALER_MERCHANT_BANK_AUTH_NONE;
    178         return GNUNET_OK;
    179       case TALER_MERCHANT_BANK_AUTH_BASIC:
    180         {
    181           const char *username;
    182           const char *password;
    183           struct GNUNET_JSON_Specification spec[] = {
    184             GNUNET_JSON_spec_string ("username",
    185                                      &username),
    186             GNUNET_JSON_spec_string ("password",
    187                                      &password),
    188             GNUNET_JSON_spec_end ()
    189           };
    190           enum GNUNET_GenericReturnValue res;
    191           const char *err;
    192           unsigned int eline;
    193 
    194           res = GNUNET_JSON_parse (cred,
    195                                    spec,
    196                                    &err,
    197                                    &eline);
    198           if (GNUNET_OK != res)
    199           {
    200             GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    201                         "Credentials malformed: %s (%u)\n",
    202                         err,
    203                         eline);
    204             GNUNET_free (auth->wire_gateway_url);
    205             return GNUNET_SYSERR;
    206           }
    207           auth->details.basic.username = GNUNET_strdup (username);
    208           auth->details.basic.password = GNUNET_strdup (password);
    209         }
    210         auth->method = TALER_MERCHANT_BANK_AUTH_BASIC;
    211         return GNUNET_OK;
    212       case TALER_MERCHANT_BANK_AUTH_BEARER:
    213         {
    214           const char *token;
    215           struct GNUNET_JSON_Specification spec[] = {
    216             GNUNET_JSON_spec_string ("token",
    217                                      &token),
    218             GNUNET_JSON_spec_end ()
    219           };
    220           enum GNUNET_GenericReturnValue res;
    221           const char *err;
    222           unsigned int eline;
    223 
    224           res = GNUNET_JSON_parse (cred,
    225                                    spec,
    226                                    &err,
    227                                    &eline);
    228           if (GNUNET_OK != res)
    229           {
    230             GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    231                         "Credentials malformed: %s (%u)\n",
    232                         err,
    233                         eline);
    234             GNUNET_free (auth->wire_gateway_url);
    235             return GNUNET_SYSERR;
    236           }
    237           auth->details.bearer.token = GNUNET_strdup (token);
    238         }
    239         auth->method = TALER_MERCHANT_BANK_AUTH_BEARER;
    240         return GNUNET_OK;
    241       }
    242     }
    243   }
    244   return GNUNET_SYSERR;
    245 }
    246 
    247 
    248 void
    249 TALER_MERCHANT_BANK_auth_free (
    250   struct TALER_MERCHANT_BANK_AuthenticationData *auth)
    251 {
    252   switch (auth->method)
    253   {
    254   case TALER_MERCHANT_BANK_AUTH_NONE:
    255     break;
    256   case TALER_MERCHANT_BANK_AUTH_BASIC:
    257     GNUNET_free (auth->details.basic.username);
    258     GNUNET_free (auth->details.basic.password);
    259     break;
    260   case TALER_MERCHANT_BANK_AUTH_BEARER:
    261     GNUNET_free (auth->details.bearer.token);
    262     break;
    263   }
    264   GNUNET_free (auth->wire_gateway_url);
    265 }
    266 
    267 
    268 /* end of mb_parse.c */