File indexing completed on 2024-05-12 05:22:15

0001 /*
0002  * This file is part of LibKGAPI library
0003  *
0004  * SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #include "authjob.h"
0010 #include "account.h"
0011 #include "debug.h"
0012 #include "job_p.h"
0013 #include "private/fullauthenticationjob_p.h"
0014 #include "private/refreshtokensjob_p.h"
0015 
0016 using namespace KGAPI2;
0017 
0018 // Used by fakeauthbrowser in tests
0019 KGAPICORE_EXPORT uint16_t kgapiTcpAuthServerPort = 0;
0020 
0021 class Q_DECL_HIDDEN AuthJob::Private
0022 {
0023 public:
0024     Private(AuthJob *qq)
0025         : q(qq)
0026     {
0027     }
0028 
0029     template<typename JobType>
0030     void jobFinished(Job *job)
0031     {
0032         if (job->error()) {
0033             q->setError(job->error());
0034             q->setErrorString(job->errorString());
0035         } else {
0036             account = static_cast<JobType *>(job)->account();
0037         }
0038 
0039         q->emitFinished();
0040     }
0041 
0042     AccountPtr account;
0043     QString apiKey;
0044     QString secretKey;
0045     QString username;
0046 
0047 private:
0048     AuthJob *const q;
0049 };
0050 
0051 AuthJob::AuthJob(const AccountPtr &account, const QString &apiKey, const QString &secretKey, QObject *parent)
0052     : Job(parent)
0053     , d(new Private(this))
0054 {
0055     d->account = account;
0056     d->apiKey = apiKey;
0057     d->secretKey = secretKey;
0058 }
0059 
0060 AuthJob::~AuthJob() = default;
0061 
0062 AccountPtr AuthJob::account() const
0063 {
0064     return d->account;
0065 }
0066 
0067 void AuthJob::setUsername(const QString &username)
0068 {
0069     d->username = username;
0070 }
0071 
0072 void AuthJob::setPassword(const QString & /*password*/)
0073 {
0074 }
0075 
0076 void AuthJob::handleReply(const QNetworkReply * /*reply*/, const QByteArray & /*rawData*/)
0077 {
0078     // Should never be called.
0079     Q_UNREACHABLE();
0080 }
0081 
0082 void AuthJob::dispatchRequest(QNetworkAccessManager * /*accessManager*/,
0083                               const QNetworkRequest & /*request*/,
0084                               const QByteArray & /*data*/,
0085                               const QString & /*contentType*/)
0086 {
0087     // Should never be called.
0088     Q_UNREACHABLE();
0089 }
0090 
0091 void AuthJob::start()
0092 {
0093     if (d->account->refreshToken().isEmpty() || (d->account->m_scopesChanged == true)) {
0094         d->account->addScope(Account::accountInfoEmailScopeUrl());
0095 
0096         auto job = new FullAuthenticationJob(d->account, d->apiKey, d->secretKey, this);
0097         job->setUsername(d->username);
0098         job->setServerPort(kgapiTcpAuthServerPort);
0099         connect(job, &Job::finished, this, [this](Job *job) {
0100             d->jobFinished<FullAuthenticationJob>(job);
0101         });
0102     } else {
0103         if (d->account->accountName().isEmpty()) {
0104             setError(KGAPI2::InvalidAccount);
0105             setErrorString(tr("Account name is empty"));
0106             emitFinished();
0107             return;
0108         }
0109 
0110         auto job = new RefreshTokensJob(d->account, d->apiKey, d->secretKey, this);
0111         connect(job, &Job::finished, this, [this](Job *job) {
0112             d->jobFinished<RefreshTokensJob>(job);
0113         });
0114     }
0115 }
0116 
0117 #include "moc_authjob.cpp"