| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 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 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 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 |
} |