File indexing completed on 2024-04-14 04:51:28

0001 /*
0002  *  SPDX-FileCopyrightText: 2013 Alejandro Fiestas Olivares <afiestas@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 #include "getcredentialsjob.h"
0007 #include "core.h"
0008 
0009 #include <Accounts/Account>
0010 #include <Accounts/AccountService>
0011 #include <Accounts/Manager>
0012 
0013 #include <SignOn/Identity>
0014 
0015 #include <KLocalizedString>
0016 
0017 #include <QDebug>
0018 #include <QTimer>
0019 
0020 namespace KAccounts
0021 {
0022 
0023 class GetCredentialsJob::Private
0024 {
0025 public:
0026     Private(GetCredentialsJob *job)
0027     {
0028         q = job;
0029     }
0030 
0031     QString serviceType;
0032     QString authMechanism;
0033     QString authMethod;
0034     Accounts::AccountId id;
0035     QVariantMap authData;
0036     Accounts::Manager *manager;
0037     SignOn::SessionData sessionData;
0038     uint repeatedTries;
0039     GetCredentialsJob *q;
0040 
0041     void getCredentials();
0042 };
0043 
0044 void GetCredentialsJob::Private::getCredentials()
0045 {
0046     Accounts::Account *acc = manager->account(id);
0047     if (!acc) {
0048         qWarning() << "Unable to find account for id" << id;
0049         if (repeatedTries < 3) {
0050             qDebug() << "Retrying in 2s";
0051             QTimer::singleShot(2000, q, SLOT(getCredentials()));
0052             repeatedTries++;
0053         } else {
0054             qDebug() << repeatedTries << "ending with error";
0055             q->setError(KJob::UserDefinedError);
0056             q->setErrorText(i18n("Could not find account"));
0057             q->emitResult();
0058         }
0059         return;
0060     }
0061 
0062     Accounts::AccountService *service = new Accounts::AccountService(acc, manager->service(serviceType), q);
0063 
0064     Accounts::AuthData serviceAuthData = service->authData();
0065     authData = serviceAuthData.parameters();
0066     SignOn::Identity *identity = SignOn::Identity::existingIdentity(acc->credentialsId(), q);
0067 
0068     if (!identity) {
0069         qWarning() << "Unable to find identity for account id" << id;
0070         q->setError(KJob::UserDefinedError);
0071         q->setErrorText(i18n("Could not find credentials"));
0072         q->emitResult();
0073         return;
0074     }
0075 
0076     authData[QStringLiteral("AccountUsername")] = acc->value(QStringLiteral("username")).toString();
0077     QPointer<SignOn::AuthSession> authSession = identity->createSession(authMethod.isEmpty() ? serviceAuthData.method() : authMethod);
0078     if (!authSession) {
0079         qWarning() << "Unable to create auth session for" << authMethod << serviceAuthData.method();
0080         q->setError(KJob::UserDefinedError);
0081         q->setErrorText(i18n("Could not create auth session"));
0082         q->emitResult();
0083         return;
0084     }
0085 
0086     QObject::connect(authSession.data(), &SignOn::AuthSession::response, q, [this](const SignOn::SessionData &data) {
0087         sessionData = data;
0088         q->emitResult();
0089     });
0090 
0091     QObject::connect(authSession.data(), &SignOn::AuthSession::error, q, [this](const SignOn::Error &error) {
0092         qDebug() << error.message();
0093         q->setError(KJob::UserDefinedError);
0094         q->setErrorText(error.message());
0095         q->emitResult();
0096     });
0097 
0098     authSession->process(serviceAuthData.parameters(), authMechanism.isEmpty() ? serviceAuthData.mechanism() : authMechanism);
0099 }
0100 
0101 GetCredentialsJob::GetCredentialsJob(Accounts::AccountId id, QObject *parent)
0102     : KJob(parent)
0103     , d(new Private(this))
0104 {
0105     d->id = id;
0106     d->manager = KAccounts::accountsManager();
0107     d->repeatedTries = 0;
0108     d->serviceType = QString();
0109 }
0110 
0111 GetCredentialsJob::GetCredentialsJob(Accounts::AccountId id, const QString &authMethod, const QString &authMechanism, QObject *parent)
0112     : KJob(parent)
0113     , d(new Private(this))
0114 {
0115     d->id = id;
0116     d->manager = KAccounts::accountsManager();
0117     d->authMechanism = authMechanism;
0118     d->authMethod = authMethod;
0119     d->repeatedTries = 0;
0120     d->serviceType = QString();
0121 }
0122 
0123 GetCredentialsJob::~GetCredentialsJob()
0124 {
0125     delete d;
0126 }
0127 
0128 void GetCredentialsJob::start()
0129 {
0130     QMetaObject::invokeMethod(this, "getCredentials", Qt::QueuedConnection);
0131 }
0132 
0133 void GetCredentialsJob::setServiceType(const QString &serviceType)
0134 {
0135     d->serviceType = serviceType;
0136 }
0137 
0138 Accounts::AccountId GetCredentialsJob::accountId() const
0139 {
0140     return d->id;
0141 }
0142 
0143 QVariantMap GetCredentialsJob::credentialsData() const
0144 {
0145     auto data = d->sessionData.toMap();
0146     data.insert(d->authData);
0147 
0148     return data;
0149 }
0150 
0151 }
0152 
0153 #include "moc_getcredentialsjob.cpp"