From 981f9df6b387c832363b53f0ba090e25bf43eca6 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 21 May 2014 09:53:09 -0700 Subject: [PATCH] add option to pass data server username and password to DS --- domain-server/src/DomainServer.cpp | 74 ++++++++++++++----- domain-server/src/DomainServer.h | 2 + libraries/shared/src/HifiConfigVariantMap.cpp | 6 +- 3 files changed, 61 insertions(+), 21 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index ab1430fc29..7b06acc9c1 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -50,26 +50,14 @@ DomainServer::DomainServer(int argc, char* argv[]) : _argumentVariantMap = HifiConfigVariantMap::mergeCLParametersWithJSONConfig(arguments()); - if (optionallyReadX509KeyAndCertificate() && optionallySetupOAuth()) { - // we either read a certificate and private key or were not passed one, good to load assignments - // and set up the node list + _networkAccessManager = new QNetworkAccessManager(this); + + if (optionallyReadX509KeyAndCertificate() && optionallySetupOAuth() && optionallyLoginAndSetupAssignmentPayment()) { + // we either read a certificate and private key or were not passed one + // and completed login or did not need to + qDebug() << "Setting up LimitedNodeList and assignments."; setupNodeListAndAssignments(); - - _networkAccessManager = new QNetworkAccessManager(this); - - // setup a timer to send transactions to pay assigned nodes every 30 seconds - QTimer* creditSetupTimer = new QTimer(this); - connect(creditSetupTimer, &QTimer::timeout, this, &DomainServer::setupPendingAssignmentCredits); - - const qint64 CREDIT_CHECK_INTERVAL_MSECS = 5 * 1000; - creditSetupTimer->start(CREDIT_CHECK_INTERVAL_MSECS); - - QTimer* nodePaymentTimer = new QTimer(this); - connect(nodePaymentTimer, &QTimer::timeout, this, &DomainServer::sendPendingTransactionsToServer); - - const qint64 TRANSACTION_SEND_INTERVAL_MSECS = 30 * 1000; - nodePaymentTimer->start(TRANSACTION_SEND_INTERVAL_MSECS); } } @@ -201,6 +189,56 @@ void DomainServer::setupNodeListAndAssignments(const QUuid& sessionUUID) { addStaticAssignmentsToQueue(); } +bool DomainServer::optionallyLoginAndSetupAssignmentPayment() { + // check if we have a username and password set via env + const QString HIFI_AUTH_ENABLED_OPTION = "hifi-auth"; + const QString HIFI_USERNAME_ENV_KEY = "DOMAIN_SERVER_USERNAME"; + const QString HIFI_PASSWORD_ENV_KEY = "DOMAIN_SERVER_PASSWORD"; + + if (_argumentVariantMap.contains(HIFI_AUTH_ENABLED_OPTION)) { + AccountManager& accountManager = AccountManager::getInstance(); + accountManager.setAuthURL(DEFAULT_NODE_AUTH_URL); + + if (!accountManager.hasValidAccessToken()) { + // we don't have a valid access token so we need to get one + QString username = QProcessEnvironment::systemEnvironment().value(HIFI_USERNAME_ENV_KEY); + QString password = QProcessEnvironment::systemEnvironment().value(HIFI_PASSWORD_ENV_KEY); + + if (!username.isEmpty() && !password.isEmpty()) { + accountManager.requestAccessToken(username, password); + + // connect to loginFailed signal from AccountManager so we can quit if that is the case + connect(&accountManager, &AccountManager::loginFailed, this, &DomainServer::loginFailed); + } else { + qDebug() << "Missing access-token or username and password combination. domain-server will now quit."; + QMetaObject::invokeMethod(this, "quit", Qt::QueuedConnection); + return false; + } + } + + // assume that the fact we are authing against HF data server means we will pay for assignments + // setup a timer to send transactions to pay assigned nodes every 30 seconds + QTimer* creditSetupTimer = new QTimer(this); + connect(creditSetupTimer, &QTimer::timeout, this, &DomainServer::setupPendingAssignmentCredits); + + const qint64 CREDIT_CHECK_INTERVAL_MSECS = 5 * 1000; + creditSetupTimer->start(CREDIT_CHECK_INTERVAL_MSECS); + + QTimer* nodePaymentTimer = new QTimer(this); + connect(nodePaymentTimer, &QTimer::timeout, this, &DomainServer::sendPendingTransactionsToServer); + + const qint64 TRANSACTION_SEND_INTERVAL_MSECS = 30 * 1000; + nodePaymentTimer->start(TRANSACTION_SEND_INTERVAL_MSECS); + } + + return true; +} + +void DomainServer::loginFailed() { + qDebug() << "Login to data server has failed. domain-server will now quit"; + QMetaObject::invokeMethod(this, "quit", Qt::QueuedConnection); +} + void DomainServer::parseAssignmentConfigs(QSet& excludedTypes) { // check for configs from the command line, these take precedence const QString ASSIGNMENT_CONFIG_REGEX_STRING = "config-([\\d]+)"; diff --git a/domain-server/src/DomainServer.h b/domain-server/src/DomainServer.h index fc8806d53e..645b023c78 100644 --- a/domain-server/src/DomainServer.h +++ b/domain-server/src/DomainServer.h @@ -48,6 +48,7 @@ public slots: void nodeKilled(SharedNodePointer node); private slots: + void loginFailed(); void readAvailableDatagrams(); void setupPendingAssignmentCredits(); void sendPendingTransactionsToServer(); @@ -55,6 +56,7 @@ private: void setupNodeListAndAssignments(const QUuid& sessionUUID = QUuid::createUuid()); bool optionallySetupOAuth(); bool optionallyReadX509KeyAndCertificate(); + bool optionallyLoginAndSetupAssignmentPayment(); void processDatagram(const QByteArray& receivedPacket, const HifiSockAddr& senderSockAddr); diff --git a/libraries/shared/src/HifiConfigVariantMap.cpp b/libraries/shared/src/HifiConfigVariantMap.cpp index d20f4276f1..e8ab59ce2d 100644 --- a/libraries/shared/src/HifiConfigVariantMap.cpp +++ b/libraries/shared/src/HifiConfigVariantMap.cpp @@ -41,19 +41,19 @@ QVariantMap HifiConfigVariantMap::mergeCLParametersWithJSONConfig(const QStringL nextKeyIndex = argumentList.indexOf(dashedKeyRegex, keyIndex + 1); - if (nextKeyIndex == keyIndex + 1) { + if (nextKeyIndex == keyIndex + 1 || keyIndex == argumentList.size() - 1) { // there's no value associated with this option, it's a boolean // so add it to the variant map with NULL as value mergedMap.insertMulti(key, QVariant()); } else { - int maxIndex = (nextKeyIndex == -1) ? argumentList.size() : nextKeyIndex; + int maxIndex = (nextKeyIndex == -1) ? argumentList.size() - 1: nextKeyIndex; // there's at least one value associated with the option // pull the first value to start QString value = argumentList[keyIndex + 1]; // for any extra values, append them, with a space, to the value string - for (int i = keyIndex + 2; i < maxIndex; i++) { + for (int i = keyIndex + 2; i <= maxIndex; i++) { value += " " + argumentList[i]; }