From a49893689eccd5a2ea74b1ca36549ebc70616f91 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Fri, 21 Feb 2014 15:05:57 -0800 Subject: [PATCH] store username in local settings so it doesn't need to be reloaded --- interface/src/Application.cpp | 2 +- libraries/shared/src/AccountManager.cpp | 44 ++++++------- libraries/shared/src/AccountManager.h | 7 +-- .../shared/src/DataServerAccountInfo.cpp | 61 +++++++++++++++++++ libraries/shared/src/DataServerAccountInfo.h | 38 ++++++++++++ libraries/shared/src/OAuthAccessToken.cpp | 2 +- libraries/shared/src/OAuthAccessToken.h | 2 +- 7 files changed, 123 insertions(+), 33 deletions(-) create mode 100644 libraries/shared/src/DataServerAccountInfo.cpp create mode 100644 libraries/shared/src/DataServerAccountInfo.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index d6dc4c4591..5a0b130fbd 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3929,7 +3929,7 @@ void Application::updateWindowTitle(){ NodeList* nodeList = NodeList::getInstance(); QString username = AccountManager::getInstance().getUsername(); - QString title = QString() + (!username.isEmpty() ? username : "") + nodeList->getSessionUUID().toString() + QString title = QString() + (!username.isEmpty() ? username + " " : QString()) + nodeList->getSessionUUID().toString() + " @ " + nodeList->getDomainInfo().getHostname() + buildVersion; qDebug("Application title set to: %s", title.toStdString().c_str()); diff --git a/libraries/shared/src/AccountManager.cpp b/libraries/shared/src/AccountManager.cpp index bb9b40635b..61f7c07701 100644 --- a/libraries/shared/src/AccountManager.cpp +++ b/libraries/shared/src/AccountManager.cpp @@ -18,7 +18,6 @@ #include "AccountManager.h" -QMap AccountManager::_accessTokens = QMap(); AccountManager& AccountManager::getInstance() { static AccountManager sharedInstance; @@ -26,25 +25,31 @@ AccountManager& AccountManager::getInstance() { } Q_DECLARE_METATYPE(OAuthAccessToken) +Q_DECLARE_METATYPE(DataServerAccountInfo) Q_DECLARE_METATYPE(QNetworkAccessManager::Operation) Q_DECLARE_METATYPE(JSONCallbackParameters) -const QString ACCOUNT_TOKEN_GROUP = "tokens"; +const QString ACCOUNTS_GROUP = "accounts"; AccountManager::AccountManager() : _rootURL(), _username(), _networkAccessManager(), - _pendingCallbackMap() + _pendingCallbackMap(), + _accounts() { qRegisterMetaType("OAuthAccessToken"); qRegisterMetaTypeStreamOperators("OAuthAccessToken"); + + qRegisterMetaType("DataServerAccountInfo"); + qRegisterMetaTypeStreamOperators("DataServerAccountInfo"); + qRegisterMetaType("QNetworkAccessManager::Operation"); qRegisterMetaType("JSONCallbackParameters"); // check if there are existing access tokens to load from settings QSettings settings; - settings.beginGroup(ACCOUNT_TOKEN_GROUP); + settings.beginGroup(ACCOUNTS_GROUP); foreach(const QString& key, settings.allKeys()) { // take a key copy to perform the double slash replacement @@ -52,7 +57,7 @@ AccountManager::AccountManager() : QUrl keyURL(keyCopy.replace("slashslash", "//")); // pull out the stored access token and put it in our in memory array - _accessTokens.insert(keyURL, settings.value(key).value()); + _accounts.insert(keyURL, settings.value(key).value()); qDebug() << "Found a data-server access token for" << qPrintable(keyURL.toString()); } } @@ -70,16 +75,6 @@ void AccountManager::setRootURL(const QUrl& rootURL) { } } -void AccountManager::setUsername(const QString& username) { - if (_username != username) { - _username = username; - - qDebug() << "Changing username to" << username; - - emit usernameChanged(username); - } -} - void AccountManager::authenticatedRequest(const QString& path, QNetworkAccessManager::Operation operation, const JSONCallbackParameters& callbackParams, const QByteArray& dataByteArray) { QMetaObject::invokeMethod(this, "invokedRequest", @@ -96,7 +91,7 @@ void AccountManager::invokedRequest(const QString& path, QNetworkAccessManager:: QUrl requestURL = _rootURL; requestURL.setPath(path); - requestURL.setQuery("access_token=" + _accessTokens.value(_rootURL).token); + requestURL.setQuery("access_token=" + _accounts.value(_rootURL).getAccessToken().token); authenticatedRequest.setUrl(requestURL); @@ -169,9 +164,9 @@ void AccountManager::passErrorToCallback(QNetworkReply::NetworkError errorCode) } bool AccountManager::hasValidAccessToken() { - OAuthAccessToken accessToken = _accessTokens.value(_rootURL); + DataServerAccountInfo accountInfo = _accounts.value(_rootURL); - if (accessToken.token.isEmpty() || accessToken.isExpired()) { + if (accountInfo.getAccessToken().token.isEmpty() || accountInfo.getAccessToken().isExpired()) { qDebug() << "An access token is required for requests to" << qPrintable(_rootURL.toString()); return false; } else { @@ -228,20 +223,17 @@ void AccountManager::requestFinished() { QUrl rootURL = requestReply->url(); rootURL.setPath(""); - qDebug() << "Storing an access token for" << qPrintable(rootURL.toString()); + qDebug() << "Storing an account with access-token for" << qPrintable(rootURL.toString()); - OAuthAccessToken freshAccessToken(rootObject); - _accessTokens.insert(rootURL, freshAccessToken); - - // pull username from the response - setUsername(rootObject["user"].toObject()["username"].toString()); + DataServerAccountInfo freshAccountInfo(rootObject); + _accounts.insert(rootURL, freshAccountInfo); emit receivedAccessToken(rootURL); // store this access token into the local settings QSettings localSettings; - localSettings.beginGroup(ACCOUNT_TOKEN_GROUP); - localSettings.setValue(rootURL.toString().replace("//", "slashslash"), QVariant::fromValue(freshAccessToken)); + localSettings.beginGroup(ACCOUNTS_GROUP); + localSettings.setValue(rootURL.toString().replace("//", "slashslash"), QVariant::fromValue(freshAccountInfo)); } } else { // TODO: error handling diff --git a/libraries/shared/src/AccountManager.h b/libraries/shared/src/AccountManager.h index 92b0f46674..fe8bf136fe 100644 --- a/libraries/shared/src/AccountManager.h +++ b/libraries/shared/src/AccountManager.h @@ -15,7 +15,7 @@ #include #include -#include "OAuthAccessToken.h" +#include "DataServerAccountInfo.h" class JSONCallbackParameters { public: @@ -49,8 +49,7 @@ public: void requestAccessToken(const QString& login, const QString& password); - const QString& getUsername() const { return _username; } - void setUsername(const QString& username); + QString getUsername() const { return _accounts[_rootURL].getUsername(); } public slots: void requestFinished(); @@ -75,7 +74,7 @@ private: QNetworkAccessManager _networkAccessManager; QMap _pendingCallbackMap; - static QMap _accessTokens; + QMap _accounts; }; #endif /* defined(__hifi__AccountManager__) */ diff --git a/libraries/shared/src/DataServerAccountInfo.cpp b/libraries/shared/src/DataServerAccountInfo.cpp new file mode 100644 index 0000000000..7225653998 --- /dev/null +++ b/libraries/shared/src/DataServerAccountInfo.cpp @@ -0,0 +1,61 @@ +// +// DataServerAccountInfo.cpp +// hifi +// +// Created by Stephen Birarda on 2/18/2014. +// Copyright (c) 2014 HighFidelity, Inc. All rights reserved. +// + +#include + +#include "DataServerAccountInfo.h" + +DataServerAccountInfo::DataServerAccountInfo() : + _accessToken(), + _username() +{ + +} + +DataServerAccountInfo::DataServerAccountInfo(const QJsonObject& jsonObject) : + _accessToken(jsonObject), + _username() +{ + setUsername(jsonObject["user"].toObject()["username"].toString()); +} + +DataServerAccountInfo::DataServerAccountInfo(const DataServerAccountInfo& otherInfo) { + _accessToken = otherInfo._accessToken; + _username = otherInfo._username; +} + +DataServerAccountInfo& DataServerAccountInfo::operator=(const DataServerAccountInfo& otherInfo) { + DataServerAccountInfo temp(otherInfo); + swap(temp); + return *this; +} + +void DataServerAccountInfo::swap(DataServerAccountInfo& otherInfo) { + using std::swap; + + swap(_accessToken, otherInfo._accessToken); + swap(_username, otherInfo._username); +} + +void DataServerAccountInfo::setUsername(const QString& username) { + if (_username != username) { + _username = username; + + qDebug() << "Username changed to" << username; + } +} + +QDataStream& operator<<(QDataStream &out, const DataServerAccountInfo& info) { + out << info._accessToken << info._username; + return out; +} + +QDataStream& operator>>(QDataStream &in, DataServerAccountInfo& info) { + in >> info._accessToken >> info._username; + return in; +} \ No newline at end of file diff --git a/libraries/shared/src/DataServerAccountInfo.h b/libraries/shared/src/DataServerAccountInfo.h new file mode 100644 index 0000000000..da7bdbe42e --- /dev/null +++ b/libraries/shared/src/DataServerAccountInfo.h @@ -0,0 +1,38 @@ +// +// DataServerAccountInfo.h +// hifi +// +// Created by Stephen Birarda on 2/21/2014. +// Copyright (c) 2014 HighFidelity, Inc. All rights reserved. +// + +#ifndef __hifi__DataServerAccountInfo__ +#define __hifi__DataServerAccountInfo__ + +#include + +#include "OAuthAccessToken.h" + +class DataServerAccountInfo : public QObject { + Q_OBJECT +public: + DataServerAccountInfo(); + DataServerAccountInfo(const QJsonObject& jsonObject); + DataServerAccountInfo(const DataServerAccountInfo& otherInfo); + DataServerAccountInfo& operator=(const DataServerAccountInfo& otherInfo); + + const OAuthAccessToken& getAccessToken() const { return _accessToken; } + + const QString& getUsername() const { return _username; } + void setUsername(const QString& username); + + friend QDataStream& operator<<(QDataStream &out, const DataServerAccountInfo& info); + friend QDataStream& operator>>(QDataStream &in, DataServerAccountInfo& info); +private: + void swap(DataServerAccountInfo& otherInfo); + + OAuthAccessToken _accessToken; + QString _username; +}; + +#endif /* defined(__hifi__DataServerAccountInfo__) */ diff --git a/libraries/shared/src/OAuthAccessToken.cpp b/libraries/shared/src/OAuthAccessToken.cpp index 7852a14878..ed08d2ccc8 100644 --- a/libraries/shared/src/OAuthAccessToken.cpp +++ b/libraries/shared/src/OAuthAccessToken.cpp @@ -6,7 +6,7 @@ // Copyright (c) 2014 HighFidelity, Inc. All rights reserved. // -#include +#include #include "OAuthAccessToken.h" diff --git a/libraries/shared/src/OAuthAccessToken.h b/libraries/shared/src/OAuthAccessToken.h index 9e7b1f01cf..e2a5eb4ce2 100644 --- a/libraries/shared/src/OAuthAccessToken.h +++ b/libraries/shared/src/OAuthAccessToken.h @@ -21,7 +21,7 @@ public: OAuthAccessToken(const OAuthAccessToken& otherToken); OAuthAccessToken& operator=(const OAuthAccessToken& otherToken); - bool isExpired() { return expiryTimestamp <= QDateTime::currentMSecsSinceEpoch(); } + bool isExpired() const { return expiryTimestamp <= QDateTime::currentMSecsSinceEpoch(); } QString token; QString refreshToken;