generated from Sithas/conan_template
68 lines
1.8 KiB
C++
68 lines
1.8 KiB
C++
#pragma once
|
|
#include <boost/log/trivial.hpp>
|
|
|
|
#include <boost/json.hpp>
|
|
#include <mysqlx/xdevapi.h>
|
|
|
|
#include "IExecutor.h"
|
|
#include "../DAO/IAuthDAO.h"
|
|
#include "../exceptions/session_exception.h"
|
|
|
|
namespace uad
|
|
{
|
|
template <class Body, class Allocator, class ResponseType>
|
|
class AuthLogoutExecutor : public IExecutor<Body, Allocator, ResponseType>
|
|
{
|
|
mysqlx::Session& session_;
|
|
const std::shared_ptr<IAuthDAO>& auth_dao_;
|
|
|
|
public:
|
|
AuthLogoutExecutor(mysqlx::Session& session,
|
|
const std::shared_ptr<IAuthDAO>& auth_dao) :
|
|
session_(session), 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;
|
|
|
|
BOOST_LOG_TRIVIAL(info) << "POST /api/v1/Auth/Logout - Request";
|
|
|
|
const auto body = req.body();
|
|
value req_json;
|
|
|
|
try
|
|
{
|
|
req_json = json::parse(body);
|
|
}
|
|
catch (const system::system_error& err)
|
|
{
|
|
BOOST_LOG_TRIVIAL(error) << "POST /api/v1/Auth/Logout - Response 500: Cannot deserialize json";
|
|
throw session_exception(http::status::internal_server_error, "cannot deserialize json"s);
|
|
}
|
|
|
|
const std::string token = req_json.as_object().at("token").as_string().c_str();
|
|
|
|
if (!auth_dao_->Logout(token))
|
|
{
|
|
BOOST_LOG_TRIVIAL(error) << "POST /api/v1/Auth/Logout - Response 400: Token is not authorized";
|
|
throw session_exception(http::status::bad_request, "Token is not authorized"s);
|
|
}
|
|
|
|
http::response<ResponseType> res{http::status::ok, req.version()};
|
|
|
|
res.body() = "null"s;
|
|
res.set(http::field::content_type, "application/json");
|
|
res.content_length(res.body().size());
|
|
|
|
return res;
|
|
}
|
|
};
|
|
}
|