minor fixing
This commit is contained in:
parent
a2a24f347c
commit
35422db397
@ -19,6 +19,7 @@
|
||||
namespace SE
|
||||
{
|
||||
|
||||
|
||||
class TAutorizatorInterface
|
||||
{
|
||||
protected:
|
||||
@ -37,6 +38,8 @@ protected:
|
||||
static std::vector<std::string> 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<void()> ErrorSignal;
|
||||
boost::signal<void(std::string)> AllowedSignal;
|
||||
boost::signal<void()> DeniedSignal;
|
||||
//boost::signal<void()> DeniedSignal; //Simple autorizator does not deny
|
||||
};
|
||||
|
||||
typedef boost::variant<std::shared_ptr<TSimpleAutorizator>> TAuthorizatorVariant;
|
||||
|
@ -165,6 +165,8 @@ void TSimpleAuthorization::Authorize()
|
||||
|
||||
dataReader->ErrorSignal.connect(ErrorSignal);
|
||||
|
||||
if (Login == "")
|
||||
{
|
||||
boost::property_tree::ptree pt;
|
||||
|
||||
pt.put("Hello", "");
|
||||
@ -173,6 +175,18 @@ void TSimpleAuthorization::Authorize()
|
||||
|
||||
dataReader->StartReadOnce();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
boost::property_tree::ptree pt;
|
||||
|
||||
pt.put("HelloAgain.Login", Login);
|
||||
pt.put("HelloAgain.Password", Password);
|
||||
|
||||
SendPropertyTree(IoService, Socket, pt);
|
||||
|
||||
dataReader->StartReadOnce();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -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<unsigned int>(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,7 +44,8 @@ namespace SE
|
||||
{
|
||||
boost::shared_ptr<TDataReader> 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);
|
||||
|
||||
@ -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<std::string>(UserPasswords.size());
|
||||
std::string Password = "12345";
|
||||
CreateNewUser();
|
||||
}
|
||||
|
||||
void TSimpleAutorizator::HandleGetLoginRequest(boost::property_tree::ptree pTree)
|
||||
{
|
||||
std::string login = pTree.get<std::string>("Login");
|
||||
std::string password = pTree.get<std::string>("Password");
|
||||
|
||||
int loginNum = boost::lexical_cast<int>(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<std::string>(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();
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user