Debian Codegraph @

contrib/epee/include/net/http_auth.h

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #pragma once
29
30 #include <boost/optional/optional.hpp>
31 #include <boost/utility/string_ref.hpp>
32 #include <cstdint>
33 #include <functional>
34 #include <string>
35 #include <utility>
36 #include "wipeable_string.h"
37 #include "http_base.h"
38
39 #undef MONERO_DEFAULT_LOG_CATEGORY
40 #define MONERO_DEFAULT_LOG_CATEGORY "net.http"
41
42 namespace epee
43 {
44 namespace net_utils
45 {
46 namespace http
47 {
48 struct login
49 {
50 login() : username(), password() {}
51 login(std::string username_, wipeable_string password_)
52 : username(std::move(username_)), password(std::move(password_))
53 {}
54
55 std::string username;
56 wipeable_string password;
57 };
58
59 //! Implements RFC 2617 digest auth. Digests from RFC 7616 can be added.
60 class http_server_auth
61 {
62 public:
63 struct session
64 {
65 session(login credentials_)
66 : credentials(std::move(credentials_)), nonce(), counter(0)
67 {}
68
69 login credentials;
70 std::string nonce;
71 std::uint32_t counter;
72 };
73
74 http_server_auth() : user(), rng() {}
75 http_server_auth(login credentials, std::function<void(size_t, uint8_t*)> r);
76
77 //! \return Auth response, or `boost::none` iff `request` had valid auth.
78 boost::optional<http_response_info> get_response(const http_request_info& request)
79 {
80 if (user)
81 return do_get_response(request);
82 return boost::none;
83 }
84
85 private:
86 boost::optional<http_response_info> do_get_response(const http_request_info& request);
87
88 boost::optional<session> user;
89
90 std::function<void(size_t, uint8_t*)> rng;
91 };
92
93 //! Implements RFC 2617 digest auth. Digests from RFC 7616 can be added.
94 class http_client_auth
95 {
96 public:
97 enum status : std::uint8_t { kSuccess = 0, kBadPassword, kParseFailure };
98
99 struct session
100 {
101 session(login credentials_)
102 : credentials(std::move(credentials_)), server(), counter(0)
103 {}
104
105 struct keys
106 {
107 using algorithm =
108 std::function<std::string(const session&, boost::string_ref, boost::string_ref)>;
109
110 keys() : nonce(), opaque(), realm(), generator() {}
111 keys(std::string nonce_, std::string opaque_, std::string realm_, algorithm generator_)
112 : nonce(std::move(nonce_))
113 , opaque(std::move(opaque_))
114 , realm(std::move(realm_))
115 , generator(std::move(generator_))
116 {}
117
118 std::string nonce;
119 std::string opaque;
120 std::string realm;
121 algorithm generator;
122 };
123
124 login credentials;
125 keys server;
126 std::uint32_t counter;
127 };
128
129 http_client_auth() : user() {}
130 http_client_auth(login credentials);
131
132 /*!
133 Clients receiving a 401 response code from the server should call this
134 function to process the server auth. Then, before every client request,
135 `get_auth_field()` should be called to retrieve the newest
136 authorization request.
137
138 \return `kBadPassword` if client will never be able to authenticate,
139 `kParseFailure` if all server authentication responses were invalid,
140 and `kSuccess` if `get_auth_field` is ready to generate authorization
141 fields.
142 */
143 status handle_401(const http_response_info& response)
144 {
145 if (user)
146 return do_handle_401(response);
147 return kBadPassword;
148 }
149
150 /*!
151 After calling `handle_401`, clients should call this function to
152 generate an authentication field for every request.
153
154 \return A HTTP "Authorization" field if `handle_401(...)` previously
155 returned `kSuccess`.
156 */
157 boost::optional<std::pair<std::string, std::string>> get_auth_field(
158 const boost::string_ref method, const boost::string_ref uri)
159 {
160 if (user)
161 return do_get_auth_field(method, uri);
162 return boost::none;
163 }
164
165 private:
166 status do_handle_401(const http_response_info&);
167 boost::optional<std::pair<std::string, std::string>> do_get_auth_field(boost::string_ref, boost::string_ref);
168
169 boost::optional<session> user;
170 };
171 }
172 }
173 }