Remember session refactoring. Browser login redirect dialog only until first confirmation

This commit is contained in:
David Kocik 2024-01-23 10:14:44 +01:00
parent 1be8144b1c
commit f716497318
9 changed files with 76 additions and 50 deletions

View File

@ -218,6 +218,9 @@ void AppConfig::set_defaults()
if (get("connect_polling").empty())
set("connect_polling", "1");
if (get("auth_login_dialog_confirmed").empty())
set("auth_login_dialog_confirmed", "0");
#ifdef _WIN32
if (get("use_legacy_3DConnexion").empty())
set("use_legacy_3DConnexion", "0");

View File

@ -132,6 +132,7 @@ bool load_secret(const std::string& opt, std::string& usr, std::string& psswd)
PrusaAuthCommunication::PrusaAuthCommunication(wxEvtHandler* evt_handler, AppConfig* app_config)
: m_evt_handler(evt_handler)
, m_app_config(app_config)
{
std::string access_token, refresh_token, shared_session_key;
if (is_secret_store_ok()) {
@ -141,13 +142,13 @@ PrusaAuthCommunication::PrusaAuthCommunication(wxEvtHandler* evt_handler, AppCon
assert(key0 == key1);
shared_session_key = key0;
} else {
access_token = app_config->get("access_token");
refresh_token = app_config->get("refresh_token");
shared_session_key = app_config->get("shared_session_key");
access_token = m_app_config->get("access_token");
refresh_token = m_app_config->get("refresh_token");
shared_session_key = m_app_config->get("shared_session_key");
}
if (!access_token.empty() || !refresh_token.empty())
m_remember_session = true;
m_session = std::make_unique<AuthSession>(evt_handler, access_token, refresh_token, shared_session_key, app_config->get_bool("connect_polling"));
m_session = std::make_unique<AuthSession>(evt_handler, access_token, refresh_token, shared_session_key, m_app_config->get_bool("connect_polling"));
init_session_thread();
// perform login at the start
if (m_remember_session)
@ -168,7 +169,7 @@ PrusaAuthCommunication::~PrusaAuthCommunication() {
}
}
void PrusaAuthCommunication::set_username(const std::string& username, AppConfig* app_config)
void PrusaAuthCommunication::set_username(const std::string& username)
{
m_username = username;
{
@ -178,13 +179,20 @@ void PrusaAuthCommunication::set_username(const std::string& username, AppConfig
save_secret("refresh_token", m_session->get_shared_session_key(), m_remember_session ? m_session->get_refresh_token() : std::string());
}
else {
app_config->set("access_token", m_remember_session ? m_session->get_access_token() : std::string());
app_config->set("refresh_token", m_remember_session ? m_session->get_refresh_token() : std::string());
app_config->set("shared_session_key", m_remember_session ? m_session->get_shared_session_key() : std::string());
m_app_config->set("access_token", m_remember_session ? m_session->get_access_token() : std::string());
m_app_config->set("refresh_token", m_remember_session ? m_session->get_refresh_token() : std::string());
m_app_config->set("shared_session_key", m_remember_session ? m_session->get_shared_session_key() : std::string());
}
}
}
void PrusaAuthCommunication::set_remember_session(bool b)
{
m_remember_session = b;
// tokens needs to be stored or deleted
set_username(m_username);
}
std::string PrusaAuthCommunication::get_access_token()
{
{
@ -231,19 +239,19 @@ void PrusaAuthCommunication::do_login()
}
wakeup_session_thread();
}
void PrusaAuthCommunication::do_logout(AppConfig* app_config)
void PrusaAuthCommunication::do_logout()
{
do_clear(app_config);
do_clear();
wxQueueEvent(m_evt_handler, new PrusaAuthSuccessEvent(GUI::EVT_LOGGEDOUT_PRUSAAUTH, {}));
}
void PrusaAuthCommunication::do_clear(AppConfig* app_config)
void PrusaAuthCommunication::do_clear()
{
{
std::lock_guard<std::mutex> lock(m_session_mutex);
m_session->clear();
}
set_username({}, app_config);
set_username({});
}
void PrusaAuthCommunication::on_login_code_recieved(const std::string& url_message)

View File

@ -35,8 +35,8 @@ public:
//
bool is_logged();
void do_login();
void do_logout(AppConfig* app_config);
void do_clear(AppConfig* app_config);
void do_logout();
void do_clear();
// Trigger function starts various remote operations
#if 0
void enqueue_user_id_action();
@ -52,8 +52,9 @@ public:
void on_login_code_recieved(const std::string& url_message);
void set_username(const std::string& username, AppConfig* app_config);
void set_remember_session(bool b) { m_remember_session = b; }
void set_username(const std::string& username);
void set_remember_session(bool b);
bool get_remember_session() const {return m_remember_session; }
std::string get_username() const { return m_username; }
std::string get_access_token();
@ -69,9 +70,10 @@ private:
bool m_thread_stop { false };
std::string m_code_verifier;
wxEvtHandler* m_evt_handler;
AppConfig* m_app_config;
// if not empty - user is logged in
std::string m_username;
bool m_remember_session {false};
bool m_remember_session {true};
void wakeup_session_thread();
void init_session_thread();

View File

@ -3436,17 +3436,14 @@ bool GUI_App::open_browser_with_warning_dialog(const wxString& url, wxWindow* pa
bool GUI_App::open_login_browser_with_dialog(const wxString& url, wxWindow* parent/* = nullptr*/, int flags/* = 0*/)
{
bool launch = true;
// warning dialog containes a "Remember my choice" checkbox
std::string option_key = "suppress_hyperlinks";
RichMessageDialog dialog(parent, _L("Open Log in page in default browser?"), _L("PrusaSlicer: Open Log in page"), wxICON_QUESTION | wxYES_NO);
dialog.ShowCheckBox(_L("Remember me"), true);
auto answer = dialog.ShowModal();
launch = answer == wxID_YES;
plater()->get_user_account()->set_remember_session(dialog.IsCheckBoxChecked());
return launch && wxLaunchDefaultBrowser(url, flags);
bool auth_login_dialog_confirmed = app_config->get_bool("auth_login_dialog_confirmed");
if (!auth_login_dialog_confirmed) {
RichMessageDialog dialog(parent, _L("Open default browser with Prusa Account Log in page?\n(On Yes, You will not be asked again.)"), _L("PrusaSlicer: Open Log in page"), wxICON_QUESTION | wxYES_NO);
if (dialog.ShowModal() != wxID_YES)
return false;
app_config->set("auth_login_dialog_confirmed", "1");
}
return wxLaunchDefaultBrowser(url, flags);
}
// static method accepting a wxWindow object as first parameter

View File

@ -893,7 +893,7 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
this->q->Bind(EVT_PA_ID_USER_SUCCESS, [this](PrusaAuthSuccessEvent& evt) {
std::string username;
bool succ = user_account->on_user_id_success(evt.data, wxGetApp().app_config, username);
bool succ = user_account->on_user_id_success(evt.data, username);
if (succ) {
// show connect tab
this->main_frame->enable_connect_tab();

View File

@ -163,16 +163,23 @@ void TopBarItemsCtrl::CreateAuthMenu()
}, get_bmp_bundle("user", 16));
m_auth_menu.AppendSeparator();
/*
m_connect_dummy_menu_item = append_menu_item(&m_auth_menu, wxID_ANY, _L("PrusaConnect Printers"), "",
[](wxCommandEvent&) { wxGetApp().plater()->get_user_account()->enqueue_connect_printers_action(); },
"", nullptr, []() { return wxGetApp().plater()->get_user_account()->is_logged(); }, this->GetParent());
*/
wxMenuItem* remember_me_menu_item = append_menu_check_item(&m_auth_menu, wxID_ANY, _L("Remember me"), ""
, [](wxCommandEvent&) { wxGetApp().plater()->get_user_account()->toggle_remember_session(); }
, &m_auth_menu
, []() { return wxGetApp().plater()->get_user_account() ? wxGetApp().plater()->get_user_account()->is_logged() : false; }
, []() { return wxGetApp().plater()->get_user_account() ? wxGetApp().plater()->get_user_account()->get_remember_session() : false; }
, this->GetParent());
m_login_menu_item = append_menu_item(&m_auth_menu, wxID_ANY, "", "",
[](wxCommandEvent&) {
auto user_account = wxGetApp().plater()->get_user_account();
if (user_account->is_logged())
user_account->do_logout(wxGetApp().app_config);
user_account->do_logout();
else
user_account->do_login();
}, get_bmp_bundle("login", 16));
@ -189,7 +196,7 @@ void TopBarItemsCtrl::UpdateAuthMenu()
const wxString user_name = user_account->is_logged() ? from_u8(user_account->get_username()) : _L("Anonymus");
if (m_user_menu_item)
m_user_menu_item->SetItemLabel(user_name);
m_auth_btn->SetLabel(user_name);
m_auth_btn->SetBitmapMargins(0, 0);
// m_auth_btn->SetBitmap_("icon_name");

View File

@ -51,11 +51,11 @@ class TopBarItemsCtrl : public wxControl
wxMenu m_main_menu;
wxMenu m_workspaces_menu;
wxMenu m_auth_menu;
// Prusa Account (Auth) menu items
wxMenuItem* m_user_menu_item{ nullptr };
wxMenuItem* m_login_menu_item{ nullptr };
wxMenuItem* m_connect_dummy_menu_item{ nullptr };
//wxMenuItem* m_connect_dummy_menu_item{ nullptr };
::TextInput* m_search{ nullptr };
public:

View File

@ -19,24 +19,32 @@ UserAccount::UserAccount(wxEvtHandler* evt_handler, AppConfig* app_config)
UserAccount::~UserAccount()
{}
void UserAccount::set_username(const std::string& username, AppConfig* app_config)
void UserAccount::set_username(const std::string& username)
{
m_username = username;
m_auth_communication->set_username(username, app_config);
m_auth_communication->set_username(username);
}
void UserAccount::reset(AppConfig* app_config)
void UserAccount::reset()
{
m_username = {};
m_user_data.clear();
m_printer_map.clear();
m_auth_communication->do_clear(app_config);
m_auth_communication->do_clear();
}
void UserAccount::set_remember_session(bool remember)
{
m_auth_communication->set_remember_session(remember);
}
void UserAccount::toggle_remember_session()
{
m_auth_communication->set_remember_session(!m_auth_communication->get_remember_session());
}
bool UserAccount::get_remember_session()
{
return m_auth_communication->get_remember_session();
}
bool UserAccount::is_logged()
{
@ -46,9 +54,9 @@ void UserAccount::do_login()
{
m_auth_communication->do_login();
}
void UserAccount::do_logout(AppConfig* app_config)
void UserAccount::do_logout()
{
m_auth_communication->do_logout(app_config);
m_auth_communication->do_logout();
}
std::string UserAccount::get_access_token()
@ -83,7 +91,7 @@ bool UserAccount::on_login_code_recieved(const std::string& url_message)
return true;
}
bool UserAccount::on_user_id_success(const std::string data, AppConfig* app_config, std::string& out_username)
bool UserAccount::on_user_id_success(const std::string data, std::string& out_username)
{
boost::property_tree::ptree ptree;
try {
@ -109,7 +117,7 @@ bool UserAccount::on_user_id_success(const std::string data, AppConfig* app_conf
return false;
}
std::string public_username = m_user_data["public_username"];
set_username(public_username, app_config);
set_username(public_username);
out_username = public_username;
// equeue GET with avatar url
if (m_user_data.find("avatar") != m_user_data.end()) {
@ -132,13 +140,13 @@ bool UserAccount::on_communication_fail(const std::string data, AppConfig* app_c
bool UserAccount::on_communication_reset(const std::string data, AppConfig* app_config)
{
set_username({}, app_config);
set_username({});
return true;
}
bool UserAccount::on_logout( AppConfig* app_config)
{
set_username({}, app_config);
set_username({});
return true;
}

View File

@ -32,10 +32,11 @@ public:
bool is_logged();
void do_login();
void do_logout(AppConfig* app_config);
void do_logout();
void set_remember_session(bool remember);
void toggle_remember_session();
bool get_remember_session();
#if 0
void enqueue_user_id_action();
void enqueue_connect_dummy_action();
@ -46,7 +47,7 @@ public:
// Functions called from UI where events emmited from AuthSession are binded
// Returns bool if data were correctly proccessed
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_user_id_success(const std::string data, 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);
@ -63,8 +64,8 @@ public:
std::string get_nozzle_from_json(const std::string& message) const;
std::string get_apikey_from_json(const std::string& message) const;
private:
void set_username(const std::string& username, AppConfig* app_config);
void reset(AppConfig* app_config);
void set_username(const std::string& username);
void reset();
std::unique_ptr<Slic3r::GUI::PrusaAuthCommunication> m_auth_communication;