gmailpasswordrequester.cpp 2.89 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
25
26
27
28
29
30

#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)
{
}

GmailPasswordRequester::~GmailPasswordRequester()
{
}

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

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

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

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