Monero
Loading...
Searching...
No Matches
http_auth.h
Go to the documentation of this file.
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
42namespace epee
43{
44namespace net_utils
45{
46 namespace http
47 {
48 struct login
49 {
51 login(std::string username_, wipeable_string password_)
52 : username(std::move(username_)), password(std::move(password_))
53 {}
54
55 std::string username;
57 };
58
61 {
62 public:
63 struct session
64 {
65 session(login credentials_)
66 : credentials(std::move(credentials_)), nonce(), counter(0)
67 {}
68
70 std::string nonce;
71 std::uint32_t counter;
72 };
73
75 http_server_auth(login credentials, std::function<void(size_t, uint8_t*)> r);
76
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
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;
122 };
123
126 std::uint32_t counter;
127 };
128
130 http_client_auth(login credentials);
131
144 {
145 if (user)
146 return do_handle_401(response);
147 return kBadPassword;
148 }
149
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:
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}
status handle_401(const http_response_info &response)
Definition http_auth.h:143
boost::optional< std::pair< std::string, std::string > > do_get_auth_field(boost::string_ref, boost::string_ref)
Definition http_auth.cpp:766
@ kSuccess
Definition http_auth.h:97
@ kParseFailure
Definition http_auth.h:97
@ kBadPassword
Definition http_auth.h:97
status do_handle_401(const http_response_info &)
Definition http_auth.cpp:753
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:157
boost::optional< session > user
Definition http_auth.h:169
http_client_auth()
Definition http_auth.h:129
boost::optional< session > user
Definition http_auth.h:88
boost::optional< http_response_info > get_response(const http_request_info &request)
Definition http_auth.h:78
std::function< void(size_t, uint8_t *)> rng
Definition http_auth.h:90
boost::optional< http_response_info > do_get_response(const http_request_info &request)
Definition http_auth.cpp:712
http_server_auth()
Definition http_auth.h:74
Definition wipeable_string.h:41
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
Definition enums.h:68
Definition uri.py:1
unsigned char uint8_t
Definition stdint.h:124
std::string opaque
Definition http_auth.h:119
std::string nonce
Definition http_auth.h:118
keys(std::string nonce_, std::string opaque_, std::string realm_, algorithm generator_)
Definition http_auth.h:111
std::function< std::string(const session &, boost::string_ref, boost::string_ref)> algorithm
Definition http_auth.h:107
std::string realm
Definition http_auth.h:120
algorithm generator
Definition http_auth.h:121
std::uint32_t counter
Definition http_auth.h:126
session(login credentials_)
Definition http_auth.h:101
login credentials
Definition http_auth.h:124
std::string nonce
Definition http_auth.h:70
login credentials
Definition http_auth.h:69
session(login credentials_)
Definition http_auth.h:65
std::uint32_t counter
Definition http_auth.h:71
Definition http_auth.h:49
std::string username
Definition http_auth.h:55
wipeable_string password
Definition http_auth.h:56
login()
Definition http_auth.h:50
login(std::string username_, wipeable_string password_)
Definition http_auth.h:51