gmailpasswordrequester.cpp 2.9 KB
Newer Older
1
/*
2
    SPDX-FileCopyrightText: 2016 Daniel Vrátil <dvratil@kde.org>
3

4
    SPDX-License-Identifier: LGPL-2.0-or-later
5
6
7
*/

#include "gmailpasswordrequester.h"
Laurent Montel's avatar
Laurent Montel committed
8
#include "imapresource_debug.h"
9
10
11
#include "imapresourcebase.h"
#include "settings.h"

Laurent Montel's avatar
Laurent Montel committed
12
#include <KGAPI/Account>
13
#include <KGAPI/AccountManager>
Laurent Montel's avatar
Laurent Montel committed
14
#include <KGAPI/AuthJob>
15
16
17
18
19
20
21
22
23
24

#define GOOGLE_API_KEY QStringLiteral("554041944266.apps.googleusercontent.com")
#define GOOGLE_API_SECRET QStringLiteral("mdT1DjzohxN3npUUzkENT0gO")

GmailPasswordRequester::GmailPasswordRequester(ImapResourceBase *resource, QObject *parent)
    : PasswordRequesterInterface(parent)
    , mResource(resource)
{
}

Laurent Montel's avatar
Laurent Montel committed
25
GmailPasswordRequester::~GmailPasswordRequester() = default;
26
27
28

void GmailPasswordRequester::requestPassword(RequestType request, const QString &serverError)
{
Laurent Montel's avatar
Laurent Montel committed
29
    Q_UNUSED(serverError) // we don't get anything useful from XOAUTH2 SASL
30
31

    if (request == WrongPasswordRequest) {
32
        auto promise = KGAPI2::AccountManager::instance()->findAccount(GOOGLE_API_KEY, mResource->settings()->userName());
Laurent Montel's avatar
Laurent Montel committed
33
        connect(promise, &KGAPI2::AccountPromise::finished, this, [this](KGAPI2::AccountPromise *promise) {
Laurent Montel's avatar
Laurent Montel committed
34
            if (promise->account()) {
Laurent Montel's avatar
Laurent Montel committed
35
                promise = KGAPI2::AccountManager::instance()->refreshTokens(GOOGLE_API_KEY, GOOGLE_API_SECRET, mResource->settings()->userName());
Laurent Montel's avatar
Laurent Montel committed
36
            } else {
Laurent Montel's avatar
Laurent Montel committed
37
38
39
40
                promise = KGAPI2::AccountManager::instance()->getAccount(GOOGLE_API_KEY,
                                                                         GOOGLE_API_SECRET,
                                                                         mResource->settings()->userName(),
                                                                         {KGAPI2::Account::mailScopeUrl()});
Laurent Montel's avatar
Laurent Montel committed
41
            }
Laurent Montel's avatar
Laurent Montel committed
42
            connect(promise, &KGAPI2::AccountPromise::finished, this, &GmailPasswordRequester::onTokenRequestFinished);
Laurent Montel's avatar
Laurent Montel committed
43
44
            mPendingPromise = promise;
        });
45
        mPendingPromise = promise;
46
    } else {
Laurent Montel's avatar
Laurent Montel committed
47
48
49
50
51
        auto promise = KGAPI2::AccountManager::instance()->getAccount(GOOGLE_API_KEY,
                                                                      GOOGLE_API_SECRET,
                                                                      mResource->settings()->userName(),
                                                                      {KGAPI2::Account::mailScopeUrl()});
        connect(promise, &KGAPI2::AccountPromise::finished, this, &GmailPasswordRequester::onTokenRequestFinished);
52
        mPendingPromise = promise;
53
54
55
56
57
    }
}

void GmailPasswordRequester::cancelPasswordRequests()
{
58
59
    if (mPendingPromise) {
        mPendingPromise->disconnect(this);
60
61
62
    }
}

63
void GmailPasswordRequester::onTokenRequestFinished(KGAPI2::AccountPromise *promise)
64
{
65
66
67
    mPendingPromise = nullptr;
    if (promise->hasError()) {
        Q_EMIT done(UserRejected, promise->errorText());
68
    } else {
69
        Q_EMIT done(PasswordRetrieved, promise->account()->accessToken());
70
71
    }
}