Merge pull request #15618 from luiscuenca/addCredentials

Pass tokens as params for automatic login
This commit is contained in:
Brad Hefta-Gaub 2019-05-24 09:13:53 -07:00 committed by GitHub
commit 5765957fe1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 126 additions and 8 deletions

View file

@ -3800,10 +3800,14 @@ void Application::handleSandboxStatus(QNetworkReply* reply) {
// If this is a first run we short-circuit the address passed in
if (_firstRun.get()) {
DependencyManager::get<AddressManager>()->goToEntry();
sentTo = SENT_TO_ENTRY;
_firstRun.set(false);
if (!_overrideEntry) {
DependencyManager::get<AddressManager>()->goToEntry();
sentTo = SENT_TO_ENTRY;
} else {
DependencyManager::get<AddressManager>()->loadSettings(addressLookupString);
sentTo = SENT_TO_PREVIOUS_LOCATION;
}
_firstRun.set(false);
} else {
QString goingTo = "";
if (addressLookupString.isEmpty()) {
@ -3819,7 +3823,7 @@ void Application::handleSandboxStatus(QNetworkReply* reply) {
DependencyManager::get<AddressManager>()->loadSettings(addressLookupString);
sentTo = SENT_TO_PREVIOUS_LOCATION;
}
UserActivityLogger::getInstance().logAction("startup_sent_to", {
{ "sent_to", sentTo },
{ "sandbox_is_running", sandboxIsRunning },
@ -9354,6 +9358,19 @@ void Application::showUrlHandler(const QUrl& url) {
}
});
}
void Application::overrideEntry(){
_overrideEntry = true;
}
void Application::forceDisplayName(const QString& displayName) {
getMyAvatar()->setDisplayName(displayName);
}
void Application::forceLoginWithTokens(const QString& tokens) {
DependencyManager::get<AccountManager>()->setAccessTokens(tokens);
Setting::Handle<bool>(KEEP_ME_LOGGED_IN_SETTING_NAME, true).set(true);
}
void Application::setConfigFileURL(const QString& fileUrl) {
DependencyManager::get<AccountManager>()->setConfigFileURL(fileUrl);
}
#if defined(Q_OS_ANDROID)
void Application::beforeEnterBackground() {

View file

@ -356,6 +356,11 @@ public:
void openDirectory(const QString& path);
void overrideEntry();
void forceDisplayName(const QString& displayName);
void forceLoginWithTokens(const QString& tokens);
void setConfigFileURL(const QString& fileUrl);
signals:
void svoImportRequested(const QString& url);
@ -828,5 +833,6 @@ private:
bool _resumeAfterLoginDialogActionTaken_WasPostponed { false };
bool _resumeAfterLoginDialogActionTaken_SafeToRun { false };
bool _startUpFinished { false };
bool _overrideEntry { false };
};
#endif // hifi_Application_h

View file

@ -83,6 +83,8 @@ int main(int argc, const char* argv[]) {
QCommandLineOption allowMultipleInstancesOption("allowMultipleInstances", "Allow multiple instances to run");
QCommandLineOption overrideAppLocalDataPathOption("cache", "set test cache <dir>", "dir");
QCommandLineOption overrideScriptsPathOption(SCRIPTS_SWITCH, "set scripts <path>", "path");
QCommandLineOption responseTokensOption("tokens", "set response tokens <json>", "json");
QCommandLineOption displayNameOption("displayName", "set user display name <string>", "string");
parser.addOption(urlOption);
parser.addOption(noLauncherOption);
@ -93,6 +95,8 @@ int main(int argc, const char* argv[]) {
parser.addOption(overrideAppLocalDataPathOption);
parser.addOption(overrideScriptsPathOption);
parser.addOption(allowMultipleInstancesOption);
parser.addOption(responseTokensOption);
parser.addOption(displayNameOption);
if (!parser.parse(arguments)) {
std::cout << parser.errorText().toStdString() << std::endl; // Avoid Qt log spam
@ -120,8 +124,10 @@ int main(int argc, const char* argv[]) {
static const QString APPLICATION_CONFIG_FILENAME = "config.json";
QDir applicationDir(applicationPath);
QFile configFile(applicationDir.filePath(APPLICATION_CONFIG_FILENAME));
QString configFileName = applicationDir.filePath(APPLICATION_CONFIG_FILENAME);
QFile configFile(configFileName);
QString launcherPath;
if (configFile.exists()) {
if (!configFile.open(QIODevice::ReadOnly)) {
qWarning() << "Found application config, but could not open it";
@ -134,7 +140,7 @@ int main(int argc, const char* argv[]) {
qWarning() << "Found application config, but could not parse it: " << error.errorString();
} else {
static const QString LAUNCHER_PATH_KEY = "launcherPath";
QString launcherPath = doc.object()[LAUNCHER_PATH_KEY].toString();
launcherPath = doc.object()[LAUNCHER_PATH_KEY].toString();
if (!launcherPath.isEmpty()) {
if (!parser.isSet(noLauncherOption)) {
qDebug() << "Found a launcherPath in application config. Starting launcher.";
@ -146,6 +152,7 @@ int main(int argc, const char* argv[]) {
qDebug() << "Found a launcherPath in application config, but the launcher"
" has been suppressed. Continuing normal execution.";
}
configFile.close();
}
}
}
@ -398,6 +405,24 @@ int main(int argc, const char* argv[]) {
printSystemInformation();
auto appPointer = dynamic_cast<Application*>(&app);
if (appPointer) {
if (parser.isSet(urlOption)) {
appPointer->overrideEntry();
}
if (parser.isSet(displayNameOption)) {
QString displayName = QString(parser.value(displayNameOption));
appPointer->forceDisplayName(displayName);
}
if (!launcherPath.isEmpty()) {
appPointer->setConfigFileURL(configFileName);
}
if (parser.isSet(responseTokensOption)) {
QString tokens = QString(parser.value(responseTokensOption));
appPointer->forceLoginWithTokens(tokens);
}
}
QTranslator translator;
translator.load("i18n/interface_en");
app.installTranslator(&translator);

View file

@ -97,6 +97,7 @@ void AccountManager::logout() {
// remove this account from the account settings file
removeAccountFromFile();
saveLoginStatus(false);
emit logoutComplete();
// the username has changed to blank
@ -650,6 +651,39 @@ void AccountManager::refreshAccessToken() {
}
}
void AccountManager::setAccessTokens(const QString& response) {
QJsonDocument jsonResponse = QJsonDocument::fromJson(response.toUtf8());
const QJsonObject& rootObject = jsonResponse.object();
if (!rootObject.contains("error")) {
// construct an OAuthAccessToken from the json object
if (!rootObject.contains("access_token") || !rootObject.contains("expires_in")
|| !rootObject.contains("token_type")) {
// TODO: error handling - malformed token response
qCDebug(networking) << "Received a response for password grant that is missing one or more expected values.";
} else {
// clear the path from the response URL so we have the right root URL for this access token
QUrl rootURL = rootObject.contains("url") ? rootObject["url"].toString() : _authURL;
rootURL.setPath("");
qCDebug(networking) << "Storing an account with access-token for" << qPrintable(rootURL.toString());
_accountInfo = DataServerAccountInfo();
_accountInfo.setAccessTokenFromJSON(rootObject);
emit loginComplete(rootURL);
persistAccountToFile();
saveLoginStatus(true);
requestProfile();
}
} else {
// TODO: error handling
qCDebug(networking) << "Error in response for password grant -" << rootObject["error_description"].toString();
emit loginFailed();
}
}
void AccountManager::requestAccessTokenFinished() {
QNetworkReply* requestReply = reinterpret_cast<QNetworkReply*>(sender());
@ -895,3 +929,34 @@ void AccountManager::handleKeypairGenerationError() {
void AccountManager::setLimitedCommerce(bool isLimited) {
_limitedCommerce = isLimited;
}
void AccountManager::saveLoginStatus(bool isLoggedIn) {
if (!_configFileURL.isEmpty()) {
QFile configFile(_configFileURL);
configFile.open(QIODevice::ReadOnly | QIODevice::Text);
QJsonParseError error;
QJsonDocument jsonDocument = QJsonDocument::fromJson(configFile.readAll(), &error);
configFile.close();
QString launcherPath;
if (error.error == QJsonParseError::NoError) {
QJsonObject rootObject = jsonDocument.object();
if (rootObject.contains("launcherPath")) {
launcherPath = rootObject["launcherPath"].toString();
}
if (rootObject.contains("loggedIn")) {
rootObject["loggedIn"] = isLoggedIn;
}
jsonDocument = QJsonDocument(rootObject);
}
configFile.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
configFile.write(jsonDocument.toJson());
configFile.close();
if (!isLoggedIn && !launcherPath.isEmpty()) {
QProcess launcher;
launcher.setProgram(launcherPath);
launcher.startDetached();
qApp->quit();
}
}
}

View file

@ -102,6 +102,10 @@ public:
bool getLimitedCommerce() { return _limitedCommerce; }
void setLimitedCommerce(bool isLimited);
void setAccessTokens(const QString& response);
void setConfigFileURL(const QString& fileURL) { _configFileURL = fileURL; }
void saveLoginStatus(bool isLoggedIn);
public slots:
void requestAccessToken(const QString& login, const QString& password);
void requestAccessTokenWithSteam(QByteArray authSessionTicket);
@ -162,6 +166,7 @@ private:
QUuid _sessionID { QUuid::createUuid() };
bool _limitedCommerce { false };
QString _configFileURL;
};
#endif // hifi_AccountManager_h