diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-09-18 11:40:41 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-09-18 11:40:45 +0200 |
commit | a7e7f7fdd2a9d38921476d57f305c9cd4459a556 (patch) | |
tree | d9ad3bdc3e275004a54f508025f0d52227ab18cb /examples | |
parent | ea2e02ad656640c17d520b5a22c168c3c1faef56 (diff) | |
download | sink-a7e7f7fdd2a9d38921476d57f305c9cd4459a556.tar.gz sink-a7e7f7fdd2a9d38921476d57f305c9cd4459a556.zip |
Avoid storing the password in the configuration
The password (or any other secret), is now cached in the client process
(in-memory only), and delivered to the resource via command.
The resource avoids doing any operations against the source until the
secret is available.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/davresource/davresource.cpp | 30 | ||||
-rw-r--r-- | examples/davresource/davresource.h | 5 | ||||
-rw-r--r-- | examples/dummyresource/resourcefactory.cpp | 2 | ||||
-rw-r--r-- | examples/imapresource/imapresource.cpp | 16 | ||||
-rw-r--r-- | examples/imapresource/imapserverproxy.cpp | 3 | ||||
-rw-r--r-- | examples/imapresource/imapserverproxy.h | 1 | ||||
-rw-r--r-- | examples/maildirresource/maildirresource.cpp | 2 |
7 files changed, 34 insertions, 25 deletions
diff --git a/examples/davresource/davresource.cpp b/examples/davresource/davresource.cpp index 22c502f..fa5e612 100644 --- a/examples/davresource/davresource.cpp +++ b/examples/davresource/davresource.cpp | |||
@@ -132,8 +132,8 @@ public: | |||
132 | KAsync::Job<void> synchronizeWithSource(const Sink::QueryBase &query) Q_DECL_OVERRIDE | 132 | KAsync::Job<void> synchronizeWithSource(const Sink::QueryBase &query) Q_DECL_OVERRIDE |
133 | { | 133 | { |
134 | if (query.type() == ApplicationDomain::getTypeName<ApplicationDomain::Addressbook>()) { | 134 | if (query.type() == ApplicationDomain::getTypeName<ApplicationDomain::Addressbook>()) { |
135 | SinkLogCtx(mLogCtx) << "Synchronizing addressbooks:" << mResourceUrl.url(); | 135 | SinkLogCtx(mLogCtx) << "Synchronizing addressbooks:" << resourceUrl().url(); |
136 | auto collectionsFetchJob = new KDAV2::DavCollectionsFetchJob(mResourceUrl); | 136 | auto collectionsFetchJob = new KDAV2::DavCollectionsFetchJob(resourceUrl()); |
137 | auto job = runJob(collectionsFetchJob).then([this, collectionsFetchJob] (const KAsync::Error &error) { | 137 | auto job = runJob(collectionsFetchJob).then([this, collectionsFetchJob] (const KAsync::Error &error) { |
138 | if (error) { | 138 | if (error) { |
139 | SinkWarningCtx(mLogCtx) << "Failed to synchronize addressbooks." << collectionsFetchJob->errorString(); | 139 | SinkWarningCtx(mLogCtx) << "Failed to synchronize addressbooks." << collectionsFetchJob->errorString(); |
@@ -147,7 +147,7 @@ public: | |||
147 | auto ridList = QSharedPointer<QByteArrayList>::create(); | 147 | auto ridList = QSharedPointer<QByteArrayList>::create(); |
148 | auto total = QSharedPointer<int>::create(0); | 148 | auto total = QSharedPointer<int>::create(0); |
149 | auto progress = QSharedPointer<int>::create(0); | 149 | auto progress = QSharedPointer<int>::create(0); |
150 | auto collectionsFetchJob = new KDAV2::DavCollectionsFetchJob(mResourceUrl); | 150 | auto collectionsFetchJob = new KDAV2::DavCollectionsFetchJob(resourceUrl()); |
151 | auto job = runJob(collectionsFetchJob).then([this, collectionsFetchJob] { | 151 | auto job = runJob(collectionsFetchJob).then([this, collectionsFetchJob] { |
152 | synchronizeAddressbooks(collectionsFetchJob ->collections()); | 152 | synchronizeAddressbooks(collectionsFetchJob ->collections()); |
153 | return collectionsFetchJob->collections(); | 153 | return collectionsFetchJob->collections(); |
@@ -233,8 +233,20 @@ KAsync::Job<QByteArray> replay(const ApplicationDomain::Contact &contact, Sink:: | |||
233 | return KAsync::null<QByteArray>(); | 233 | return KAsync::null<QByteArray>(); |
234 | } | 234 | } |
235 | 235 | ||
236 | KDAV2::DavUrl resourceUrl() const | ||
237 | { | ||
238 | if (secret().isEmpty()) { | ||
239 | return {}; | ||
240 | } | ||
241 | auto resourceUrl = mServer; | ||
242 | resourceUrl.setUserName(mUsername); | ||
243 | resourceUrl.setPassword(secret()); | ||
244 | return KDAV2::DavUrl{resourceUrl, KDAV2::CardDav}; | ||
245 | } | ||
246 | |||
236 | public: | 247 | public: |
237 | KDAV2::DavUrl mResourceUrl; | 248 | QUrl mServer; |
249 | QString mUsername; | ||
238 | }; | 250 | }; |
239 | 251 | ||
240 | 252 | ||
@@ -242,14 +254,12 @@ DavResource::DavResource(const Sink::ResourceContext &resourceContext) | |||
242 | : Sink::GenericResource(resourceContext) | 254 | : Sink::GenericResource(resourceContext) |
243 | { | 255 | { |
244 | auto config = ResourceConfig::getConfiguration(resourceContext.instanceId()); | 256 | auto config = ResourceConfig::getConfiguration(resourceContext.instanceId()); |
245 | auto resourceUrl = QUrl::fromUserInput(config.value("server").toString()); | 257 | auto server = QUrl::fromUserInput(config.value("server").toString()); |
246 | resourceUrl.setUserName(config.value("username").toString()); | 258 | auto username = config.value("username").toString(); |
247 | resourceUrl.setPassword(config.value("password").toString()); | ||
248 | |||
249 | mResourceUrl = KDAV2::DavUrl(resourceUrl, KDAV2::CardDav); | ||
250 | 259 | ||
251 | auto synchronizer = QSharedPointer<ContactSynchronizer>::create(resourceContext); | 260 | auto synchronizer = QSharedPointer<ContactSynchronizer>::create(resourceContext); |
252 | synchronizer->mResourceUrl = mResourceUrl; | 261 | synchronizer->mServer = server; |
262 | synchronizer->mUsername = username; | ||
253 | setupSynchronizer(synchronizer); | 263 | setupSynchronizer(synchronizer); |
254 | 264 | ||
255 | setupPreprocessors(ENTITY_TYPE_CONTACT, QVector<Sink::Preprocessor*>() << new ContactPropertyExtractor); | 265 | setupPreprocessors(ENTITY_TYPE_CONTACT, QVector<Sink::Preprocessor*>() << new ContactPropertyExtractor); |
diff --git a/examples/davresource/davresource.h b/examples/davresource/davresource.h index db175a4..b4f9e5a 100644 --- a/examples/davresource/davresource.h +++ b/examples/davresource/davresource.h | |||
@@ -44,11 +44,6 @@ class DavResource : public Sink::GenericResource | |||
44 | { | 44 | { |
45 | public: | 45 | public: |
46 | DavResource(const Sink::ResourceContext &resourceContext); | 46 | DavResource(const Sink::ResourceContext &resourceContext); |
47 | |||
48 | private: | ||
49 | QStringList listAvailableFolders(); | ||
50 | |||
51 | KDAV2::DavUrl mResourceUrl; | ||
52 | }; | 47 | }; |
53 | 48 | ||
54 | class DavResourceFactory : public Sink::ResourceFactory | 49 | class DavResourceFactory : public Sink::ResourceFactory |
diff --git a/examples/dummyresource/resourcefactory.cpp b/examples/dummyresource/resourcefactory.cpp index dffdfc9..f3c8be2 100644 --- a/examples/dummyresource/resourcefactory.cpp +++ b/examples/dummyresource/resourcefactory.cpp | |||
@@ -50,7 +50,7 @@ class DummySynchronizer : public Sink::Synchronizer { | |||
50 | DummySynchronizer(const Sink::ResourceContext &context) | 50 | DummySynchronizer(const Sink::ResourceContext &context) |
51 | : Sink::Synchronizer(context) | 51 | : Sink::Synchronizer(context) |
52 | { | 52 | { |
53 | 53 | setSecret("dummy"); | |
54 | } | 54 | } |
55 | 55 | ||
56 | Sink::ApplicationDomain::Event::Ptr createEvent(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data) | 56 | Sink::ApplicationDomain::Event::Ptr createEvent(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data) |
diff --git a/examples/imapresource/imapresource.cpp b/examples/imapresource/imapresource.cpp index 3ae7fd7..2aba6b0 100644 --- a/examples/imapresource/imapresource.cpp +++ b/examples/imapresource/imapresource.cpp | |||
@@ -470,7 +470,7 @@ public: | |||
470 | { | 470 | { |
471 | SinkTrace() << "Connecting to:" << mServer << mPort; | 471 | SinkTrace() << "Connecting to:" << mServer << mPort; |
472 | SinkTrace() << "as:" << mUser; | 472 | SinkTrace() << "as:" << mUser; |
473 | return imap->login(mUser, mPassword) | 473 | return imap->login(mUser, secret()) |
474 | .addToContext(imap); | 474 | .addToContext(imap); |
475 | } | 475 | } |
476 | 476 | ||
@@ -513,6 +513,8 @@ public: | |||
513 | return {ApplicationDomain::NoServerError, error.errorMessage}; | 513 | return {ApplicationDomain::NoServerError, error.errorMessage}; |
514 | case Imap::ConnectionLost: | 514 | case Imap::ConnectionLost: |
515 | return {ApplicationDomain::ConnectionLostError, error.errorMessage}; | 515 | return {ApplicationDomain::ConnectionLostError, error.errorMessage}; |
516 | case Imap::MissingCredentialsError: | ||
517 | return {ApplicationDomain::MissingCredentialsError, error.errorMessage}; | ||
516 | default: | 518 | default: |
517 | return {ApplicationDomain::UnknownError, error.errorMessage}; | 519 | return {ApplicationDomain::UnknownError, error.errorMessage}; |
518 | } | 520 | } |
@@ -631,7 +633,7 @@ public: | |||
631 | } | 633 | } |
632 | } | 634 | } |
633 | auto imap = QSharedPointer<ImapServerProxy>::create(mServer, mPort, &mSessionCache); | 635 | auto imap = QSharedPointer<ImapServerProxy>::create(mServer, mPort, &mSessionCache); |
634 | auto login = imap->login(mUser, mPassword); | 636 | auto login = imap->login(mUser, secret()); |
635 | KAsync::Job<QByteArray> job = KAsync::null<QByteArray>(); | 637 | KAsync::Job<QByteArray> job = KAsync::null<QByteArray>(); |
636 | if (operation == Sink::Operation_Creation) { | 638 | if (operation == Sink::Operation_Creation) { |
637 | const QString mailbox = syncStore().resolveLocalId(ENTITY_TYPE_FOLDER, mail.getFolder()); | 639 | const QString mailbox = syncStore().resolveLocalId(ENTITY_TYPE_FOLDER, mail.getFolder()); |
@@ -716,7 +718,7 @@ public: | |||
716 | } | 718 | } |
717 | } | 719 | } |
718 | auto imap = QSharedPointer<ImapServerProxy>::create(mServer, mPort, &mSessionCache); | 720 | auto imap = QSharedPointer<ImapServerProxy>::create(mServer, mPort, &mSessionCache); |
719 | auto login = imap->login(mUser, mPassword); | 721 | auto login = imap->login(mUser, secret()); |
720 | if (operation == Sink::Operation_Creation) { | 722 | if (operation == Sink::Operation_Creation) { |
721 | QString parentFolder; | 723 | QString parentFolder; |
722 | if (!folder.getParent().isEmpty()) { | 724 | if (!folder.getParent().isEmpty()) { |
@@ -736,7 +738,7 @@ public: | |||
736 | }); | 738 | }); |
737 | } else { //We try to merge special purpose folders first | 739 | } else { //We try to merge special purpose folders first |
738 | auto specialPurposeFolders = QSharedPointer<QHash<QByteArray, QString>>::create(); | 740 | auto specialPurposeFolders = QSharedPointer<QHash<QByteArray, QString>>::create(); |
739 | auto mergeJob = imap->login(mUser, mPassword) | 741 | auto mergeJob = imap->login(mUser, secret()) |
740 | .then(imap->fetchFolders([=](const Imap::Folder &folder) { | 742 | .then(imap->fetchFolders([=](const Imap::Folder &folder) { |
741 | if (SpecialPurpose::isSpecialPurposeFolderName(folder.name())) { | 743 | if (SpecialPurpose::isSpecialPurposeFolderName(folder.name())) { |
742 | specialPurposeFolders->insert(SpecialPurpose::getSpecialPurposeType(folder.name()), folder.path()); | 744 | specialPurposeFolders->insert(SpecialPurpose::getSpecialPurposeType(folder.name()), folder.path()); |
@@ -790,7 +792,6 @@ public: | |||
790 | QString mServer; | 792 | QString mServer; |
791 | int mPort; | 793 | int mPort; |
792 | QString mUser; | 794 | QString mUser; |
793 | QString mPassword; | ||
794 | int mDaysToSync = 0; | 795 | int mDaysToSync = 0; |
795 | QByteArray mResourceInstanceIdentifier; | 796 | QByteArray mResourceInstanceIdentifier; |
796 | Imap::SessionCache mSessionCache; | 797 | Imap::SessionCache mSessionCache; |
@@ -959,7 +960,6 @@ ImapResource::ImapResource(const ResourceContext &resourceContext) | |||
959 | auto server = config.value("server").toString(); | 960 | auto server = config.value("server").toString(); |
960 | auto port = config.value("port").toInt(); | 961 | auto port = config.value("port").toInt(); |
961 | auto user = config.value("username").toString(); | 962 | auto user = config.value("username").toString(); |
962 | auto password = config.value("password").toString(); | ||
963 | if (server.startsWith("imap")) { | 963 | if (server.startsWith("imap")) { |
964 | server.remove("imap://"); | 964 | server.remove("imap://"); |
965 | server.remove("imaps://"); | 965 | server.remove("imaps://"); |
@@ -974,7 +974,6 @@ ImapResource::ImapResource(const ResourceContext &resourceContext) | |||
974 | synchronizer->mServer = server; | 974 | synchronizer->mServer = server; |
975 | synchronizer->mPort = port; | 975 | synchronizer->mPort = port; |
976 | synchronizer->mUser = user; | 976 | synchronizer->mUser = user; |
977 | synchronizer->mPassword = password; | ||
978 | synchronizer->mDaysToSync = 14; | 977 | synchronizer->mDaysToSync = 14; |
979 | setupSynchronizer(synchronizer); | 978 | setupSynchronizer(synchronizer); |
980 | 979 | ||
@@ -982,7 +981,8 @@ ImapResource::ImapResource(const ResourceContext &resourceContext) | |||
982 | inspector->mServer = server; | 981 | inspector->mServer = server; |
983 | inspector->mPort = port; | 982 | inspector->mPort = port; |
984 | inspector->mUser = user; | 983 | inspector->mUser = user; |
985 | inspector->mPassword = password; | 984 | //TODO |
985 | // inspector->mPassword = password; | ||
986 | setupInspector(inspector); | 986 | setupInspector(inspector); |
987 | 987 | ||
988 | setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new SpecialPurposeProcessor << new MailPropertyExtractor); | 988 | setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new SpecialPurposeProcessor << new MailPropertyExtractor); |
diff --git a/examples/imapresource/imapserverproxy.cpp b/examples/imapresource/imapserverproxy.cpp index 16887b1..317fbdc 100644 --- a/examples/imapresource/imapserverproxy.cpp +++ b/examples/imapresource/imapserverproxy.cpp | |||
@@ -139,6 +139,9 @@ ImapServerProxy::ImapServerProxy(const QString &serverUrl, int port, SessionCach | |||
139 | 139 | ||
140 | KAsync::Job<void> ImapServerProxy::login(const QString &username, const QString &password) | 140 | KAsync::Job<void> ImapServerProxy::login(const QString &username, const QString &password) |
141 | { | 141 | { |
142 | if (password.isEmpty()) { | ||
143 | return KAsync::error(Imap::MissingCredentialsError); | ||
144 | } | ||
142 | if (mSessionCache) { | 145 | if (mSessionCache) { |
143 | auto session = mSessionCache->getSession(); | 146 | auto session = mSessionCache->getSession(); |
144 | if (session.isValid()) { | 147 | if (session.isValid()) { |
diff --git a/examples/imapresource/imapserverproxy.h b/examples/imapresource/imapserverproxy.h index 86e3378..9e73f68 100644 --- a/examples/imapresource/imapserverproxy.h +++ b/examples/imapresource/imapserverproxy.h | |||
@@ -35,6 +35,7 @@ enum ErrorCode { | |||
35 | CouldNotConnectError, | 35 | CouldNotConnectError, |
36 | SslHandshakeError, | 36 | SslHandshakeError, |
37 | ConnectionLost, | 37 | ConnectionLost, |
38 | MissingCredentialsError, | ||
38 | UnknownError | 39 | UnknownError |
39 | }; | 40 | }; |
40 | 41 | ||
diff --git a/examples/maildirresource/maildirresource.cpp b/examples/maildirresource/maildirresource.cpp index b406f63..41f2433 100644 --- a/examples/maildirresource/maildirresource.cpp +++ b/examples/maildirresource/maildirresource.cpp | |||
@@ -215,7 +215,7 @@ public: | |||
215 | MaildirSynchronizer(const Sink::ResourceContext &resourceContext) | 215 | MaildirSynchronizer(const Sink::ResourceContext &resourceContext) |
216 | : Sink::Synchronizer(resourceContext) | 216 | : Sink::Synchronizer(resourceContext) |
217 | { | 217 | { |
218 | 218 | setSecret("dummy"); | |
219 | } | 219 | } |
220 | 220 | ||
221 | static QStringList listRecursive( const QString &root, const KPIM::Maildir &dir ) | 221 | static QStringList listRecursive( const QString &root, const KPIM::Maildir &dir ) |