diff --git a/services/tunnelbroker/src/Database/DeviceSessionItem.cpp b/services/tunnelbroker/src/Database/DeviceSessionItem.cpp index ac8b642d4..40de62be0 100644 --- a/services/tunnelbroker/src/Database/DeviceSessionItem.cpp +++ b/services/tunnelbroker/src/Database/DeviceSessionItem.cpp @@ -1,113 +1,118 @@ #include "DeviceSessionItem.h" #include "ConfigManager.h" +#include "Tools.h" + +#include namespace comm { namespace network { namespace database { const std::string DeviceSessionItem::FIELD_SESSION_ID = "SessionId"; const std::string DeviceSessionItem::FIELD_DEVICE_ID = "DeviceId"; const std::string DeviceSessionItem::FIELD_PUBKEY = "PubKey"; const std::string DeviceSessionItem::FIELD_NOTIFY_TOKEN = "NotifyToken"; const std::string DeviceSessionItem::FIELD_DEVICE_TYPE = "DeviceType"; const std::string DeviceSessionItem::FIELD_APP_VERSION = "AppVersion"; const std::string DeviceSessionItem::FIELD_DEVICE_OS = "DeviceOS"; const std::string DeviceSessionItem::FIELD_CHECKPOINT_TIME = "CheckpointTime"; const std::string DeviceSessionItem::FIELD_EXPIRE = "Expire"; DeviceSessionItem::DeviceSessionItem( const std::string sessionID, const std::string deviceID, const std::string pubKey, const std::string notifyToken, const std::string deviceType, const std::string appVersion, const std::string deviceOs) : sessionID(sessionID), deviceID(deviceID), pubKey(pubKey), notifyToken(notifyToken), deviceType(deviceType), appVersion(appVersion), deviceOs(deviceOs) { this->validate(); } DeviceSessionItem::DeviceSessionItem(const AttributeValues &itemFromDB) { this->assignItemFromDatabase(itemFromDB); } void DeviceSessionItem::validate() const { - if (!this->sessionID.size()) { - throw std::runtime_error("Error: SessionID is empty."); - } + tools::checkIfNotEmpty("pubKey", this->pubKey); + tools::checkIfNotEmpty("notifyToken", this->notifyToken); + tools::checkIfNotEmpty("deviceType", this->deviceType); + tools::checkIfNotEmpty("appVersion", this->appVersion); + tools::checkIfNotEmpty("deviceOs", this->deviceOs); } void DeviceSessionItem::assignItemFromDatabase( const AttributeValues &itemFromDB) { try { this->sessionID = itemFromDB.at(DeviceSessionItem::FIELD_SESSION_ID).GetS(); this->deviceID = itemFromDB.at(DeviceSessionItem::FIELD_DEVICE_ID).GetS(); this->pubKey = itemFromDB.at(DeviceSessionItem::FIELD_PUBKEY).GetS(); this->notifyToken = itemFromDB.at(DeviceSessionItem::FIELD_NOTIFY_TOKEN).GetS(); this->deviceType = itemFromDB.at(DeviceSessionItem::FIELD_DEVICE_TYPE).GetS(); this->appVersion = itemFromDB.at(DeviceSessionItem::FIELD_APP_VERSION).GetS(); this->deviceOs = itemFromDB.at(DeviceSessionItem::FIELD_DEVICE_OS).GetS(); this->checkpointTime = std::stoll( std::string( itemFromDB.at(DeviceSessionItem::FIELD_CHECKPOINT_TIME).GetS()) .c_str()); } catch (std::logic_error &e) { throw std::runtime_error( "Invalid device session database value " + std::string(e.what())); } this->validate(); } std::string DeviceSessionItem::getTableName() const { return config::ConfigManager::getInstance().getParameter( config::ConfigManager::OPTION_DYNAMODB_SESSIONS_TABLE); } std::string DeviceSessionItem::getPrimaryKey() const { return DeviceSessionItem::FIELD_SESSION_ID; } std::string DeviceSessionItem::getSessionID() const { return this->sessionID; } std::string DeviceSessionItem::getDeviceID() const { return this->deviceID; } std::string DeviceSessionItem::getPubKey() const { return this->pubKey; } std::string DeviceSessionItem::getNotifyToken() const { return this->notifyToken; } std::string DeviceSessionItem::getDeviceType() const { return this->deviceType; } std::string DeviceSessionItem::getAppVersion() const { return this->appVersion; } std::string DeviceSessionItem::getDeviceOs() const { return this->deviceOs; } long long DeviceSessionItem::getCheckpointTime() const { return this->checkpointTime; } } // namespace database } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/Database/MessageItem.cpp b/services/tunnelbroker/src/Database/MessageItem.cpp index bc2ec76fc..41aa9bedb 100644 --- a/services/tunnelbroker/src/Database/MessageItem.cpp +++ b/services/tunnelbroker/src/Database/MessageItem.cpp @@ -1,101 +1,94 @@ #include "MessageItem.h" #include "ConfigManager.h" +#include "Tools.h" + +#include namespace comm { namespace network { namespace database { const std::string MessageItem::FIELD_MESSAGE_ID = "MessageID"; const std::string MessageItem::FIELD_FROM_DEVICE_ID = "FromDeviceID"; const std::string MessageItem::FIELD_TO_DEVICE_ID = "ToDeviceID"; const std::string MessageItem::FIELD_PAYLOAD = "Payload"; const std::string MessageItem::FIELD_BLOB_HASHES = "BlobHashes"; const std::string MessageItem::FIELD_EXPIRE = "Expire"; MessageItem::MessageItem( const std::string messageID, const std::string fromDeviceID, const std::string toDeviceID, const std::string payload, const std::string blobHashes, const uint64_t expire) : messageID(messageID), fromDeviceID(fromDeviceID), toDeviceID(toDeviceID), payload(payload), blobHashes(blobHashes), expire(expire) { this->validate(); } MessageItem::MessageItem(const AttributeValues &itemFromDB) { this->assignItemFromDatabase(itemFromDB); } void MessageItem::validate() const { - if (!this->messageID.size()) { - throw std::runtime_error("Error: messageID is empty"); - } - if (!this->fromDeviceID.size()) { - throw std::runtime_error("Error: fromDeviceID is empty"); - } - if (!this->toDeviceID.size()) { - throw std::runtime_error("Error: toDeviceID is empty"); - } - if (!this->expire == 0) { - throw std::runtime_error("Error: expire field not provided"); - } + tools::checkIfNotEmpty("messageID", this->messageID); + tools::checkIfNotZero("expire", this->expire); } void MessageItem::assignItemFromDatabase(const AttributeValues &itemFromDB) { try { this->messageID = itemFromDB.at(MessageItem::FIELD_MESSAGE_ID).GetS(); this->fromDeviceID = itemFromDB.at(MessageItem::FIELD_FROM_DEVICE_ID).GetS(); this->toDeviceID = itemFromDB.at(MessageItem::FIELD_TO_DEVICE_ID).GetS(); this->payload = itemFromDB.at(MessageItem::FIELD_PAYLOAD).GetS(); this->blobHashes = itemFromDB.at(MessageItem::FIELD_BLOB_HASHES).GetS(); this->expire = std::stoull(itemFromDB.at(MessageItem::FIELD_EXPIRE).GetN()); } catch (const std::exception &e) { throw std::runtime_error( "Got an exception at MessageItem: " + std::string(e.what())); } this->validate(); } std::string MessageItem::getTableName() const { return config::ConfigManager::getInstance().getParameter( config::ConfigManager::OPTION_DYNAMODB_MESSAGES_TABLE); } std::string MessageItem::getPrimaryKey() const { return MessageItem::FIELD_MESSAGE_ID; } std::string MessageItem::getMessageID() const { return this->messageID; } std::string MessageItem::getFromDeviceID() const { return this->fromDeviceID; } std::string MessageItem::getToDeviceID() const { return this->toDeviceID; } std::string MessageItem::getPayload() const { return this->payload; } std::string MessageItem::getBlobHashes() const { return this->blobHashes; } uint64_t MessageItem::getExpire() const { return this->expire; } } // namespace database } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/Database/PublicKeyItem.cpp b/services/tunnelbroker/src/Database/PublicKeyItem.cpp index 1b9eb4dfe..a0f6f02a9 100644 --- a/services/tunnelbroker/src/Database/PublicKeyItem.cpp +++ b/services/tunnelbroker/src/Database/PublicKeyItem.cpp @@ -1,61 +1,57 @@ #include "PublicKeyItem.h" #include "ConfigManager.h" +#include "Tools.h" namespace comm { namespace network { namespace database { const std::string PublicKeyItem::FIELD_DEVICE_ID = "DeviceId"; const std::string PublicKeyItem::FIELD_PUBLIC_KEY = "PublicKey"; PublicKeyItem::PublicKeyItem( const std::string deviceID, const std::string publicKey) : deviceID(deviceID), publicKey(publicKey) { this->validate(); } PublicKeyItem::PublicKeyItem(const AttributeValues &itemFromDB) { this->assignItemFromDatabase(itemFromDB); } void PublicKeyItem::validate() const { - if (!this->deviceID.size()) { - throw std::runtime_error("Error: DeviceID is empty"); - } - if (!this->publicKey.size()) { - throw std::runtime_error("Error: PublicKey is empty"); - } + tools::checkIfNotEmpty("publicKey", this->publicKey); } void PublicKeyItem::assignItemFromDatabase(const AttributeValues &itemFromDB) { try { this->publicKey = itemFromDB.at(PublicKeyItem::FIELD_PUBLIC_KEY).GetS(); this->deviceID = itemFromDB.at(PublicKeyItem::FIELD_DEVICE_ID).GetS(); } catch (const std::exception &e) { throw std::runtime_error( "Got an exception at PublicKeyItem: " + std::string(e.what())); } this->validate(); } std::string PublicKeyItem::getTableName() const { return config::ConfigManager::getInstance().getParameter( config::ConfigManager::OPTION_DYNAMODB_SESSIONS_PUBLIC_KEY_TABLE); } std::string PublicKeyItem::getPrimaryKey() const { return PublicKeyItem::FIELD_DEVICE_ID; } std::string PublicKeyItem::getDeviceID() const { return this->deviceID; } std::string PublicKeyItem::getPublicKey() const { return this->publicKey; } } // namespace database } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/Database/SessionSignItem.cpp b/services/tunnelbroker/src/Database/SessionSignItem.cpp index d80e43234..a127b2df2 100644 --- a/services/tunnelbroker/src/Database/SessionSignItem.cpp +++ b/services/tunnelbroker/src/Database/SessionSignItem.cpp @@ -1,65 +1,61 @@ #include "SessionSignItem.h" #include "ConfigManager.h" +#include "Tools.h" namespace comm { namespace network { namespace database { const std::string SessionSignItem::FIELD_SESSION_VERIFICATION = "VerificationMessage"; const std::string SessionSignItem::FIELD_DEVICE_ID = "DeviceId"; const std::string SessionSignItem::FIELD_EXPIRE = "Expire"; SessionSignItem::SessionSignItem( const std::string sign, const std::string deviceID) : sign(sign), deviceID(deviceID) { this->validate(); } SessionSignItem::SessionSignItem(const AttributeValues &itemFromDB) { this->assignItemFromDatabase(itemFromDB); } void SessionSignItem::validate() const { - if (!this->deviceID.size()) { - throw std::runtime_error("Error: DeviceID is empty"); - } - if (!this->sign.size()) { - throw std::runtime_error("Error: Sign is empty"); - } + tools::checkIfNotEmpty("sign", this->sign); } void SessionSignItem::assignItemFromDatabase( const AttributeValues &itemFromDB) { try { this->sign = itemFromDB.at(SessionSignItem::FIELD_SESSION_VERIFICATION).GetS(); this->deviceID = itemFromDB.at(SessionSignItem::FIELD_DEVICE_ID).GetS(); } catch (const std::exception &e) { throw std::runtime_error( "Got an exception at SessionSignItem: " + std::string(e.what())); } this->validate(); } std::string SessionSignItem::getTableName() const { return config::ConfigManager::getInstance().getParameter( config::ConfigManager::OPTION_DYNAMODB_SESSIONS_VERIFICATION_TABLE); } std::string SessionSignItem::getPrimaryKey() const { return SessionSignItem::FIELD_DEVICE_ID; } std::string SessionSignItem::getSign() const { return this->sign; } std::string SessionSignItem::getDeviceID() const { return this->deviceID; } } // namespace database } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/Tools/Tools.cpp b/services/tunnelbroker/src/Tools/Tools.cpp index 3cb95012f..2bc665428 100644 --- a/services/tunnelbroker/src/Tools/Tools.cpp +++ b/services/tunnelbroker/src/Tools/Tools.cpp @@ -1,73 +1,87 @@ #include "Tools.h" #include "ConfigManager.h" #include "Constants.h" #include #include #include #include #include #include #include #include namespace comm { namespace network { namespace tools { std::string generateRandomString(std::size_t length) { const std::string CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; thread_local std::random_device generator; std::uniform_int_distribution<> distribution(0, CHARACTERS.size() - 1); std::string random_string; for (std::size_t i = 0; i < length; ++i) { random_string += CHARACTERS[distribution(generator)]; } return random_string; } long long getCurrentTimestamp() { using namespace std::chrono; return duration_cast(system_clock::now().time_since_epoch()) .count(); } bool validateDeviceID(std::string deviceID) { try { static const std::regex deviceIDKeyserverRegexp("^ks:.*"); if (std::regex_match(deviceID, deviceIDKeyserverRegexp)) { return ( deviceID == config::ConfigManager::getInstance().getParameter( config::ConfigManager::OPTION_DEFAULT_KEYSERVER_ID)); } return std::regex_match(deviceID, DEVICEID_FORMAT_REGEX); } catch (const std::exception &e) { std::cout << "Tools: " << "Got an exception at `validateDeviceID`: " << e.what() << std::endl; return false; } } std::string generateUUID() { thread_local boost::uuids::random_generator random_generator; return boost::uuids::to_string(random_generator()); } bool validateSessionID(std::string sessionID) { try { return std::regex_match(sessionID, SESSION_ID_FORMAT_REGEX); } catch (const std::exception &e) { std::cout << "Tools: " << "Got an exception at `validateSessionId`: " << e.what() << std::endl; return false; } } +void checkIfNotEmpty(std::string fieldName, std::string stringToCheck) { + if (stringToCheck.empty()) { + throw std::runtime_error( + "Error: Required text field " + fieldName + " is empty."); + } +} + +void checkIfNotZero(std::string fieldName, uint64_t numberToCheck) { + if (numberToCheck == 0) { + throw std::runtime_error( + "Error: Required number " + fieldName + " is zero."); + } +} + } // namespace tools } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/Tools/Tools.h b/services/tunnelbroker/src/Tools/Tools.h index 0aeccb61c..eb73aa486 100644 --- a/services/tunnelbroker/src/Tools/Tools.h +++ b/services/tunnelbroker/src/Tools/Tools.h @@ -1,18 +1,20 @@ #pragma once #include #include namespace comm { namespace network { namespace tools { std::string generateRandomString(std::size_t length); long long getCurrentTimestamp(); bool validateDeviceID(std::string deviceID); std::string generateUUID(); bool validateSessionID(std::string sessionID); +void checkIfNotEmpty(std::string fieldName, std::string stringToCheck); +void checkIfNotZero(std::string fieldName, uint64_t numberToCheck); } // namespace tools } // namespace network } // namespace comm