Do not use bool state behave_as_mater, instead do so only after token refresh event.

This commit is contained in:
David Kocik 2025-01-29 13:18:52 +01:00 committed by Lukas Matena
parent 9c3829c879
commit 88a08cd212
8 changed files with 29 additions and 29 deletions

View File

@ -957,7 +957,7 @@ void Plater::priv::init()
this->show_action_buttons(this->ready_to_slice); this->show_action_buttons(this->ready_to_slice);
}); });
this->q->Bind(EVT_UA_ID_USER_SUCCESS, [this](UserAccountSuccessEvent& evt) { auto on_id_user_success = [this](UserAccountSuccessEvent& evt, bool after_token_success) {
if (login_dialog != nullptr) { if (login_dialog != nullptr) {
login_dialog->EndModal(wxID_CANCEL); login_dialog->EndModal(wxID_CANCEL);
} }
@ -965,7 +965,7 @@ void Plater::priv::init()
evt.Skip(); evt.Skip();
std::string who = user_account->get_username(); std::string who = user_account->get_username();
std::string username; std::string username;
if (user_account->on_user_id_success(evt.data, username)) { if (user_account->on_user_id_success(evt.data, username, after_token_success)) {
if (who != username) { if (who != username) {
// show notification only on login (not refresh). // show notification only on login (not refresh).
std::string text = format(_u8L("Logged to Prusa Account as %1%."), username); std::string text = format(_u8L("Logged to Prusa Account as %1%."), username);
@ -999,9 +999,16 @@ void Plater::priv::init()
this->main_frame->refresh_account_menu(true); this->main_frame->refresh_account_menu(true);
// Update sidebar printer status // Update sidebar printer status
sidebar->update_printer_presets_combobox(); sidebar->update_printer_presets_combobox();
} }
};
this->q->Bind(EVT_UA_ID_USER_SUCCESS, [on_id_user_success](UserAccountSuccessEvent& evt) {
on_id_user_success(evt, false);
}); });
this->q->Bind(EVT_UA_ID_USER_SUCCESS_AFTER_TOKEN_SUCCESS, [on_id_user_success](UserAccountSuccessEvent& evt) {
on_id_user_success(evt, true);
});
this->q->Bind(EVT_UA_RESET, [this](UserAccountFailEvent& evt) { this->q->Bind(EVT_UA_RESET, [this](UserAccountFailEvent& evt) {
BOOST_LOG_TRIVIAL(error) << "Reseting Prusa Account communication. Error message: " << evt.data; BOOST_LOG_TRIVIAL(error) << "Reseting Prusa Account communication. Error message: " << evt.data;
user_account->clear(); user_account->clear();

View File

@ -33,10 +33,10 @@ UserAccount::UserAccount(wxEvtHandler* evt_handler, AppConfig* app_config, const
UserAccount::~UserAccount() UserAccount::~UserAccount()
{} {}
void UserAccount::set_username(const std::string& username) void UserAccount::set_username(const std::string& username, bool store)
{ {
m_username = username; m_username = username;
m_communication->set_username(username); m_communication->set_username(username, store);
} }
void UserAccount::clear() void UserAccount::clear()
@ -126,7 +126,7 @@ bool UserAccount::on_login_code_recieved(const std::string& url_message)
return true; return true;
} }
bool UserAccount::on_user_id_success(const std::string data, std::string& out_username) bool UserAccount::on_user_id_success(const std::string data, std::string& out_username, bool after_token_success)
{ {
boost::property_tree::ptree ptree; boost::property_tree::ptree ptree;
try { try {
@ -151,7 +151,7 @@ bool UserAccount::on_user_id_success(const std::string data, std::string& out_us
return false; return false;
} }
std::string public_username = m_account_user_data["public_username"]; std::string public_username = m_account_user_data["public_username"];
set_username(public_username); set_username(public_username, after_token_success);
out_username = public_username; out_username = public_username;
// enqueue GET with avatar url // enqueue GET with avatar url

View File

@ -56,7 +56,7 @@ public:
// Functions called from UI where events emmited from UserAccountSession are binded // Functions called from UI where events emmited from UserAccountSession are binded
// Returns bool if data were correctly proccessed // Returns bool if data were correctly proccessed
bool on_login_code_recieved(const std::string& url_message); bool on_login_code_recieved(const std::string& url_message);
bool on_user_id_success(const std::string data, std::string& out_username); bool on_user_id_success(const std::string data, std::string& out_username, bool after_token_success);
// Called on EVT_UA_FAIL, triggers test after several calls // Called on EVT_UA_FAIL, triggers test after several calls
void on_communication_fail(); void on_communication_fail();
void on_race_lost(); void on_race_lost();
@ -82,7 +82,7 @@ public:
void on_store_read_request() { m_communication->on_store_read_request(); } void on_store_read_request() { m_communication->on_store_read_request(); }
private: private:
void set_username(const std::string& username); void set_username(const std::string& username, bool store);
std::string m_instance_hash; // used in avatar path std::string m_instance_hash; // used in avatar path

View File

@ -258,8 +258,6 @@ UserAccountCommunication::UserAccountCommunication(wxEvtHandler* evt_handler, Ap
long long remain_time = next_timeout_long - std::time(nullptr); long long remain_time = next_timeout_long - std::time(nullptr);
if (remain_time <= 0) { if (remain_time <= 0) {
stored_data.access_token.clear(); stored_data.access_token.clear();
// if there is no access token to be used - consider yourself as master (either for case refresh token is going to be used now or future login)
m_behave_as_master = true;
} else { } else {
set_refresh_time((int)remain_time); set_refresh_time((int)remain_time);
} }
@ -295,10 +293,10 @@ UserAccountCommunication::~UserAccountCommunication()
} }
} }
void UserAccountCommunication::set_username(const std::string& username) void UserAccountCommunication::set_username(const std::string& username, bool store)
{ {
m_username = username; m_username = username;
if (!m_behave_as_master && !username.empty()) { if (!store && !username.empty()) {
return; return;
} }
{ {
@ -355,7 +353,7 @@ void UserAccountCommunication::set_remember_session(bool b)
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__; BOOST_LOG_TRIVIAL(debug) << __FUNCTION__;
m_remember_session = b; m_remember_session = b;
// tokens needs to be stored or deleted // tokens needs to be stored or deleted
set_username(m_username); set_username(m_username, true);
} }
std::string UserAccountCommunication::get_access_token() std::string UserAccountCommunication::get_access_token()
@ -450,12 +448,11 @@ void UserAccountCommunication::do_clear()
{ {
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__; BOOST_LOG_TRIVIAL(debug) << __FUNCTION__;
m_session->clear(); m_session->clear();
set_username({}); set_username({}, true);
m_token_timer->Stop(); m_token_timer->Stop();
m_slave_read_timer->Stop(); m_slave_read_timer->Stop();
m_after_race_lost_timer->Stop(); m_after_race_lost_timer->Stop();
m_next_token_refresh_at = 0; m_next_token_refresh_at = 0;
m_behave_as_master = true;
} }
void UserAccountCommunication::on_login_code_recieved(const std::string& url_message) void UserAccountCommunication::on_login_code_recieved(const std::string& url_message)
@ -544,7 +541,6 @@ void UserAccountCommunication::enqueue_refresh()
BOOST_LOG_TRIVIAL(debug) << "User Account: Token refresh already enqueued, skipping..."; BOOST_LOG_TRIVIAL(debug) << "User Account: Token refresh already enqueued, skipping...";
return; return;
} }
m_behave_as_master = true;
m_session->enqueue_refresh({}); m_session->enqueue_refresh({});
wakeup_session_thread(); wakeup_session_thread();
} }
@ -698,14 +694,9 @@ void UserAccountCommunication::on_token_timer(wxTimerEvent& evt)
long long expires_in_second = stored_data.next_timeout.empty() ? 0 : std::stoll(stored_data.next_timeout) - std::time(nullptr); long long expires_in_second = stored_data.next_timeout.empty() ? 0 : std::stoll(stored_data.next_timeout) - std::time(nullptr);
if (my_pid == stored_data.master_pid) { if (my_pid == stored_data.master_pid) {
// this is master instance - writing to secret store is permited
m_behave_as_master = true;
enqueue_refresh(); enqueue_refresh();
return; return;
} }
// this is not master instance - writing to secret store is not permited until it is clear current master did not renew
m_behave_as_master = false;
// token could be either already new -> we want to start using it now // token could be either already new -> we want to start using it now
const auto prior_expiration_secs = std::max(m_last_token_duration_seconds / 24, 10); const auto prior_expiration_secs = std::max(m_last_token_duration_seconds / 24, 10);
if (expires_in_second >= 0 && expires_in_second > prior_expiration_secs) { if (expires_in_second >= 0 && expires_in_second > prior_expiration_secs) {
@ -765,7 +756,6 @@ void UserAccountCommunication::enqueue_refresh_race(const std::string refresh_to
return; return;
} }
// At this point, last master did not renew the tokens, behave like master // At this point, last master did not renew the tokens, behave like master
m_behave_as_master = true;
m_session->enqueue_refresh_race(); m_session->enqueue_refresh_race();
wakeup_session_thread(); wakeup_session_thread();
} }
@ -776,7 +766,6 @@ void UserAccountCommunication::on_race_lost()
// race from on_slave_read_timer has been lost // race from on_slave_read_timer has been lost
// other instance was faster to renew tokens so refresh token from this app was denied (invalid grant) // other instance was faster to renew tokens so refresh token from this app was denied (invalid grant)
// we should read the other token now. // we should read the other token now.
m_behave_as_master = false;
std::string current_access_token = m_session->get_access_token(); std::string current_access_token = m_session->get_access_token();
StoreData stored_data; StoreData stored_data;
read_stored_data(stored_data); read_stored_data(stored_data);

View File

@ -73,7 +73,7 @@ public:
void on_activate_app(bool active); void on_activate_app(bool active);
void set_username(const std::string& username); void set_username(const std::string& username, bool store);
void set_remember_session(bool b); void set_remember_session(bool b);
bool get_remember_session() const {return m_remember_session; } bool get_remember_session() const {return m_remember_session; }
@ -90,7 +90,7 @@ public:
void on_polling_timer(wxTimerEvent& evt); void on_polling_timer(wxTimerEvent& evt);
void set_tokens(const StoreData store_data); void set_tokens(const StoreData store_data);
void on_race_lost(); // T5 void on_race_lost(); // T4
void on_store_read_request(); void on_store_read_request();
private: private:
std::unique_ptr<UserAccountSession> m_session; std::unique_ptr<UserAccountSession> m_session;
@ -118,7 +118,6 @@ private:
std::string client_id() const { return Utils::ServiceConfig::instance().account_client_id(); } std::string client_id() const { return Utils::ServiceConfig::instance().account_client_id(); }
// master / slave logic // master / slave logic
bool m_behave_as_master {false};
wxTimer* m_slave_read_timer; // T2 timer wxTimer* m_slave_read_timer; // T2 timer
wxTimer* m_after_race_lost_timer; // T5 timer wxTimer* m_after_race_lost_timer; // T5 timer
int m_last_token_duration_seconds {0}; int m_last_token_duration_seconds {0};

View File

@ -21,6 +21,7 @@ namespace GUI {
wxDEFINE_EVENT(EVT_OPEN_PRUSAAUTH, OpenPrusaAuthEvent); wxDEFINE_EVENT(EVT_OPEN_PRUSAAUTH, OpenPrusaAuthEvent);
wxDEFINE_EVENT(EVT_UA_LOGGEDOUT, UserAccountSuccessEvent); wxDEFINE_EVENT(EVT_UA_LOGGEDOUT, UserAccountSuccessEvent);
wxDEFINE_EVENT(EVT_UA_ID_USER_SUCCESS, UserAccountSuccessEvent); wxDEFINE_EVENT(EVT_UA_ID_USER_SUCCESS, UserAccountSuccessEvent);
wxDEFINE_EVENT(EVT_UA_ID_USER_SUCCESS_AFTER_TOKEN_SUCCESS, UserAccountSuccessEvent);
wxDEFINE_EVENT(EVT_UA_SUCCESS, UserAccountSuccessEvent); wxDEFINE_EVENT(EVT_UA_SUCCESS, UserAccountSuccessEvent);
wxDEFINE_EVENT(EVT_UA_PRUSACONNECT_STATUS_SUCCESS, UserAccountSuccessEvent); wxDEFINE_EVENT(EVT_UA_PRUSACONNECT_STATUS_SUCCESS, UserAccountSuccessEvent);
wxDEFINE_EVENT(EVT_UA_PRUSACONNECT_PRINTER_MODELS_SUCCESS, UserAccountSuccessEvent); wxDEFINE_EVENT(EVT_UA_PRUSACONNECT_PRINTER_MODELS_SUCCESS, UserAccountSuccessEvent);
@ -252,7 +253,7 @@ void UserAccountSession::token_success_callback(const std::string& body)
m_shared_session_key = shared_session_key; m_shared_session_key = shared_session_key;
m_next_token_timeout = std::time(nullptr) + expires_in; m_next_token_timeout = std::time(nullptr) + expires_in;
} }
enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_USER_ID, nullptr, nullptr, {}); enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_USER_ID_AFTER_TOKEN_SUCCESS, nullptr, nullptr, {});
wxQueueEvent(p_evt_handler, new UserAccountTimeEvent(EVT_UA_REFRESH_TIME, expires_in)); wxQueueEvent(p_evt_handler, new UserAccountTimeEvent(EVT_UA_REFRESH_TIME, expires_in));
} }

View File

@ -21,6 +21,7 @@ using UserAccountTimeEvent = Event<int>;
wxDECLARE_EVENT(EVT_OPEN_PRUSAAUTH, OpenPrusaAuthEvent); wxDECLARE_EVENT(EVT_OPEN_PRUSAAUTH, OpenPrusaAuthEvent);
wxDECLARE_EVENT(EVT_UA_LOGGEDOUT, UserAccountSuccessEvent); wxDECLARE_EVENT(EVT_UA_LOGGEDOUT, UserAccountSuccessEvent);
wxDECLARE_EVENT(EVT_UA_ID_USER_SUCCESS, UserAccountSuccessEvent); wxDECLARE_EVENT(EVT_UA_ID_USER_SUCCESS, UserAccountSuccessEvent);
wxDECLARE_EVENT(EVT_UA_ID_USER_SUCCESS_AFTER_TOKEN_SUCCESS, UserAccountSuccessEvent);
wxDECLARE_EVENT(EVT_UA_SUCCESS, UserAccountSuccessEvent); wxDECLARE_EVENT(EVT_UA_SUCCESS, UserAccountSuccessEvent);
wxDECLARE_EVENT(EVT_UA_PRUSACONNECT_STATUS_SUCCESS, UserAccountSuccessEvent); wxDECLARE_EVENT(EVT_UA_PRUSACONNECT_STATUS_SUCCESS, UserAccountSuccessEvent);
wxDECLARE_EVENT(EVT_UA_PRUSACONNECT_PRINTER_MODELS_SUCCESS, UserAccountSuccessEvent); wxDECLARE_EVENT(EVT_UA_PRUSACONNECT_PRINTER_MODELS_SUCCESS, UserAccountSuccessEvent);
@ -45,6 +46,7 @@ enum class UserAccountActionID {
USER_ACCOUNT_ACTION_REFRESH_TOKEN, USER_ACCOUNT_ACTION_REFRESH_TOKEN,
USER_ACCOUNT_ACTION_CODE_FOR_TOKEN, USER_ACCOUNT_ACTION_CODE_FOR_TOKEN,
USER_ACCOUNT_ACTION_USER_ID, USER_ACCOUNT_ACTION_USER_ID,
USER_ACCOUNT_ACTION_USER_ID_AFTER_TOKEN_SUCCESS,
USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN, USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN,
USER_ACCOUNT_ACTION_TEST_CONNECTION, USER_ACCOUNT_ACTION_TEST_CONNECTION,
USER_ACCOUNT_ACTION_CONNECT_STATUS, // status of all printers by UUID USER_ACCOUNT_ACTION_CONNECT_STATUS, // status of all printers by UUID
@ -124,6 +126,7 @@ public:
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_REFRESH_TOKEN] = std::make_unique<UserActionPost>("EXCHANGE_TOKENS", sc.account_token_url()); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_REFRESH_TOKEN] = std::make_unique<UserActionPost>("EXCHANGE_TOKENS", sc.account_token_url());
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CODE_FOR_TOKEN] = std::make_unique<UserActionPost>("EXCHANGE_TOKENS", sc.account_token_url()); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CODE_FOR_TOKEN] = std::make_unique<UserActionPost>("EXCHANGE_TOKENS", sc.account_token_url());
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_USER_ID] = std::make_unique<UserActionGetWithEvent>("USER_ID", sc.account_me_url(), EVT_UA_ID_USER_SUCCESS, EVT_UA_RESET); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_USER_ID] = std::make_unique<UserActionGetWithEvent>("USER_ID", sc.account_me_url(), EVT_UA_ID_USER_SUCCESS, EVT_UA_RESET);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_USER_ID_AFTER_TOKEN_SUCCESS] = std::make_unique<UserActionGetWithEvent>("USER_ID_AFTER_TOKEN_SUCCESS", sc.account_me_url(), EVT_UA_ID_USER_SUCCESS_AFTER_TOKEN_SUCCESS, EVT_UA_RESET);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN] = std::make_unique<UserActionGetWithEvent>("TEST_ACCESS_TOKEN", sc.account_me_url(), EVT_UA_ID_USER_SUCCESS, EVT_UA_FAIL); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN] = std::make_unique<UserActionGetWithEvent>("TEST_ACCESS_TOKEN", sc.account_me_url(), EVT_UA_ID_USER_SUCCESS, EVT_UA_FAIL);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_CONNECTION] = std::make_unique<UserActionGetWithEvent>("TEST_CONNECTION", sc.account_me_url(), wxEVT_NULL, EVT_UA_RESET); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_CONNECTION] = std::make_unique<UserActionGetWithEvent>("TEST_CONNECTION", sc.account_me_url(), wxEVT_NULL, EVT_UA_RESET);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_STATUS] = std::make_unique<UserActionGetWithEvent>("CONNECT_STATUS", sc.connect_status_url(), EVT_UA_PRUSACONNECT_STATUS_SUCCESS, EVT_UA_FAIL); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_STATUS] = std::make_unique<UserActionGetWithEvent>("CONNECT_STATUS", sc.connect_status_url(), EVT_UA_PRUSACONNECT_STATUS_SUCCESS, EVT_UA_FAIL);

View File

@ -615,6 +615,7 @@ ConnectWebViewPanel::ConnectWebViewPanel(wxWindow* parent)
auto* plater = wxGetApp().plater(); auto* plater = wxGetApp().plater();
plater->Bind(EVT_UA_LOGGEDOUT, &ConnectWebViewPanel::on_user_logged_out, this); plater->Bind(EVT_UA_LOGGEDOUT, &ConnectWebViewPanel::on_user_logged_out, this);
plater->Bind(EVT_UA_ID_USER_SUCCESS, &ConnectWebViewPanel::on_user_token, this); plater->Bind(EVT_UA_ID_USER_SUCCESS, &ConnectWebViewPanel::on_user_token, this);
plater->Bind(EVT_UA_ID_USER_SUCCESS_AFTER_TOKEN_SUCCESS, &ConnectWebViewPanel::on_user_token, this);
m_actions["appQuit"] = std::bind(&WebViewPanel::on_app_quit_event, this, std::placeholders::_1); m_actions["appQuit"] = std::bind(&WebViewPanel::on_app_quit_event, this, std::placeholders::_1);
m_actions["appMinimize"] = std::bind(&WebViewPanel::on_app_minimize_event, this, std::placeholders::_1); m_actions["appMinimize"] = std::bind(&WebViewPanel::on_app_minimize_event, this, std::placeholders::_1);