exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

bank_api_common.c (3666B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2015-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_common.c
     19  * @brief Common functions for the bank API
     20  * @author Christian Grothoff
     21  */
     22 #include "bank_api_common.h"
     23 
     24 
     25 /**
     26  * Check that @a str is usable in the credentials of an HTTP
     27  * "Basic" authentication header as per RFC 7617.  Section 2
     28  * forbids a colon in the user-id (it separates user-id and
     29  * password, and there is no escaping mechanism), Section 2.1
     30  * forbids control characters in either field.
     31  *
     32  * @param str string to check, non-NULL
     33  * @param allow_colon true if @a str may contain a colon
     34  * @return true if @a str is acceptable
     35  */
     36 static bool
     37 valid_basic_credential (const char *str,
     38                         bool allow_colon)
     39 {
     40   for (const char *p = str; '\0' != *p; p++)
     41   {
     42     unsigned char c = (unsigned char) *p;
     43 
     44     if ( (c < 0x20) ||
     45          (0x7F == c) )
     46       return false;
     47     if ( (':' == c) &&
     48          (! allow_colon) )
     49       return false;
     50   }
     51   return true;
     52 }
     53 
     54 
     55 enum GNUNET_GenericReturnValue
     56 TALER_BANK_setup_auth_ (CURL *easy,
     57                         const struct TALER_BANK_AuthenticationData *auth)
     58 {
     59   enum GNUNET_GenericReturnValue ret;
     60 
     61   ret = GNUNET_OK;
     62   switch (auth->method)
     63   {
     64   case TALER_BANK_AUTH_NONE:
     65     return GNUNET_OK;
     66   case TALER_BANK_AUTH_BASIC:
     67     {
     68       char *up;
     69 
     70       if (! valid_basic_credential (auth->details.basic.username,
     71                                     false))
     72       {
     73         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     74                     "Username for HTTP basic authentication must not contain"
     75                     " a colon or control characters (see RFC 7617)\n");
     76         return GNUNET_SYSERR;
     77       }
     78       if (! valid_basic_credential (auth->details.basic.password,
     79                                     true))
     80       {
     81         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     82                     "Password for HTTP basic authentication must not contain"
     83                     " control characters (see RFC 7617)\n");
     84         return GNUNET_SYSERR;
     85       }
     86       GNUNET_asprintf (&up,
     87                        "%s:%s",
     88                        auth->details.basic.username,
     89                        auth->details.basic.password);
     90       if ( (CURLE_OK !=
     91             curl_easy_setopt (easy,
     92                               CURLOPT_HTTPAUTH,
     93                               CURLAUTH_BASIC)) ||
     94            (CURLE_OK !=
     95             curl_easy_setopt (easy,
     96                               CURLOPT_USERPWD,
     97                               up)) )
     98         ret = GNUNET_SYSERR;
     99       GNUNET_free (up);
    100       break;
    101     }
    102   case TALER_BANK_AUTH_BEARER:
    103     {
    104       if ( (CURLE_OK !=
    105             curl_easy_setopt (easy,
    106                               CURLOPT_HTTPAUTH,
    107                               CURLAUTH_BEARER)) ||
    108            (CURLE_OK !=
    109             curl_easy_setopt (easy,
    110                               CURLOPT_XOAUTH2_BEARER,
    111                               auth->details.bearer.token)) )
    112         ret = GNUNET_SYSERR;
    113       break;
    114     }
    115   }
    116   return ret;
    117 }
    118 
    119 
    120 /* end of bank_api_common.c */