3#include "../../core/http/type_traits.hpp"
4#include "../../core/http/response.hpp"
5#include "../../core/http/generator.hpp"
11namespace malloy::server::http::auth
25 void operator()(std::string_view v, std::string s)
27 auto b = boost::asio::dynamic_buffer(s);
35 { base64_decoder_helper<D>{}(v, s) };
38 template<base64_decoder Decoder>
40 std::optional<auth_field>
41 parse_auth_field(std::string_view txt)
43 auto method_pos = txt.find_first_of(
' ');
44 if (method_pos == std::string::npos)
47 auto encoded_usepass = txt.substr(method_pos+1);
50 auto outbuff = boost::asio::dynamic_buffer(out);
51 Decoder::decode(encoded_usepass, outbuff);
52 out.erase(std::find(out.begin(), out.end(),
'\0'), out.end());
54 if (parsed_usepass.size() != 2)
57 return auth_field{std::string{txt.begin(), txt.begin() + method_pos}, std::string{parsed_usepass.at(0)}, std::string{parsed_usepass.at(1)}};
59 catch (
const std::invalid_argument&) {
65 template<detail::base64_decoder Decoder, malloy::http::concepts::body BodyType = boost::beast::http::
string_body>
68 static constexpr auto cli_auth_field_name =
"Authorization";
71 using body_type = BodyType;
74 basic(std::string username, std::string password, std::string realm) :
75 m_username{std::move(username)},
76 m_password{std::move(password)},
77 m_realm{std::move(realm)}
79 rebuild_www_auth_txt();
83 set_charset(
const std::string& chset)
86 rebuild_www_auth_txt();
89 std::optional<response_type>
90 operator()(
const boost::beast::http::request_header<>& h)
92 auto auth_iter = h.find(cli_auth_field_name);
93 if (auth_iter == h.end()) {
95 return not_authed_resp();
98 auto maybe_parsed = detail::parse_auth_field<Decoder>(auth_iter->value());
100 return not_authed_resp();
102 const auto auth_details = std::move(*maybe_parsed);
103 const auto r1 = auth_details.method !=
"Basic";
104 const auto r2 = auth_details.username != m_username;
105 const auto r3 = auth_details.password != m_password;
106 if ((auth_details.method != std::string_view{
"Basic"}) || (auth_details.username != m_username) || (auth_details.password != m_password))
107 return not_authed_resp();
114 rebuild_www_auth_txt()
116 m_www_auth = fmt::format(R
"(Basic realm="{}", charset="{}")", m_realm, m_charset);
123 resp.set(malloy::http::field::www_authenticate, m_www_auth);
128 std::string m_username;
129 std::string m_password;
131 std::string m_www_auth;
132 std::string m_charset{
"UTF-8"};
Definition: response.hpp:22
std::vector< std::string_view > split(std::string_view str, std::string_view delimiter)
Definition: utils.hpp:88