Skip to content

Commit

Permalink
Move AdminVerificationStrategy to Server (XRPLF#965)
Browse files Browse the repository at this point in the history
  • Loading branch information
cindyyan317 authored Nov 2, 2023
1 parent 058df4d commit 320ebaa
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/web/HttpSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class HttpSession : public detail::HttpBase<HttpSession, HandlerType>,
explicit HttpSession(
tcp::socket&& socket,
std::string const& ip,
std::optional<std::string> adminPassword,
std::shared_ptr<detail::AdminVerificationStrategy> const& adminVerification,
std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
std::reference_wrapper<web::DOSGuard> dosGuard,
std::shared_ptr<HandlerType> const& handler,
Expand All @@ -64,7 +64,7 @@ class HttpSession : public detail::HttpBase<HttpSession, HandlerType>,
: detail::HttpBase<HttpSession, HandlerType>(
ip,
tagFactory,
std::move(adminPassword),
adminVerification,
dosGuard,
handler,
std::move(buffer)
Expand Down
24 changes: 12 additions & 12 deletions src/web/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Detector : public std::enable_shared_from_this<Detector<PlainSessionType,
std::reference_wrapper<web::DOSGuard> const dosGuard_;
std::shared_ptr<HandlerType> const handler_;
boost::beast::flat_buffer buffer_;
std::optional<std::string> adminPassword_;
std::shared_ptr<detail::AdminVerificationStrategy> const adminVerification_;

public:
/**
Expand All @@ -75,15 +75,15 @@ class Detector : public std::enable_shared_from_this<Detector<PlainSessionType,
std::optional<std::reference_wrapper<boost::asio::ssl::context>> ctx,
std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
std::reference_wrapper<web::DOSGuard> dosGuard,
std::shared_ptr<HandlerType> const& handler,
std::optional<std::string> adminPassword
std::shared_ptr<HandlerType> handler,
std::shared_ptr<detail::AdminVerificationStrategy> adminVerification
)
: stream_(std::move(socket))
, ctx_(ctx)
, tagFactory_(std::cref(tagFactory))
, dosGuard_(dosGuard)
, handler_(handler)
, adminPassword_(std::move(adminPassword))
, handler_(std::move(handler))
, adminVerification_(std::move(adminVerification))
{
}

Expand Down Expand Up @@ -136,7 +136,7 @@ class Detector : public std::enable_shared_from_this<Detector<PlainSessionType,
std::make_shared<SslSessionType<HandlerType>>(
stream_.release_socket(),
ip,
adminPassword_,
adminVerification_,
*ctx_,
tagFactory_,
dosGuard_,
Expand All @@ -148,7 +148,7 @@ class Detector : public std::enable_shared_from_this<Detector<PlainSessionType,
}

std::make_shared<PlainSessionType<HandlerType>>(
stream_.release_socket(), ip, adminPassword_, tagFactory_, dosGuard_, handler_, std::move(buffer_)
stream_.release_socket(), ip, adminVerification_, tagFactory_, dosGuard_, handler_, std::move(buffer_)
)
->run();
}
Expand All @@ -174,7 +174,7 @@ class Server : public std::enable_shared_from_this<Server<PlainSessionType, SslS
std::reference_wrapper<web::DOSGuard> dosGuard_;
std::shared_ptr<HandlerType> handler_;
tcp::acceptor acceptor_;
std::optional<std::string> adminPassword_;
std::shared_ptr<detail::AdminVerificationStrategy> adminVerification_;

public:
/**
Expand All @@ -194,16 +194,16 @@ class Server : public std::enable_shared_from_this<Server<PlainSessionType, SslS
tcp::endpoint endpoint,
util::TagDecoratorFactory tagFactory,
web::DOSGuard& dosGuard,
std::shared_ptr<HandlerType> const& handler,
std::shared_ptr<HandlerType> handler,
std::optional<std::string> adminPassword
)
: ioc_(std::ref(ioc))
, ctx_(ctx)
, tagFactory_(tagFactory)
, dosGuard_(std::ref(dosGuard))
, handler_(handler)
, handler_(std::move(handler))
, acceptor_(boost::asio::make_strand(ioc))
, adminPassword_(std::move(adminPassword))
, adminVerification_(detail::make_AdminVerificationStrategy(std::move(adminPassword)))
{
boost::beast::error_code ec;

Expand Down Expand Up @@ -257,7 +257,7 @@ class Server : public std::enable_shared_from_this<Server<PlainSessionType, SslS
ctx_ ? std::optional<std::reference_wrapper<boost::asio::ssl::context>>{ctx_.value()} : std::nullopt;

std::make_shared<Detector<PlainSessionType, SslSessionType, HandlerType>>(
std::move(socket), ctxRef, std::cref(tagFactory_), dosGuard_, handler_, adminPassword_
std::move(socket), ctxRef, std::cref(tagFactory_), dosGuard_, handler_, adminVerification_
)
->run();
}
Expand Down
4 changes: 2 additions & 2 deletions src/web/SslHttpSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SslHttpSession : public detail::HttpBase<SslHttpSession, HandlerType>,
explicit SslHttpSession(
tcp::socket&& socket,
std::string const& ip,
std::optional<std::string> adminPassword,
std::shared_ptr<detail::AdminVerificationStrategy> const& adminVerification,
boost::asio::ssl::context& ctx,
std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
std::reference_wrapper<web::DOSGuard> dosGuard,
Expand All @@ -66,7 +66,7 @@ class SslHttpSession : public detail::HttpBase<SslHttpSession, HandlerType>,
: detail::HttpBase<SslHttpSession, HandlerType>(
ip,
tagFactory,
std::move(adminPassword),
adminVerification,
dosGuard,
handler,
std::move(buffer)
Expand Down
8 changes: 4 additions & 4 deletions src/web/SslWsSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,19 @@ class SslWsUpgrader : public std::enable_shared_from_this<SslWsUpgrader<HandlerT
std::string ip,
std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
std::reference_wrapper<web::DOSGuard> dosGuard,
std::shared_ptr<HandlerType> const& handler,
std::shared_ptr<HandlerType> handler,
boost::beast::flat_buffer&& buffer,
http::request<http::string_body> request,
bool isAdmiin
bool isAdmin
)
: https_(std::move(stream))
, buffer_(std::move(buffer))
, ip_(std::move(ip))
, tagFactory_(tagFactory)
, dosGuard_(dosGuard)
, handler_(handler)
, handler_(std::move(handler))
, req_(std::move(request))
, isAdmin_(isAdmiin)
, isAdmin_(isAdmin)
{
}

Expand Down
6 changes: 3 additions & 3 deletions src/web/impl/AdminVerificationStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ PasswordAdminVerificationStrategy::isAdmin(RequestType const& request, std::stri
return passwordSha256_ == userAuth;
}

std::unique_ptr<AdminVerificationStrategy>
std::shared_ptr<AdminVerificationStrategy>
make_AdminVerificationStrategy(std::optional<std::string> password)
{
if (password.has_value()) {
return std::make_unique<PasswordAdminVerificationStrategy>(std::move(*password));
return std::make_shared<PasswordAdminVerificationStrategy>(std::move(*password));
}
return std::make_unique<IPAdminVerificationStrategy>();
return std::make_shared<IPAdminVerificationStrategy>();
}

} // namespace web::detail
2 changes: 1 addition & 1 deletion src/web/impl/AdminVerificationStrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class PasswordAdminVerificationStrategy : public AdminVerificationStrategy {
isAdmin(RequestType const& request, std::string_view) const override;
};

std::unique_ptr<AdminVerificationStrategy>
std::shared_ptr<AdminVerificationStrategy>
make_AdminVerificationStrategy(std::optional<std::string> password);

} // namespace web::detail
10 changes: 5 additions & 5 deletions src/web/impl/HttpBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class HttpBase : public ConnectionBase {

std::shared_ptr<void> res_;
SendLambda sender_;
std::unique_ptr<AdminVerificationStrategy> adminVerification_;
std::shared_ptr<AdminVerificationStrategy> adminVerification_;

protected:
boost::beast::flat_buffer buffer_;
Expand Down Expand Up @@ -129,17 +129,17 @@ class HttpBase : public ConnectionBase {
HttpBase(
std::string const& ip,
std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
std::optional<std::string> adminPassword,
std::shared_ptr<AdminVerificationStrategy> adminVerification,
std::reference_wrapper<web::DOSGuard> dosGuard,
std::shared_ptr<HandlerType> const& handler,
std::shared_ptr<HandlerType> handler,
boost::beast::flat_buffer buffer
)
: ConnectionBase(tagFactory, ip)
, sender_(*this)
, adminVerification_(make_AdminVerificationStrategy(std::move(adminPassword)))
, adminVerification_(std::move(adminVerification))
, buffer_(std::move(buffer))
, dosGuard_(dosGuard)
, handler_(handler)
, handler_(std::move(handler))
{
LOG(perfLog_.debug()) << tag() << "http session created";
dosGuard_.get().increment(ip);
Expand Down

0 comments on commit 320ebaa

Please sign in to comment.