mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-08-01 00:11:57 +08:00
ConnectWebViewPanel: fixed login-logout-login issue (removed stub for message handler when calling WebView::RemoveAllUserScripts()), current implementation sends message to C++ runtime to invoke login flow with up-to-date token from C++ runtime
This commit is contained in:
parent
76313f5148
commit
c42efdfef9
@ -518,7 +518,13 @@ void UserAccountCommunication::on_activate_app(bool active)
|
|||||||
}
|
}
|
||||||
auto now = std::time(nullptr);
|
auto now = std::time(nullptr);
|
||||||
BOOST_LOG_TRIVIAL(info) << "UserAccountCommunication activate: active " << active;
|
BOOST_LOG_TRIVIAL(info) << "UserAccountCommunication activate: active " << active;
|
||||||
if (active && m_next_token_refresh_at > 0 && m_next_token_refresh_at - now < 60) {
|
#ifndef _NDEBUG
|
||||||
|
// constexpr auto refresh_threshold = 110 * 60;
|
||||||
|
constexpr auto refresh_threshold = 60;
|
||||||
|
#else
|
||||||
|
constexpr auto refresh_threshold = 60;
|
||||||
|
#endif
|
||||||
|
if (active && m_next_token_refresh_at > 0 && m_next_token_refresh_at - now < refresh_threshold) {
|
||||||
BOOST_LOG_TRIVIAL(info) << "Enqueue access token refresh on activation";
|
BOOST_LOG_TRIVIAL(info) << "Enqueue access token refresh on activation";
|
||||||
m_token_timer->Stop();
|
m_token_timer->Stop();
|
||||||
enqueue_refresh();
|
enqueue_refresh();
|
||||||
|
@ -493,6 +493,7 @@ SourceViewDialog::SourceViewDialog(wxWindow* parent, wxString source) :
|
|||||||
|
|
||||||
ConnectRequestHandler::ConnectRequestHandler()
|
ConnectRequestHandler::ConnectRequestHandler()
|
||||||
{
|
{
|
||||||
|
m_actions["REQUEST_LOGIN"] = std::bind(&ConnectRequestHandler::on_connect_action_request_login, this, std::placeholders::_1);
|
||||||
m_actions["REQUEST_CONFIG"] = std::bind(&ConnectRequestHandler::on_connect_action_request_config, this, std::placeholders::_1);
|
m_actions["REQUEST_CONFIG"] = std::bind(&ConnectRequestHandler::on_connect_action_request_config, this, std::placeholders::_1);
|
||||||
m_actions["WEBAPP_READY"] = std::bind(&ConnectRequestHandler::on_connect_action_webapp_ready,this, std::placeholders::_1);
|
m_actions["WEBAPP_READY"] = std::bind(&ConnectRequestHandler::on_connect_action_webapp_ready,this, std::placeholders::_1);
|
||||||
m_actions["SELECT_PRINTER"] = std::bind(&ConnectRequestHandler::on_connect_action_select_printer, this, std::placeholders::_1);
|
m_actions["SELECT_PRINTER"] = std::bind(&ConnectRequestHandler::on_connect_action_select_printer, this, std::placeholders::_1);
|
||||||
@ -553,6 +554,10 @@ void ConnectRequestHandler::on_connect_action_log(const std::string& message_dat
|
|||||||
BOOST_LOG_TRIVIAL(info) << "WebView log: " << message_data;
|
BOOST_LOG_TRIVIAL(info) << "WebView log: " << message_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ConnectRequestHandler::on_connect_action_request_login(const std::string &message_data)
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
void ConnectRequestHandler::on_connect_action_request_config(const std::string& message_data)
|
void ConnectRequestHandler::on_connect_action_request_config(const std::string& message_data)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
@ -590,9 +595,11 @@ void ConnectRequestHandler::on_connect_action_request_open_in_browser(const std:
|
|||||||
ConnectWebViewPanel::ConnectWebViewPanel(wxWindow* parent)
|
ConnectWebViewPanel::ConnectWebViewPanel(wxWindow* parent)
|
||||||
: WebViewPanel(parent, GUI::from_u8(Utils::ServiceConfig::instance().connect_url()), { "_prusaSlicer" }, "connect_loading")
|
: WebViewPanel(parent, GUI::from_u8(Utils::ServiceConfig::instance().connect_url()), { "_prusaSlicer" }, "connect_loading")
|
||||||
{
|
{
|
||||||
//m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new WebViewHandler("https")));
|
// m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new WebViewHandler("https")));
|
||||||
|
|
||||||
wxGetApp().plater()->Bind(EVT_UA_ID_USER_SUCCESS, &ConnectWebViewPanel::on_user_token, this);
|
auto* plater = wxGetApp().plater();
|
||||||
|
plater->Bind(EVT_UA_ID_USER_SUCCESS, &ConnectWebViewPanel::on_user_token, this);
|
||||||
|
plater->Bind(EVT_UA_LOGGEDOUT, &ConnectWebViewPanel::on_user_logged_out, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectWebViewPanel::~ConnectWebViewPanel()
|
ConnectWebViewPanel::~ConnectWebViewPanel()
|
||||||
@ -645,17 +652,19 @@ wxString ConnectWebViewPanel::get_login_script(bool refresh)
|
|||||||
refresh
|
refresh
|
||||||
?
|
?
|
||||||
R"(
|
R"(
|
||||||
if (window._prusaSlicer_initLogin !== undefined) {
|
if (location.protocol === 'https:') {
|
||||||
console.log('Refreshing login');
|
if (window._prusaSlicer_initLogin !== undefined) {
|
||||||
if (window._prusaSlicer !== undefined)
|
console.log('Init login');
|
||||||
_prusaSlicer.postMessage({action: 'LOG', message: 'Refreshing login'});
|
if (window._prusaSlicer !== undefined)
|
||||||
_prusaSlicer_initLogin('%s', 'refresh');
|
_prusaSlicer.postMessage({action: 'LOG', message: 'Refreshing login'});
|
||||||
} else {
|
_prusaSlicer_initLogin('%s');
|
||||||
console.log('Refreshing login skipped as no _prusaSlicer_initLogin defined (yet?)');
|
|
||||||
if (window._prusaSlicer === undefined) {
|
|
||||||
console.log('Message handler _prusaSlicer not defined yet');
|
|
||||||
} else {
|
} else {
|
||||||
_prusaSlicer.postMessage({action: 'LOG', message: 'Refreshing login skipped as no _prusaSlicer_initLogin defined (yet?)'});
|
console.log('Refreshing login skipped as no _prusaSlicer_login defined (yet?)');
|
||||||
|
if (window._prusaSlicer === undefined) {
|
||||||
|
console.log('Message handler _prusaSlicer not defined yet');
|
||||||
|
} else {
|
||||||
|
_prusaSlicer.postMessage({action: 'LOG', message: 'Refreshing login skipped as no _prusaSlicer_initLogin defined (yet?)'});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)"
|
)"
|
||||||
@ -682,7 +691,7 @@ wxString ConnectWebViewPanel::get_login_script(bool refresh)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function _prusaSlicer_initLogin(token, reason) {
|
async function _prusaSlicer_initLogin(token) {
|
||||||
const parts = token.split('.');
|
const parts = token.split('.');
|
||||||
const claims = JSON.parse(atob(parts[1]));
|
const claims = JSON.parse(atob(parts[1]));
|
||||||
const now = new Date().getTime() / 1000;
|
const now = new Date().getTime() / 1000;
|
||||||
@ -699,10 +708,10 @@ wxString ConnectWebViewPanel::get_login_script(bool refresh)
|
|||||||
let error = false;
|
let error = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
_prusaSlicer_log('Slicer Login request (' + reason + ') ' + token.substring(token.length - 8));
|
_prusaSlicer_log('Slicer Login request ' + token.substring(token.length - 8));
|
||||||
let resp = await fetch('/slicer/login', {method: 'POST', headers: {Authorization: 'Bearer ' + token}});
|
let resp = await fetch('/slicer/login', {method: 'POST', headers: {Authorization: 'Bearer ' + token}});
|
||||||
let body = await resp.text();
|
let body = await resp.text();
|
||||||
_prusaSlicer_log('Slicer Login resp ' + resp.status + ' (' +reason + ' ' + token.substring(token.length - 8) + ') body: ' + body);
|
_prusaSlicer_log('Slicer Login resp ' + resp.status + ' (' + token.substring(token.length - 8) + ') body: ' + body);
|
||||||
if (resp.status >= 500 || resp.status == 408) {
|
if (resp.status >= 500 || resp.status == 408) {
|
||||||
retry = true;
|
retry = true;
|
||||||
} else {
|
} else {
|
||||||
@ -724,10 +733,10 @@ wxString ConnectWebViewPanel::get_login_script(bool refresh)
|
|||||||
}
|
}
|
||||||
} while (retry);
|
} while (retry);
|
||||||
}
|
}
|
||||||
if (window._prusaSlicer_initialLoad === undefined) {
|
|
||||||
console.log('Initial login');
|
if (location.protocol === 'https:' && window._prusaSlicer) {
|
||||||
_prusaSlicer_initLogin('%s', 'init-load');
|
_prusaSlicer_log('Requesting login');
|
||||||
window._prusaSlicer_initialLoad = true;
|
_prusaSlicer.postMessage({action: 'REQUEST_LOGIN'});
|
||||||
}
|
}
|
||||||
)",
|
)",
|
||||||
#endif
|
#endif
|
||||||
@ -736,6 +745,11 @@ wxString ConnectWebViewPanel::get_login_script(bool refresh)
|
|||||||
return javascript;
|
return javascript;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wxString ConnectWebViewPanel::get_logout_script()
|
||||||
|
{
|
||||||
|
return "sessionStorage.removeItem('_slicer_token');";
|
||||||
|
}
|
||||||
|
|
||||||
void ConnectWebViewPanel::on_page_will_load()
|
void ConnectWebViewPanel::on_page_will_load()
|
||||||
{
|
{
|
||||||
auto javascript = get_login_script(false);
|
auto javascript = get_login_script(false);
|
||||||
@ -748,18 +762,20 @@ void ConnectWebViewPanel::on_user_token(UserAccountSuccessEvent& e)
|
|||||||
e.Skip();
|
e.Skip();
|
||||||
auto access_token = wxGetApp().plater()->get_user_account()->get_access_token();
|
auto access_token = wxGetApp().plater()->get_user_account()->get_access_token();
|
||||||
assert(!access_token.empty());
|
assert(!access_token.empty());
|
||||||
wxString javascript = get_login_script(false);
|
|
||||||
|
|
||||||
m_browser->RemoveAllUserScripts();
|
wxString javascript = get_login_script(true);
|
||||||
m_browser->AddUserScript(javascript);
|
|
||||||
|
|
||||||
javascript = get_login_script(true);
|
|
||||||
//m_browser->AddUserScript(javascript, wxWEBVIEW_INJECT_AT_DOCUMENT_END);
|
|
||||||
BOOST_LOG_TRIVIAL(debug) << "RunScript " << javascript << "\n";
|
BOOST_LOG_TRIVIAL(debug) << "RunScript " << javascript << "\n";
|
||||||
m_browser->RunScriptAsync(javascript);
|
m_browser->RunScriptAsync(javascript);
|
||||||
resend_config();
|
resend_config();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ConnectWebViewPanel::on_user_logged_out(UserAccountSuccessEvent& e)
|
||||||
|
{
|
||||||
|
e.Skip();
|
||||||
|
// clear token from session storage
|
||||||
|
m_browser->RunScriptAsync(get_logout_script());
|
||||||
|
}
|
||||||
|
|
||||||
void ConnectWebViewPanel::on_script_message(wxWebViewEvent& evt)
|
void ConnectWebViewPanel::on_script_message(wxWebViewEvent& evt)
|
||||||
{
|
{
|
||||||
BOOST_LOG_TRIVIAL(debug) << "received message from Prusa Connect FE: " << evt.GetString();
|
BOOST_LOG_TRIVIAL(debug) << "received message from Prusa Connect FE: " << evt.GetString();
|
||||||
@ -803,11 +819,11 @@ void ConnectWebViewPanel::logout()
|
|||||||
Plater* plater = wxGetApp().plater();
|
Plater* plater = wxGetApp().plater();
|
||||||
auto javascript = wxString::Format(
|
auto javascript = wxString::Format(
|
||||||
R"(
|
R"(
|
||||||
console.log('Preparing login');
|
console.log('Preparing logout');
|
||||||
window.fetch('/slicer/logout', {method: 'POST', headers: {Authorization: 'Bearer %s'}})
|
window.fetch('/slicer/logout', {method: 'POST', headers: {Authorization: 'Bearer %s'}})
|
||||||
.then(function (resp){
|
.then(function (resp){
|
||||||
console.log('Login resp', resp);
|
console.log('Logout resp', resp);
|
||||||
resp.text().then(function (json) { console.log('Login resp body', json) });
|
resp.text().then(function (json) { console.log('Logout resp body', json) });
|
||||||
});
|
});
|
||||||
)",
|
)",
|
||||||
plater->get_user_account()->get_access_token()
|
plater->get_user_account()->get_access_token()
|
||||||
@ -822,6 +838,12 @@ void ConnectWebViewPanel::sys_color_changed()
|
|||||||
resend_config();
|
resend_config();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ConnectWebViewPanel::on_connect_action_request_login(const std::string &message_data)
|
||||||
|
{
|
||||||
|
run_script_bridge(get_login_script(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void ConnectWebViewPanel::on_connect_action_select_printer(const std::string& message_data)
|
void ConnectWebViewPanel::on_connect_action_select_printer(const std::string& message_data)
|
||||||
{
|
{
|
||||||
assert(!message_data.empty());
|
assert(!message_data.empty());
|
||||||
|
@ -186,6 +186,7 @@ protected:
|
|||||||
// action callbacs stored in m_actions
|
// action callbacs stored in m_actions
|
||||||
virtual void on_connect_action_log(const std::string& message_data);
|
virtual void on_connect_action_log(const std::string& message_data);
|
||||||
virtual void on_connect_action_error(const std::string& message_data);
|
virtual void on_connect_action_error(const std::string& message_data);
|
||||||
|
virtual void on_connect_action_request_login(const std::string& message_data);
|
||||||
virtual void on_connect_action_request_config(const std::string& message_data);
|
virtual void on_connect_action_request_config(const std::string& message_data);
|
||||||
virtual void on_connect_action_request_open_in_browser(const std::string& message_data);
|
virtual void on_connect_action_request_open_in_browser(const std::string& message_data);
|
||||||
virtual void on_connect_action_select_printer(const std::string& message_data) = 0;
|
virtual void on_connect_action_select_printer(const std::string& message_data) = 0;
|
||||||
@ -206,6 +207,7 @@ public:
|
|||||||
void sys_color_changed() override;
|
void sys_color_changed() override;
|
||||||
void on_navigation_request(wxWebViewEvent &evt) override;
|
void on_navigation_request(wxWebViewEvent &evt) override;
|
||||||
protected:
|
protected:
|
||||||
|
void on_connect_action_request_login(const std::string &message_data) override;
|
||||||
void on_connect_action_select_printer(const std::string& message_data) override;
|
void on_connect_action_select_printer(const std::string& message_data) override;
|
||||||
void on_connect_action_print(const std::string& message_data) override;
|
void on_connect_action_print(const std::string& message_data) override;
|
||||||
void on_connect_action_webapp_ready(const std::string& message_data) override {}
|
void on_connect_action_webapp_ready(const std::string& message_data) override {}
|
||||||
@ -214,7 +216,9 @@ protected:
|
|||||||
void on_connect_action_error(const std::string &message_data) override;
|
void on_connect_action_error(const std::string &message_data) override;
|
||||||
private:
|
private:
|
||||||
static wxString get_login_script(bool refresh);
|
static wxString get_login_script(bool refresh);
|
||||||
|
static wxString get_logout_script();
|
||||||
void on_user_token(UserAccountSuccessEvent& e);
|
void on_user_token(UserAccountSuccessEvent& e);
|
||||||
|
void on_user_logged_out(UserAccountSuccessEvent& e);
|
||||||
bool m_reached_default_url {false};
|
bool m_reached_default_url {false};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user