mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 15:23:56 +02:00
store username in local settings so it doesn't need to be reloaded
This commit is contained in:
parent
76e6c9342f
commit
a49893689e
7 changed files with 123 additions and 33 deletions
|
@ -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());
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
|
||||
#include "AccountManager.h"
|
||||
|
||||
QMap<QUrl, OAuthAccessToken> AccountManager::_accessTokens = QMap<QUrl, OAuthAccessToken>();
|
||||
|
||||
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>("OAuthAccessToken");
|
||||
qRegisterMetaTypeStreamOperators<OAuthAccessToken>("OAuthAccessToken");
|
||||
|
||||
qRegisterMetaType<DataServerAccountInfo>("DataServerAccountInfo");
|
||||
qRegisterMetaTypeStreamOperators<DataServerAccountInfo>("DataServerAccountInfo");
|
||||
|
||||
qRegisterMetaType<QNetworkAccessManager::Operation>("QNetworkAccessManager::Operation");
|
||||
qRegisterMetaType<JSONCallbackParameters>("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<OAuthAccessToken>());
|
||||
_accounts.insert(keyURL, settings.value(key).value<DataServerAccountInfo>());
|
||||
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
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
#include <QtNetwork/QNetworkAccessManager>
|
||||
#include <QtNetwork/QNetworkReply>
|
||||
|
||||
#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<QNetworkReply*, JSONCallbackParameters> _pendingCallbackMap;
|
||||
|
||||
static QMap<QUrl, OAuthAccessToken> _accessTokens;
|
||||
QMap<QUrl, DataServerAccountInfo> _accounts;
|
||||
};
|
||||
|
||||
#endif /* defined(__hifi__AccountManager__) */
|
||||
|
|
61
libraries/shared/src/DataServerAccountInfo.cpp
Normal file
61
libraries/shared/src/DataServerAccountInfo.cpp
Normal file
|
@ -0,0 +1,61 @@
|
|||
//
|
||||
// DataServerAccountInfo.cpp
|
||||
// hifi
|
||||
//
|
||||
// Created by Stephen Birarda on 2/18/2014.
|
||||
// Copyright (c) 2014 HighFidelity, Inc. All rights reserved.
|
||||
//
|
||||
|
||||
#include <QtCore/QDebug>
|
||||
|
||||
#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;
|
||||
}
|
38
libraries/shared/src/DataServerAccountInfo.h
Normal file
38
libraries/shared/src/DataServerAccountInfo.h
Normal file
|
@ -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 <QtCore/QObject>
|
||||
|
||||
#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__) */
|
|
@ -6,7 +6,7 @@
|
|||
// Copyright (c) 2014 HighFidelity, Inc. All rights reserved.
|
||||
//
|
||||
|
||||
#include <QtCore/qdebug.h>
|
||||
#include <QtCore/QDataStream>
|
||||
|
||||
#include "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;
|
||||
|
|
Loading…
Reference in a new issue