1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qoauthoobreplyhandler.h"
5#include "qoauthoobreplyhandler_p.h"
6#include "qabstractoauthreplyhandler_p.h"
7
8#include <QtCore/qurlquery.h>
9#include <QtCore/qjsonobject.h>
10#include <QtCore/qjsondocument.h>
11#include <QtCore/qloggingcategory.h>
12
13#include <QtNetwork/qnetworkreply.h>
14#include <QtNetwork/qrestreply.h>
15
16QT_BEGIN_NAMESPACE
17
18using namespace Qt::StringLiterals;
19
20QOAuthOobReplyHandler::QOAuthOobReplyHandler(QObject *parent)
21 : QAbstractOAuthReplyHandler(parent)
22{}
23
24/*! \internal */
25QOAuthOobReplyHandler::QOAuthOobReplyHandler(QOAuthOobReplyHandlerPrivate &d, QObject *parent)
26 : QAbstractOAuthReplyHandler(d, parent)
27{}
28
29QString QOAuthOobReplyHandler::callback() const
30{
31 return QStringLiteral("oob");
32}
33
34void QOAuthOobReplyHandler::networkReplyFinished(QNetworkReply *reply)
35{
36 QRestReply restReply(reply);
37
38 if (restReply.hasError()) {
39 emit tokenRequestErrorOccurred(error: QAbstractOAuth::Error::NetworkError, errorString: reply->errorString());
40 return;
41 }
42 if (!restReply.isHttpStatusSuccess()) {
43 emit tokenRequestErrorOccurred(error: QAbstractOAuth::Error::ServerError, errorString: reply->errorString());
44 return;
45 }
46 if (reply->header(header: QNetworkRequest::ContentTypeHeader).isNull()) {
47 emit tokenRequestErrorOccurred(error: QAbstractOAuth::Error::ServerError,
48 errorString: u"Empty Content-type header"_s);
49 return;
50 }
51 const QString contentType = reply->header(header: QNetworkRequest::ContentTypeHeader).isNull() ?
52 QStringLiteral("text/html") :
53 reply->header(header: QNetworkRequest::ContentTypeHeader).toString();
54 const QByteArray data = reply->readAll();
55 if (data.isEmpty()) {
56 emit tokenRequestErrorOccurred(error: QAbstractOAuth::Error::ServerError, errorString: u"No data received"_s);
57 return;
58 }
59
60 Q_EMIT replyDataReceived(data);
61
62 QVariantMap ret;
63
64 if (contentType.startsWith(QStringLiteral("text/html")) ||
65 contentType.startsWith(QStringLiteral("application/x-www-form-urlencoded"))) {
66 ret = parseResponse(response: data);
67 } else if (contentType.startsWith(QStringLiteral("application/json"))
68 || contentType.startsWith(QStringLiteral("text/javascript"))) {
69 const QJsonDocument document = QJsonDocument::fromJson(json: data);
70 if (!document.isObject()) {
71 emit tokenRequestErrorOccurred(error: QAbstractOAuth::Error::ServerError,
72 errorString: u"Received data is not a JSON object: %1"_s.arg(a: QString::fromUtf8(ba: data)));
73 return;
74 }
75 const QJsonObject object = document.object();
76 if (object.isEmpty()) {
77 emit tokenRequestErrorOccurred(error: QAbstractOAuth::Error::ServerError,
78 errorString: u"Received an empty JSON object"_s);
79 return;
80 }
81 ret = object.toVariantMap();
82 } else {
83 emit tokenRequestErrorOccurred(error: QAbstractOAuth::Error::ServerError,
84 errorString: u"Unknown Content-type %1"_s.arg(a: contentType));
85 return;
86 }
87
88 Q_EMIT tokensReceived(tokens: ret);
89}
90
91QVariantMap QOAuthOobReplyHandler::parseResponse(const QByteArray &response)
92{
93 QVariantMap ret;
94 QUrlQuery query(QString::fromUtf8(ba: response));
95 const auto queryItems = query.queryItems(encoding: QUrl::FullyDecoded);
96 for (auto it = queryItems.begin(), end = queryItems.end(); it != end; ++it)
97 ret.insert(key: it->first, value: it->second);
98 return ret;
99}
100
101QT_END_NAMESPACE
102
103#include "moc_qoauthoobreplyhandler.cpp"
104

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of qtnetworkauth/src/oauth/qoauthoobreplyhandler.cpp