mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-10 05:46:13 +02:00
Merge pull request #2101 from birarda/authentication
fixes to login/logout flow when jumping domains
This commit is contained in:
commit
4b3911c5c1
4 changed files with 33 additions and 28 deletions
|
@ -96,7 +96,7 @@ Menu::Menu() :
|
||||||
toggleLoginMenuItem();
|
toggleLoginMenuItem();
|
||||||
|
|
||||||
// connect to the appropriate slots of the AccountManager so that we can change the Login/Logout menu item
|
// 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::accessTokenChanged, this, &Menu::toggleLoginMenuItem);
|
||||||
connect(&accountManager, &AccountManager::logoutComplete, this, &Menu::toggleLoginMenuItem);
|
connect(&accountManager, &AccountManager::logoutComplete, this, &Menu::toggleLoginMenuItem);
|
||||||
|
|
||||||
addDisabledActionAndSeparator(fileMenu, "Scripts");
|
addDisabledActionAndSeparator(fileMenu, "Scripts");
|
||||||
|
|
|
@ -37,7 +37,7 @@ AccountManager::AccountManager() :
|
||||||
_authURL(),
|
_authURL(),
|
||||||
_networkAccessManager(),
|
_networkAccessManager(),
|
||||||
_pendingCallbackMap(),
|
_pendingCallbackMap(),
|
||||||
_accounts()
|
_accountInfo()
|
||||||
{
|
{
|
||||||
qRegisterMetaType<OAuthAccessToken>("OAuthAccessToken");
|
qRegisterMetaType<OAuthAccessToken>("OAuthAccessToken");
|
||||||
qRegisterMetaTypeStreamOperators<OAuthAccessToken>("OAuthAccessToken");
|
qRegisterMetaTypeStreamOperators<OAuthAccessToken>("OAuthAccessToken");
|
||||||
|
@ -47,27 +47,13 @@ AccountManager::AccountManager() :
|
||||||
|
|
||||||
qRegisterMetaType<QNetworkAccessManager::Operation>("QNetworkAccessManager::Operation");
|
qRegisterMetaType<QNetworkAccessManager::Operation>("QNetworkAccessManager::Operation");
|
||||||
qRegisterMetaType<JSONCallbackParameters>("JSONCallbackParameters");
|
qRegisterMetaType<JSONCallbackParameters>("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<DataServerAccountInfo>());
|
|
||||||
qDebug() << "Found a data-server access token for" << qPrintable(keyURL.toString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString DOUBLE_SLASH_SUBSTITUTE = "slashslash";
|
const QString DOUBLE_SLASH_SUBSTITUTE = "slashslash";
|
||||||
|
|
||||||
void AccountManager::logout() {
|
void AccountManager::logout() {
|
||||||
// a logout means we want to delete the DataServerAccountInfo we currently have for this URL, in-memory and in file
|
// 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;
|
QSettings settings;
|
||||||
settings.beginGroup(ACCOUNTS_GROUP);
|
settings.beginGroup(ACCOUNTS_GROUP);
|
||||||
|
@ -80,7 +66,6 @@ void AccountManager::logout() {
|
||||||
emit logoutComplete();
|
emit logoutComplete();
|
||||||
// the username has changed to blank
|
// the username has changed to blank
|
||||||
emit usernameChanged(QString());
|
emit usernameChanged(QString());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AccountManager::setAuthURL(const QUrl& authURL) {
|
void AccountManager::setAuthURL(const QUrl& authURL) {
|
||||||
|
@ -90,6 +75,24 @@ void AccountManager::setAuthURL(const QUrl& authURL) {
|
||||||
qDebug() << "URL for node authentication has been changed to" << qPrintable(_authURL.toString());
|
qDebug() << "URL for node authentication has been changed to" << qPrintable(_authURL.toString());
|
||||||
qDebug() << "Re-setting authentication flow.";
|
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<DataServerAccountInfo>();
|
||||||
|
qDebug() << "Found a data-server access token for" << qPrintable(keyURL.toString());
|
||||||
|
|
||||||
|
emit accessTokenChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// tell listeners that the auth endpoint has changed
|
// tell listeners that the auth endpoint has changed
|
||||||
emit authEndpointChanged();
|
emit authEndpointChanged();
|
||||||
}
|
}
|
||||||
|
@ -111,7 +114,7 @@ void AccountManager::invokedRequest(const QString& path, QNetworkAccessManager::
|
||||||
|
|
||||||
QUrl requestURL = _authURL;
|
QUrl requestURL = _authURL;
|
||||||
requestURL.setPath(path);
|
requestURL.setPath(path);
|
||||||
requestURL.setQuery("access_token=" + _accounts.value(_authURL).getAccessToken().token);
|
requestURL.setQuery("access_token=" + _accountInfo.getAccessToken().token);
|
||||||
|
|
||||||
authenticatedRequest.setUrl(requestURL);
|
authenticatedRequest.setUrl(requestURL);
|
||||||
|
|
||||||
|
@ -202,9 +205,8 @@ void AccountManager::passErrorToCallback(QNetworkReply::NetworkError errorCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AccountManager::hasValidAccessToken() {
|
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) {
|
if (VERBOSE_HTTP_REQUEST_DEBUGGING) {
|
||||||
qDebug() << "An access token is required for requests to" << qPrintable(_authURL.toString());
|
qDebug() << "An access token is required for requests to" << qPrintable(_authURL.toString());
|
||||||
}
|
}
|
||||||
|
@ -266,18 +268,20 @@ void AccountManager::requestFinished() {
|
||||||
|
|
||||||
qDebug() << "Storing an account with access-token for" << qPrintable(rootURL.toString());
|
qDebug() << "Storing an account with access-token for" << qPrintable(rootURL.toString());
|
||||||
|
|
||||||
DataServerAccountInfo freshAccountInfo(rootObject);
|
_accountInfo = DataServerAccountInfo(rootObject);
|
||||||
_accounts.insert(rootURL, freshAccountInfo);
|
|
||||||
|
|
||||||
emit loginComplete(rootURL);
|
emit loginComplete(rootURL);
|
||||||
// the username has changed to whatever came back
|
// the username has changed to whatever came back
|
||||||
emit usernameChanged(freshAccountInfo.getUsername());
|
emit usernameChanged(_accountInfo.getUsername());
|
||||||
|
|
||||||
|
// we have found or requested an access token
|
||||||
|
emit accessTokenChanged();
|
||||||
|
|
||||||
// store this access token into the local settings
|
// store this access token into the local settings
|
||||||
QSettings localSettings;
|
QSettings localSettings;
|
||||||
localSettings.beginGroup(ACCOUNTS_GROUP);
|
localSettings.beginGroup(ACCOUNTS_GROUP);
|
||||||
localSettings.setValue(rootURL.toString().replace("//", DOUBLE_SLASH_SUBSTITUTE),
|
localSettings.setValue(rootURL.toString().replace("//", DOUBLE_SLASH_SUBSTITUTE),
|
||||||
QVariant::fromValue(freshAccountInfo));
|
QVariant::fromValue(_accountInfo));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// TODO: error handling
|
// TODO: error handling
|
||||||
|
|
|
@ -51,7 +51,7 @@ public:
|
||||||
|
|
||||||
void requestAccessToken(const QString& login, const QString& password);
|
void requestAccessToken(const QString& login, const QString& password);
|
||||||
|
|
||||||
QString getUsername() const { return _accounts[_authURL].getUsername(); }
|
QString getUsername() const { return _accountInfo.getUsername(); }
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void requestFinished();
|
void requestFinished();
|
||||||
|
@ -61,6 +61,7 @@ signals:
|
||||||
void authRequired();
|
void authRequired();
|
||||||
void authEndpointChanged();
|
void authEndpointChanged();
|
||||||
void usernameChanged(const QString& username);
|
void usernameChanged(const QString& username);
|
||||||
|
void accessTokenChanged();
|
||||||
void loginComplete(const QUrl& authURL);
|
void loginComplete(const QUrl& authURL);
|
||||||
void logoutComplete();
|
void logoutComplete();
|
||||||
private slots:
|
private slots:
|
||||||
|
@ -78,7 +79,7 @@ private:
|
||||||
QNetworkAccessManager _networkAccessManager;
|
QNetworkAccessManager _networkAccessManager;
|
||||||
QMap<QNetworkReply*, JSONCallbackParameters> _pendingCallbackMap;
|
QMap<QNetworkReply*, JSONCallbackParameters> _pendingCallbackMap;
|
||||||
|
|
||||||
QMap<QUrl, DataServerAccountInfo> _accounts;
|
DataServerAccountInfo _accountInfo;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* defined(__hifi__AccountManager__) */
|
#endif /* defined(__hifi__AccountManager__) */
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
OAuthAccessToken::OAuthAccessToken() :
|
OAuthAccessToken::OAuthAccessToken() :
|
||||||
token(),
|
token(),
|
||||||
refreshToken(),
|
refreshToken(),
|
||||||
expiryTimestamp(),
|
expiryTimestamp(0),
|
||||||
tokenType()
|
tokenType()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue