getcredentialsjob.cpp 4.14 KB
Newer Older
1
2
3
4
5
/*
 *  SPDX-FileCopyrightText: 2013 Alejandro Fiestas Olivares <afiestas@kde.org>
 *
 *  SPDX-License-Identifier: LGPL-2.0-or-later
 */
6
#include "getcredentialsjob.h"
7
#include "core.h"
8
9
10
11
12
13
14

#include <Accounts/Manager>
#include <Accounts/Account>
#include <Accounts/AccountService>

#include <SignOn/Identity>

Laurent Montel's avatar
Laurent Montel committed
15
16
#include <KLocalizedString>

Martin Klapetek's avatar
Martin Klapetek committed
17
#include <QDebug>
18
19
20
21
#include <QTimer>

class GetCredentialsJob::Private {
public:
22
23
24
25
26
    Private(GetCredentialsJob *job)
    {
        q = job;
    }

27
28
29
30
31
32
33
34
    QString serviceType;
    QString authMechanism;
    QString authMethod;
    Accounts::AccountId id;
    QVariantMap authData;
    Accounts::Manager *manager;
    SignOn::SessionData sessionData;
    uint repeatedTries;
35
36
37
    GetCredentialsJob *q;

    void getCredentials();
38
39
};

40
41
42
43
44
45
46
47
48
49
50
51
void GetCredentialsJob::Private::getCredentials()
{
    Accounts::Account *acc = manager->account(id);
    if (!acc) {
        qWarning() << "Unable to find account for id" << id;
        if (repeatedTries < 3) {
            qDebug() << "Retrying in 2s";
            QTimer::singleShot(2000, q, SLOT(getCredentials()));
            repeatedTries++;
        } else {
            qDebug() << repeatedTries << "ending with error";
            q->setError(KJob::UserDefinedError);
Laurent Montel's avatar
Laurent Montel committed
52
            q->setErrorText(i18n("Could not find account"));
53
54
55
56
57
58
59
60
61
            q->emitResult();
        }
        return;
    }

    Accounts::AccountService *service = new Accounts::AccountService(acc, manager->service(serviceType), q);

    Accounts::AuthData serviceAuthData = service->authData();
    authData = serviceAuthData.parameters();
62
    SignOn::Identity *identity = SignOn::Identity::existingIdentity(acc->credentialsId(), q);
63
64
65
66

    if (!identity) {
        qWarning() << "Unable to find identity for account id" << id;
        q->setError(KJob::UserDefinedError);
Laurent Montel's avatar
Laurent Montel committed
67
        q->setErrorText(i18n("Could not find credentials"));
68
69
70
71
        q->emitResult();
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
72
    authData[QStringLiteral("AccountUsername")] = acc->value(QStringLiteral("username")).toString();
73
    QPointer<SignOn::AuthSession> authSession = identity->createSession(authMethod.isEmpty() ? serviceAuthData.method() : authMethod);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
74
75
76
    if (!authSession) {
        qWarning() << "Unable to create auth session for" << authMethod << serviceAuthData.method();
        q->setError(KJob::UserDefinedError);
Laurent Montel's avatar
Laurent Montel committed
77
        q->setErrorText(i18n("Could not create auth session"));
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
78
79
80
        q->emitResult();
        return;
    }
81

Nicolas Fella's avatar
Nicolas Fella committed
82
    QObject::connect(authSession.data(), &SignOn::AuthSession::response, q,
83
            [this](const SignOn::SessionData &data) {
84
85
86
87
                sessionData = data;
                q->emitResult();
            });

Nicolas Fella's avatar
Nicolas Fella committed
88
    QObject::connect(authSession.data(), &SignOn::AuthSession::error, q,
89
90
91
92
93
94
95
96
97
98
            [this](const SignOn::Error &error) {
                qDebug() << error.message();
                q->setError(KJob::UserDefinedError);
                q->setErrorText(error.message());
                q->emitResult();
            });

    authSession->process(serviceAuthData.parameters(), authMechanism.isEmpty() ? serviceAuthData.mechanism() : authMechanism);
}

Laurent Montel's avatar
Laurent Montel committed
99
GetCredentialsJob::GetCredentialsJob(Accounts::AccountId id, QObject *parent)
100
    : KJob(parent)
101
    , d(new Private(this))
102
{
103
104
105
    d->id = id;
    d->manager = KAccounts::accountsManager();
    d->repeatedTries = 0;
106
    d->serviceType = QString();
107
108
}

109

Laurent Montel's avatar
Laurent Montel committed
110
GetCredentialsJob::GetCredentialsJob(Accounts::AccountId id, const QString &authMethod, const QString &authMechanism, QObject *parent)
111
    : KJob(parent)
112
    , d(new Private(this))
113
114
115
116
117
{
    d->id = id;
    d->manager = KAccounts::accountsManager();
    d->authMechanism = authMechanism;
    d->authMethod = authMethod;
118
    d->repeatedTries = 0;
119
    d->serviceType = QString();
120
121
}

Laurent Montel's avatar
Laurent Montel committed
122
123
124
125
126
GetCredentialsJob::~GetCredentialsJob()
{
    delete d;
}

127
void GetCredentialsJob::start()
128
129
130
131
{
    QMetaObject::invokeMethod(this, "getCredentials", Qt::QueuedConnection);
}

132
void GetCredentialsJob::setServiceType(const QString& serviceType)
133
{
134
    d->serviceType = serviceType;
135
136
}

137
Accounts::AccountId GetCredentialsJob::accountId() const
138
{
139
    return d->id;
140
141
142
143
}

QVariantMap GetCredentialsJob::credentialsData() const
{
144
    return d->sessionData.toMap().unite(d->authData);
145
}
146
147

#include "moc_getcredentialsjob.cpp"