diff options
author | Tobias Fella <fella@posteo.de> | 2022-05-27 21:03:31 +0200 |
---|---|---|
committer | Tobias Fella <fella@posteo.de> | 2022-05-27 21:13:44 +0200 |
commit | 946cd4cb73f95526aa09777afc2a493610d9696d (patch) | |
tree | ee3bb02888addc2afc6d51528574d6b2cfa31b9d /lib/connection.cpp | |
parent | 729ba7da174eacc88bf9bd4e2e80eeab3fc92716 (diff) | |
download | libquotient-946cd4cb73f95526aa09777afc2a493610d9696d.tar.gz libquotient-946cd4cb73f95526aa09777afc2a493610d9696d.zip |
Load and store accounts in the keychain
Diffstat (limited to 'lib/connection.cpp')
-rw-r--r-- | lib/connection.cpp | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp index dba18cb1..526feb26 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -41,12 +41,12 @@ # include "e2ee/qolmsession.h" # include "e2ee/qolmutils.h" -# if QT_VERSION_MAJOR >= 6 -# include <qt6keychain/keychain.h> -# else -# include <qt5keychain/keychain.h> -# endif #endif // Quotient_E2EE_ENABLED +#if QT_VERSION_MAJOR >= 6 +# include <qt6keychain/keychain.h> +#else +# include <qt5keychain/keychain.h> +#endif #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) # include <QtCore/QCborValue> @@ -368,6 +368,32 @@ public: void saveDevicesList(); void loadDevicesList(); #endif + + void saveAccessTokenToKeychain() + { + qCDebug(MAIN) << "Saving access token to keychain for" << q->userId(); + auto job = new QKeychain::WritePasswordJob(qAppName()); + job->setAutoDelete(false); + job->setKey(q->userId()); + job->setBinaryData(data->accessToken()); + job->start(); + //TODO error handling + } + + void removeAccessTokenFromKeychain() + { + qCDebug(MAIN) << "Removing access token from keychain for" << q->userId(); + auto job = new QKeychain::DeletePasswordJob(qAppName()); + job->setAutoDelete(true); + job->setKey(q->userId()); + job->start(); + + auto pickleJob = new QKeychain::DeletePasswordJob(qAppName()); + pickleJob->setAutoDelete(true); + pickleJob->setKey(q->userId() + "-Pickle"_ls); + pickleJob->start(); + //TODO error handling + } }; Connection::Connection(const QUrl& server, QObject* parent) @@ -546,6 +572,7 @@ void Connection::Private::loginToServer(LoginArgTs&&... loginArgs) data->setToken(loginJob->accessToken().toLatin1()); data->setDeviceId(loginJob->deviceId()); completeSetup(loginJob->userId()); + saveAccessTokenToKeychain(); #ifndef Quotient_E2EE_ENABLED qCWarning(E2EE) << "End-to-end encryption (E2EE) support is turned off."; #else // Quotient_E2EE_ENABLED @@ -684,6 +711,8 @@ void Connection::logout() disconnect(d->syncLoopConnection); d->data->setToken({}); emit loggedOut(); + SettingsGroup("Accounts").remove(userId()); + d->removeAccessTokenFromKeychain(); deleteLater(); } else { // logout() somehow didn't proceed - restore the session state emit stateChanged(); |