From e2c3b626b334e2598e9b1ac75ee31a385175d30b Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 18 Sep 2014 09:05:25 -0700 Subject: [PATCH] add a DataWebDialog for authenticated access to data-web html --- interface/src/Application.cpp | 4 ++ interface/src/ui/DataWebDialog.cpp | 39 +++++++++++++++++ interface/src/ui/DataWebDialog.h | 25 +++++++++++ libraries/networking/src/AccountManager.cpp | 2 - libraries/networking/src/AccountManager.h | 2 + libraries/networking/src/LimitedNodeList.cpp | 2 +- .../src/OAuthNetworkAccessManager.cpp | 43 +++++++++++++++++++ .../src/OAuthNetworkAccessManager.h | 24 +++++++++++ 8 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 interface/src/ui/DataWebDialog.cpp create mode 100644 interface/src/ui/DataWebDialog.h create mode 100644 libraries/networking/src/OAuthNetworkAccessManager.cpp create mode 100644 libraries/networking/src/OAuthNetworkAccessManager.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index efa5a481f6..f0b386e5fa 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -68,6 +68,7 @@ #include #include "Application.h" +#include "ui/DataWebDialog.h" #include "InterfaceVersion.h" #include "Menu.h" #include "ModelUploader.h" @@ -429,6 +430,9 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : MIDIManager& midiManagerInstance = MIDIManager::getInstance(); midiManagerInstance.openDefaultPort(); #endif + + DataWebDialog* dialogForPath = DataWebDialog::dialogForPath("/locations"); + dialogForPath->show(); } Application::~Application() { diff --git a/interface/src/ui/DataWebDialog.cpp b/interface/src/ui/DataWebDialog.cpp new file mode 100644 index 0000000000..9914383220 --- /dev/null +++ b/interface/src/ui/DataWebDialog.cpp @@ -0,0 +1,39 @@ +// +// DataWebDialog.cpp +// interface/src/ui +// +// Created by Stephen Birarda on 2014-09-17. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include + +#include +#include +#include + +#include "DataWebDialog.h" + +DataWebDialog::DataWebDialog() { + // make sure the dialog deletes itself when it closes + setAttribute(Qt::WA_DeleteOnClose); + + // use an OAuthNetworkAccessManager instead of regular QNetworkAccessManager so our requests are authed + page()->setNetworkAccessManager(OAuthNetworkAccessManager::getInstance()); +} + +DataWebDialog* DataWebDialog::dialogForPath(const QString& path) { + DataWebDialog* dialogWebView = new DataWebDialog(); + + QUrl dataWebUrl(DEFAULT_NODE_AUTH_URL); + dataWebUrl.setPath(path); + + qDebug() << "Opening a data web dialog for" << dataWebUrl.toString(); + + dialogWebView->load(dataWebUrl); + + return dialogWebView; +} \ No newline at end of file diff --git a/interface/src/ui/DataWebDialog.h b/interface/src/ui/DataWebDialog.h new file mode 100644 index 0000000000..c69a9207b9 --- /dev/null +++ b/interface/src/ui/DataWebDialog.h @@ -0,0 +1,25 @@ +// +// DataWebDialog.h +// interface/src/ui +// +// Created by Stephen Birarda on 2014-09-17. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_DataWebDialog_h +#define hifi_DataWebDialog_h + +#include +#include + +class DataWebDialog : public QWebView { + Q_OBJECT +public: + DataWebDialog(); + static DataWebDialog* dialogForPath(const QString& path); +}; + +#endif // hifi_WebkitDialog_h \ No newline at end of file diff --git a/libraries/networking/src/AccountManager.cpp b/libraries/networking/src/AccountManager.cpp index b24f720edf..88e4bad7b2 100644 --- a/libraries/networking/src/AccountManager.cpp +++ b/libraries/networking/src/AccountManager.cpp @@ -25,8 +25,6 @@ const bool VERBOSE_HTTP_REQUEST_DEBUGGING = false; -const QByteArray ACCESS_TOKEN_AUTHORIZATION_HEADER = "Authorization"; - AccountManager& AccountManager::getInstance() { static AccountManager sharedInstance; return sharedInstance; diff --git a/libraries/networking/src/AccountManager.h b/libraries/networking/src/AccountManager.h index 64d62cd1c2..edccab0b75 100644 --- a/libraries/networking/src/AccountManager.h +++ b/libraries/networking/src/AccountManager.h @@ -37,6 +37,8 @@ public: QString updateSlot; }; +const QByteArray ACCESS_TOKEN_AUTHORIZATION_HEADER = "Authorization"; + class AccountManager : public QObject { Q_OBJECT public: diff --git a/libraries/networking/src/LimitedNodeList.cpp b/libraries/networking/src/LimitedNodeList.cpp index d42cab6210..bfdfc28d5e 100644 --- a/libraries/networking/src/LimitedNodeList.cpp +++ b/libraries/networking/src/LimitedNodeList.cpp @@ -33,7 +33,7 @@ const char SOLO_NODE_TYPES[2] = { NodeType::AudioMixer }; -const QUrl DEFAULT_NODE_AUTH_URL = QUrl("https://data.highfidelity.io"); +const QUrl DEFAULT_NODE_AUTH_URL = QUrl("http://localhost:3000"); LimitedNodeList* LimitedNodeList::_sharedInstance = NULL; diff --git a/libraries/networking/src/OAuthNetworkAccessManager.cpp b/libraries/networking/src/OAuthNetworkAccessManager.cpp new file mode 100644 index 0000000000..493398230a --- /dev/null +++ b/libraries/networking/src/OAuthNetworkAccessManager.cpp @@ -0,0 +1,43 @@ +// +// OAuthNetworkAccessManager.cpp +// libraries/networking/src +// +// Created by Stephen Birarda on 2014-09-18. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include +#include +#include + +#include "AccountManager.h" + +#include "OAuthNetworkAccessManager.h" + +QThreadStorage oauthNetworkAccessManagers; + +OAuthNetworkAccessManager* OAuthNetworkAccessManager::getInstance() { + if (!oauthNetworkAccessManagers.hasLocalData()) { + oauthNetworkAccessManagers.setLocalData(new OAuthNetworkAccessManager()); + } + + return oauthNetworkAccessManagers.localData(); +} + +QNetworkReply* OAuthNetworkAccessManager::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest& req, + QIODevice* outgoingData) { + AccountManager& accountManager = AccountManager::getInstance(); + + if (accountManager.hasValidAccessToken()) { + QNetworkRequest authenticatedRequest(req); + authenticatedRequest.setRawHeader(ACCESS_TOKEN_AUTHORIZATION_HEADER, + accountManager.getAccountInfo().getAccessToken().authorizationHeaderValue()); + + return QNetworkAccessManager::createRequest(op, authenticatedRequest, outgoingData); + } else { + return QNetworkAccessManager::createRequest(op, req, outgoingData); + } +} diff --git a/libraries/networking/src/OAuthNetworkAccessManager.h b/libraries/networking/src/OAuthNetworkAccessManager.h new file mode 100644 index 0000000000..acfd52e18f --- /dev/null +++ b/libraries/networking/src/OAuthNetworkAccessManager.h @@ -0,0 +1,24 @@ +// +// OAuthNetworkAccessManager.h +// libraries/networking/src +// +// Created by Stephen Birarda on 2014-09-18. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_OAuthNetworkAccessManager_h +#define hifi_OAuthNetworkAccessManager_h + +#include + +class OAuthNetworkAccessManager : public QNetworkAccessManager { +public: + static OAuthNetworkAccessManager* getInstance(); +protected: + virtual QNetworkReply* createRequest(Operation op, const QNetworkRequest& req, QIODevice* outgoingData = 0); +}; + +#endif // hifi_OAuthNetworkAccessManager_h \ No newline at end of file