From 2adca010074def05ca5989a0358a91b94785c98a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 20 Sep 2025 09:28:46 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=87=D0=B0=D1=8F=20?= =?UTF-8?q?=D1=81=D0=B1=D0=BE=D1=80=D0=BA=D0=B0=20=D0=B2=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE=20executor'=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 - src/endpoints_handlers/AuthLoginExecutor.h | 80 +++++----------------- src/helpers/helpers.cpp | 6 ++ src/helpers/helpers.h | 2 + 4 files changed, 27 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 0f7596e..f56d429 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,6 @@ ``` { "token": af32df3bas739f272bd109c823 - "expiresIn": 36000000 // 10 часов } ``` diff --git a/src/endpoints_handlers/AuthLoginExecutor.h b/src/endpoints_handlers/AuthLoginExecutor.h index 7f34958..07e2dd3 100644 --- a/src/endpoints_handlers/AuthLoginExecutor.h +++ b/src/endpoints_handlers/AuthLoginExecutor.h @@ -45,6 +45,25 @@ public: try { req_json = json::parse(body); + + std::string login = req_json.as_object().at("login").as_string().c_str(); + std::string password = req_json.as_object().at("password").as_string().c_str(); + + if (login.empty() || password.empty()) + { + http::response 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::string hashed_password = HashPassword(password); + + std::optional maybe_user = user_dao_->GetByLogin(login); } catch (const system::system_error& err) { @@ -57,67 +76,6 @@ public: return res; } - - std::string login = req_json.as_object().at("login").as_string().c_str(); - std::string password = req_json.as_object().at("password").as_string().c_str(); - - if (!ValidateLogin(login) || !ValidatePassword(password)) - { - http::response res{http::status::unprocessable_entity, req.version()}; - - response_body.as_object().emplace( - "Result", - "Validations failed. Login should have length from 3 to 50. Password from 5 characters length." - ); - - res.body() = serialize(response_body); - res.set(http::field::content_type, "application/json"); - res.content_length(res.body().size()); - - return res; - } - - if (user_dao_->GetByLogin(login).has_value()) - { - http::response res{http::status::conflict, req.version()}; - - response_body.as_object().emplace( - "Result", - "user with login "s + login + " exists"s - ); - - res.body() = serialize(response_body); - res.set(http::field::content_type, "application/json"); - res.content_length(res.body().size()); - - return res; - } - - User user; - - user.SetLogin(login); - user.SetPassword(password); - - const auto uuid_stringified = user_dao_->Create(user); - - http::response res{ - http::status::created, req.version() - }; - - response_body.as_object().emplace( - "uuid", - uuid_stringified - ); - response_body.as_object().emplace( - "login", - user.GetLogin() - ); - - res.body() = serialize(response_body); - res.set(http::field::content_type, "application/json"); - res.content_length(res.body().size()); - - return res; } private: diff --git a/src/helpers/helpers.cpp b/src/helpers/helpers.cpp index c565453..d643162 100644 --- a/src/helpers/helpers.cpp +++ b/src/helpers/helpers.cpp @@ -116,6 +116,12 @@ std::string ToHex(std::byte* src, size_t len) return ret; } +std::string HashPassword(const std::string& password) +{ + size_t calculated_hash = std::hash{}(password); + return ToHex((byte*)&calculated_hash, sizeof(calculated_hash)); +} + uint64_t Random() { std::random_device device; diff --git a/src/helpers/helpers.h b/src/helpers/helpers.h index 318f47f..3169d5b 100644 --- a/src/helpers/helpers.h +++ b/src/helpers/helpers.h @@ -12,5 +12,7 @@ void Fail(boost::beast::error_code ec, char const* what); std::string ToHex(std::byte* src, size_t len); +std::string HashPassword(const std::string& password); + uint64_t Random(); } // namespace uad