From 8042b4f905499905bf49e33b14dfb421b4910440 Mon Sep 17 00:00:00 2001 From: David Kocik Date: Wed, 26 Jun 2024 09:19:42 +0200 Subject: [PATCH] rename token file, delete old dat file --- src/slic3r/GUI/UserAccountCommunication.cpp | 46 ++++++++++++++++----- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/slic3r/GUI/UserAccountCommunication.cpp b/src/slic3r/GUI/UserAccountCommunication.cpp index 47af20d5f8..405cf55f12 100644 --- a/src/slic3r/GUI/UserAccountCommunication.cpp +++ b/src/slic3r/GUI/UserAccountCommunication.cpp @@ -136,6 +136,40 @@ bool load_secret(const std::string& opt, std::string& usr, std::string& psswd) return false; #endif // wxUSE_SECRETSTORE } + +#ifdef __linux__ +void load_refresh_token_linux(std::string& refresh_token) +{ + // Load refresh token from UserAccount.dat + boost::filesystem::path source(boost::filesystem::path(Slic3r::data_dir()) / "UserAccount.dat") ; + // since there was for a short period different file in use, if present, load it and delete it. + boost::system::error_code ec; + bool delete_after_read = false; + if (!boost::filesystem::exists(source, ec) || ec) { + source = boost::filesystem::path(Slic3r::data_dir()) / "UserAcountData.dat"; + ec.clear(); + if (!boost::filesystem::exists(source, ec) || ec) { + BOOST_LOG_TRIVIAL(error) << "UserAccount: Failed to read token - no datafile found."; + return; + } + delete_after_read = true; + } + boost::nowide::ifstream stream(source.generic_string(), std::ios::in | std::ios::binary); + if (!stream) { + BOOST_LOG_TRIVIAL(error) << "UserAccount: Failed to read token from " << source; + return; + } + std::getline(stream, refresh_token); + stream.close(); + if (delete_after_read) { + ec.clear(); + if (!boost::filesystem::remove(source, ec) || ec) { + BOOST_LOG_TRIVIAL(error) << "UserAccount: Failed to remove file " << source; + } + + } +} +#endif //__linux__ } UserAccountCommunication::UserAccountCommunication(wxEvtHandler* evt_handler, AppConfig* app_config) @@ -168,15 +202,7 @@ UserAccountCommunication::UserAccountCommunication(wxEvtHandler* evt_handler, Ap } else { #ifdef __linux__ - // Load refresh token from UserAcountData.txt - boost::filesystem::path source(boost::filesystem::path(Slic3r::data_dir()) / "UserAcountData.dat") ; - boost::nowide::ifstream stream(source.generic_string(), std::ios::in | std::ios::binary); - if (stream) { - std::getline(stream, refresh_token); - stream.close(); - } else { - BOOST_LOG_TRIVIAL(error) << "UserAccount: Failed to read token from " << source; - } + load_refresh_token_linux(refresh_token); #endif } long long next = next_timeout.empty() ? 0 : std::stoll(next_timeout); @@ -229,7 +255,7 @@ void UserAccountCommunication::set_username(const std::string& username) else { #ifdef __linux__ // If we can't store the tokens in secret store, store them in file with chmod 600 - boost::filesystem::path target(boost::filesystem::path(Slic3r::data_dir()) / "UserAcountData.dat") ; + boost::filesystem::path target(boost::filesystem::path(Slic3r::data_dir()) / "UserAccount.dat") ; std::string data = m_session->get_refresh_token(); FILE* file; static const auto perms = boost::filesystem::owner_read | boost::filesystem::owner_write; // aka 600