File indexing completed on 2024-11-17 04:45:06
0001 /* 0002 SPDX-FileCopyrightText: 2016 Daniel Vrátil <dvratil@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "gmailpasswordrequester.h" 0008 #include "imapresourcebase.h" 0009 #include "settings.h" 0010 0011 #include <KGAPI/Account> 0012 #include <KGAPI/AccountManager> 0013 #include <KGAPI/AuthJob> 0014 0015 #define GOOGLE_API_KEY QStringLiteral("554041944266.apps.googleusercontent.com") 0016 #define GOOGLE_API_SECRET QStringLiteral("mdT1DjzohxN3npUUzkENT0gO") 0017 0018 GmailPasswordRequester::GmailPasswordRequester(ImapResourceBase *resource, QObject *parent) 0019 : PasswordRequesterInterface(parent) 0020 , mResource(resource) 0021 { 0022 } 0023 0024 GmailPasswordRequester::~GmailPasswordRequester() = default; 0025 0026 void GmailPasswordRequester::requestPassword(RequestType request, const QString &serverError) 0027 { 0028 Q_UNUSED(serverError) // we don't get anything useful from XOAUTH2 SASL 0029 0030 if (request == WrongPasswordRequest) { 0031 auto promise = KGAPI2::AccountManager::instance()->findAccount(GOOGLE_API_KEY, mResource->settings()->userName()); 0032 connect(promise, &KGAPI2::AccountPromise::finished, this, [this](KGAPI2::AccountPromise *promise) { 0033 if (promise->account()) { 0034 promise = KGAPI2::AccountManager::instance()->refreshTokens(GOOGLE_API_KEY, GOOGLE_API_SECRET, mResource->settings()->userName()); 0035 } else { 0036 promise = KGAPI2::AccountManager::instance()->getAccount(GOOGLE_API_KEY, 0037 GOOGLE_API_SECRET, 0038 mResource->settings()->userName(), 0039 {KGAPI2::Account::mailScopeUrl()}); 0040 } 0041 connect(promise, &KGAPI2::AccountPromise::finished, this, &GmailPasswordRequester::onTokenRequestFinished); 0042 mPendingPromise = promise; 0043 }); 0044 mPendingPromise = promise; 0045 } else { 0046 auto promise = KGAPI2::AccountManager::instance()->getAccount(GOOGLE_API_KEY, 0047 GOOGLE_API_SECRET, 0048 mResource->settings()->userName(), 0049 {KGAPI2::Account::mailScopeUrl()}); 0050 connect(promise, &KGAPI2::AccountPromise::finished, this, &GmailPasswordRequester::onTokenRequestFinished); 0051 mPendingPromise = promise; 0052 } 0053 } 0054 0055 void GmailPasswordRequester::cancelPasswordRequests() 0056 { 0057 if (mPendingPromise) { 0058 mPendingPromise->disconnect(this); 0059 } 0060 } 0061 0062 void GmailPasswordRequester::onTokenRequestFinished(KGAPI2::AccountPromise *promise) 0063 { 0064 mPendingPromise = nullptr; 0065 if (promise->hasError()) { 0066 Q_EMIT done(UserRejected, promise->errorText()); 0067 } else { 0068 Q_EMIT done(PasswordRetrieved, promise->account()->accessToken()); 0069 } 0070 } 0071 0072 #include "moc_gmailpasswordrequester.cpp"