Release KSwitcher v0.7.3
Commit Settings drafts through Apply or Save and make Close and Cancel reflect the pending state.
This commit is contained in:
1 parent
0ce99e5af9
commit
ee9b953533
27 files changed
+951
-323
No files matched your search
+70
-2
@@ -157,10 +157,14 @@ class OpenBao final : public SyncProvider
|
||||
}
|
||||
bool syncVisible(const std::filesystem::path &root) { return std::filesystem::exists(root / L"sync.json"); }
|
||||
SyncConfig loadSyncConfig(const std::filesystem::path &root)
|
||||
{
|
||||
return syncVisible(root) ? decodeSyncConfig(readFile(root / L"sync.json", 16384)) : SyncConfig{};
|
||||
}
|
||||
Bytes encodeSyncConfig(const SyncConfig &config) { return bytes(configJson(config)); }
|
||||
SyncConfig decodeSyncConfig(const Bytes &encoded)
|
||||
{
|
||||
SyncConfig c;
|
||||
if (!syncVisible(root)) return c;
|
||||
auto j = parse(readFile(root / L"sync.json", 16384));
|
||||
auto j = parse(encoded);
|
||||
c.enabled = j.value("enabled", false);
|
||||
c.keepLocalLayouts = j.value("keepLocalLayouts", false);
|
||||
c.provider = wide(j.value("provider", "openbao"));
|
||||
@@ -212,6 +216,70 @@ void forgetSyncPassword(const SyncConfig &c)
|
||||
if (!CredDeleteW(credentialName(c).c_str(), CRED_TYPE_GENERIC, 0) && GetLastError() != ERROR_NOT_FOUND)
|
||||
throw std::runtime_error("Cannot remove saved sync password.");
|
||||
}
|
||||
void applySyncConnection(const std::filesystem::path &root, const SyncConfig &config,
|
||||
const Bytes &protectedPassword, bool forgetPassword, const std::function<void()> &commit)
|
||||
{
|
||||
const auto path = root / L"sync.json";
|
||||
const auto previousFile = syncVisible(root) ? std::optional{readFile(path, 16384)} : std::nullopt;
|
||||
const auto credentialConfig = forgetPassword && previousFile ? decodeSyncConfig(*previousFile) : config;
|
||||
const bool editCredential = forgetPassword || !protectedPassword.empty();
|
||||
PCREDENTIALW previousCredential{};
|
||||
struct Cleanup
|
||||
{
|
||||
PCREDENTIALW &credential;
|
||||
~Cleanup()
|
||||
{
|
||||
if (!credential) return;
|
||||
SecureZeroMemory(credential->CredentialBlob, credential->CredentialBlobSize);
|
||||
CredFree(credential);
|
||||
}
|
||||
} cleanup{previousCredential};
|
||||
if (editCredential &&
|
||||
!CredReadW(credentialName(credentialConfig).c_str(), CRED_TYPE_GENERIC, 0, &previousCredential) &&
|
||||
GetLastError() != ERROR_NOT_FOUND)
|
||||
throw std::runtime_error("Cannot read the saved Windows credential.");
|
||||
|
||||
saveSyncConfig(root, config);
|
||||
bool credentialChanged = false;
|
||||
try
|
||||
{
|
||||
if (forgetPassword) forgetSyncPassword(credentialConfig);
|
||||
else if (!protectedPassword.empty())
|
||||
{
|
||||
auto secret = unprotect(protectedPassword);
|
||||
std::wstring password;
|
||||
try
|
||||
{
|
||||
if (secret.size() % sizeof(wchar_t)) throw std::runtime_error("Invalid Windows credential.");
|
||||
password.resize(secret.size() / sizeof(wchar_t));
|
||||
memcpy(password.data(), secret.data(), secret.size());
|
||||
saveSyncPassword(config, password);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
SecureZeroMemory(password.data(), password.size() * sizeof(wchar_t));
|
||||
SecureZeroMemory(secret.data(), secret.size());
|
||||
throw;
|
||||
}
|
||||
SecureZeroMemory(password.data(), password.size() * sizeof(wchar_t));
|
||||
SecureZeroMemory(secret.data(), secret.size());
|
||||
}
|
||||
credentialChanged = editCredential;
|
||||
commit();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
if (previousFile) atomicWrite(path, *previousFile);
|
||||
else std::filesystem::remove(path);
|
||||
if (credentialChanged)
|
||||
{
|
||||
if (!previousCredential) forgetSyncPassword(credentialConfig);
|
||||
else if (!CredWriteW(previousCredential, 0))
|
||||
throw std::runtime_error("Cannot restore the previous Windows credential.");
|
||||
}
|
||||
throw;
|
||||
}
|
||||
}
|
||||
std::unique_ptr<SyncProvider> makeSyncProvider(const SyncConfig &c, SyncTransport transport)
|
||||
{
|
||||
return std::make_unique<OpenBao>(c, std::move(transport));
|
||||
|
||||
Reference in new issue
Block a user