From 35422db39756842ca8cc31893671421c544ced3a Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Mon, 27 May 2013 13:24:27 +0000 Subject: [PATCH] minor fixing --- include/Utils/Network/Server.h | 10 +++- src/Utils/Network/Network.cpp | 22 +++++++-- src/Utils/Network/Server.cpp | 89 +++++++++++++++++++++++++++++----- 3 files changed, 104 insertions(+), 17 deletions(-) diff --git a/include/Utils/Network/Server.h b/include/Utils/Network/Server.h index c446308..c3ce051 100644 --- a/include/Utils/Network/Server.h +++ b/include/Utils/Network/Server.h @@ -19,6 +19,7 @@ namespace SE { + class TAutorizatorInterface { protected: @@ -37,6 +38,8 @@ protected: static std::vector UserPasswords; static boost::mutex UserPasswordsMutex; + static std::string GenerateRandomPassword(); + public: boost::asio::io_service& IoService; @@ -47,11 +50,14 @@ public: virtual void StartListen(); - void HandleGetData(boost::property_tree::ptree pTree); + void HandleGetRegisterRequest(boost::property_tree::ptree pTree); + void HandleGetLoginRequest(boost::property_tree::ptree pTree); + + void CreateNewUser(); boost::signal ErrorSignal; boost::signal AllowedSignal; - boost::signal DeniedSignal; + //boost::signal DeniedSignal; //Simple autorizator does not deny }; typedef boost::variant> TAuthorizatorVariant; diff --git a/src/Utils/Network/Network.cpp b/src/Utils/Network/Network.cpp index 44ad742..c325208 100644 --- a/src/Utils/Network/Network.cpp +++ b/src/Utils/Network/Network.cpp @@ -165,14 +165,28 @@ void TSimpleAuthorization::Authorize() dataReader->ErrorSignal.connect(ErrorSignal); - boost::property_tree::ptree pt; + if (Login == "") + { + boost::property_tree::ptree pt; - pt.put("Hello", ""); + pt.put("Hello", ""); - SendPropertyTree(IoService, Socket, pt); + SendPropertyTree(IoService, Socket, pt); - dataReader->StartReadOnce(); + dataReader->StartReadOnce(); + } + else + { + boost::property_tree::ptree pt; + + pt.put("HelloAgain.Login", Login); + pt.put("HelloAgain.Password", Password); + + SendPropertyTree(IoService, Socket, pt); + + dataReader->StartReadOnce(); + } } diff --git a/src/Utils/Network/Server.cpp b/src/Utils/Network/Server.cpp index fb4cfb9..5dc9bc1 100644 --- a/src/Utils/Network/Server.cpp +++ b/src/Utils/Network/Server.cpp @@ -1,4 +1,5 @@ #include "include/Utils/Utils.h" +#include "boost/random.hpp" namespace SE @@ -8,6 +9,29 @@ namespace SE boost::mutex TSimpleAutorizator::UserPasswordsMutex; + std::string TSimpleAutorizator::GenerateRandomPassword() + { + boost::random::mt19937 RandomGenerator; + + RandomGenerator.seed(static_cast(std::time(0))); + + static const int passwordLength = 18; + static const std::string symbols = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!()-=_+[]{};:,.?"; + + std::string result; + + boost::random::uniform_int_distribution<> symbolRandom(0, symbols.size()-1); + + for (size_t i = 0; i < passwordLength; i++) + { + result += symbols[symbolRandom(RandomGenerator)]; + } + + return result; + + } + + TSimpleAutorizator::TSimpleAutorizator(boost::asio::io_service& ioService, boost::asio::ip::tcp::socket& socket) : IoService(ioService) , Socket(socket) @@ -20,8 +44,9 @@ namespace SE { boost::shared_ptr dataReader(new TDataReader(Socket)); - dataReader->DataReadSignalMap.AddSlot("Hello", boost::bind(&TSimpleAutorizator::HandleGetData, this, _1)); - + dataReader->DataReadSignalMap.AddSlot("Hello", boost::bind(&TSimpleAutorizator::HandleGetRegisterRequest, this, _1)); + dataReader->DataReadSignalMap.AddSlot("HelloAgain", boost::bind(&TSimpleAutorizator::HandleGetLoginRequest, this, _1)); + dataReader->ErrorSignal.connect(ErrorSignal); dataReader->StartReadOnce(); @@ -29,27 +54,69 @@ namespace SE } - void TSimpleAutorizator::HandleGetData(boost::property_tree::ptree pTree) + void TSimpleAutorizator::HandleGetRegisterRequest(boost::property_tree::ptree pTree) { - std::string Login = boost::lexical_cast(UserPasswords.size()); - std::string Password = "12345"; + CreateNewUser(); + } + + void TSimpleAutorizator::HandleGetLoginRequest(boost::property_tree::ptree pTree) + { + std::string login = pTree.get("Login"); + std::string password = pTree.get("Password"); + + int loginNum = boost::lexical_cast(login); + + bool loginIsValid = loginNum < UserPasswords.size(); + + if (!loginIsValid) + { + CreateNewUser(); + return; + } + + bool passwordIsValid = UserPasswords[loginNum] == password; + + if (!passwordIsValid) + { + CreateNewUser(); + return; + } + + //Login and password are ok + boost::property_tree::ptree pt; + + pt.put("OnHello.Login", login); + pt.put("OnHello.Password", password); + + SendPropertyTree(IoService, Socket, pt); + + AllowedSignal(login); + + } + + void TSimpleAutorizator::CreateNewUser() + { + + std::string login = boost::lexical_cast(UserPasswords.size()); + std::string password = GenerateRandomPassword(); UserPasswordsMutex.lock(); - UserPasswords.push_back(Password); + UserPasswords.push_back(password); UserPasswordsMutex.unlock(); boost::property_tree::ptree pt; - pt.put("OnHello.Login", Login); - pt.put("OnHello.Password", Password); + pt.put("OnHello.Login", login); + pt.put("OnHello.Password", password); SendPropertyTree(IoService, Socket, pt); - AllowedSignal(Login); - + AllowedSignal(login); } + //================================ + TConnectedUser::TConnectedUser(TServerSocket& server) : Server(server) @@ -90,7 +157,7 @@ namespace SE authorizator->ErrorSignal.disconnect_all_slots(); authorizator->AllowedSignal.disconnect_all_slots(); - authorizator->DeniedSignal.disconnect_all_slots(); + //authorizator->DeniedSignal.disconnect_all_slots(); }