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
43
namespace
epee
44
{
45
namespace
net_utils
46
{
47
namespace
http
48
{
49
struct
login
50
{
51
login
() :
username
(),
password
() {}
52
login
(std::string username_,
wipeable_string
password_)
53
:
username
(
std
::move(username_)),
password
(
std
::move(password_))
54
{}
55
56
std::string
username
;
57
wipeable_string
password
;
58
};
59
61
class
http_server_auth
62
{
63
public
:
64
struct
session
65
{
66
session
(
login
credentials_)
67
:
credentials
(
std
::move(credentials_)),
nonce
(),
counter
(0)
68
{}
69
70
login
credentials
;
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
95
class
http_client_auth
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
;
122
algorithm
generator
;
123
};
124
125
login
credentials
;
126
keys
server
;
127
std::uint32_t
counter
;
128
};
129
130
http_client_auth
() : user() {}
131
http_client_auth
(
login
credentials);
132
144
status
handle_401
(
const
http_response_info
& response)
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
}
epee::net_utils::http::http_client_auth::handle_401
status handle_401(const http_response_info &response)
Definition
http_auth.h:144
epee::net_utils::http::http_client_auth::status
status
Definition
http_auth.h:98
epee::net_utils::http::http_client_auth::kSuccess
@ kSuccess
Definition
http_auth.h:98
epee::net_utils::http::http_client_auth::kParseFailure
@ kParseFailure
Definition
http_auth.h:98
epee::net_utils::http::http_client_auth::kBadPassword
@ kBadPassword
Definition
http_auth.h:98
epee::net_utils::http::http_client_auth::get_auth_field
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
epee::net_utils::http::http_client_auth::http_client_auth
http_client_auth()
Definition
http_auth.h:130
epee::net_utils::http::http_server_auth::get_response
boost::optional< http_response_info > get_response(const http_request_info &request)
Definition
http_auth.h:79
epee::net_utils::http::http_server_auth::http_server_auth
http_server_auth()
Definition
http_auth.h:75
epee::wipeable_string
Definition
wipeable_string.h:41
http_base.h
epee::net_utils::http
Definition
http_auth.h:48
epee
Definition
ado_db_helper.h:67
std
STL namespace.
uint8_t
unsigned char uint8_t
Definition
stdint.h:124
epee::net_utils::http::http_client_auth::session::keys
Definition
http_auth.h:107
epee::net_utils::http::http_client_auth::session::keys::opaque
std::string opaque
Definition
http_auth.h:120
epee::net_utils::http::http_client_auth::session::keys::nonce
std::string nonce
Definition
http_auth.h:119
epee::net_utils::http::http_client_auth::session::keys::keys
keys(std::string nonce_, std::string opaque_, std::string realm_, algorithm generator_)
Definition
http_auth.h:112
epee::net_utils::http::http_client_auth::session::keys::algorithm
std::function< std::string(const session &, boost::string_ref, boost::string_ref)> algorithm
Definition
http_auth.h:108
epee::net_utils::http::http_client_auth::session::keys::realm
std::string realm
Definition
http_auth.h:121
epee::net_utils::http::http_client_auth::session::keys::generator
algorithm generator
Definition
http_auth.h:122
epee::net_utils::http::http_client_auth::session::keys::keys
keys()
Definition
http_auth.h:111
epee::net_utils::http::http_client_auth::session::counter
std::uint32_t counter
Definition
http_auth.h:127
epee::net_utils::http::http_client_auth::session::server
keys server
Definition
http_auth.h:126
epee::net_utils::http::http_client_auth::session::session
session(login credentials_)
Definition
http_auth.h:102
epee::net_utils::http::http_client_auth::session::credentials
login credentials
Definition
http_auth.h:125
epee::net_utils::http::http_request_info
Definition
http_base.h:157
epee::net_utils::http::http_response_info
Definition
http_base.h:188
epee::net_utils::http::http_server_auth::session::nonce
std::string nonce
Definition
http_auth.h:71
epee::net_utils::http::http_server_auth::session::credentials
login credentials
Definition
http_auth.h:70
epee::net_utils::http::http_server_auth::session::session
session(login credentials_)
Definition
http_auth.h:66
epee::net_utils::http::http_server_auth::session::counter
std::uint32_t counter
Definition
http_auth.h:72
epee::net_utils::http::login
Definition
http_auth.h:50
epee::net_utils::http::login::username
std::string username
Definition
http_auth.h:56
epee::net_utils::http::login::password
wipeable_string password
Definition
http_auth.h:57
epee::net_utils::http::login::login
login()
Definition
http_auth.h:51
epee::net_utils::http::login::login
login(std::string username_, wipeable_string password_)
Definition
http_auth.h:52
wipeable_string.h
contrib
epee
include
net
http_auth.h
Generated on
for Electroneum by
1.16.1