MailTransport

gmailpasswordrequester.cpp
1/*
2 SPDX-FileCopyrightText: 2024 g10 Code GmbH
3 SPDX-FileContributor: Daniel Vrátil <dvratil@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "gmailpasswordrequester.h"
9#include "mailtransportplugin_smtp_debug.h"
10#include "plugins/smtp/xoauthpasswordrequester.h"
11#include "transport.h"
12
13#include <KGAPI/Account>
14#include <KGAPI/AccountManager>
15
16using namespace MailTransport;
17
18static const QString apiKey = QStringLiteral("554041944266.apps.googleusercontent.com");
19static const QString apiSecret = QStringLiteral("mdT1DjzohxN3npUUzkENT0gO");
20
21GmailPasswordRequester::GmailPasswordRequester(Transport *transport, QObject *parent)
22 : XOAuthPasswordRequester(transport, parent)
23{
24}
25
26GmailPasswordRequester::~GmailPasswordRequester() = default;
27
28void GmailPasswordRequester::requestPassword(bool forceRefresh)
29{
30 auto promise = KGAPI2::AccountManager::instance()->findAccount(apiKey, transport()->userName(), {KGAPI2::Account::mailScopeUrl()});
31 connect(promise, &KGAPI2::AccountPromise::finished, this, [forceRefresh, this](KGAPI2::AccountPromise *promise) {
32 if (promise->account()) {
33 if (forceRefresh) {
34 promise = KGAPI2::AccountManager::instance()->refreshTokens(apiKey, apiSecret, transport()->userName());
35 } else {
36 onTokenRequestFinished(promise);
37 return;
38 }
39 } else {
40 promise = KGAPI2::AccountManager::instance()->getAccount(apiKey, apiSecret, transport()->userName(), {KGAPI2::Account::mailScopeUrl()});
41 }
42 connect(promise, &KGAPI2::AccountPromise::finished, this, &GmailPasswordRequester::onTokenRequestFinished);
43 });
44}
45
46void GmailPasswordRequester::onTokenRequestFinished(KGAPI2::AccountPromise *promise)
47{
48 if (promise->hasError()) {
49 qCWarning(MAILTRANSPORT_SMTP_LOG) << "Error obtaining XOAUTH2 Gmail token:" << promise->errorText();
50 Q_EMIT done(Error, {});
51 return;
52 }
53
54 const auto account = promise->account();
55 const QString tokens = QStringLiteral("%1\001%2").arg(account->accessToken(), account->refreshToken());
56
57 Q_EMIT done(PasswordRetrieved, tokens);
58}
59
60#include "moc_gmailpasswordrequester.cpp"
void finished(KGAPI2::AccountPromise *self)
static QUrl mailScopeUrl()
Represents the settings of a specific mail transport.
Definition transport.h:33
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString arg(Args &&... args) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:07:33 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.