mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
Switched to binary pubkeys from base64 for internal user data (#29)
* String copy optmisations. * User pubkey binary.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/websocket.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include <sodium.h>
|
||||
#include "../util.hpp"
|
||||
#include "../sock/socket_session.hpp"
|
||||
#include "usr.hpp"
|
||||
@@ -62,17 +63,26 @@ void user_session_handler::on_message(sock::socket_session *session, std::string
|
||||
{
|
||||
// Challenge singature verification successful.
|
||||
|
||||
// Decode b64 pubkey and get binary pubkey. We area only going to keep
|
||||
// the binary pubkey due to reduced memory footprint.
|
||||
std::string userpubkey;
|
||||
userpubkey.resize(crypto_sign_PUBLICKEYBYTES);
|
||||
util::base64_decode(
|
||||
reinterpret_cast<unsigned char *>(userpubkey.data()),
|
||||
userpubkey.length(),
|
||||
userpubkeyb64);
|
||||
|
||||
// Now check whether this user public key is duplicate.
|
||||
if (usr::sessionids.count(userpubkeyb64) == 0)
|
||||
if (usr::sessionids.count(userpubkey) == 0)
|
||||
{
|
||||
// All good. Unique public key.
|
||||
// Promote the connection from pending-challenges to authenticated users.
|
||||
|
||||
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_, userpubkeyb64); // Add the user to the global authed user list
|
||||
usr::add_user(session->uniqueid_, 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 "
|
||||
<< userpubkeyb64 << std::endl;
|
||||
return;
|
||||
@@ -100,10 +110,10 @@ void user_session_handler::on_message(sock::socket_session *session, std::string
|
||||
// This is an authed user.
|
||||
usr::connected_user &user = itr->second;
|
||||
|
||||
//Hand over the bytes into user inbuffer.
|
||||
//Append the bytes into connected user input buffer.
|
||||
user.inbuffer.append(message);
|
||||
|
||||
std::cout << "Collected " << user.inbuffer.length() << " bytes from user " << user.pubkeyb64 << std::endl;
|
||||
std::cout << "Collected " << user.inbuffer.length() << " bytes from user" << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ std::unordered_map<std::string, usr::connected_user> users;
|
||||
|
||||
/**
|
||||
* Holds set of connected user session ids for lookups. (Exposed to other sub systems)
|
||||
* Map key: User pubkey
|
||||
* Map key: User binary pubkey
|
||||
*/
|
||||
std::unordered_map<std::string, std::string> sessionids;
|
||||
|
||||
@@ -197,10 +197,10 @@ int verify_user_challenge_response(std::string &extracted_pubkeyb64, std::string
|
||||
* This should get called after the challenge handshake is verified.
|
||||
*
|
||||
* @param sessionid User socket session id.
|
||||
* @param pubkeyb64 User's base64 public key.
|
||||
* @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 &pubkeyb64)
|
||||
int add_user(const std::string &sessionid, const std::string &pubkey)
|
||||
{
|
||||
if (users.count(sessionid) == 1)
|
||||
{
|
||||
@@ -208,10 +208,10 @@ int add_user(const std::string &sessionid, const std::string &pubkeyb64)
|
||||
return -1;
|
||||
}
|
||||
|
||||
users.emplace(sessionid, usr::connected_user(pubkeyb64));
|
||||
users.emplace(sessionid, usr::connected_user(pubkey));
|
||||
|
||||
// Populate sessionid map so we can lookup by user pubkey.
|
||||
sessionids.emplace(pubkeyb64, sessionid);
|
||||
sessionids[pubkey] = sessionid;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -235,7 +235,7 @@ int remove_user(const std::string &sessionid)
|
||||
|
||||
usr::connected_user &user = itr->second;
|
||||
|
||||
sessionids.erase(user.pubkeyb64);
|
||||
sessionids.erase(user.pubkey);
|
||||
users.erase(itr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -18,15 +18,18 @@ namespace usr
|
||||
*/
|
||||
struct connected_user
|
||||
{
|
||||
// Base64 user public key
|
||||
std::string pubkeyb64;
|
||||
|
||||
// User binary public key
|
||||
std::string pubkey;
|
||||
|
||||
// Holds the unprocessed user input collected from websocket.
|
||||
std::string inbuffer;
|
||||
|
||||
connected_user(std::string_view _pubkeyb64)
|
||||
/**
|
||||
* @param _pubkey The public key of the user in binary format.
|
||||
*/
|
||||
connected_user(std::string_view _pubkey)
|
||||
{
|
||||
pubkeyb64 = _pubkeyb64;
|
||||
pubkey = _pubkey;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -54,7 +57,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 &pubkeyb64);
|
||||
int add_user(const std::string &sessionid, const std::string &pubkey);
|
||||
|
||||
int remove_user(const std::string &sessionid);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user