From 0191d75e865a08f057ab5d369e917d546231c67c Mon Sep 17 00:00:00 2001
From: Stephen Birarda <commit@birarda.com>
Date: Tue, 23 Feb 2016 17:29:15 -0800
Subject: [PATCH] allow temporary domain to upload public key

---
 domain-server/resources/web/settings/js/settings.js |  2 +-
 domain-server/src/DomainServer.cpp                  | 13 ++++++++-----
 libraries/networking/src/AccountManager.cpp         | 10 +++++++---
 3 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/domain-server/resources/web/settings/js/settings.js b/domain-server/resources/web/settings/js/settings.js
index 7dc94421be..fae07ace45 100644
--- a/domain-server/resources/web/settings/js/settings.js
+++ b/domain-server/resources/web/settings/js/settings.js
@@ -778,7 +778,7 @@ function chooseFromHighFidelityDomains(clickedButton) {
 function createTemporaryDomain() {
   swal({
     title: 'Create temporary place name',
-    text: "This will create a temporary place name and domain ID (valid for 30 days)"
+    text: "This will create a temporary place name and domain ID"
       + " so other users can easily connect to your domain.</br></br>"
       + "In order to make your domain reachable, this will also enable full automatic networking.",
     showCancelButton: true,
diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp
index 0fe5fd3300..69a2b05812 100644
--- a/domain-server/src/DomainServer.cpp
+++ b/domain-server/src/DomainServer.cpp
@@ -110,7 +110,7 @@ DomainServer::DomainServer(int argc, char* argv[]) :
 
         // preload some user public keys so they can connect on first request
         _gatekeeper.preloadAllowedUserPublicKeys();
-
+        
         optionallyGetTemporaryName(args);
     }
 }
@@ -393,9 +393,10 @@ bool DomainServer::resetAccountManagerAccessToken() {
             if (accessTokenVariant && accessTokenVariant->canConvert(QMetaType::QString)) {
                 accessToken = accessTokenVariant->toString();
             } else {
-                qDebug() << "A domain-server feature that requires authentication is enabled but no access token is present."
-                    << "Set an access token via the web interface, in your user or master config"
+                qDebug() << "A domain-server feature that requires authentication is enabled but no access token is present.";
+                qDebug() << "Set an access token via the web interface, in your user or master config"
                     << "at keypath metaverse.access_token or in your ENV at key DOMAIN_SERVER_ACCESS_TOKEN";
+                AccountManager::getInstance().setAccessTokenForCurrentAuthURL(QString());
                 return false;
             }
         } else {
@@ -432,8 +433,8 @@ void DomainServer::setupAutomaticNetworking() {
     }
 
     if (!resetAccountManagerAccessToken()) {
-        qDebug() << "Cannot send heartbeat to data server without an access token.";
-        qDebug() << "Add an access token to your config file or via the web interface.";
+        qDebug() << "Will not send heartbeat to Metaverse API without an access token.";
+        qDebug() << "If this is not a temporary domain add an access token to your config file or via the web interface.";
 
         return;
     }
@@ -494,6 +495,8 @@ void DomainServer::setupICEHeartbeatForFullNetworking() {
     auto& accountManager = AccountManager::getInstance();
     auto domainID = accountManager.getAccountInfo().getDomainID();
 
+    // if we have an access token and we don't have a private key or the current domain ID has changed
+    // we should generate a new keypair
     if (!accountManager.getAccountInfo().hasPrivateKey() || domainID != limitedNodeList->getSessionUUID()) {
         accountManager.generateNewDomainKeypair(limitedNodeList->getSessionUUID());
     }
diff --git a/libraries/networking/src/AccountManager.cpp b/libraries/networking/src/AccountManager.cpp
index e8fc5d743b..fbd62ba0af 100644
--- a/libraries/networking/src/AccountManager.cpp
+++ b/libraries/networking/src/AccountManager.cpp
@@ -450,8 +450,12 @@ void AccountManager::setAccessTokenForCurrentAuthURL(const QString& accessToken)
     // replace the account info access token with a new OAuthAccessToken
     OAuthAccessToken newOAuthToken;
     newOAuthToken.token = accessToken;
-    
-    qCDebug(networking) << "Setting new account manager access token. F2C:" << accessToken.left(2) << "L2C:" << accessToken.right(2);
+
+    if (!accessToken.isEmpty()) {
+        qCDebug(networking) << "Setting new AccountManager OAuth token. F2C:" << accessToken.left(2) << "L2C:" << accessToken.right(2);
+    } else if (!_accountInfo.getAccessToken().token.isEmpty()) {
+        qCDebug(networking) << "Clearing AccountManager OAuth token.";
+    }
     
     _accountInfo.setAccessToken(newOAuthToken);
 
@@ -658,7 +662,7 @@ void AccountManager::processGeneratedKeypair() {
         callbackParameters.errorCallbackReceiver = this;
         callbackParameters.errorCallbackMethod = "publicKeyUploadFailed";
 
-        sendRequest(uploadPath, AccountManagerAuth::Required, QNetworkAccessManager::PutOperation,
+        sendRequest(uploadPath, AccountManagerAuth::Optional, QNetworkAccessManager::PutOperation,
                     callbackParameters, QByteArray(), requestMultiPart);
         
         keypairGenerator->deleteLater();