Add username/password to Hello message

This commit is contained in:
badaix 2025-02-06 21:11:38 +01:00
parent 3cb2b4dd93
commit 49d6d1c58f
6 changed files with 40 additions and 4 deletions

View file

@ -66,6 +66,10 @@ struct ClientSettings
std::string protocol{"tcp"};
/// server port
size_t port{1704};
/// username
std::optional<std::string> username;
/// password
std::optional<std::string> password;
/// server certificate
std::optional<std::filesystem::path> server_certificate;
/// Certificate file

View file

@ -454,7 +454,7 @@ void Controller::worker()
settings_.host_id = ::getHostId(macAddress);
// Say hello to the server
auto hello = std::make_shared<msg::Hello>(macAddress, settings_.host_id, settings_.instance);
auto hello = std::make_shared<msg::Hello>(macAddress, settings_.host_id, settings_.instance, settings_.server.username, settings_.server.password);
clientConnection_->sendRequest<msg::ServerSettings>(
hello, 2s, [this](const boost::system::error_code& ec, std::unique_ptr<msg::ServerSettings> response) mutable
{

View file

@ -368,6 +368,10 @@ int main(int argc, char** argv)
throw SnapException("Snapclient is built without wss support");
#endif
}
if (!uri.user.empty())
settings.server.username = uri.user;
if (!uri.password.empty())
settings.server.password = uri.password;
}
if (server_cert_opt->is_set())

View file

@ -24,6 +24,7 @@
#include "json_message.hpp"
// standard headers
#include <optional>
#include <string>
@ -41,7 +42,8 @@ public:
}
/// c'tor taking @p macAddress, @p id and @p instance
Hello(const std::string& mac_address, const std::string& id, size_t instance) : JsonMessage(message_type::kHello)
Hello(const std::string& mac_address, const std::string& id, size_t instance, std::optional<std::string> username, std::optional<std::string> password)
: JsonMessage(message_type::kHello)
{
msg["MAC"] = mac_address;
msg["HostName"] = ::getHostName();
@ -51,6 +53,10 @@ public:
msg["Arch"] = ::getArch();
msg["Instance"] = instance;
msg["ID"] = id;
if (username.has_value())
msg["Username"] = username.value();
if (password.has_value())
msg["Password"] = password.value();
msg["SnapStreamProtocolVersion"] = 2;
}
@ -122,6 +128,22 @@ public:
}
return id;
}
/// @return the username
std::optional<std::string> getUsername() const
{
if (!msg.contains("Username"))
return std::nullopt;
return msg["Username"];
}
/// @return the password
std::optional<std::string> getPassword() const
{
if (!msg.contains("Password"))
return std::nullopt;
return msg["Password"];
}
};
} // namespace msg

View file

@ -118,11 +118,15 @@ Sample JSON payload (whitespace added for readability):
"Instance": 1,
"MAC": "00:11:22:33:44:55",
"OS": "Arch Linux",
"Username": "Badaix",
"Password": "$ecret",
"SnapStreamProtocolVersion": 2,
"Version": "0.17.1"
}
```
The fields `Username` and `Password` are optional and only used if authentication and authorization is enabled on the server.
### Client Info
| Field | Type | Description |

View file

@ -310,8 +310,10 @@ void Server::onMessageReceived(StreamSession* streamSession, const msg::BaseMess
streamSession->clientId = helloMsg.getUniqueId();
LOG(INFO, LOG_TAG) << "Hello from " << streamSession->clientId << ", host: " << helloMsg.getHostName() << ", v" << helloMsg.getVersion()
<< ", ClientName: " << helloMsg.getClientName() << ", OS: " << helloMsg.getOS() << ", Arch: " << helloMsg.getArch()
<< ", Protocol version: " << helloMsg.getProtocolVersion() << "\n";
<< ", Protocol version: " << helloMsg.getProtocolVersion() << ", Userrname: " << helloMsg.getUsername().value_or("<not set>")
<< ", Password: " << (helloMsg.getPassword().has_value() ? "<password is set>" : "<not set>") << "\n";
streamSession->stop();
return;
bool newGroup(false);
GroupPtr group = Config::instance().getGroupFromClient(streamSession->clientId);
if (group == nullptr)