Line data Source code
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 540 : valid_basic_credential (const char *str,
38 : bool allow_colon)
39 : {
40 4868 : for (const char *p = str; '\0' != *p; p++)
41 : {
42 4328 : unsigned char c = (unsigned char) *p;
43 :
44 4328 : if ( (c < 0x20) ||
45 : (0x7F == c) )
46 0 : return false;
47 4328 : if ( (':' == c) &&
48 0 : (! allow_colon) )
49 0 : return false;
50 : }
51 540 : return true;
52 : }
53 :
54 :
55 : enum GNUNET_GenericReturnValue
56 309 : TALER_BANK_setup_auth_ (CURL *easy,
57 : const struct TALER_BANK_AuthenticationData *auth)
58 : {
59 : enum GNUNET_GenericReturnValue ret;
60 :
61 309 : ret = GNUNET_OK;
62 309 : switch (auth->method)
63 : {
64 39 : case TALER_BANK_AUTH_NONE:
65 39 : return GNUNET_OK;
66 270 : case TALER_BANK_AUTH_BASIC:
67 : {
68 : char *up;
69 :
70 270 : if (! valid_basic_credential (auth->details.basic.username,
71 : false))
72 : {
73 0 : 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 0 : return GNUNET_SYSERR;
77 : }
78 270 : if (! valid_basic_credential (auth->details.basic.password,
79 : true))
80 : {
81 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
82 : "Password for HTTP basic authentication must not contain"
83 : " control characters (see RFC 7617)\n");
84 0 : return GNUNET_SYSERR;
85 : }
86 270 : GNUNET_asprintf (&up,
87 : "%s:%s",
88 270 : auth->details.basic.username,
89 270 : auth->details.basic.password);
90 270 : if ( (CURLE_OK !=
91 270 : curl_easy_setopt (easy,
92 : CURLOPT_HTTPAUTH,
93 270 : CURLAUTH_BASIC)) ||
94 : (CURLE_OK !=
95 270 : curl_easy_setopt (easy,
96 : CURLOPT_USERPWD,
97 : up)) )
98 0 : ret = GNUNET_SYSERR;
99 270 : GNUNET_free (up);
100 270 : break;
101 : }
102 0 : case TALER_BANK_AUTH_BEARER:
103 : {
104 0 : if ( (CURLE_OK !=
105 0 : curl_easy_setopt (easy,
106 : CURLOPT_HTTPAUTH,
107 0 : CURLAUTH_BEARER)) ||
108 : (CURLE_OK !=
109 0 : curl_easy_setopt (easy,
110 : CURLOPT_XOAUTH2_BEARER,
111 : auth->details.bearer.token)) )
112 0 : ret = GNUNET_SYSERR;
113 0 : break;
114 : }
115 : }
116 270 : return ret;
117 : }
118 :
119 :
120 : /* end of bank_api_common.c */
|