Automatic reset on failed login and other communication fixes

This commit is contained in:
David Kocik 2023-12-20 14:42:10 +01:00
parent e065f0c83b
commit 167d92872f
8 changed files with 71 additions and 27 deletions

View File

@ -149,7 +149,7 @@ PrusaAuthCommunication::PrusaAuthCommunication(wxEvtHandler* evt_handler, AppCon
m_remember_session = true;
m_session = std::make_unique<AuthSession>(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<std::mutex> 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<std::mutex> 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<std::mutex> 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()
{

View File

@ -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();

View File

@ -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 });
}

View File

@ -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<void(const std::string& body)> UserActionSuccessFn;
typedef std::function<void(const std::string& body)> 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<UserActionPost>("EXCHANGE_TOKENS", "https://test-account.prusa3d.com/o/token/");
m_actions[UserActionID::CODE_FOR_TOKEN] = std::make_unique<UserActionPost>("EXCHANGE_TOKENS", "https://test-account.prusa3d.com/o/token/");
m_actions[UserActionID::TEST_CONNECTION] = std::make_unique<UserActionGetWithEvent>("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<UserActionGetWithEvent>("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<UserActionGetWithEvent>("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<UserActionGetWithEvent>("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<UserActionGetWithEvent>("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;

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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<Slic3r::GUI::PrusaAuthCommunication> m_auth_communication;