Kgapi

refreshtokensjob.cpp
1/*
2 * This file is part of LibKGAPI
3 *
4 * SPDX-FileCopyrightText: 2020 Daniel Vrátil <dvratil@kde.org>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "account.h"
10#include "debug.h"
11#include "job_p.h"
12#include "refreshtokensjob_p.h"
13
14#include <QJsonDocument>
15#include <QNetworkAccessManager>
16#include <QNetworkCookieJar>
17#include <QNetworkRequest>
18#include <QUrl>
19#include <QUrlQuery>
20
21using namespace KGAPI2;
22
23namespace KGAPI2
24{
25
26class Q_DECL_HIDDEN RefreshTokensJob::Private
27{
28public:
29 Private(const AccountPtr &account, const QString &apiKey, const QString &secretKey, RefreshTokensJob *qq)
30 : mAccount(account)
31 , mApiKey(apiKey)
32 , mSecretKey(secretKey)
33 , q(qq)
34 {
35 }
36
37 void emitError(Error error, const QString &text)
38 {
39 q->setError(error);
40 q->setErrorString(text);
41 q->emitFinished();
42 }
43
44public:
45 AccountPtr mAccount;
46 QString mApiKey;
47 QString mSecretKey;
48
49private:
50 RefreshTokensJob *const q;
51};
52
53}
54
55RefreshTokensJob::RefreshTokensJob(const AccountPtr &account, const QString &apiKey, const QString &secretKey, QObject *parent)
56 : Job(parent)
57 , d(new Private(account, apiKey, secretKey, this))
58{
59}
60
61RefreshTokensJob::~RefreshTokensJob() = default;
62
63AccountPtr RefreshTokensJob::account() const
64{
65 return d->mAccount;
66}
67
68void RefreshTokensJob::start()
69{
70 QNetworkRequest request;
71 request.setUrl(QUrl(QStringLiteral("https://accounts.google.com/o/oauth2/token")));
72 request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/x-www-form-urlencoded"));
73
74 QUrlQuery params;
75 params.addQueryItem(QStringLiteral("client_id"), d->mApiKey);
76 params.addQueryItem(QStringLiteral("client_secret"), d->mSecretKey);
77 params.addQueryItem(QStringLiteral("refresh_token"), d->mAccount->refreshToken());
78 params.addQueryItem(QStringLiteral("grant_type"), QStringLiteral("refresh_token"));
79
80 qCDebug(KGAPIDebug) << "Requesting token refresh.";
81
82 enqueueRequest(request, params.toString(QUrl::FullyEncoded).toLatin1());
83}
84
85void RefreshTokensJob::handleReply(const QNetworkReply *reply, const QByteArray &rawData)
86{
87 Q_UNUSED(reply)
88
89 QJsonDocument document = QJsonDocument::fromJson(rawData);
90 if (document.isNull()) {
92 setErrorString(tr("Failed to parse newly fetched tokens"));
93 emitFinished();
94 return;
95 }
96
97 QVariantMap map = document.toVariant().toMap();
98
99 /* Expected structure:
100 * {
101 * "access_token": "the_access_token",
102 * "token_type":"Bearer",
103 * "expires_in":3600
104 * }
105 */
106 const qlonglong expiresIn = map.value(QStringLiteral("expires_in")).toLongLong();
107 d->mAccount->setExpireDateTime(QDateTime::currentDateTime().addSecs(expiresIn));
108 d->mAccount->setAccessToken(map.value(QStringLiteral("access_token")).toString());
109 emitFinished();
110}
111
112void RefreshTokensJob::dispatchRequest(QNetworkAccessManager *accessManager, const QNetworkRequest &request, const QByteArray &data, const QString &contentType)
113{
114 Q_UNUSED(contentType)
115
116 accessManager->setCookieJar(new QNetworkCookieJar);
117 accessManager->post(request, data);
118}
119
120#include "moc_refreshtokensjob_p.cpp"
Abstract base class for all jobs in LibKGAPI.
Definition job.h:41
char * toString(const EngineQuery &query)
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
Error
Job error codes.
Definition types.h:176
@ InvalidResponse
LibKGAPI error - Google returned invalid response.
Definition types.h:183
QDateTime currentDateTime()
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool isNull() const const
QVariant toVariant() const const
QNetworkReply * post(const QNetworkRequest &request, QHttpMultiPart *multiPart)
void setCookieJar(QNetworkCookieJar *cookieJar)
void setHeader(KnownHeaders header, const QVariant &value)
void setUrl(const QUrl &url)
QByteArray toLatin1() const const
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
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 Mon Nov 4 2024 16:36:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.