Electroneum
Loading...
Searching...
No Matches
http_auth.h
Go to the documentation of this file.
1// Copyrights(c) 2017-2021, The Electroneum Project
2// Copyrights(c) 2014-2019, The Monero Project
3//
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without modification, are
7// permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this list of
10// conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13// of conditions and the following disclaimer in the documentation and/or other
14// materials provided with the distribution.
15//
16// 3. Neither the name of the copyright holder nor the names of its contributors may be
17// used to endorse or promote products derived from this software without specific
18// prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29#pragma once
30
31#include <boost/optional/optional.hpp>
32#include <boost/utility/string_ref.hpp>
33#include <cstdint>
34#include <functional>
35#include <string>
36#include <utility>
37#include "wipeable_string.h"
38#include "http_base.h"
39
40#undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
41#define ELECTRONEUM_DEFAULT_LOG_CATEGORY "net.http"
42
43namespace epee
44{
45namespace net_utils
46{
47 namespace http
48 {
49 struct login
50 {
52 login(std::string username_, wipeable_string password_)
53 : username(std::move(username_)), password(std::move(password_))
54 {}
55
56 std::string username;
58 };
59
62 {
63 public:
64 struct session
65 {
66 session(login credentials_)
67 : credentials(std::move(credentials_)), nonce(), counter(0)
68 {}
69
71 std::string nonce;
72 std::uint32_t counter;
73 };
74
75 http_server_auth() : user(), rng() {}
76 http_server_auth(login credentials, std::function<void(size_t, uint8_t*)> r);
77
79 boost::optional<http_response_info> get_response(const http_request_info& request)
80 {
81 if (user)
82 return do_get_response(request);
83 return boost::none;
84 }
85
86 private:
87 boost::optional<http_response_info> do_get_response(const http_request_info& request);
88
89 boost::optional<session> user;
90
91 std::function<void(size_t, uint8_t*)> rng;
92 };
93
96 {
97 public:
98 enum status : std::uint8_t { kSuccess = 0, kBadPassword, kParseFailure };
99
100 struct session
101 {
102 session(login credentials_)
103 : credentials(std::move(credentials_)), server(), counter(0)
104 {}
105
106 struct keys
107 {
108 using algorithm =
109 std::function<std::string(const session&, boost::string_ref, boost::string_ref)>;
110
111 keys() : nonce(), opaque(), realm(), generator() {}
112 keys(std::string nonce_, std::string opaque_, std::string realm_, algorithm generator_)
113 : nonce(std::move(nonce_))
114 , opaque(std::move(opaque_))
115 , realm(std::move(realm_))
116 , generator(std::move(generator_))
117 {}
118
119 std::string nonce;
120 std::string opaque;
121 std::string realm;
123 };
124
127 std::uint32_t counter;
128 };
129
130 http_client_auth() : user() {}
131 http_client_auth(login credentials);
132
145 {
146 if (user)
147 return do_handle_401(response);
148 return kBadPassword;
149 }
150
158 boost::optional<std::pair<std::string, std::string>> get_auth_field(
159 const boost::string_ref method, const boost::string_ref uri)
160 {
161 if (user)
162 return do_get_auth_field(method, uri);
163 return boost::none;
164 }
165
166 private:
167 status do_handle_401(const http_response_info&);
168 boost::optional<std::pair<std::string, std::string>> do_get_auth_field(boost::string_ref, boost::string_ref);
169
170 boost::optional<session> user;
171 };
172 }
173}
174}
status handle_401(const http_response_info &response)
Definition http_auth.h:144
boost::optional< std::pair< std::string, std::string > > get_auth_field(const boost::string_ref method, const boost::string_ref uri)
Definition http_auth.h:158
boost::optional< http_response_info > get_response(const http_request_info &request)
Definition http_auth.h:79
STL namespace.
unsigned char uint8_t
Definition stdint.h:124
keys(std::string nonce_, std::string opaque_, std::string realm_, algorithm generator_)
Definition http_auth.h:112
std::function< std::string(const session &, boost::string_ref, boost::string_ref)> algorithm
Definition http_auth.h:108
wipeable_string password
Definition http_auth.h:57
login(std::string username_, wipeable_string password_)
Definition http_auth.h:52