From f4b0375b3564a3ba1b3dee75ab5e946302a6f348 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 18 Feb 2014 14:48:14 -0800 Subject: [PATCH] stub a method to ask for domain-server auth from NodeList --- libraries/shared/src/AccountManager.cpp | 9 +++- libraries/shared/src/AccountManager.h | 1 + libraries/shared/src/NodeList.cpp | 72 ++++++++++++++----------- libraries/shared/src/NodeList.h | 1 + 4 files changed, 50 insertions(+), 33 deletions(-) diff --git a/libraries/shared/src/AccountManager.cpp b/libraries/shared/src/AccountManager.cpp index 7362631c87..633590c001 100644 --- a/libraries/shared/src/AccountManager.cpp +++ b/libraries/shared/src/AccountManager.cpp @@ -38,15 +38,20 @@ bool AccountManager::hasValidAccessTokenForRootURL(const QUrl &rootURL) { OAuthAccessToken accessToken = _accessTokens.value(rootURL); if (accessToken.token.isEmpty() || accessToken.isExpired()) { - // emit a signal so somebody can call back to us and request an access token given a username and password qDebug() << "An access token is required for requests to" << qPrintable(rootURL.toString()); - emit authenticationRequiredForRootURL(rootURL); return false; } else { return true; } } +bool AccountManager::checkAndSignalForAccessTokenForRootURL(const QUrl& rootURL) { + if (!hasValidAccessTokenForRootURL(rootURL)) { + // emit a signal so somebody can call back to us and request an access token given a username and password + emit authenticationRequiredForRootURL(rootURL); + } +} + void AccountManager::requestAccessToken(const QUrl& rootURL, const QString& username, const QString& password) { if (_networkAccessManager) { if (_clientIDs.contains(rootURL)) { diff --git a/libraries/shared/src/AccountManager.h b/libraries/shared/src/AccountManager.h index 440ac3bb48..5863e702b7 100644 --- a/libraries/shared/src/AccountManager.h +++ b/libraries/shared/src/AccountManager.h @@ -23,6 +23,7 @@ public: static AccountManager& getInstance(); bool hasValidAccessTokenForRootURL(const QUrl& rootURL); + bool checkAndSignalForAccessTokenForRootURL(const QUrl& rootURL); void requestAccessToken(const QUrl& rootURL, const QString& username, const QString& password); diff --git a/libraries/shared/src/NodeList.cpp b/libraries/shared/src/NodeList.cpp index e9a5a0ec9a..d277ea92dc 100644 --- a/libraries/shared/src/NodeList.cpp +++ b/libraries/shared/src/NodeList.cpp @@ -474,40 +474,45 @@ void NodeList::sendDomainServerCheckIn() { // we don't know our public socket and we need to send it to the domain server // send a STUN request to figure it out sendSTUNRequest(); - } else if (!_domainInfo.getIP().isNull() - && (_domainInfo.getRootAuthenticationURL().isEmpty() - || !_sessionUUID.isNull() - || !_domainInfo.getRegistrationToken().isEmpty()) ) { - // construct the DS check in packet - - PacketType domainPacketType = _domainInfo.getRegistrationToken().isEmpty() + } else if (!_domainInfo.getIP().isNull()) { + if (_domainInfo.getRootAuthenticationURL().isEmpty() || !_sessionUUID.isNull() + || !_domainInfo.getRegistrationToken().isEmpty()) { + // construct the DS check in packet + + PacketType domainPacketType = _domainInfo.getRegistrationToken().isEmpty() ? PacketTypeDomainListRequest : PacketTypeDomainConnectRequest; - - QByteArray domainServerPacket = byteArrayWithPopluatedHeader(domainPacketType); - QDataStream packetStream(&domainServerPacket, QIODevice::Append); - - // pack our data to send to the domain-server - packetStream << _ownerType << _publicSockAddr + + QByteArray domainServerPacket = byteArrayWithPopluatedHeader(domainPacketType); + QDataStream packetStream(&domainServerPacket, QIODevice::Append); + + // pack our data to send to the domain-server + packetStream << _ownerType << _publicSockAddr << HifiSockAddr(QHostAddress(getHostOrderLocalAddress()), _nodeSocket.localPort()) << (quint8) _nodeTypesOfInterest.size(); - - // copy over the bytes for node types of interest, if required - foreach (NodeType_t nodeTypeOfInterest, _nodeTypesOfInterest) { - packetStream << nodeTypeOfInterest; + + // copy over the bytes for node types of interest, if required + foreach (NodeType_t nodeTypeOfInterest, _nodeTypesOfInterest) { + packetStream << nodeTypeOfInterest; + } + + writeDatagram(domainServerPacket, _domainInfo.getSockAddr(), _domainInfo.getConnectionSecret()); + const int NUM_DOMAIN_SERVER_CHECKINS_PER_STUN_REQUEST = 5; + static unsigned int numDomainCheckins = 0; + + // send a STUN request every Nth domain server check in so we update our public socket, if required + if (numDomainCheckins++ % NUM_DOMAIN_SERVER_CHECKINS_PER_STUN_REQUEST == 0) { + sendSTUNRequest(); + } + + // increment the count of un-replied check-ins + _numNoReplyDomainCheckIns++; + } else if (!_domainInfo.getRootAuthenticationURL().isEmpty() && _sessionUUID.isNull() + && AccountManager::getInstance().hasValidAccessTokenForRootURL(_domainInfo.getRootAuthenticationURL())) { + // we have an access token we can use for the authentication server the domain-server requested + // so ask that server to provide us with information to connect to the domain-server + requestAuthForDomainServer(); } - - writeDatagram(domainServerPacket, _domainInfo.getSockAddr(), _domainInfo.getConnectionSecret()); - const int NUM_DOMAIN_SERVER_CHECKINS_PER_STUN_REQUEST = 5; - static unsigned int numDomainCheckins = 0; - - // send a STUN request every Nth domain server check in so we update our public socket, if required - if (numDomainCheckins++ % NUM_DOMAIN_SERVER_CHECKINS_PER_STUN_REQUEST == 0) { - sendSTUNRequest(); - } - - // increment the count of un-replied check-ins - _numNoReplyDomainCheckIns++; } } @@ -566,6 +571,10 @@ int NodeList::processDomainServerList(const QByteArray& packet) { return readNodes; } +void NodeList::requestAuthForDomainServer() { + +} + void NodeList::processDomainServerAuthRequest(const QByteArray& packet) { QDataStream authPacketStream(packet); authPacketStream.skipRawData(numBytesForPacketHeader(packet)); @@ -576,8 +585,9 @@ void NodeList::processDomainServerAuthRequest(const QByteArray& packet) { _domainInfo.setRootAuthenticationURL(authenticationRootURL); - if (AccountManager::getInstance().hasValidAccessTokenForRootURL(authenticationRootURL)) { - // request a domain-server registration + if (AccountManager::getInstance().checkAndSignalForAccessTokenForRootURL(authenticationRootURL)) { + // request a domain-server auth + requestAuthForDomainServer(); } } diff --git a/libraries/shared/src/NodeList.h b/libraries/shared/src/NodeList.h index c43b53d52e..1772a8f3d8 100644 --- a/libraries/shared/src/NodeList.h +++ b/libraries/shared/src/NodeList.h @@ -148,6 +148,7 @@ private: void clear(); void processDomainServerAuthRequest(const QByteArray& packet); + void requestAuthForDomainServer(); NodeHash _nodeHash; QMutex _nodeHashMutex;