diff --git a/src/slic3r/GUI/UserAccountCommunication.cpp b/src/slic3r/GUI/UserAccountCommunication.cpp index db0f3d6129..01ae2d7bf8 100644 --- a/src/slic3r/GUI/UserAccountCommunication.cpp +++ b/src/slic3r/GUI/UserAccountCommunication.cpp @@ -300,7 +300,11 @@ void UserAccountCommunication::set_username(const std::string& username, bool st return; } { - BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << " empty: " << username.empty(); + //BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << " empty: " << username.empty(); + std::string at = m_session->get_access_token(); + if (!at.empty()) + at = at.substr(0,5) + "..." + at.substr(at.size()-5); + BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ <<" access_token: " << (username.empty() ? "" : at); if (is_secret_store_ok()) { std::string tokens = "|||"; if (m_remember_session && !username.empty()) { @@ -667,9 +671,12 @@ void UserAccountCommunication::request_refresh() return; } + // Here we need to count with situation when token was renewed in m_session but was not yet stored. + // Then store token is not valid - it should has erlier expiration long long expires_in_second = stored_data.next_timeout.empty() ? 0 : std::stoll(stored_data.next_timeout) - std::time(nullptr); - if (stored_data.access_token != current_access_token && expires_in_second > 0) { - BOOST_LOG_TRIVIAL(debug) << "Found usable token"; + BOOST_LOG_TRIVIAL(error) << "Compare " << expires_in_second << " vs " << m_next_token_refresh_at - std::time(nullptr) << (stored_data.access_token != current_access_token ? " not same" : " same"); + if (stored_data.access_token != current_access_token && expires_in_second > 0 && expires_in_second > m_next_token_refresh_at - std::time(nullptr)) { + BOOST_LOG_TRIVIAL(debug) << "Found usable token. Expires in " << expires_in_second; set_tokens(stored_data); } else { BOOST_LOG_TRIVIAL(debug) << "No new token"; diff --git a/src/slic3r/GUI/UserAccountSession.cpp b/src/slic3r/GUI/UserAccountSession.cpp index 3b4d349579..114fd5258e 100644 --- a/src/slic3r/GUI/UserAccountSession.cpp +++ b/src/slic3r/GUI/UserAccountSession.cpp @@ -195,10 +195,37 @@ void UserAccountSession::init_with_code(const std::string& code, const std::stri } } +void UserAccountSession::remove_from_queue(UserAccountActionID action_id) +{ + { + std::lock_guard lock(m_session_mutex); + + auto it = std::find_if( + std::begin(m_priority_action_queue), std::end(m_priority_action_queue), + [action_id](const ActionQueueData& item) { return item.action_id == action_id; } + ); + while (it != m_priority_action_queue.end()) + { + BOOST_LOG_TRIVIAL(debug) << __FUNCTION__; + m_priority_action_queue.erase(it); + it = std::find_if( + std::begin(m_priority_action_queue), std::end(m_priority_action_queue), + [action_id](const ActionQueueData& item) { return item.action_id == action_id; } + ); + } + } + +} + void UserAccountSession::token_success_callback(const std::string& body) { // No need to use lock m_session_mutex here + // This is here to prevent performing refresh again until USER_ACCOUNT_ACTION_USER_ID_AFTER_TOKEN_SUCCESS is performed. + // If refresh with stored token was enqueued during performing one we are in its success_callback, + // It would fail and prevent USER_ID to write this tokens to store. + remove_from_queue(UserAccountActionID::USER_ACCOUNT_ACTION_REFRESH_TOKEN); + BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << " Access token refreshed"; // Data we need std::string access_token, refresh_token, shared_session_key; diff --git a/src/slic3r/GUI/UserAccountSession.hpp b/src/slic3r/GUI/UserAccountSession.hpp index 5e3c368972..b3f468c7e6 100644 --- a/src/slic3r/GUI/UserAccountSession.hpp +++ b/src/slic3r/GUI/UserAccountSession.hpp @@ -207,6 +207,8 @@ private: std::string client_id() const { return Utils::ServiceConfig::instance().account_client_id(); } void process_action_queue_inner(); + void remove_from_queue(UserAccountActionID action_id); + // called from m_session_mutex protected code only void enqueue_action_inner(UserAccountActionID id, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input); diff --git a/src/slic3r/GUI/WebViewPanel.cpp b/src/slic3r/GUI/WebViewPanel.cpp index 4be65e488d..dc931daafb 100644 --- a/src/slic3r/GUI/WebViewPanel.cpp +++ b/src/slic3r/GUI/WebViewPanel.cpp @@ -1413,7 +1413,7 @@ void PrintablesWebViewPanel::sys_color_changed() void PrintablesWebViewPanel::on_printables_event_access_token_expired(const std::string& message_data) { - // { "event": "accessTokenExpired:) + // { "event": "accessTokenExpired") // There seems to be a situation where we get accessTokenExpired when there is active token from Slicer POW // We need get new token and freeze webview until its not refreshed if (m_refreshing_token) {