merchant

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

mb_parse.c (8895B)


      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 src/bank/mb_parse.c
     19  * @brief Convenience function to parse authentication configuration
     20  * @author Christian Grothoff
     21  */
     22 #include "platform.h"
     23 #include "taler/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         {
     93           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     94                                      section,
     95                                      "USERNAME");
     96           GNUNET_free (method);
     97           GNUNET_free (auth->wire_gateway_url);
     98           return GNUNET_SYSERR;
     99         }
    100         if (GNUNET_OK !=
    101             GNUNET_CONFIGURATION_get_value_string (cfg,
    102                                                    section,
    103                                                    "PASSWORD",
    104                                                    &auth->details.basic.password
    105                                                    ))
    106         {
    107           GNUNET_free (auth->details.basic.username);
    108           auth->details.basic.username = NULL;
    109           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
    110                                      section,
    111                                      "PASSWORD");
    112           GNUNET_free (method);
    113           GNUNET_free (auth->wire_gateway_url);
    114           return GNUNET_SYSERR;
    115         }
    116         auth->method = TALER_MERCHANT_BANK_AUTH_BASIC;
    117         GNUNET_free (method);
    118         return GNUNET_OK;
    119       case TALER_MERCHANT_BANK_AUTH_BEARER:
    120         if (GNUNET_OK !=
    121             GNUNET_CONFIGURATION_get_value_string (cfg,
    122                                                    section,
    123                                                    "TOKEN",
    124                                                    &auth->details.bearer.token))
    125         {
    126           GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
    127                                      section,
    128                                      "TOKEN");
    129           GNUNET_free (method);
    130           GNUNET_free (auth->wire_gateway_url);
    131           return GNUNET_SYSERR;
    132         }
    133         auth->method = TALER_MERCHANT_BANK_AUTH_BEARER;
    134         GNUNET_free (method);
    135         return GNUNET_OK;
    136       }
    137     }
    138   }
    139   GNUNET_free (method);
    140   return GNUNET_SYSERR;
    141 }
    142 
    143 
    144 enum GNUNET_GenericReturnValue
    145 TALER_MERCHANT_BANK_auth_parse_json (
    146   const json_t *cred,
    147   const char *backend_url,
    148   struct TALER_MERCHANT_BANK_AuthenticationData *auth)
    149 {
    150   const char *method;
    151 
    152   if (NULL == backend_url)
    153   {
    154     GNUNET_break_op (0);
    155     return GNUNET_SYSERR;
    156   }
    157   if ( (0 == strlen (backend_url)) ||
    158        ('/' != backend_url[strlen (backend_url) - 1]) )
    159   {
    160     GNUNET_break_op (0);
    161     return GNUNET_SYSERR;
    162   }
    163   auth->wire_gateway_url = GNUNET_strdup (backend_url);
    164   method = json_string_value (json_object_get (cred,
    165                                                "type"));
    166   if (NULL == method)
    167   {
    168     GNUNET_break_op (0);
    169     GNUNET_free (auth->wire_gateway_url);
    170     return GNUNET_SYSERR;
    171   }
    172   for (unsigned int i = 0; NULL != methods[i].m; i++)
    173   {
    174     if (0 == strcasecmp (method,
    175                          methods[i].m))
    176     {
    177       switch (methods[i].e)
    178       {
    179       case TALER_MERCHANT_BANK_AUTH_NONE:
    180         auth->method = TALER_MERCHANT_BANK_AUTH_NONE;
    181         return GNUNET_OK;
    182       case TALER_MERCHANT_BANK_AUTH_BASIC:
    183         {
    184           const char *username;
    185           const char *password;
    186           struct GNUNET_JSON_Specification spec[] = {
    187             GNUNET_JSON_spec_string ("username",
    188                                      &username),
    189             GNUNET_JSON_spec_string ("password",
    190                                      &password),
    191             GNUNET_JSON_spec_end ()
    192           };
    193           enum GNUNET_GenericReturnValue res;
    194           const char *err;
    195           unsigned int eline;
    196 
    197           res = GNUNET_JSON_parse (cred,
    198                                    spec,
    199                                    &err,
    200                                    &eline);
    201           if (GNUNET_OK != res)
    202           {
    203             GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    204                         "Credentials malformed: %s (%u)\n",
    205                         err,
    206                         eline);
    207             GNUNET_free (auth->wire_gateway_url);
    208             return GNUNET_SYSERR;
    209           }
    210           auth->details.basic.username = GNUNET_strdup (username);
    211           auth->details.basic.password = GNUNET_strdup (password);
    212         }
    213         auth->method = TALER_MERCHANT_BANK_AUTH_BASIC;
    214         return GNUNET_OK;
    215       case TALER_MERCHANT_BANK_AUTH_BEARER:
    216         {
    217           const char *token;
    218           struct GNUNET_JSON_Specification spec[] = {
    219             GNUNET_JSON_spec_string ("token",
    220                                      &token),
    221             GNUNET_JSON_spec_end ()
    222           };
    223           enum GNUNET_GenericReturnValue res;
    224           const char *err;
    225           unsigned int eline;
    226 
    227           res = GNUNET_JSON_parse (cred,
    228                                    spec,
    229                                    &err,
    230                                    &eline);
    231           if (GNUNET_OK != res)
    232           {
    233             GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    234                         "Credentials malformed: %s (%u)\n",
    235                         err,
    236                         eline);
    237             GNUNET_free (auth->wire_gateway_url);
    238             return GNUNET_SYSERR;
    239           }
    240           auth->details.bearer.token = GNUNET_strdup (token);
    241         }
    242         auth->method = TALER_MERCHANT_BANK_AUTH_BEARER;
    243         return GNUNET_OK;
    244       }
    245     }
    246   }
    247   GNUNET_free (auth->wire_gateway_url);
    248   return GNUNET_SYSERR;
    249 }
    250 
    251 
    252 void
    253 TALER_MERCHANT_BANK_auth_free (
    254   struct TALER_MERCHANT_BANK_AuthenticationData *auth)
    255 {
    256   switch (auth->method)
    257   {
    258   case TALER_MERCHANT_BANK_AUTH_NONE:
    259     break;
    260   case TALER_MERCHANT_BANK_AUTH_BASIC:
    261     GNUNET_free (auth->details.basic.username);
    262     GNUNET_free (auth->details.basic.password);
    263     break;
    264   case TALER_MERCHANT_BANK_AUTH_BEARER:
    265     GNUNET_free (auth->details.bearer.token);
    266     break;
    267   }
    268   GNUNET_free (auth->wire_gateway_url);
    269 }
    270 
    271 
    272 /* end of mb_parse.c */