summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
authorGluzskiy Alexandr <sss@sss.chaoslab.ru>2015-08-11 14:29:43 +0300
committerGluzskiy Alexandr <sss@sss.chaoslab.ru>2015-08-11 14:29:43 +0300
commit04edaab436dae1747270d7d10c0586a2a4c222bf (patch)
treee8c059076ec341861c734ce2efd0d8afef431a33 /server/src
parent1aaddd285d1c3ba2ba99eef006062a32c4dedafc (diff)
missed changes
Diffstat (limited to 'server/src')
-rw-r--r--server/src/main.cpp5
-rw-r--r--server/src/server_session.cpp59
-rw-r--r--server/src/utilities.cpp22
3 files changed, 85 insertions, 1 deletions
diff --git a/server/src/main.cpp b/server/src/main.cpp
index 3d3d0c4..30126fa 100644
--- a/server/src/main.cpp
+++ b/server/src/main.cpp
@@ -21,6 +21,7 @@
#include <boost/filesystem.hpp>
#include <boost/asio.hpp>
#include <iostream>
+#include <map>
#include "api_module_metadata_storage.h"
#include "modules_handler.h"
@@ -28,6 +29,7 @@
#include "server.h"
#include "main.h"
#include "config.h"
+#include "client.h"
core_api *module_api = nullptr;
@@ -36,6 +38,7 @@ modules_handler *modules = nullptr;
namespace bpo = boost::program_options;
+std::map<std::string, client> clients; //auth token used for key
runtime_config_s runtime_config;
@@ -177,7 +180,7 @@ int main(int argc, char *argv[])
{
//TODO:
}
- //TODO: run in separate thread
+ //TODO: run in separate thread ?
boost::system::error_code ec;
io_service_server.run(ec);
if(ec)
diff --git a/server/src/server_session.cpp b/server/src/server_session.cpp
index 234f130..3b8c3b1 100644
--- a/server/src/server_session.cpp
+++ b/server/src/server_session.cpp
@@ -121,6 +121,65 @@ void server_session::handle_command(client_msg *msg)
{
switch(msg->type())
{
+ case CLIENT_MSG_TYPE::CLIENT_AUTH_REQUEST:
+ {
+ server_msg m;
+ m.set_type(SERVER_MSG_TYPE::SERVER_AUTH_REPLY);
+ //TODO: check for already existing auth token
+ std::string server_password = runtime_config.config_file.get<std::string>("server.password", "");
+ if(server_password.empty())
+ m.mutable_auth_reply()->set_status(true);
+ else
+ {
+ switch(msg->auth_info().hash_type())
+ {
+ case PASSWD_HASH_TYPE::HASH_NONE:
+ {
+ if(msg->auth_info().password() != server_password)
+ {
+ m.mutable_auth_reply()->set_status(false);
+ m.mutable_auth_reply()->set_error_description("wrong password");
+ }
+ else
+ m.mutable_auth_reply()->set_status(true);
+ }
+ break;
+ case PASSWD_HASH_TYPE::HASH_MD5:
+ {
+ //TODO:
+ }
+ break;
+ case PASSWD_HASH_TYPE::HASH_SHA2:
+ {
+ //TODO:
+ }
+ break;
+ case PASSWD_HASH_TYPE::HASH_SHA512:
+ {
+ unsigned char hash[64];
+ SHA512((unsigned char*)server_password.c_str(), server_password.length(), hash);
+ if(msg->auth_info().password() != std::string((const char*)hash))
+ {
+ m.mutable_auth_reply()->set_status(false);
+ m.mutable_auth_reply()->set_error_description("wrong password");
+ }
+ else
+ m.mutable_auth_reply()->set_status(true);
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ if(m.auth_reply().status() == true)
+ {
+ client_auth_token = generate_auth_token();
+ m.mutable_auth_reply()->set_auth_token(client_auth_token);
+ //set auth token
+ }
+ send_message(&m);
+ }
+ break;
default:
break;
}
diff --git a/server/src/utilities.cpp b/server/src/utilities.cpp
index ab10e30..c8510c9 100644
--- a/server/src/utilities.cpp
+++ b/server/src/utilities.cpp
@@ -20,6 +20,10 @@
#include <boost/log/trivial.hpp>
+#include <boost/random.hpp>
+#include <boost/nondet_random.hpp>
+
+
#include "utilities.h"
#include "../../protocol/udm.pb.h"
@@ -56,3 +60,21 @@ std::shared_ptr<char*> pack_msg(server_msg *msg, int *size_)
msg->SerializeToString(&msg_buf);
return pack_data(msg_buf, size_);
}
+
+std::string random_string(int length)
+{
+ BOOST_LOG_TRIVIAL(debug)<<__FILE__<<":"<<__LINE__<<"\t"<<__func__;
+ std::string chars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
+ std::string data;
+ boost::random_device rng;
+ boost::variate_generator<boost::random_device&, boost::uniform_int<>> gen(rng, boost::uniform_int<>(0, (int)chars.length()-1));
+ for(int i = 0; i < length; ++i)
+ data += chars[gen()];
+ return data;
+}
+
+std::string generate_auth_token()
+{
+ return random_string(64);
+}
+