From bc64551d4083e47fad3deefd69f66b745bbbee2d Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 18 Sep 2017 13:12:38 +0200 Subject: keyring --- components/kube/contents/ui/Kube.qml | 7 +++- framework/qml/Messages.qml | 2 + framework/src/CMakeLists.txt | 1 + framework/src/domain/settings/accountsettings.cpp | 17 ++++----- framework/src/keyring.cpp | 46 +++++++++++++++++++++++ framework/src/keyring.h | 37 ++++++++++++++++++ framework/src/sinkfabric.cpp | 10 ++++- 7 files changed, 109 insertions(+), 11 deletions(-) create mode 100644 framework/src/keyring.cpp create mode 100644 framework/src/keyring.h diff --git a/components/kube/contents/ui/Kube.qml b/components/kube/contents/ui/Kube.qml index 86e0f5bb..a9e8af09 100644 --- a/components/kube/contents/ui/Kube.qml +++ b/components/kube/contents/ui/Kube.qml @@ -111,7 +111,12 @@ Controls2.ApplicationWindow { Shortcut { onActivated: Kube.Fabric.postMessage(Kube.Messages.search, {}) sequence: StandardKey.Find - + } + Shortcut { + onActivated: { + Kube.Fabric.postMessage(Kube.Messages.unlockKeyring, {accountId: app.currentAccount}) + } + sequence: "Ctrl+l" } Shortcut { id: syncShortcut diff --git a/framework/qml/Messages.qml b/framework/qml/Messages.qml index 1d2ca002..8d10bc3b 100644 --- a/framework/qml/Messages.qml +++ b/framework/qml/Messages.qml @@ -34,6 +34,8 @@ Item { property string toggleImportant: "toggleImportant" property string moveToFolder: "moveToFolder" property string moveToDrafts: "moveToDrafts" + property string credentials: "credentials" + property string unlockKeyring: "unlockKeyring" property string notification: "notification" property string progressNotification: "progressNotification" diff --git a/framework/src/CMakeLists.txt b/framework/src/CMakeLists.txt index 5e9dd269..b66f45ed 100644 --- a/framework/src/CMakeLists.txt +++ b/framework/src/CMakeLists.txt @@ -47,6 +47,7 @@ set(SRCS krecursivefilterproxymodel.cpp webengineprofile.cpp startupcheck.cpp + keyring.cpp ) add_library(frameworkplugin SHARED ${SRCS}) diff --git a/framework/src/domain/settings/accountsettings.cpp b/framework/src/domain/settings/accountsettings.cpp index 232f7aba..09cdf279 100644 --- a/framework/src/domain/settings/accountsettings.cpp +++ b/framework/src/domain/settings/accountsettings.cpp @@ -24,6 +24,8 @@ #include #include +#include "keyring.h" + using namespace Sink; using namespace Sink::ApplicationDomain; @@ -196,7 +198,6 @@ void AccountSettings::loadImapResource() mImapIdentifier = resource.identifier(); mImapServer = resource.getProperty("server").toString(); mImapUsername = resource.getProperty("username").toString(); - mImapPassword = resource.getProperty("password").toString(); emit imapResourceChanged(); }).onError([](const KAsync::Error &error) { qWarning() << "Failed to load the imap resource: " << error.errorMessage; @@ -222,7 +223,6 @@ void AccountSettings::loadMailtransportResource() mMailtransportIdentifier = resource.identifier(); mSmtpServer = resource.getProperty("server").toString(); mSmtpUsername = resource.getProperty("username").toString(); - mSmtpPassword = resource.getProperty("password").toString(); emit smtpResourceChanged(); }).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to load the smtp resource: " << error.errorMessage; @@ -250,7 +250,6 @@ void AccountSettings::loadCardDavResource() mCardDavIdentifier = resource.identifier(); mCardDavServer = resource.getProperty("server").toString(); mCardDavUsername = resource.getProperty("username").toString(); - mCardDavPassword = resource.getProperty("password").toString(); emit cardDavResourceChanged(); }).onError([](const KAsync::Error &error) { qWarning() << "Failed to load the CardDAV resource: " << error.errorMessage; @@ -291,18 +290,18 @@ void AccountSettings::saveImapResource() { mImapIdentifier = saveResource(mAccountIdentifier, mImapIdentifier, { {"server", mImapServer}, - {"username", mImapUsername}, - {"password", mImapPassword}, + {"username", mImapUsername} }); + Kube::Keyring{mAccountIdentifier}.storePassword(mImapIdentifier, mImapPassword); } void AccountSettings::saveCardDavResource() { mCardDavIdentifier = saveResource(mAccountIdentifier, mCardDavIdentifier, { {"server", mCardDavServer}, - {"username", mCardDavUsername}, - {"password", mCardDavPassword}, + {"username", mCardDavUsername} }); + Kube::Keyring{mAccountIdentifier}.storePassword(mCardDavIdentifier, mCardDavPassword); } void AccountSettings::saveMaildirResource() @@ -316,9 +315,9 @@ void AccountSettings::saveMailtransportResource() { mMailtransportIdentifier = saveResource(mAccountIdentifier, mMailtransportIdentifier, { {"server", mSmtpServer}, - {"username", mSmtpUsername}, - {"password", mSmtpPassword}, + {"username", mSmtpUsername} }); + Kube::Keyring{mAccountIdentifier}.storePassword(mMailtransportIdentifier, mSmtpPassword); } void AccountSettings::saveIdentity() diff --git a/framework/src/keyring.cpp b/framework/src/keyring.cpp new file mode 100644 index 00000000..759d0c4c --- /dev/null +++ b/framework/src/keyring.cpp @@ -0,0 +1,46 @@ +/* + Copyright (c) 2017 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ +#include "keyring.h" + +#include +#include + +using namespace Kube; + +Keyring::Keyring(const QByteArray &accountId, QObject *parent) + : QObject(parent), + mAccountIdentifier(accountId) +{ +} + +void Keyring::storePassword(const QByteArray &resourceId, const QString &password) +{ + QSettings settings{mAccountIdentifier + ".keyring", QSettings::IniFormat}; + settings.setValue(resourceId, password); + Sink::SecretStore::instance().insert(resourceId, password); +} + +void Keyring::unlock() +{ + QSettings settings{mAccountIdentifier + ".keyring", QSettings::IniFormat}; + for (const auto &resourceId : settings.allKeys()) { + Sink::SecretStore::instance().insert(resourceId.toLatin1(), settings.value(resourceId).toString()); + } +} + diff --git a/framework/src/keyring.h b/framework/src/keyring.h new file mode 100644 index 00000000..ee9c3577 --- /dev/null +++ b/framework/src/keyring.h @@ -0,0 +1,37 @@ +/* + Copyright (c) 2017 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include + +namespace Kube { + +class Keyring : public QObject { + Q_OBJECT +public: + Keyring(const QByteArray &accountId, QObject *parent = nullptr); + void storePassword(const QByteArray &resourceId, const QString &password); + void unlock(); + +private: + Q_DISABLE_COPY(Keyring); + + QByteArray mAccountIdentifier; +}; + +} diff --git a/framework/src/sinkfabric.cpp b/framework/src/sinkfabric.cpp index 79afd33b..954186bb 100644 --- a/framework/src/sinkfabric.cpp +++ b/framework/src/sinkfabric.cpp @@ -24,8 +24,10 @@ #include #include #include +#include #include "fabric.h" +#include "keyring.h" using namespace Kube; using namespace Sink; @@ -131,7 +133,10 @@ public: Store::modify(*mail).exec(); } } - + if (id == "unlockKeyring") { + auto accountId = message["accountId"].value(); + Kube::Keyring{accountId}.unlock(); + } } }; @@ -180,6 +185,9 @@ public: case Sink::ApplicationDomain::ConnectionLostError: message["message"] = QObject::tr("Connection lost."); break; + case Sink::ApplicationDomain::MissingCredentialsError: + message["message"] = QObject::tr("No credentials available."); + break; default: message["message"] = QObject::tr("An unknown error occurred."); } -- cgit v1.2.3