From 6d847f29fb4b9ebfc0289e132f2c79ebae8827d1 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 25 Feb 2014 16:05:05 -0800 Subject: [PATCH 1/3] currently support system wide auth only via hifi data-web --- libraries/shared/src/AccountManager.cpp | 47 +++++++++++------------ libraries/shared/src/AccountManager.h | 4 +- libraries/shared/src/OAuthAccessToken.cpp | 2 +- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/libraries/shared/src/AccountManager.cpp b/libraries/shared/src/AccountManager.cpp index b8fc1061f8..7c2a4439b8 100644 --- a/libraries/shared/src/AccountManager.cpp +++ b/libraries/shared/src/AccountManager.cpp @@ -37,7 +37,7 @@ AccountManager::AccountManager() : _authURL(), _networkAccessManager(), _pendingCallbackMap(), - _accounts() + _accountInfo() { qRegisterMetaType("OAuthAccessToken"); qRegisterMetaTypeStreamOperators("OAuthAccessToken"); @@ -47,27 +47,13 @@ AccountManager::AccountManager() : qRegisterMetaType("QNetworkAccessManager::Operation"); qRegisterMetaType("JSONCallbackParameters"); - - // check if there are existing access tokens to load from settings - QSettings settings; - settings.beginGroup(ACCOUNTS_GROUP); - - foreach(const QString& key, settings.allKeys()) { - // take a key copy to perform the double slash replacement - QString keyCopy(key); - QUrl keyURL(keyCopy.replace("slashslash", "//")); - - // pull out the stored access token and put it in our in memory array - _accounts.insert(keyURL, settings.value(key).value()); - qDebug() << "Found a data-server access token for" << qPrintable(keyURL.toString()); - } } const QString DOUBLE_SLASH_SUBSTITUTE = "slashslash"; void AccountManager::logout() { // a logout means we want to delete the DataServerAccountInfo we currently have for this URL, in-memory and in file - _accounts.remove(_authURL); + _accountInfo = DataServerAccountInfo(); QSettings settings; settings.beginGroup(ACCOUNTS_GROUP); @@ -80,7 +66,6 @@ void AccountManager::logout() { emit logoutComplete(); // the username has changed to blank emit usernameChanged(QString()); - } void AccountManager::setAuthURL(const QUrl& authURL) { @@ -90,6 +75,22 @@ void AccountManager::setAuthURL(const QUrl& authURL) { qDebug() << "URL for node authentication has been changed to" << qPrintable(_authURL.toString()); qDebug() << "Re-setting authentication flow."; + // check if there are existing access tokens to load from settings + QSettings settings; + settings.beginGroup(ACCOUNTS_GROUP); + + foreach(const QString& key, settings.allKeys()) { + // take a key copy to perform the double slash replacement + QString keyCopy(key); + QUrl keyURL(keyCopy.replace("slashslash", "//")); + + if (keyURL == _authURL) { + // pull out the stored access token and store it in memory + _accountInfo = settings.value(key).value(); + qDebug() << "Found a data-server access token for" << qPrintable(keyURL.toString()); + } + } + // tell listeners that the auth endpoint has changed emit authEndpointChanged(); } @@ -111,7 +112,7 @@ void AccountManager::invokedRequest(const QString& path, QNetworkAccessManager:: QUrl requestURL = _authURL; requestURL.setPath(path); - requestURL.setQuery("access_token=" + _accounts.value(_authURL).getAccessToken().token); + requestURL.setQuery("access_token=" + _accountInfo.getAccessToken().token); authenticatedRequest.setUrl(requestURL); @@ -202,9 +203,8 @@ void AccountManager::passErrorToCallback(QNetworkReply::NetworkError errorCode) } bool AccountManager::hasValidAccessToken() { - DataServerAccountInfo accountInfo = _accounts.value(_authURL); - if (accountInfo.getAccessToken().token.isEmpty() || accountInfo.getAccessToken().isExpired()) { + if (_accountInfo.getAccessToken().token.isEmpty() || _accountInfo.getAccessToken().isExpired()) { if (VERBOSE_HTTP_REQUEST_DEBUGGING) { qDebug() << "An access token is required for requests to" << qPrintable(_authURL.toString()); } @@ -266,18 +266,17 @@ void AccountManager::requestFinished() { qDebug() << "Storing an account with access-token for" << qPrintable(rootURL.toString()); - DataServerAccountInfo freshAccountInfo(rootObject); - _accounts.insert(rootURL, freshAccountInfo); + _accountInfo = DataServerAccountInfo(rootObject); emit loginComplete(rootURL); // the username has changed to whatever came back - emit usernameChanged(freshAccountInfo.getUsername()); + emit usernameChanged(_accountInfo.getUsername()); // store this access token into the local settings QSettings localSettings; localSettings.beginGroup(ACCOUNTS_GROUP); localSettings.setValue(rootURL.toString().replace("//", DOUBLE_SLASH_SUBSTITUTE), - QVariant::fromValue(freshAccountInfo)); + QVariant::fromValue(_accountInfo)); } } else { // TODO: error handling diff --git a/libraries/shared/src/AccountManager.h b/libraries/shared/src/AccountManager.h index 35819f2c9a..fe144d6e73 100644 --- a/libraries/shared/src/AccountManager.h +++ b/libraries/shared/src/AccountManager.h @@ -51,7 +51,7 @@ public: void requestAccessToken(const QString& login, const QString& password); - QString getUsername() const { return _accounts[_authURL].getUsername(); } + QString getUsername() const { return _accountInfo.getUsername(); } public slots: void requestFinished(); @@ -78,7 +78,7 @@ private: QNetworkAccessManager _networkAccessManager; QMap _pendingCallbackMap; - QMap _accounts; + DataServerAccountInfo _accountInfo; }; #endif /* defined(__hifi__AccountManager__) */ diff --git a/libraries/shared/src/OAuthAccessToken.cpp b/libraries/shared/src/OAuthAccessToken.cpp index 5dc6672290..20a9dc3aa5 100644 --- a/libraries/shared/src/OAuthAccessToken.cpp +++ b/libraries/shared/src/OAuthAccessToken.cpp @@ -13,7 +13,7 @@ OAuthAccessToken::OAuthAccessToken() : token(), refreshToken(), - expiryTimestamp(), + expiryTimestamp(0), tokenType() { From 2496c6252fb2f312ff8c20a92d728b1019d4a798 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 25 Feb 2014 16:10:08 -0800 Subject: [PATCH 2/3] fix login/logout flow when jumping domains --- interface/src/Menu.cpp | 2 +- libraries/shared/src/AccountManager.cpp | 5 +++++ libraries/shared/src/AccountManager.h | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/interface/src/Menu.cpp b/interface/src/Menu.cpp index a0710cf463..1180f4753e 100644 --- a/interface/src/Menu.cpp +++ b/interface/src/Menu.cpp @@ -94,7 +94,7 @@ Menu::Menu() : toggleLoginMenuItem(); // connect to the appropriate slots of the AccountManager so that we can change the Login/Logout menu item - connect(&accountManager, &AccountManager::loginComplete, this, &Menu::toggleLoginMenuItem); + connect(&accountManager, &AccountManager::foundOrRequestedAccessToken, this, &Menu::toggleLoginMenuItem); connect(&accountManager, &AccountManager::logoutComplete, this, &Menu::toggleLoginMenuItem); addDisabledActionAndSeparator(fileMenu, "Scripts"); diff --git a/libraries/shared/src/AccountManager.cpp b/libraries/shared/src/AccountManager.cpp index 7c2a4439b8..4acb9e080e 100644 --- a/libraries/shared/src/AccountManager.cpp +++ b/libraries/shared/src/AccountManager.cpp @@ -88,6 +88,8 @@ void AccountManager::setAuthURL(const QUrl& authURL) { // pull out the stored access token and store it in memory _accountInfo = settings.value(key).value(); qDebug() << "Found a data-server access token for" << qPrintable(keyURL.toString()); + + emit foundOrRequestedAccessToken(); } } @@ -272,6 +274,9 @@ void AccountManager::requestFinished() { // the username has changed to whatever came back emit usernameChanged(_accountInfo.getUsername()); + // we have found or requested an access token + emit foundOrRequestedAccessToken(); + // store this access token into the local settings QSettings localSettings; localSettings.beginGroup(ACCOUNTS_GROUP); diff --git a/libraries/shared/src/AccountManager.h b/libraries/shared/src/AccountManager.h index fe144d6e73..1b2bb1a071 100644 --- a/libraries/shared/src/AccountManager.h +++ b/libraries/shared/src/AccountManager.h @@ -61,6 +61,7 @@ signals: void authRequired(); void authEndpointChanged(); void usernameChanged(const QString& username); + void foundOrRequestedAccessToken(); void loginComplete(const QUrl& authURL); void logoutComplete(); private slots: From d5fc1caaaf36ce3eec92f98c2cf2a86cce89a107 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 25 Feb 2014 16:12:25 -0800 Subject: [PATCH 3/3] fix wording for access token change signal --- interface/src/Menu.cpp | 2 +- libraries/shared/src/AccountManager.cpp | 4 ++-- libraries/shared/src/AccountManager.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/interface/src/Menu.cpp b/interface/src/Menu.cpp index 1180f4753e..96b8654c2b 100644 --- a/interface/src/Menu.cpp +++ b/interface/src/Menu.cpp @@ -94,7 +94,7 @@ Menu::Menu() : toggleLoginMenuItem(); // connect to the appropriate slots of the AccountManager so that we can change the Login/Logout menu item - connect(&accountManager, &AccountManager::foundOrRequestedAccessToken, this, &Menu::toggleLoginMenuItem); + connect(&accountManager, &AccountManager::accessTokenChanged, this, &Menu::toggleLoginMenuItem); connect(&accountManager, &AccountManager::logoutComplete, this, &Menu::toggleLoginMenuItem); addDisabledActionAndSeparator(fileMenu, "Scripts"); diff --git a/libraries/shared/src/AccountManager.cpp b/libraries/shared/src/AccountManager.cpp index 4acb9e080e..0f39ba4146 100644 --- a/libraries/shared/src/AccountManager.cpp +++ b/libraries/shared/src/AccountManager.cpp @@ -89,7 +89,7 @@ void AccountManager::setAuthURL(const QUrl& authURL) { _accountInfo = settings.value(key).value(); qDebug() << "Found a data-server access token for" << qPrintable(keyURL.toString()); - emit foundOrRequestedAccessToken(); + emit accessTokenChanged(); } } @@ -275,7 +275,7 @@ void AccountManager::requestFinished() { emit usernameChanged(_accountInfo.getUsername()); // we have found or requested an access token - emit foundOrRequestedAccessToken(); + emit accessTokenChanged(); // store this access token into the local settings QSettings localSettings; diff --git a/libraries/shared/src/AccountManager.h b/libraries/shared/src/AccountManager.h index 1b2bb1a071..c741beb461 100644 --- a/libraries/shared/src/AccountManager.h +++ b/libraries/shared/src/AccountManager.h @@ -61,7 +61,7 @@ signals: void authRequired(); void authEndpointChanged(); void usernameChanged(const QString& username); - void foundOrRequestedAccessToken(); + void accessTokenChanged(); void loginComplete(const QUrl& authURL); void logoutComplete(); private slots: