Kgapi

newtokensfetchjob.cpp
1/*
2
3 * SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8#include "debug.h"
9#include "newtokensfetchjob_p.h"
10
11#include <QNetworkAccessManager>
12#include <QNetworkReply>
13#include <QNetworkRequest>
14
15#include <QJsonDocument>
16#include <QUrlQuery>
17
18using namespace KGAPI2;
19
20class Q_DECL_HIDDEN NewTokensFetchJob::Private
21{
22public:
23 QString tmpToken;
24 QString apiKey;
25 QString secretKey;
26 int localPort = 0;
27
28 QString accessToken;
29 QString refreshToken;
30 qulonglong expiresIn;
31};
32
33NewTokensFetchJob::NewTokensFetchJob(const QString &tmpToken, const QString &apiKey, const QString &secretKey, int localPort, QObject *parent)
34 : Job(parent)
35 , d(new Private)
36{
37 d->tmpToken = tmpToken;
38 d->apiKey = apiKey;
39 d->secretKey = secretKey;
40 d->localPort = localPort;
41}
42
43NewTokensFetchJob::~NewTokensFetchJob()
44{
45 delete d;
46}
47
48QString NewTokensFetchJob::accessToken() const
49{
50 if (isRunning()) {
51 qCWarning(KGAPIDebug) << "Called accessToken() on running job!";
52 return QString();
53 }
54
55 return d->accessToken;
56}
57
58QString NewTokensFetchJob::refreshToken() const
59{
60 if (isRunning()) {
61 qCWarning(KGAPIDebug) << "Called refreshToken() on running job!";
62 return QString();
63 }
64
65 return d->refreshToken;
66}
67
68qulonglong NewTokensFetchJob::expiresIn() const
69{
70 if (isRunning()) {
71 qCWarning(KGAPIDebug) << "Called expiresIn() on running job!";
72 return 0;
73 }
74
75 return d->expiresIn;
76}
77
78void NewTokensFetchJob::start()
79{
80 QNetworkRequest request;
81
82 request.setUrl(QUrl(QStringLiteral("https://accounts.google.com/o/oauth2/token")));
83 request.setHeader(QNetworkRequest::ContentTypeHeader, QLatin1StringView("application/x-www-form-urlencoded"));
84
85 QUrlQuery params;
86 params.addQueryItem(QStringLiteral("client_id"), d->apiKey);
87 params.addQueryItem(QStringLiteral("client_secret"), d->secretKey);
88 params.addQueryItem(QStringLiteral("code"), d->tmpToken);
89 params.addQueryItem(QStringLiteral("redirect_uri"),
90 QStringLiteral("http://127.0.0.1:%1").arg(d->localPort)); // we need to use the same URL as in AuthWidget
91 params.addQueryItem(QStringLiteral("grant_type"), QStringLiteral("authorization_code"));
92
93 enqueueRequest(request, params.toString(QUrl::FullyEncoded).toLatin1());
94}
95
96void NewTokensFetchJob::dispatchRequest(QNetworkAccessManager *accessManager,
97 const QNetworkRequest &request,
98 const QByteArray &data,
99 const QString &contentType)
100{
101 Q_UNUSED(contentType)
102
103 accessManager->post(request, data);
104}
105
106void NewTokensFetchJob::handleReply(const QNetworkReply *reply, const QByteArray &rawData)
107{
108 Q_UNUSED(reply)
109
110 QJsonDocument document = QJsonDocument::fromJson(rawData);
111 if (document.isNull()) {
112 qCWarning(KGAPIDebug) << "Failed to parse server response.";
113 qCDebug(KGAPIDebug) << rawData;
114 setError(KGAPI2::AuthCancelled);
115 setErrorString(tr("Failed to parse server response."));
116 return;
117 }
118 QVariantMap parsed_data = document.toVariant().toMap();
119
120 qCDebug(KGAPIDebug) << "Retrieved new tokens pair.";
121
122 d->accessToken = parsed_data.value(QStringLiteral("access_token")).toString();
123 d->refreshToken = parsed_data.value(QStringLiteral("refresh_token")).toString();
124 d->expiresIn = parsed_data.value(QStringLiteral("expires_in")).toULongLong();
125}
126
127#include "moc_newtokensfetchjob_p.cpp"
Abstract base class for all jobs in LibKGAPI.
Definition job.h:41
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
@ AuthCancelled
LibKGAPI error - when the authentication dialog is canceled.
Definition types.h:187
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool isNull() const const
QVariant toVariant() const const
QNetworkReply * post(const QNetworkRequest &request, QHttpMultiPart *multiPart)
void setHeader(KnownHeaders header, const QVariant &value)
void setUrl(const QUrl &url)
QByteArray toLatin1() const const
FullyEncoded
void addQueryItem(const QString &key, const QString &value)
QString toString(QUrl::ComponentFormattingOptions encoding) const const
QMap< QString, QVariant > toMap() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:54:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.