From 167d92872f794acd74bbb02840166a08a3df99d4 Mon Sep 17 00:00:00 2001 From: David Kocik Date: Wed, 20 Dec 2023 14:42:10 +0100 Subject: [PATCH] Automatic reset on failed login and other communication fixes --- src/slic3r/GUI/Auth.cpp | 18 ++++++++++++------ src/slic3r/GUI/Auth.hpp | 5 ++++- src/slic3r/GUI/AuthSession.cpp | 27 ++++++++++++++++----------- src/slic3r/GUI/AuthSession.hpp | 16 ++++++++++++---- src/slic3r/GUI/Plater.cpp | 8 +++++++- src/slic3r/GUI/TopBar.cpp | 2 +- src/slic3r/GUI/UserAccount.cpp | 18 ++++++++++++++++-- src/slic3r/GUI/UserAccount.hpp | 4 +++- 8 files changed, 71 insertions(+), 27 deletions(-) diff --git a/src/slic3r/GUI/Auth.cpp b/src/slic3r/GUI/Auth.cpp index 599b59a993..68ed0a2d4b 100644 --- a/src/slic3r/GUI/Auth.cpp +++ b/src/slic3r/GUI/Auth.cpp @@ -149,7 +149,7 @@ PrusaAuthCommunication::PrusaAuthCommunication(wxEvtHandler* evt_handler, AppCon m_remember_session = true; m_session = std::make_unique(evt_handler, access_token, refresh_token, shared_session_key); init_session_thread(); - // perform login at the start - do we want this + // perform login at the start if (m_remember_session) do_login(); } @@ -218,19 +218,24 @@ void PrusaAuthCommunication::do_login() std::lock_guard lock(m_session_mutex); if (!m_session->is_initialized()) { login_redirect(); - //return; } - m_session->enqueue_action(UserActionID::USER_ID, nullptr, nullptr, {}); + m_session->enqueue_action(UserActionID::LOGIN_USER_ID, nullptr, nullptr, {}); } wakeup_session_thread(); } -void PrusaAuthCommunication::do_logout() +void PrusaAuthCommunication::do_logout(AppConfig* app_config) +{ + do_clear(app_config); + wxQueueEvent(m_evt_handler, new PrusaAuthSuccessEvent(GUI::EVT_LOGGEDOUT_PRUSAAUTH, {})); +} + +void PrusaAuthCommunication::do_clear(AppConfig* app_config) { { std::lock_guard lock(m_session_mutex); m_session->clear(); } - wxQueueEvent(m_evt_handler, new PrusaAuthSuccessEvent(GUI::EVT_LOGGEDOUT_PRUSAAUTH, {})); + set_username({}, app_config); } void PrusaAuthCommunication::on_login_code_recieved(const std::string& url_message) @@ -243,12 +248,12 @@ void PrusaAuthCommunication::on_login_code_recieved(const std::string& url_messa wakeup_session_thread(); } +#if 0 void PrusaAuthCommunication::enqueue_user_id_action() { { std::lock_guard lock(m_session_mutex); if (!m_session->is_initialized()) { - //login_redirect(); return; } m_session->enqueue_action(UserActionID::USER_ID, nullptr, nullptr, {}); @@ -268,6 +273,7 @@ void PrusaAuthCommunication::enqueue_connect_dummy_action() } wakeup_session_thread(); } +#endif void PrusaAuthCommunication::enqueue_connect_printers_action() { diff --git a/src/slic3r/GUI/Auth.hpp b/src/slic3r/GUI/Auth.hpp index 50a8a9b568..408c221139 100644 --- a/src/slic3r/GUI/Auth.hpp +++ b/src/slic3r/GUI/Auth.hpp @@ -35,10 +35,13 @@ public: // bool is_logged(); void do_login(); - void do_logout(); + void do_logout(AppConfig* app_config); + void do_clear(AppConfig* app_config); // Trigger function starts various remote operations +#if 0 void enqueue_user_id_action(); void enqueue_connect_dummy_action(); +#endif void enqueue_connect_printers_action(); diff --git a/src/slic3r/GUI/AuthSession.cpp b/src/slic3r/GUI/AuthSession.cpp index 56a5f2a3c1..0d625fd5e9 100644 --- a/src/slic3r/GUI/AuthSession.cpp +++ b/src/slic3r/GUI/AuthSession.cpp @@ -25,22 +25,23 @@ wxDEFINE_EVENT(EVT_PA_ID_USER_FAIL, PrusaAuthFailEvent); wxDEFINE_EVENT(EVT_PRUSAAUTH_SUCCESS, PrusaAuthSuccessEvent); wxDEFINE_EVENT(EVT_PRUSACONNECT_PRINTERS_SUCCESS, PrusaAuthSuccessEvent); wxDEFINE_EVENT(EVT_PRUSAAUTH_FAIL, PrusaAuthFailEvent); +wxDEFINE_EVENT(EVT_PRUSAAUTH_RESET, PrusaAuthFailEvent); void UserActionPost::perform(const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input) { std::string url = m_url; - BOOST_LOG_TRIVIAL(info) << m_action_name <<" POST " << url << " body: " << input; + //BOOST_LOG_TRIVIAL(info) << m_action_name <<" POST " << url << " body: " << input; auto http = Http::post(std::move(url)); if (!input.empty()) http.set_post_body(input); http.header("Content-type", "application/x-www-form-urlencoded"); http.on_error([&](std::string body, std::string error, unsigned status) { - BOOST_LOG_TRIVIAL(error) << m_action_name << " action failed. status: " << status << " Body: " << body; + //BOOST_LOG_TRIVIAL(error) << m_action_name << " action failed. status: " << status << " Body: " << body; if (fail_callback) fail_callback(body); }); http.on_complete([&, this](std::string body, unsigned status) { - BOOST_LOG_TRIVIAL(info) << m_action_name << "action success. Status: " << status << " Body: " << body; + //BOOST_LOG_TRIVIAL(info) << m_action_name << "action success. Status: " << status << " Body: " << body; if (success_callback) success_callback(body); }); @@ -50,11 +51,11 @@ void UserActionPost::perform(const std::string& access_token, UserActionSuccessF void UserActionGetWithEvent::perform(const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, /*UNUSED*/ const std::string& input) { std::string url = m_url; - BOOST_LOG_TRIVIAL(info) << m_action_name << " GET " << url; + //BOOST_LOG_TRIVIAL(info) << m_action_name << " GET " << url; auto http = Http::get(url); http.header("Authorization", "Bearer " + access_token); http.on_error([&](std::string body, std::string error, unsigned status) { - BOOST_LOG_TRIVIAL(error) << m_action_name << " action failed. status: " << status << " Body: " << body; + //BOOST_LOG_TRIVIAL(error) << m_action_name << " action failed. status: " << status << " Body: " << body; if (fail_callback) fail_callback(body); std::string message = GUI::format("%1% action failed (%2%): %3%", m_action_name, std::to_string(status), body); @@ -62,7 +63,7 @@ void UserActionGetWithEvent::perform(const std::string& access_token, UserAction wxQueueEvent(m_evt_handler, new PrusaAuthFailEvent(m_fail_evt_type, std::move(message))); }); http.on_complete([&, this](std::string body, unsigned status) { - BOOST_LOG_TRIVIAL(info) << m_action_name << " action success. Status: " << status << " Body: " << body; + //BOOST_LOG_TRIVIAL(info) << m_action_name << " action success. Status: " << status << " Body: " << body; if (success_callback) success_callback(body); if (m_succ_evt_type != wxEVT_NULL) @@ -74,9 +75,11 @@ void UserActionGetWithEvent::perform(const std::string& access_token, UserAction void AuthSession::process_action_queue() { - BOOST_LOG_TRIVIAL(debug) << "process_action_queue start"; + if (!m_proccessing_enabled) + return; + //BOOST_LOG_TRIVIAL(debug) << "process_action_queue start"; if (m_priority_action_queue.empty() && m_action_queue.empty()) { - BOOST_LOG_TRIVIAL(debug) << "process_action_queue queues empty"; + //BOOST_LOG_TRIVIAL(debug) << "process_action_queue queues empty"; // update printers on every periodic wakeup call enqueue_action(UserActionID::CONNECT_PRINTERS, nullptr, nullptr, {}); //return; @@ -94,7 +97,7 @@ void AuthSession::process_action_queue() } if (!this->is_initialized()) { - BOOST_LOG_TRIVIAL(debug) << "process_action_queue not initialized"; + //BOOST_LOG_TRIVIAL(debug) << "process_action_queue not initialized"; return; } @@ -103,12 +106,13 @@ void AuthSession::process_action_queue() if (!m_action_queue.empty()) m_action_queue.pop(); } - BOOST_LOG_TRIVIAL(debug) << "process_action_queue end"; + //BOOST_LOG_TRIVIAL(debug) << "process_action_queue end"; } void AuthSession::enqueue_action(UserActionID id, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input) { - BOOST_LOG_TRIVIAL(info) << "enqueue_action " << (int)id; + //BOOST_LOG_TRIVIAL(info) << "enqueue_action " << (int)id; + m_proccessing_enabled = true; m_action_queue.push({ id, success_callback, fail_callback, input }); } @@ -163,6 +167,7 @@ void AuthSession::init_with_code(const std::string& code, const std::string& cod m_shared_session_key = shared_session_key; }; + m_proccessing_enabled = true; // fail fn might be cancel_queue here m_priority_action_queue.push({ UserActionID::CODE_FOR_TOKEN, succ_fn, std::bind(&AuthSession::enqueue_refresh, this, std::placeholders::_1), post_fields }); } diff --git a/src/slic3r/GUI/AuthSession.hpp b/src/slic3r/GUI/AuthSession.hpp index 27f8681514..7a5a476d85 100644 --- a/src/slic3r/GUI/AuthSession.hpp +++ b/src/slic3r/GUI/AuthSession.hpp @@ -23,6 +23,7 @@ wxDECLARE_EVENT(EVT_PA_ID_USER_FAIL, PrusaAuthFailEvent); wxDECLARE_EVENT(EVT_PRUSAAUTH_SUCCESS, PrusaAuthSuccessEvent); wxDECLARE_EVENT(EVT_PRUSACONNECT_PRINTERS_SUCCESS, PrusaAuthSuccessEvent); wxDECLARE_EVENT(EVT_PRUSAAUTH_FAIL, PrusaAuthFailEvent); +wxDECLARE_EVENT(EVT_PRUSAAUTH_RESET, PrusaAuthFailEvent); typedef std::function UserActionSuccessFn; typedef std::function UserActionFailFn; @@ -33,7 +34,7 @@ enum class UserActionID { REFRESH_TOKEN, CODE_FOR_TOKEN, TEST_CONNECTION, - USER_ID, + LOGIN_USER_ID, CONNECT_DUMMY, CONNECT_PRINTERS, }; @@ -102,7 +103,7 @@ public: m_actions[UserActionID::REFRESH_TOKEN] = std::make_unique("EXCHANGE_TOKENS", "https://test-account.prusa3d.com/o/token/"); m_actions[UserActionID::CODE_FOR_TOKEN] = std::make_unique("EXCHANGE_TOKENS", "https://test-account.prusa3d.com/o/token/"); m_actions[UserActionID::TEST_CONNECTION] = std::make_unique("TEST_CONNECTION", "https://test-account.prusa3d.com/api/v1/me/", evt_handler, wxEVT_NULL, EVT_PRUSAAUTH_FAIL); - m_actions[UserActionID::USER_ID] = std::make_unique("USER_ID", "https://test-account.prusa3d.com/api/v1/me/", evt_handler, EVT_PA_ID_USER_SUCCESS, EVT_PRUSAAUTH_FAIL); + m_actions[UserActionID::LOGIN_USER_ID] = std::make_unique("USER_ID", "https://test-account.prusa3d.com/api/v1/me/", evt_handler, EVT_PA_ID_USER_SUCCESS, EVT_PRUSAAUTH_FAIL); m_actions[UserActionID::CONNECT_DUMMY] = std::make_unique("CONNECT_DUMMY", "https://dev.connect.prusa3d.com/slicer/dummy"/*"dev.connect.prusa:8000/slicer/dummy"*/, evt_handler, EVT_PRUSAAUTH_SUCCESS, EVT_PRUSAAUTH_FAIL); m_actions[UserActionID::CONNECT_PRINTERS] = std::make_unique("CONNECT_PRINTERS", "https://dev.connect.prusa3d.com/slicer/printers"/*"dev.connect.prusa:8000/slicer/printers"*/, evt_handler, EVT_PRUSACONNECT_PRINTERS_SUCCESS, EVT_PRUSAAUTH_FAIL); } @@ -112,7 +113,7 @@ public: m_actions[UserActionID::REFRESH_TOKEN].reset(nullptr); m_actions[UserActionID::CODE_FOR_TOKEN].reset(nullptr); m_actions[UserActionID::TEST_CONNECTION].reset(nullptr); - m_actions[UserActionID::USER_ID].reset(nullptr); + m_actions[UserActionID::LOGIN_USER_ID].reset(nullptr); m_actions[UserActionID::CONNECT_DUMMY].reset(nullptr); m_actions[UserActionID::CONNECT_PRINTERS].reset(nullptr); //assert(m_actions.empty()); @@ -121,11 +122,14 @@ public: m_access_token.clear(); m_refresh_token.clear(); m_shared_session_key.clear(); + m_proccessing_enabled = false; } + // Functions that automatically enable action queu processing void init_with_code(const std::string& code, const std::string& code_verifier); - void process_action_queue(); void enqueue_action(UserActionID id, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input); + + void process_action_queue(); bool is_initialized() { return !m_access_token.empty() || !m_refresh_token.empty(); } std::string get_access_token() const { return m_access_token; } std::string get_refresh_token() const { return m_refresh_token; } @@ -138,6 +142,10 @@ private: void cancel_queue(); std::string client_id() const { return "UfTRUm5QjWwaQEGpWQBHGHO3reAyuzgOdBaiqO52"; } + // false prevents action queu to be processed - no communication is done + // sets to true by init_with_code or enqueue_action call + bool m_proccessing_enabled {false}; + std::string m_access_token; std::string m_refresh_token; std::string m_shared_session_key; diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 5e9afdc627..3a9ee15e08 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -906,11 +906,17 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame) } }); + this->q->Bind(EVT_PRUSAAUTH_RESET, [this](PrusaAuthFailEvent& evt) { + BOOST_LOG_TRIVIAL(error) << "Network error message: " << evt.data; + user_account->on_communication_fail(evt.data, wxGetApp().app_config); + this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); + this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::WarningNotificationLevel, _u8L("Connection to PrusaConnect has failed.")); + }); this->q->Bind(EVT_PRUSAAUTH_FAIL, [this](PrusaAuthFailEvent& evt) { BOOST_LOG_TRIVIAL(error) << "Network error message: " << evt.data; user_account->on_communication_fail(evt.data, wxGetApp().app_config); this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); - this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::WarningNotificationLevel, evt.data); + this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::WarningNotificationLevel, _u8L("Connection to PrusaConnect has failed.")); }); this->q->Bind(EVT_PRUSAAUTH_SUCCESS, [this](PrusaAuthSuccessEvent& evt) { this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); diff --git a/src/slic3r/GUI/TopBar.cpp b/src/slic3r/GUI/TopBar.cpp index 3e0403a6d6..f9edb504fd 100644 --- a/src/slic3r/GUI/TopBar.cpp +++ b/src/slic3r/GUI/TopBar.cpp @@ -164,7 +164,7 @@ void TopBarItemsCtrl::CreateAuthMenu() [this](wxCommandEvent&) { auto user_account = wxGetApp().plater()->get_user_account(); if (user_account->is_logged()) - user_account->do_logout(); + user_account->do_logout(wxGetApp().app_config); else user_account->do_login(); }, get_bmp_bundle("login", 16), nullptr, []() { return true; }, this); diff --git a/src/slic3r/GUI/UserAccount.cpp b/src/slic3r/GUI/UserAccount.cpp index 91114c30e2..f799608375 100644 --- a/src/slic3r/GUI/UserAccount.cpp +++ b/src/slic3r/GUI/UserAccount.cpp @@ -24,6 +24,14 @@ void UserAccount::set_username(const std::string& username, AppConfig* app_confi m_auth_communication->set_username(username, app_config); } +void UserAccount::reset(AppConfig* app_config) +{ + m_username = {}; + m_user_data.clear(); + m_printer_map.clear(); + m_auth_communication->do_clear(app_config); +} + void UserAccount::set_remember_session(bool remember) { m_auth_communication->set_remember_session(remember); @@ -37,9 +45,9 @@ void UserAccount::do_login() { m_auth_communication->do_login(); } -void UserAccount::do_logout() +void UserAccount::do_logout(AppConfig* app_config) { - m_auth_communication->do_logout(); + m_auth_communication->do_logout(app_config); } std::string UserAccount::get_access_token() @@ -110,6 +118,12 @@ bool UserAccount::on_communication_fail(const std::string data, AppConfig* app_c return true; } +bool UserAccount::on_communication_reset(const std::string data, AppConfig* app_config) +{ + set_username({}, app_config); + return true; +} + bool UserAccount::on_logout( AppConfig* app_config) { set_username({}, app_config); diff --git a/src/slic3r/GUI/UserAccount.hpp b/src/slic3r/GUI/UserAccount.hpp index e7cb914d51..79e580f74b 100644 --- a/src/slic3r/GUI/UserAccount.hpp +++ b/src/slic3r/GUI/UserAccount.hpp @@ -32,7 +32,7 @@ public: bool is_logged(); void do_login(); - void do_logout(); + void do_logout(AppConfig* app_config); void set_remember_session(bool remember); @@ -47,6 +47,7 @@ public: bool on_login_code_recieved(const std::string& url_message); bool on_user_id_success(const std::string data, AppConfig* app_config, std::string& out_username); bool on_communication_fail(const std::string data, AppConfig* app_config); + bool on_communication_reset(const std::string data, AppConfig* app_config); bool on_logout(AppConfig* app_config); bool on_connect_printers_success(const std::string data, AppConfig* app_config, bool& out_printers_changed, std::string& out_message); @@ -60,6 +61,7 @@ public: std::string get_nozzle_from_json(const std::string& message) const; private: void set_username(const std::string& username, AppConfig* app_config); + void reset(AppConfig* app_config); std::unique_ptr m_auth_communication;