Kgapi

authjob.cpp
1/*
2 * This file is part of LibKGAPI library
3 *
4 * SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "authjob.h"
10#include "account.h"
11#include "debug.h"
12#include "job_p.h"
13#include "private/fullauthenticationjob_p.h"
14#include "private/refreshtokensjob_p.h"
15
16using namespace KGAPI2;
17
18// Used by fakeauthbrowser in tests
19KGAPICORE_EXPORT uint16_t kgapiTcpAuthServerPort = 0;
20
21class Q_DECL_HIDDEN AuthJob::Private
22{
23public:
24 Private(AuthJob *qq)
25 : q(qq)
26 {
27 }
28
29 template<typename JobType>
30 void jobFinished(Job *job)
31 {
32 if (job->error()) {
33 q->setError(job->error());
34 q->setErrorString(job->errorString());
35 } else {
36 account = static_cast<JobType *>(job)->account();
37 }
38
39 q->emitFinished();
40 }
41
42 AccountPtr account;
43 QString apiKey;
44 QString secretKey;
45 QString username;
46
47private:
48 AuthJob *const q;
49};
50
51AuthJob::AuthJob(const AccountPtr &account, const QString &apiKey, const QString &secretKey, QObject *parent)
52 : Job(parent)
53 , d(new Private(this))
54{
55 d->account = account;
56 d->apiKey = apiKey;
57 d->secretKey = secretKey;
58}
59
60AuthJob::~AuthJob() = default;
61
63{
64 return d->account;
65}
66
67void AuthJob::setUsername(const QString &username)
68{
69 d->username = username;
70}
71
72void AuthJob::setPassword(const QString & /*password*/)
73{
74}
75
76void AuthJob::handleReply(const QNetworkReply * /*reply*/, const QByteArray & /*rawData*/)
77{
78 // Should never be called.
79 Q_UNREACHABLE();
80}
81
83 const QNetworkRequest & /*request*/,
84 const QByteArray & /*data*/,
85 const QString & /*contentType*/)
86{
87 // Should never be called.
88 Q_UNREACHABLE();
89}
90
92{
93 if (d->account->refreshToken().isEmpty() || (d->account->m_scopesChanged == true)) {
94 d->account->addScope(Account::accountInfoEmailScopeUrl());
95
96 auto job = new FullAuthenticationJob(d->account, d->apiKey, d->secretKey, this);
97 job->setUsername(d->username);
98 job->setServerPort(kgapiTcpAuthServerPort);
99 connect(job, &Job::finished, this, [this](Job *job) {
100 d->jobFinished<FullAuthenticationJob>(job);
101 });
102 } else {
103 if (d->account->accountName().isEmpty()) {
105 setErrorString(tr("Account name is empty"));
106 emitFinished();
107 return;
108 }
109
110 auto job = new RefreshTokensJob(d->account, d->apiKey, d->secretKey, this);
111 connect(job, &Job::finished, this, [this](Job *job) {
112 d->jobFinished<RefreshTokensJob>(job);
113 });
114 }
115}
116
117#include "moc_authjob.cpp"
static QUrl accountInfoEmailScopeUrl()
Returns scope URL to retrieve AccountInfo with email.
Definition account.cpp:155
A job to authenticate against Google and fetch tokens.
Definition authjob.h:34
AuthJob(const AccountPtr &account, const QString &apiKey, const QString &secretKey, QObject *parent=nullptr)
Creates a new authentication job.
Definition authjob.cpp:51
void handleReply(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::Job::handleReply implementation.
Definition authjob.cpp:76
void start() override
KGAPI2::Job::start implementation.
Definition authjob.cpp:91
void setPassword(const QString &password)
Sets the password that will be used when authenticate is called.
Definition authjob.cpp:72
AccountPtr account() const
Returns reauthenticated account.
Definition authjob.cpp:62
~AuthJob() override
Destructor.
void setUsername(const QString &username)
Sets the username that will be used when authenticate is called.
Definition authjob.cpp:67
void dispatchRequest(QNetworkAccessManager *accessManager, const QNetworkRequest &request, const QByteArray &data, const QString &contentType) override
KGAPI2::Job::displayRequest implementation.
Definition authjob.cpp:82
Abstract base class for all jobs in LibKGAPI.
Definition job.h:41
KGAPI2::Error error() const
Error code.
Definition job.cpp:391
void finished(KGAPI2::Job *job)
Emitted when job has finished.
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition job.cpp:401
QString errorString() const
Error string.
Definition job.cpp:406
virtual void emitFinished()
Emits Job::finished() signal.
Definition job.cpp:493
void setError(KGAPI2::Error error)
Set job error to error.
Definition job.cpp:386
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
@ InvalidAccount
LibKGAPI error - the KGAPI2::Account object is invalid.
Definition types.h:185
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString tr(const char *sourceText, const char *disambiguation, int n)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 19 2024 11:58:17 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.