generated from Sithas/conan_template
Рабочая сборка с третьей ручкой
This commit is contained in:
+4
-3
@@ -45,9 +45,10 @@ add_executable(App ./src/main.cpp
|
|||||||
./src/endpoints_handlers/IExecutor.h
|
./src/endpoints_handlers/IExecutor.h
|
||||||
./src/endpoints_handlers/AuthRegistrationExecutor.h
|
./src/endpoints_handlers/AuthRegistrationExecutor.h
|
||||||
./src/endpoints_handlers/RootExecutor.h
|
./src/endpoints_handlers/RootExecutor.h
|
||||||
src/DAO/IAuthDAO.h
|
./src/DAO/IAuthDAO.h
|
||||||
src/DAO/MemoryAuthDAO.cpp
|
./src/DAO/MemoryAuthDAO.cpp
|
||||||
src/DAO/MemoryAuthDAO.h
|
./src/DAO/MemoryAuthDAO.h
|
||||||
|
./src/endpoints_handlers/AuthLogoutExecutor.h
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(App PRIVATE Boost::boost Boost::json Threads::Threads mysql::concpp)
|
target_link_libraries(App PRIVATE Boost::boost Boost::json Threads::Threads mysql::concpp)
|
||||||
|
|||||||
@@ -0,0 +1,103 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <regex>
|
||||||
|
#include <boost/json.hpp>
|
||||||
|
#include <mysqlx/xdevapi.h>
|
||||||
|
#include <mysqlx/common/api.h>
|
||||||
|
#include <boost/uuid.hpp>
|
||||||
|
|
||||||
|
#include "IExecutor.h"
|
||||||
|
#include "../DAO/IUserDAO.h"
|
||||||
|
#include "../DAO/IAuthDAO.h"
|
||||||
|
#include "../helpers/helpers.h"
|
||||||
|
|
||||||
|
namespace uad
|
||||||
|
{
|
||||||
|
template <class Body, class Allocator, class ResponseType>
|
||||||
|
class AuthLogoutExecutor : public IExecutor<Body, Allocator, ResponseType>
|
||||||
|
{
|
||||||
|
mysqlx::Session& session_;
|
||||||
|
const std::shared_ptr<IUserDAO>& user_dao_;
|
||||||
|
const std::shared_ptr<IAuthDAO>& auth_dao_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
AuthLogoutExecutor(mysqlx::Session& session,
|
||||||
|
const std::shared_ptr<IUserDAO>& user_dao,
|
||||||
|
const std::shared_ptr<IAuthDAO>& auth_dao)
|
||||||
|
: session_(session), user_dao_(user_dao), auth_dao_(auth_dao)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::beast::http::response<ResponseType> operator ()(
|
||||||
|
boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator>>&& req
|
||||||
|
) override
|
||||||
|
{
|
||||||
|
using namespace boost;
|
||||||
|
using namespace boost::json;
|
||||||
|
using namespace boost::beast;
|
||||||
|
using namespace std::string_literals;
|
||||||
|
|
||||||
|
auto body = req.body();
|
||||||
|
value req_json;
|
||||||
|
value response_body;
|
||||||
|
|
||||||
|
response_body.emplace_object();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
req_json = json::parse(body);
|
||||||
|
|
||||||
|
const std::string login = req_json.as_object().at("login").as_string().c_str();
|
||||||
|
const std::string password = req_json.as_object().at("password").as_string().c_str();
|
||||||
|
|
||||||
|
if (login.empty() || password.empty())
|
||||||
|
{
|
||||||
|
http::response<ResponseType> res{http::status::unprocessable_entity, req.version()};
|
||||||
|
response_body.as_object().emplace("Result", "Login or password are empty");
|
||||||
|
|
||||||
|
res.body() = serialize(response_body);
|
||||||
|
res.set(http::field::content_type, "application/json");
|
||||||
|
res.content_length(res.body().size());
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<User> maybe_user = user_dao_->GetByLogin(login);
|
||||||
|
|
||||||
|
if (!maybe_user.has_value() || (maybe_user.value().GetHashedPassword() != HashPassword(password)))
|
||||||
|
{
|
||||||
|
http::response<ResponseType> res{http::status::unprocessable_entity, req.version()};
|
||||||
|
response_body.as_object().emplace("Result", "Incorrect login or password");
|
||||||
|
|
||||||
|
res.body() = serialize(response_body);
|
||||||
|
res.set(http::field::content_type, "application/json");
|
||||||
|
res.content_length(res.body().size());
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
auto token = GenerateUUID();
|
||||||
|
auth_dao_->Login(maybe_user.value().GetUUID(), token);
|
||||||
|
|
||||||
|
http::response<ResponseType> res{http::status::ok, req.version()};
|
||||||
|
response_body.as_object().emplace("token", token);
|
||||||
|
|
||||||
|
res.body() = serialize(response_body);
|
||||||
|
res.set(http::field::content_type, "application/json");
|
||||||
|
res.content_length(res.body().size());
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
catch (const system::system_error& err)
|
||||||
|
{
|
||||||
|
http::response<ResponseType> res{http::status::bad_request, req.version()};
|
||||||
|
response_body.as_object().emplace("Result", "cannot deserialize json");
|
||||||
|
|
||||||
|
res.body() = serialize(response_body);
|
||||||
|
res.set(http::field::content_type, "application/json");
|
||||||
|
res.content_length(res.body().size());
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
#include "Controller.h"
|
#include "Controller.h"
|
||||||
#include "AuthRegistrationExecutor.h"
|
#include "AuthRegistrationExecutor.h"
|
||||||
#include "AuthLoginExecutor.h"
|
#include "AuthLoginExecutor.h"
|
||||||
|
#include "AuthLogoutExecutor.h"
|
||||||
#include "../DAO/IUserDAO.h"
|
#include "../DAO/IUserDAO.h"
|
||||||
#include "../DAO/IAuthDAO.h"
|
#include "../DAO/IAuthDAO.h"
|
||||||
#include "./../helpers/helpers.h"
|
#include "./../helpers/helpers.h"
|
||||||
@@ -17,6 +18,8 @@ class RootExecutor
|
|||||||
Body, Allocator, boost::beast::http::string_body>;
|
Body, Allocator, boost::beast::http::string_body>;
|
||||||
using RouteAuthLoginExecutor = AuthLoginExecutor<
|
using RouteAuthLoginExecutor = AuthLoginExecutor<
|
||||||
Body, Allocator, boost::beast::http::string_body>;
|
Body, Allocator, boost::beast::http::string_body>;
|
||||||
|
using RouteAuthLogoutExecutor = AuthLogoutExecutor<
|
||||||
|
Body, Allocator, boost::beast::http::string_body>;
|
||||||
using IRouteController = IController<Body, Allocator, boost::beast::http::string_body>;
|
using IRouteController = IController<Body, Allocator, boost::beast::http::string_body>;
|
||||||
using RouteController = Controller<Body, Allocator, boost::beast::http::string_body>;
|
using RouteController = Controller<Body, Allocator, boost::beast::http::string_body>;
|
||||||
using RoutesPathes = std::unordered_map<std::string, std::unique_ptr<IRouteController>>;
|
using RoutesPathes = std::unordered_map<std::string, std::unique_ptr<IRouteController>>;
|
||||||
@@ -53,6 +56,13 @@ public:
|
|||||||
std::make_shared<RouteAuthLoginExecutor>(session_, user_dao_, auth_dao_)}
|
std::make_shared<RouteAuthLoginExecutor>(session_, user_dao_, auth_dao_)}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
routes_pathes_["/api/v1/Auth/Logout"] = std::make_unique<RouteController>(
|
||||||
|
typename RouteController::HTTPMethodsToExecutors{
|
||||||
|
{boost::beast::http::verb::post,
|
||||||
|
std::make_shared<RouteAuthLogoutExecutor>(session_, user_dao_, auth_dao_)}
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator ()(
|
void operator ()(
|
||||||
|
|||||||
Reference in New Issue
Block a user