Implemented sending contract output back to the user.

This commit is contained in:
Ravin Perera
2019-10-16 17:26:33 +05:30
parent 8b003aeaa2
commit 8a22748c8d
7 changed files with 121 additions and 44 deletions

View File

@@ -6,6 +6,7 @@
#include <sodium.h>
#include "../util.hpp"
#include "../sock/socket_session.hpp"
#include "../proc.hpp"
#include "usr.hpp"
#include "user_session_handler.hpp"
@@ -80,7 +81,7 @@ void user_session_handler::on_message(sock::socket_session *session, std::string
session->flags_.reset(util::SESSION_FLAG::USER_CHALLENGE_ISSUED); // Clear challenge-issued flag
session->flags_.set(util::SESSION_FLAG::USER_AUTHED); // Set the user-authed flag
usr::add_user(session->uniqueid_, userpubkey); // Add the user to the global authed user list
usr::add_user(session, userpubkey); // Add the user to the global authed user list
usr::pending_challenges.erase(session->uniqueid_); // Remove the stored challenge
std::cout << "User connection " << session->uniqueid_ << " authenticated. Public key "
@@ -138,6 +139,8 @@ void user_session_handler::on_close(sock::socket_session *session)
// Session belongs to an authed user.
else if (session->flags_[util::SESSION_FLAG::USER_AUTHED])
{
// Wait for SC process completion before we remove existing user.
proc::await_contract_execution();
usr::remove_user(session->uniqueid_);
}

View File

@@ -16,13 +16,14 @@ namespace usr
{
/**
* Global user list. (Exposed to other sub systems)
* Connected (authenticated) user list. (Exposed to other sub systems)
* Map key: User socket session id (<ip:port>)
*/
std::unordered_map<std::string, usr::connected_user> users;
/**
* Holds set of connected user session ids for lookups. (Exposed to other sub systems)
* Holds set of connected user session ids and public keys for lookups.
* This is used for pubkey duplicate checks as well.
* Map key: User binary pubkey
*/
std::unordered_map<std::string, std::string> sessionids;
@@ -196,19 +197,20 @@ int verify_user_challenge_response(std::string &extracted_pubkeyb64, std::string
* Adds the user denoted by specified session id and public key to the global authed user list.
* This should get called after the challenge handshake is verified.
*
* @param sessionid User socket session id.
* @param session User socket session.
* @param pubkey User's binary public key.
* @return 0 on successful additions. -1 on failure.
*/
int add_user(const std::string &sessionid, const std::string &pubkey)
int add_user(sock::socket_session *session, const std::string &pubkey)
{
const std::string &sessionid = session->uniqueid_;
if (users.count(sessionid) == 1)
{
std::cerr << sessionid << " already exist. Cannot add user.\n";
return -1;
}
users.emplace(sessionid, usr::connected_user(pubkey));
users.emplace(sessionid, usr::connected_user(session, pubkey));
// Populate sessionid map so we can lookup by user pubkey.
sessionids[pubkey] = sessionid;

View File

@@ -5,6 +5,7 @@
#include <string_view>
#include <unordered_map>
#include "../util.hpp"
#include "../sock/socket_session.hpp"
/**
* Maintains the global user list with pending input outputs and manages user connections.
@@ -24,17 +25,23 @@ struct connected_user
// Holds the unprocessed user input collected from websocket.
std::string inbuffer;
// Holds the websocket session of this user.
// We don't need to own the session object since the lifetime of user and session are coupled.
sock::socket_session *session;
/**
* @param _pubkey The public key of the user in binary format.
*/
connected_user(std::string_view _pubkey)
connected_user(sock::socket_session *_session, std::string_view _pubkey)
{
session = _session;
pubkey = _pubkey;
}
};
/**
* Global authenticated (challenge-verified) user list.
* Connected (authenticated) user list. (Exposed to other sub systems)
* Map key: User socket session id (<ip:port>)
*/
extern std::unordered_map<std::string, usr::connected_user> users;
@@ -57,7 +64,7 @@ void create_user_challenge(std::string &msg, std::string &challengeb64);
int verify_user_challenge_response(std::string &extracted_pubkeyb64, std::string_view response, std::string_view original_challenge);
int add_user(const std::string &sessionid, const std::string &pubkey);
int add_user(sock::socket_session *session, const std::string &pubkey);
int remove_user(const std::string &sessionid);