merchant

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

mb_parse.c (8815B)


      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 "taler/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     return GNUNET_SYSERR;
    170   }
    171   for (unsigned int i = 0; NULL != methods[i].m; i++)
    172   {
    173     if (0 == strcasecmp (method,
    174                          methods[i].m))
    175     {
    176       switch (methods[i].e)
    177       {
    178       case TALER_MERCHANT_BANK_AUTH_NONE:
    179         auth->method = TALER_MERCHANT_BANK_AUTH_NONE;
    180         return GNUNET_OK;
    181       case TALER_MERCHANT_BANK_AUTH_BASIC:
    182         {
    183           const char *username;
    184           const char *password;
    185           struct GNUNET_JSON_Specification spec[] = {
    186             GNUNET_JSON_spec_string ("username",
    187                                      &username),
    188             GNUNET_JSON_spec_string ("password",
    189                                      &password),
    190             GNUNET_JSON_spec_end ()
    191           };
    192           enum GNUNET_GenericReturnValue res;
    193           const char *err;
    194           unsigned int eline;
    195 
    196           res = GNUNET_JSON_parse (cred,
    197                                    spec,
    198                                    &err,
    199                                    &eline);
    200           if (GNUNET_OK != res)
    201           {
    202             GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    203                         "Credentials malformed: %s (%u)\n",
    204                         err,
    205                         eline);
    206             GNUNET_free (auth->wire_gateway_url);
    207             return GNUNET_SYSERR;
    208           }
    209           auth->details.basic.username = GNUNET_strdup (username);
    210           auth->details.basic.password = GNUNET_strdup (password);
    211         }
    212         auth->method = TALER_MERCHANT_BANK_AUTH_BASIC;
    213         return GNUNET_OK;
    214       case TALER_MERCHANT_BANK_AUTH_BEARER:
    215         {
    216           const char *token;
    217           struct GNUNET_JSON_Specification spec[] = {
    218             GNUNET_JSON_spec_string ("token",
    219                                      &token),
    220             GNUNET_JSON_spec_end ()
    221           };
    222           enum GNUNET_GenericReturnValue res;
    223           const char *err;
    224           unsigned int eline;
    225 
    226           res = GNUNET_JSON_parse (cred,
    227                                    spec,
    228                                    &err,
    229                                    &eline);
    230           if (GNUNET_OK != res)
    231           {
    232             GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    233                         "Credentials malformed: %s (%u)\n",
    234                         err,
    235                         eline);
    236             GNUNET_free (auth->wire_gateway_url);
    237             return GNUNET_SYSERR;
    238           }
    239           auth->details.bearer.token = GNUNET_strdup (token);
    240         }
    241         auth->method = TALER_MERCHANT_BANK_AUTH_BEARER;
    242         return GNUNET_OK;
    243       }
    244     }
    245   }
    246   return GNUNET_SYSERR;
    247 }
    248 
    249 
    250 void
    251 TALER_MERCHANT_BANK_auth_free (
    252   struct TALER_MERCHANT_BANK_AuthenticationData *auth)
    253 {
    254   switch (auth->method)
    255   {
    256   case TALER_MERCHANT_BANK_AUTH_NONE:
    257     break;
    258   case TALER_MERCHANT_BANK_AUTH_BASIC:
    259     GNUNET_free (auth->details.basic.username);
    260     GNUNET_free (auth->details.basic.password);
    261     break;
    262   case TALER_MERCHANT_BANK_AUTH_BEARER:
    263     GNUNET_free (auth->details.bearer.token);
    264     break;
    265   }
    266   GNUNET_free (auth->wire_gateway_url);
    267 }
    268 
    269 
    270 /* end of mb_parse.c */