From c513e4db1cd7fdbecd59c6d385bc5f970827401e Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 5 Dec 2013 15:37:09 +0100 Subject: Add login/logout. --- src/connectionhandler.cc | 17 +++++++++++++++++ src/connectionhandler.h | 7 +++++++ src/message.h | 10 +++++++++- src/messagehandler.cc | 15 +++++++++++++++ src/messageparser.cc | 22 ++++++++++++++++++++-- 5 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/connectionhandler.cc b/src/connectionhandler.cc index d78c053..fe83861 100644 --- a/src/connectionhandler.cc +++ b/src/connectionhandler.cc @@ -56,6 +56,23 @@ void ConnectionHandler::close(clientid_t clientid) DEBUG(conn, "Removed connection\n"); } +void ConnectionHandler::login(clientid_t clientid, std::string user, std::string password) +{ + authlist[clientid] = (password == "hundemad"); + DEBUG(conn, "Authentication %d\n", authlist[clientid]); +} + +void ConnectionHandler::logout(clientid_t clientid) +{ + authlist[clientid] = false; + DEBUG(conn, "Authentication %d\n", authlist[clientid]); +} + +bool ConnectionHandler::authenticated(clientid_t clientid) +{ + return authlist.find(clientid) != authlist.end() && authlist[clientid]; +} + void ConnectionHandler::observe(clientid_t clientid, taskid_t taskid) { connlist[clientid].insert(taskid); diff --git a/src/connectionhandler.h b/src/connectionhandler.h index 7779b1f..402f531 100644 --- a/src/connectionhandler.h +++ b/src/connectionhandler.h @@ -38,6 +38,7 @@ typedef struct libwebsocket* clientid_t; +typedef std::map AuthList; typedef std::map > ConnectionList; typedef std::list > ObserverList; @@ -47,6 +48,11 @@ public: void init(clientid_t clientid); void close(clientid_t clientid); + + void login(clientid_t clientid, std::string user, std::string password); + void logout(clientid_t clientid); + bool authenticated(clientid_t clientid); + void observe(clientid_t clientid, taskid_t taskid); void unobserve(clientid_t clientid, taskid_t taskid); @@ -55,6 +61,7 @@ public: private: ConnectionList connlist; + AuthList authlist; }; // Global ConnectionHandler diff --git a/src/message.h b/src/message.h index 92e58ac..6e1804d 100644 --- a/src/message.h +++ b/src/message.h @@ -41,10 +41,17 @@ namespace cmd { move, create, remove, - error + login, + logout, + error, } cmd_t; }; +typedef struct { + std::string user; + std::string password; +} login_t; + typedef struct { taskid_t id; } observe_t; @@ -83,6 +90,7 @@ typedef struct { remove_t remove; move_t move; update_t update; + login_t login; TaskIdList nodes; diff --git a/src/messagehandler.cc b/src/messagehandler.cc index d3c3420..c946bbe 100644 --- a/src/messagehandler.cc +++ b/src/messagehandler.cc @@ -44,6 +44,13 @@ MessageList handle_msg(MessageList msgList, clientid_t wsi) { it++) { message_t &m = *it; + if(m.cmd == cmd::login) { + connection_handler.login(wsi, m.login.user, m.login.password); + } + + // If client is not authenticated; do not continue beyond this point... + if(!connection_handler.authenticated(wsi)) continue; + switch(m.cmd) { case cmd::create: { @@ -166,6 +173,14 @@ MessageList handle_msg(MessageList msgList, clientid_t wsi) { } break; + case cmd::login: + // Already handled, before this switch ... case + break; + + case cmd::logout: + connection_handler.logout(wsi); + break; + case cmd::error: ERR(messagehandler, "An error occurred.\n"); break; diff --git a/src/messageparser.cc b/src/messageparser.cc index 4f047d9..409ffeb 100644 --- a/src/messageparser.cc +++ b/src/messageparser.cc @@ -94,7 +94,7 @@ inline static void parse_into_msg_tokens(std::string& data, tokenVector.clear(); } - + /* // WARNING: Enabling this will print passwords in plaintext... // Debug: print tokens DEBUG(parser, "tokenlists: %d\n", msgTokensList.size()); MsgTokensList::iterator j = msgTokensList.begin(); @@ -108,7 +108,7 @@ inline static void parse_into_msg_tokens(std::string& data, } j++; } - + */ } static taskid_t getId(std::map &sym, std::string token) @@ -156,6 +156,8 @@ inline static void create_msg_list(MsgTokensList& msgTokensList, if(t[origin] == "observe") m.cmd = cmd::observe; else if(t[origin] == "unobserve") m.cmd = cmd::unobserve; + else if(t[origin] == "login") m.cmd = cmd::login; + else if(t[origin] == "logout") m.cmd = cmd::logout; else if(t[origin] == "create") m.cmd = cmd::create; else if(t[origin] == "remove") m.cmd = cmd::remove; else if(t[origin] == "move") m.cmd = cmd::move; @@ -180,6 +182,22 @@ inline static void create_msg_list(MsgTokensList& msgTokensList, DEBUG(msgparser, "unobserve @ %d\n", m.unobserve.id); break; } + case cmd::login: { + if(t.size() != origin+1+2) { + DEBUG(msgparser, "Wrong number of parameters\n"); + continue; + } + m.login.user = t[origin+1]; + m.login.password = t[origin+2]; + DEBUG(msgparser, "login %s\n", m.login.user.c_str()); + break; + } + case cmd::logout: + if(t.size() != origin+1) { + DEBUG(msgparser, "Wrong number of parameters\n"); + continue; + } + break; case cmd::create: { if(!clientmode && t.size() != origin+1+2) { DEBUG(msgparser, "Wrong number of parameters\n"); -- cgit v1.2.3