From b959af26c40634b7c8f065814f99f199352dea98 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sat, 15 Mar 2025 23:04:18 -0500 Subject: [PATCH] refactor --- CHANGELOG.md | 2 +- README.md | 6 ++-- repertory/librepertory/include/app_config.hpp | 6 ++-- repertory/librepertory/include/common.hpp | 2 +- repertory/librepertory/include/rpc/common.hpp | 4 +-- .../librepertory/include/types/repertory.hpp | 3 +- repertory/librepertory/src/app_config.cpp | 29 ++++++++++++------- .../librepertory/src/utils/cli_utils.cpp | 2 +- .../repertory/include/ui/mgmt_app_config.hpp | 6 ++-- .../repertory/src/ui/mgmt_app_config.cpp | 4 +-- .../repertory_test/src/app_config_test.cpp | 10 +++---- web/repertory/lib/helpers.dart | 2 +- web/repertory/lib/widgets/mount_settings.dart | 2 +- 13 files changed, 44 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93acf11b..3bb33123 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,7 +31,7 @@ * Added Sia API version check prior to mounting * Added back `-cv` (check version) CLI option * Continue documentation updates -* Fixed setting `ApiAuth` via `set_value_by_name` +* Fixed setting `ApiPassword` via `set_value_by_name` * Fixed setting `HostConfig.ApiUser` via `set_value_by_name` * Fixed setting `HostConfig.Path` via `set_value_by_name` * Fixed setting `HostConfig.Protocol` via `set_value_by_name` diff --git a/README.md b/README.md index 0a757c4a..02f2ea99 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ Only 64-bit operating systems are supported ```json { - "ApiAuth": "", + "ApiPassword": "", "ApiPort": 10000, "ApiUser": "repertory", "DatabaseType": "rocksdb", @@ -220,7 +220,7 @@ Only 64-bit operating systems are supported ```json { - "ApiAuth": "", + "ApiPassword": "", "ApiPort": 10100, "ApiUser": "repertory", "DatabaseType": "rocksdb", @@ -360,7 +360,7 @@ for S3 providers. ```json { - "ApiAuth": "", + "ApiPassword": "", "ApiPort": 10010, "ApiUser": "repertory", "EnableDriveEvents": false, diff --git a/repertory/librepertory/include/app_config.hpp b/repertory/librepertory/include/app_config.hpp index 7d825e3e..359eb5da 100644 --- a/repertory/librepertory/include/app_config.hpp +++ b/repertory/librepertory/include/app_config.hpp @@ -73,7 +73,7 @@ public: private: provider_type prov_; - atomic api_auth_; + atomic api_password_; std::atomic api_port_; atomic api_user_; std::atomic config_changed_; @@ -123,7 +123,7 @@ private: auto set_value(dest &dst, const source &src) -> bool; public: - [[nodiscard]] auto get_api_auth() const -> std::string; + [[nodiscard]] auto get_api_password() const -> std::string; [[nodiscard]] auto get_api_port() const -> std::uint16_t; @@ -201,7 +201,7 @@ public: void save(); - void set_api_auth(const std::string &value); + void set_api_password(const std::string &value); void set_api_port(std::uint16_t value); diff --git a/repertory/librepertory/include/common.hpp b/repertory/librepertory/include/common.hpp index 2c5d6e40..7c92b326 100644 --- a/repertory/librepertory/include/common.hpp +++ b/repertory/librepertory/include/common.hpp @@ -57,7 +57,7 @@ using json = nlohmann::json; inline constexpr const std::string_view REPERTORY = "repertory"; inline constexpr const std::wstring_view REPERTORY_W = L"repertory"; -inline constexpr const std::uint64_t REPERTORY_CONFIG_VERSION = 1ULL; +inline constexpr const std::uint64_t REPERTORY_CONFIG_VERSION = 2ULL; inline constexpr const std::string_view REPERTORY_DATA_NAME = "repertory2"; inline constexpr const std::string_view REPERTORY_MIN_REMOTE_VERSION = "2.0.0"; diff --git a/repertory/librepertory/include/rpc/common.hpp b/repertory/librepertory/include/rpc/common.hpp index b7ed4bf5..fb2827a4 100644 --- a/repertory/librepertory/include/rpc/common.hpp +++ b/repertory/librepertory/include/rpc/common.hpp @@ -31,7 +31,7 @@ namespace repertory::rpc { const httplib::Request &req) -> bool { REPERTORY_USES_FUNCTION_NAME(); - if (cfg.get_api_auth().empty() || cfg.get_api_user().empty()) { + if (cfg.get_api_password().empty() || cfg.get_api_user().empty()) { utils::error::raise_error(function_name, "authorization user or password is not set"); return false; @@ -70,7 +70,7 @@ namespace repertory::rpc { auth.erase(auth.begin()); auto pwd = utils::string::join(auth, ':'); - if ((user != cfg.get_api_user()) || (pwd != cfg.get_api_auth())) { + if ((user != cfg.get_api_user()) || (pwd != cfg.get_api_password())) { utils::error::raise_error(function_name, "authorization failed"); return false; } diff --git a/repertory/librepertory/include/types/repertory.hpp b/repertory/librepertory/include/types/repertory.hpp index b5a3fed5..0aee6f93 100644 --- a/repertory/librepertory/include/types/repertory.hpp +++ b/repertory/librepertory/include/types/repertory.hpp @@ -23,7 +23,7 @@ #define REPERTORY_INCLUDE_TYPES_REPERTORY_HPP_ namespace repertory { -constexpr const auto default_api_auth_size{48U}; +constexpr const auto default_api_password_size{48U}; constexpr const auto default_download_timeout_secs{30U}; constexpr const auto default_eviction_delay_mins{1U}; constexpr const auto default_high_freq_interval_secs{std::uint16_t{30U}}; @@ -462,7 +462,6 @@ using meta_provider_callback = std::function; inline constexpr const auto JSON_ACCESS_KEY{"AccessKey"}; inline constexpr const auto JSON_AGENT_STRING{"AgentString"}; -inline constexpr const auto JSON_API_AUTH{"ApiAuth"}; inline constexpr const auto JSON_API_PARENT{"ApiParent"}; inline constexpr const auto JSON_API_PASSWORD{"ApiPassword"}; inline constexpr const auto JSON_API_PATH{"ApiPath"}; diff --git a/repertory/librepertory/src/app_config.cpp b/repertory/librepertory/src/app_config.cpp index e41c7600..cff3f9ea 100644 --- a/repertory/librepertory/src/app_config.cpp +++ b/repertory/librepertory/src/app_config.cpp @@ -66,7 +66,7 @@ void app_config::set_stop_requested() { stop_requested.store(true); } app_config::app_config(const provider_type &prov, std::string_view data_directory) : prov_(prov), - api_auth_(utils::generate_random_string(default_api_auth_size)), + api_password_(utils::generate_random_string(default_api_password_size)), api_port_(default_rpc_port(prov)), api_user_(std::string{REPERTORY}), config_changed_(false), @@ -124,7 +124,7 @@ app_config::app_config(const provider_type &prov, } value_get_lookup_ = { - {JSON_API_AUTH, [this]() { return get_api_auth(); }}, + {JSON_API_PASSWORD, [this]() { return get_api_password(); }}, {JSON_API_PORT, [this]() { return std::to_string(get_api_port()); }}, {JSON_API_USER, [this]() { return get_api_user(); }}, {JSON_DATABASE_TYPE, @@ -253,10 +253,10 @@ app_config::app_config(const provider_type &prov, value_set_lookup_ = { { - JSON_API_AUTH, + JSON_API_PASSWORD, [this](const std::string &value) { - set_api_auth(value); - return get_api_auth(); + set_api_password(value); + return get_api_password(); }, }, { @@ -755,7 +755,9 @@ auto app_config::default_rpc_port(const provider_type &prov) -> std::uint16_t { return PROVIDER_RPC_PORTS.at(static_cast(prov)); } -auto app_config::get_api_auth() const -> std::string { return api_auth_; } +auto app_config::get_api_password() const -> std::string { + return api_password_; +} auto app_config::get_api_port() const -> std::uint16_t { return api_port_; } @@ -816,7 +818,7 @@ auto app_config::get_host_config() const -> host_config { return host_config_; } auto app_config::get_json() const -> json { json ret = { - {JSON_API_AUTH, api_auth_}, + {JSON_API_PASSWORD, api_password_}, {JSON_API_PORT, api_port_}, {JSON_API_USER, api_user_}, {JSON_DOWNLOAD_TIMEOUT_SECS, download_timeout_secs_}, @@ -1037,7 +1039,7 @@ auto app_config::load() -> bool { auto found{true}; auto json_document = json::parse(json_text); - get_value(json_document, JSON_API_AUTH, api_auth_, found); + get_value(json_document, JSON_API_PASSWORD, api_password_, found); get_value(json_document, JSON_API_PORT, api_port_, found); get_value(json_document, JSON_API_USER, api_user_, found); get_value(json_document, JSON_DATABASE_TYPE, db_type_, found); @@ -1094,6 +1096,13 @@ auto app_config::load() -> bool { set_value(max_cache_size_bytes_, default_max_cache_size_bytes); } } + + if (version_ == 2U) { + if (json_document.contains("ApiAuth")) { + api_password_ = json_document.at("ApiAuth").get(); + } + } + found = false; } @@ -1132,8 +1141,8 @@ void app_config::save() { }); } -void app_config::set_api_auth(const std::string &value) { - set_value(api_auth_, value); +void app_config::set_api_password(const std::string &value) { + set_value(api_password_, value); } void app_config::set_api_port(std::uint16_t value) { diff --git a/repertory/librepertory/src/utils/cli_utils.cpp b/repertory/librepertory/src/utils/cli_utils.cpp index ecd46f85..4e9857ba 100644 --- a/repertory/librepertory/src/utils/cli_utils.cpp +++ b/repertory/librepertory/src/utils/cli_utils.cpp @@ -45,7 +45,7 @@ void get_api_authentication_data(std::string &user, std::string &password, if (success) { if (user.empty() && password.empty()) { - password = data[JSON_API_AUTH].get(); + password = data[JSON_API_PASSWORD].get(); user = data[JSON_API_USER].get(); } port = data[JSON_API_PORT].get(); diff --git a/repertory/repertory/include/ui/mgmt_app_config.hpp b/repertory/repertory/include/ui/mgmt_app_config.hpp index 4a38243d..84dd4748 100644 --- a/repertory/repertory/include/ui/mgmt_app_config.hpp +++ b/repertory/repertory/include/ui/mgmt_app_config.hpp @@ -30,7 +30,7 @@ public: mgmt_app_config(); private: - atomic api_auth_{"repertory"}; + atomic api_password_{"repertory"}; std::atomic api_port_{default_ui_mgmt_port}; atomic api_user_{"repertory"}; std::unordered_map std::string { return api_auth_; } + [[nodiscard]] auto get_api_password() const -> std::string { + return api_password_; + } [[nodiscard]] auto get_api_port() const -> std::uint16_t { return api_port_; } diff --git a/repertory/repertory/src/ui/mgmt_app_config.cpp b/repertory/repertory/src/ui/mgmt_app_config.cpp index dbf72aa3..81aab7db 100644 --- a/repertory/repertory/src/ui/mgmt_app_config.cpp +++ b/repertory/repertory/src/ui/mgmt_app_config.cpp @@ -90,7 +90,7 @@ mgmt_app_config::mgmt_app_config() { nlohmann::json data; if (utils::file::read_json_file(config_file, data)) { - api_auth_ = data.at(JSON_API_PASSWORD).get(); + api_password_ = data.at(JSON_API_PASSWORD).get(); api_port_ = data.at(JSON_API_PORT).get(); api_user_ = data.at(JSON_API_USER).get(); locations_ = from_json(data.at(JSON_MOUNT_LOCATIONS)); @@ -135,7 +135,7 @@ void mgmt_app_config::save() const { } nlohmann::json data; - data[JSON_API_PASSWORD] = api_auth_; + data[JSON_API_PASSWORD] = api_password_; data[JSON_API_PORT] = api_port_; data[JSON_API_USER] = api_user_; data[JSON_MOUNT_LOCATIONS] = to_json(locations_); diff --git a/repertory/repertory_test/src/app_config_test.cpp b/repertory/repertory_test/src/app_config_test.cpp index 33d0809c..b556ff9a 100644 --- a/repertory/repertory_test/src/app_config_test.cpp +++ b/repertory/repertory_test/src/app_config_test.cpp @@ -185,9 +185,9 @@ static void defaults_tests(const json &json_data, provider_type prov) { } fmt::println("testing default|{}-{}", app_config::get_provider_name(prov), - JSON_API_AUTH); - ASSERT_EQ(std::size_t(default_api_auth_size), - json_data.at(JSON_API_AUTH).get().size()); + JSON_API_PASSWORD); + ASSERT_EQ(std::size_t(default_api_password_size), + json_data.at(JSON_API_PASSWORD).get().size()); for (const auto &[key, value] : json_defaults.items()) { fmt::println("testing default|{}-{}", app_config::get_provider_name(prov), key); @@ -216,11 +216,11 @@ static void common_tests(app_config &config, provider_type prov) { ASSERT_EQ(config.get_provider_type(), prov); std::map> methods{ - {JSON_API_AUTH, + {JSON_API_PASSWORD, [](app_config &cfg) { test_getter_setter(cfg, &app_config::get_api_auth, &app_config::set_api_auth, "", "auth", - JSON_API_AUTH, "auth2"); + JSON_API_PASSWORD, "auth2"); }}, {JSON_API_PORT, [](app_config &cfg) { diff --git a/web/repertory/lib/helpers.dart b/web/repertory/lib/helpers.dart index 4aca5b5e..6b9e174f 100644 --- a/web/repertory/lib/helpers.dart +++ b/web/repertory/lib/helpers.dart @@ -124,7 +124,7 @@ String getBaseUri() { List getSettingValidators(String settingPath) { switch (settingPath) { - case 'ApiAuth': + case 'ApiPassword': return [notEmptyValidator]; case 'DatabaseType': return [(value) => constants.databaseTypeList.contains(value)]; diff --git a/web/repertory/lib/widgets/mount_settings.dart b/web/repertory/lib/widgets/mount_settings.dart index 50c6cced..ed619820 100644 --- a/web/repertory/lib/widgets/mount_settings.dart +++ b/web/repertory/lib/widgets/mount_settings.dart @@ -346,7 +346,7 @@ class _MountSettingsWidgetState extends State { widget.settings.forEach((key, value) { switch (key) { - case 'ApiAuth': + case 'ApiPassword': { _addPasswordSetting( commonSettings,