generated from Sithas/conan_template
мелкие правки
This commit is contained in:
@@ -10,8 +10,7 @@
|
||||
#include "../DAO/IUserDAO.h"
|
||||
#include "../DAO/IAuthDAO.h"
|
||||
#include "../helpers/helpers.h"
|
||||
#include "../exceptions/exception400_bad_request.h"
|
||||
#include "../exceptions/exception422_unprocessable_entity.h"
|
||||
#include "../exceptions/session_exception.h"
|
||||
|
||||
namespace uad
|
||||
{
|
||||
@@ -48,7 +47,7 @@ public:
|
||||
}
|
||||
catch (const system::system_error& err)
|
||||
{
|
||||
throw exception400_bad_request("cannot deserialize json");
|
||||
throw session_exception(http::status::bad_request, "cannot deserialize json");
|
||||
}
|
||||
|
||||
|
||||
@@ -57,14 +56,14 @@ public:
|
||||
|
||||
if (login.empty() || password.empty())
|
||||
{
|
||||
throw exception422_unprocessable_entity("Login or password are empty"s);
|
||||
throw session_exception(http::status::unprocessable_entity, "Login or password are empty"s);
|
||||
}
|
||||
|
||||
const std::optional<user> maybe_user = user_dao_->GetByLogin(login);
|
||||
|
||||
if (!maybe_user.has_value() || maybe_user.value().hashed_password != HashPassword(password))
|
||||
if (!maybe_user.has_value() && maybe_user.value().hashed_password != HashPassword(password))
|
||||
{
|
||||
throw exception422_unprocessable_entity("Incorrect login or password");
|
||||
throw session_exception(http::status::forbidden,"Incorrect login or password");
|
||||
}
|
||||
const std::string token = GenerateUUID();
|
||||
auth_dao_->Login(maybe_user.value().uuid, token);
|
||||
|
||||
@@ -21,14 +21,14 @@ class AuthLogoutExecutor : public IExecutor<Body, Allocator, ResponseType>
|
||||
|
||||
public:
|
||||
AuthLogoutExecutor(mysqlx::Session& session,
|
||||
const std::shared_ptr<IAuthDAO>& auth_dao)
|
||||
: session_(session), auth_dao_(auth_dao)
|
||||
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
|
||||
) override
|
||||
{
|
||||
using namespace boost;
|
||||
using namespace boost::json;
|
||||
@@ -44,14 +44,14 @@ public:
|
||||
}
|
||||
catch (const system::system_error& err)
|
||||
{
|
||||
throw exception400_bad_request("cannot deserialize json"s);
|
||||
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))
|
||||
{
|
||||
throw exception400_bad_request("token is not authorized"s);
|
||||
throw session_exception(http::status::bad_request, "token is not authorized"s);
|
||||
}
|
||||
|
||||
http::response<ResponseType> res{http::status::ok, req.version()};
|
||||
|
||||
@@ -8,9 +8,7 @@
|
||||
|
||||
#include "IExecutor.h"
|
||||
#include "../DAO/IUserDAO.h"
|
||||
#include "../exceptions/exception400_bad_request.h"
|
||||
#include "../exceptions/exception409_conflict.h"
|
||||
#include "../exceptions/exception422_unprocessable_entity.h"
|
||||
#include "../exceptions/session_exception.h"
|
||||
|
||||
namespace uad
|
||||
{
|
||||
@@ -45,7 +43,7 @@ public:
|
||||
}
|
||||
catch (const system::system_error& err)
|
||||
{
|
||||
throw exception400_bad_request("cannot deserialize json");
|
||||
throw session_exception(http::status::bad_request, "cannot deserialize json");
|
||||
}
|
||||
|
||||
const std::string login = req_json.as_object().at("login").as_string().c_str();
|
||||
@@ -53,14 +51,15 @@ public:
|
||||
|
||||
if (!ValidateLogin(login) || !ValidatePassword(password))
|
||||
{
|
||||
throw exception422_unprocessable_entity(
|
||||
throw session_exception(
|
||||
http::status::unprocessable_entity,
|
||||
"Validations failed. Login should have length from 3 to 50. Password from 5 characters length."s
|
||||
);
|
||||
}
|
||||
|
||||
if (user_dao_->GetByLogin(login).has_value())
|
||||
{
|
||||
throw exception409_conflict("user with login "s + login + " exists"s);
|
||||
throw session_exception(http::status::conflict, "user with login "s + login + " exists"s);
|
||||
}
|
||||
|
||||
user user;
|
||||
|
||||
@@ -10,9 +10,7 @@
|
||||
#include "../DAO/IUserDAO.h"
|
||||
#include "../DAO/IAuthDAO.h"
|
||||
#include "./../helpers/helpers.h"
|
||||
#include "./../exceptions/exception400_bad_request.h"
|
||||
#include "./../exceptions/exception409_conflict.h"
|
||||
#include "./../exceptions/exception422_unprocessable_entity.h"
|
||||
#include "./../exceptions/session_exception.h"
|
||||
|
||||
namespace uad
|
||||
{
|
||||
@@ -96,37 +94,9 @@ public:
|
||||
|
||||
return send(std::move(res));
|
||||
}
|
||||
catch (const exception400_bad_request& e)
|
||||
catch (const session_exception& e)
|
||||
{
|
||||
boost::beast::http::response<ResponseType> res{boost::beast::http::status::bad_request, req.version()};
|
||||
boost::json::value response_body;
|
||||
|
||||
response_body.emplace_object();
|
||||
response_body.as_object().emplace("Result", e.what());
|
||||
|
||||
res.body() = serialize(response_body);
|
||||
res.set(boost::beast::http::field::content_type, "application/json");
|
||||
res.content_length(res.body().size());
|
||||
|
||||
return send(std::move(res));
|
||||
}
|
||||
catch (const exception409_conflict& e)
|
||||
{
|
||||
boost::beast::http::response<ResponseType> res{boost::beast::http::status::conflict, req.version()};
|
||||
boost::json::value response_body;
|
||||
|
||||
response_body.emplace_object();
|
||||
response_body.as_object().emplace("Result", e.what());
|
||||
|
||||
res.body() = serialize(response_body);
|
||||
res.set(boost::beast::http::field::content_type, "application/json");
|
||||
res.content_length(res.body().size());
|
||||
|
||||
return send(std::move(res));
|
||||
}
|
||||
catch (const exception422_unprocessable_entity& e)
|
||||
{
|
||||
boost::beast::http::response<ResponseType> res{boost::beast::http::status::unprocessable_entity, req.version()};
|
||||
boost::beast::http::response<ResponseType> res{e.code, req.version()};
|
||||
boost::json::value response_body;
|
||||
|
||||
response_body.emplace_object();
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
#include "exception400_bad_request.h"
|
||||
|
||||
#include <boost/beast/core/error.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace std::literals;
|
||||
|
||||
namespace uad
|
||||
{
|
||||
exception400_bad_request::exception400_bad_request(const string& info): message_(info)
|
||||
{
|
||||
}
|
||||
|
||||
char const* exception400_bad_request::what() const
|
||||
{
|
||||
return message_.c_str();
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
namespace uad
|
||||
{
|
||||
struct exception400_bad_request : std::exception
|
||||
{
|
||||
private:
|
||||
std::string message_;
|
||||
public:
|
||||
exception400_bad_request(const std::string& info);
|
||||
char const* what() const override;
|
||||
};
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
#include "exception409_conflict.h"
|
||||
|
||||
#include <boost/beast/core/error.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace std::literals;
|
||||
|
||||
namespace uad
|
||||
{
|
||||
exception409_conflict::exception409_conflict(const string& info): message_(info)
|
||||
{
|
||||
}
|
||||
|
||||
char const* exception409_conflict::what() const
|
||||
{
|
||||
return message_.c_str();
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
namespace uad
|
||||
{
|
||||
struct exception409_conflict : std::exception
|
||||
{
|
||||
private:
|
||||
std::string message_;
|
||||
public:
|
||||
exception409_conflict(const std::string& info);
|
||||
char const* what() const override;
|
||||
};
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
#include "exception422_unprocessable_entity.h"
|
||||
|
||||
#include <boost/beast/core/error.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace std::literals;
|
||||
|
||||
namespace uad
|
||||
{
|
||||
exception422_unprocessable_entity::exception422_unprocessable_entity(const string& info): message_(info)
|
||||
{
|
||||
}
|
||||
|
||||
char const* exception422_unprocessable_entity::what() const
|
||||
{
|
||||
return message_.c_str();
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
namespace uad
|
||||
{
|
||||
struct exception422_unprocessable_entity : std::exception
|
||||
{
|
||||
private:
|
||||
std::string message_;
|
||||
public:
|
||||
exception422_unprocessable_entity(const std::string& info);
|
||||
char const* what() const override;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#include "session_exception.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace uad
|
||||
{
|
||||
char const* session_exception::what() const
|
||||
{
|
||||
return message.c_str();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/beast/http/status.hpp>
|
||||
#include <string>
|
||||
#include <boost/exception/to_string.hpp>
|
||||
|
||||
namespace uad
|
||||
{
|
||||
struct session_exception : std::exception
|
||||
{
|
||||
const boost::beast::http::status code;
|
||||
const std::string comment;
|
||||
const std::string message;
|
||||
|
||||
session_exception(const boost::beast::http::status ec, const std::string info)
|
||||
: code(ec), comment(info), message(std::to_string(static_cast<uint64_t>(ec)) + " - " + comment)
|
||||
{}
|
||||
char const* what() const override;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user