diff --git a/src/DAO/IAuthDAO.h b/src/DAO/IAuthDAO.h index 45746cd..6fbde19 100644 --- a/src/DAO/IAuthDAO.h +++ b/src/DAO/IAuthDAO.h @@ -10,7 +10,9 @@ namespace uad class IAuthDAO { public: - virtual std::string Login(const std::string& registrated_user_uuid) = 0; + virtual std::string Login( + const std::string& registrated_user_uuid, + const std::string& auth_token) = 0; virtual bool HasAuthorized(const std::string& auth_token) = 0; diff --git a/src/DAO/MemoryAuthDAO.cpp b/src/DAO/MemoryAuthDAO.cpp index 3573c29..8c9d71f 100644 --- a/src/DAO/MemoryAuthDAO.cpp +++ b/src/DAO/MemoryAuthDAO.cpp @@ -10,12 +10,10 @@ MemoryAuthDAO::MemoryAuthDAO(mysqlx::Session& session): session_(session) { } -std::string MemoryAuthDAO::Login(const std::string& registrated_user_uuid) +std::string MemoryAuthDAO::Login( + const std::string& registrated_user_uuid, + const std::string& auth_token) { - boost::uuids::random_generator generator; - boost::uuids::uuid uuid = generator(); - std::string auth_token = boost::uuids::to_string(uuid); - users_uuids_to_auth_tokens_[registrated_user_uuid] = auth_token; auth_tokens_to_users_uuids_[auth_token] = registrated_user_uuid; diff --git a/src/DAO/MemoryAuthDAO.h b/src/DAO/MemoryAuthDAO.h index c28cc0d..8d29ff1 100644 --- a/src/DAO/MemoryAuthDAO.h +++ b/src/DAO/MemoryAuthDAO.h @@ -19,7 +19,9 @@ class MemoryAuthDAO : public uad::IAuthDAO public: explicit MemoryAuthDAO(mysqlx::Session& session); - std::string Login(const std::string& registrated_user_uuid) override; + std::string Login( + const std::string& registrated_user_uuid, + const std::string& auth_token) override; bool HasAuthorized(const std::string& auth_token) override; diff --git a/src/endpoints_handlers/AuthLoginExecutor.h b/src/endpoints_handlers/AuthLoginExecutor.h index b17e65d..54ee140 100644 --- a/src/endpoints_handlers/AuthLoginExecutor.h +++ b/src/endpoints_handlers/AuthLoginExecutor.h @@ -9,6 +9,7 @@ #include "IExecutor.h" #include "../DAO/IUserDAO.h" #include "../DAO/IAuthDAO.h" +#include "../helpers/helpers.h" namespace uad { @@ -74,6 +75,17 @@ public: return res; } + auto token = GenerateUUID(); + auth_dao_->Login(user.value().GetUUID(), token); + + http::response 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) { diff --git a/src/helpers/helpers.cpp b/src/helpers/helpers.cpp index d643162..dee82aa 100644 --- a/src/helpers/helpers.cpp +++ b/src/helpers/helpers.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -122,6 +123,13 @@ std::string HashPassword(const std::string& password) return ToHex((byte*)&calculated_hash, sizeof(calculated_hash)); } +std::string GenerateUUID() +{ + uuids::random_generator generator; + + return uuids::to_string(generator()); +} + uint64_t Random() { std::random_device device; diff --git a/src/helpers/helpers.h b/src/helpers/helpers.h index 3169d5b..e1def2f 100644 --- a/src/helpers/helpers.h +++ b/src/helpers/helpers.h @@ -14,5 +14,7 @@ std::string ToHex(std::byte* src, size_t len); std::string HashPassword(const std::string& password); +std::string GenerateUUID(); + uint64_t Random(); } // namespace uad