From ee67037a3fce6413ec6b6a78a8588d775020f1c8 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Mon, 8 Aug 2022 23:25:34 -0400 Subject: [PATCH] store token in keychain --- src/settings.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/settings.cpp b/src/settings.cpp index 242bd7c..7564d4d 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1,6 +1,15 @@ #include "settings.hpp" #include #include +#include + +#ifdef WITH_KEYCHAIN + #include +#endif + +const std::string KeychainPackage = "com.github.uowuo.abaddon"; +const std::string KeychainService = "client-token"; +const std::string KeychainUser = "discord"; SettingsManager::SettingsManager(const std::string &filename) : m_filename(filename) { @@ -36,7 +45,6 @@ void SettingsManager::ReadSettings() { SMSTR("discord", "api_base", APIBaseURL); SMSTR("discord", "gateway", GatewayURL); - SMSTR("discord", "token", DiscordToken); SMBOOL("discord", "memory_db", UseMemoryDB); SMBOOL("discord", "prefetch", Prefetch); SMSTR("gui", "css", MainCSS); @@ -58,6 +66,32 @@ void SettingsManager::ReadSettings() { SMSTR("style", "mentionbadgetextcolor", MentionBadgeTextColor); SMSTR("style", "unreadcolor", UnreadIndicatorColor); +#ifdef WITH_KEYCHAIN + keychain::Error error {}; + + // convert to keychain if present in normal settings + SMSTR("discord", "token", DiscordToken); + + if (!m_settings.DiscordToken.empty()) { + keychain::Error set_error {}; + + keychain::setPassword(KeychainPackage, KeychainService, KeychainUser, m_settings.DiscordToken, set_error); + if (set_error) { + printf("keychain error setting token: %s\n", set_error.message.c_str()); + } else { + m_file.remove_key("discord", "token"); + } + } + + m_settings.DiscordToken = keychain::getPassword(KeychainPackage, KeychainService, KeychainUser, error); + if (error && error.type != keychain::ErrorType::NotFound) { + printf("keychain error reading token: %s (%d)\n", error.message.c_str(), error.code); + } + +#else + SMSTR("discord", "token", DiscordToken); +#endif + #undef SMBOOL #undef SMSTR #undef SMINT @@ -89,7 +123,6 @@ void SettingsManager::Close() { SMSTR("discord", "api_base", APIBaseURL); SMSTR("discord", "gateway", GatewayURL); - SMSTR("discord", "token", DiscordToken); SMBOOL("discord", "memory_db", UseMemoryDB); SMBOOL("discord", "prefetch", Prefetch); SMSTR("gui", "css", MainCSS); @@ -111,6 +144,17 @@ void SettingsManager::Close() { SMSTR("style", "mentionbadgetextcolor", MentionBadgeTextColor); SMSTR("style", "unreadcolor", UnreadIndicatorColor); +#ifdef WITH_KEYCHAIN + keychain::Error error {}; + + keychain::setPassword(KeychainPackage, KeychainService, KeychainUser, m_settings.DiscordToken, error); + if (error) { + printf("keychain error setting token: %s\n", error.message.c_str()); + } +#else + SMSTR("discord", "token", DiscordToken); +#endif + #undef SMSTR #undef SMBOOL #undef SMINT