1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Network Auth module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 or (at your option) any later version
20 ** approved by the KDE Free Qt Foundation. The licenses are as published by
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
26 ** $QT_END_LICENSE$
27 **
28 ****************************************************************************/
29 
30 #ifndef QT_NO_HTTP
31 
32 #include "qoauthoobreplyhandler.h"
33 #include "qabstractoauthreplyhandler_p.h"
34 
35 #include <QtCore/qurlquery.h>
36 #include <QtCore/qjsonobject.h>
37 #include <QtCore/qjsondocument.h>
38 #include <QtCore/qloggingcategory.h>
39 
40 #include <QtNetwork/qnetworkreply.h>
41 
42 QT_BEGIN_NAMESPACE
43 
QOAuthOobReplyHandler(QObject * parent)44 QOAuthOobReplyHandler::QOAuthOobReplyHandler(QObject *parent)
45     : QAbstractOAuthReplyHandler(parent)
46 {}
47 
callback() const48 QString QOAuthOobReplyHandler::callback() const
49 {
50     return QStringLiteral("oob");
51 }
52 
networkReplyFinished(QNetworkReply * reply)53 void QOAuthOobReplyHandler::networkReplyFinished(QNetworkReply *reply)
54 {
55     if (reply->error() != QNetworkReply::NoError) {
56         qCWarning(lcReplyHandler, "%s", qPrintable(reply->errorString()));
57         return;
58     }
59     if (reply->header(QNetworkRequest::ContentTypeHeader).isNull()) {
60         qCWarning(lcReplyHandler, "Empty Content-type header");
61         return;
62     }
63     const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).isNull() ?
64                 QStringLiteral("text/html") :
65                 reply->header(QNetworkRequest::ContentTypeHeader).toString();
66     const QByteArray data = reply->readAll();
67     if (data.isEmpty()) {
68         qCWarning(lcReplyHandler, "No received data");
69         return;
70     }
71 
72     Q_EMIT replyDataReceived(data);
73 
74     QVariantMap ret;
75 
76     if (contentType.startsWith(QStringLiteral("text/html")) ||
77             contentType.startsWith(QStringLiteral("application/x-www-form-urlencoded"))) {
78         ret = parseResponse(data);
79     } else if (contentType.startsWith(QStringLiteral("application/json"))
80                || contentType.startsWith(QStringLiteral("text/javascript"))) {
81         const QJsonDocument document = QJsonDocument::fromJson(data);
82         if (!document.isObject()) {
83             qCWarning(lcReplyHandler, "Received data is not a JSON object: %s",
84                       qPrintable(QString::fromUtf8(data)));
85             return;
86         }
87         const QJsonObject object = document.object();
88         if (object.isEmpty()) {
89             qCWarning(lcReplyHandler, "Received empty JSON object: %s",
90                       qPrintable(QString::fromUtf8(data)));
91         }
92         ret = object.toVariantMap();
93     } else {
94         qCWarning(lcReplyHandler, "Unknown Content-type: %s", qPrintable(contentType));
95         return;
96     }
97 
98     Q_EMIT tokensReceived(ret);
99 }
100 
parseResponse(const QByteArray & response)101 QVariantMap QOAuthOobReplyHandler::parseResponse(const QByteArray &response)
102 {
103     QVariantMap ret;
104     QUrlQuery query(QString::fromUtf8(response));
105     auto queryItems = query.queryItems(QUrl::FullyDecoded);
106     for (auto it = queryItems.begin(), end = queryItems.end(); it != end; ++it)
107         ret.insert(it->first, it->second);
108     return ret;
109 }
110 
111 QT_END_NAMESPACE
112 
113 #endif // QT_NO_HTTP
114