From 91cd9b60ece0f0486f9a0c155da2e73fc2afb94b Mon Sep 17 00:00:00 2001 From: DarthArgus Date: Thu, 4 Aug 2016 21:36:55 +0000 Subject: [PATCH] needs QA/TEST before even seeing TC - implement login based on returned username from web api --- .../src/shared/CentralServer.cpp | 3 +- .../src/shared/ClientConnection.cpp | 53 +++++++------------ external/3rd/library/webAPI/webAPI.cpp | 29 +++------- external/3rd/library/webAPI/webAPI.h | 2 +- 4 files changed, 31 insertions(+), 56 deletions(-) diff --git a/engine/server/application/CentralServer/src/shared/CentralServer.cpp b/engine/server/application/CentralServer/src/shared/CentralServer.cpp index 76ba6120..47798110 100755 --- a/engine/server/application/CentralServer/src/shared/CentralServer.cpp +++ b/engine/server/application/CentralServer/src/shared/CentralServer.cpp @@ -2864,13 +2864,14 @@ void CentralServer::sendPopulationUpdateToLoginServer() sendToAllLoginServers(upm); } +// TODO: make this togglable on/off in a config setting void CentralServer::sendMetricsToWebAPI(std::string updateURL) { std::ostringstream postBuf; postBuf << "totalPlayerCount=" << m_totalPlayerCount << "&totalGameServers=" << m_gameServers.size() - 1 << "&totalPlanetServers=" << m_planetServers.size() << "&isPublic=" << getIsClusterPublic() << "&isLocked=" << getIsClusterLocked() << "&isSecret=" << getIsClusterSecret() << "&preloadFinished=" << getClusterStartupTime() << "&databasebacklogged=" << isDatabaseBacklogged() << "&totalTutorialSceneCount=" << m_totalTutorialSceneCount << "&totalFalconSceneCount=" << m_totalFalconSceneCount; - std::string response = webAPI::simplePost(updateURL, std::string(postBuf.str()), ""); + std::string response = webAPI::simplePost(updateURL, std::string(postBuf.str()), "status", "message"); WARNING((response != "success"), ("Error sending stats: %s", response.c_str())); } diff --git a/engine/server/application/LoginServer/src/shared/ClientConnection.cpp b/engine/server/application/LoginServer/src/shared/ClientConnection.cpp index 82aa6b38..bb92df9f 100755 --- a/engine/server/application/LoginServer/src/shared/ClientConnection.cpp +++ b/engine/server/application/LoginServer/src/shared/ClientConnection.cpp @@ -172,63 +172,50 @@ void ClientConnection::onReceive(const Archive::ByteStream & message) // originally was used to validate station API credentials, now uses our custom api void ClientConnection::validateClient(const std::string & id, const std::string & key) { - // to avoid having to re-type this stupid var all over the place - // ideally we wouldn't copy this here, but it would be a huge pain - std::string tmp = trim(id); - - if (tmp.length() > MAX_ACCOUNT_NAME_LENGTH) { - tmp.resize(MAX_ACCOUNT_NAME_LENGTH); // truncate name after the trim - } - - const std::string trimmedId = tmp; - const std::string trimmedKey = trim(key); - - // and to avoid funny business with atoi and casing - // make it a separate var than the one we send the auth server - std::string lcaseId; - lcaseId.resize(trimmedId.size()); - - std::transform(trimmedId.begin(),trimmedId.end(),lcaseId.begin(),::tolower); - - StationId suid = atoi(lcaseId.c_str()); int authOK = 0; - - if (suid == 0) - { - std::hash h; - suid = h(lcaseId.c_str()); //lint !e603 // Symbol 'h' not initialized (it's a functor) - } - - LOG("LoginClientConnection", ("validateClient() for stationId (%lu) at IP (%s), id (%s)", m_stationId, getRemoteAddress().c_str(), lcaseId.c_str())); + StationId suid = atoi(id.c_str()); + std::string uname; std::string authURL(ConfigLoginServer::getExternalAuthUrl()); - if (!authURL.empty()) { std::ostringstream postBuf; - postBuf << "user_name=" << trimmedId << "&user_password=" << trimmedKey << "&stationID=" << suid << "&ip=" << getRemoteAddress(); + postBuf << "user_name=" << id << "&user_password=" << key << "&ip=" << getRemoteAddress(); - std::string response = webAPI::simplePost(authURL, std::string(postBuf.str()), ""); + const std::string response = webAPI::simplePost(authURL, std::string(postBuf.str()), "username", "message"); - if (response == "success") + if (!response.empty()) { authOK = 1; + uname = response; } else { ErrorMessage err("Login Failed", response); this->send(err, true); } - } else { + // test mode authOK = 1; + uname = id; + + if (uname.length() > MAX_ACCOUNT_NAME_LENGTH) { + uname.resize(MAX_ACCOUNT_NAME_LENGTH); + } } if (authOK) { - LoginServer::getInstance().onValidateClient(suid, lcaseId, this, true, NULL, 0xFFFFFFFF, 0xFFFFFFFF); + if (suid == 0) + { + std::hash h; + suid = h(uname.c_str()); + } + + LOG("LoginClientConnection", ("validateClient() for stationId (%lu) at IP (%s), id (%s)", m_stationId, getRemoteAddress().c_str(), uname.c_str())); + LoginServer::getInstance().onValidateClient(suid, uname, this, true, NULL, 0xFFFFFFFF, 0xFFFFFFFF); } // else this case will never be reached, noop } diff --git a/external/3rd/library/webAPI/webAPI.cpp b/external/3rd/library/webAPI/webAPI.cpp index 61cb403d..69e4d67e 100644 --- a/external/3rd/library/webAPI/webAPI.cpp +++ b/external/3rd/library/webAPI/webAPI.cpp @@ -17,40 +17,27 @@ License: what's a license? we're a bunch of dirty pirates! using namespace std; -// if status == success, returns "success", or slotName's contents if specified... -// otherwise returns the "message" if no success -string webAPI::simplePost(const string &endpoint, const string &data, const string &slotName) +// if there is a response contained in slotName, it is returned +string webAPI::simplePost(const string &endpoint, const string &data, const string &slotName, const string &messageSlot) { // declare our output and go ahead and attempt to get data from remote nlohmann::json response = request(endpoint, data, 1); string output; // if we got data back... - if (response.count("status") && response["status"].get() == "success") + if (response.count(slotName)) { - // use custom slot if specified (not "") - if (!(slotName.empty()) && response.count(slotName)) - { - output = response[slotName].get(); - } - else - { - output = "success"; - } + return response[slotName].get(); } else //default message is an error, the other end always assumes "success" or the specified slot { - if (response.count("message")) + if (response.count(messageSlot)) { - output = response["message"].get(); - } - else - { - output = "Message not provided by remote."; + return response[messageSlot].get(); } } - return output; + return "Message not provided by remote."; } // this can be broken out to separate the json bits later if we need raw or other http type requests @@ -122,4 +109,4 @@ size_t webAPI::writeCallback(void *contents, size_t size, size_t nmemb, void *us { ((string*)userp)->append((char*)contents, size * nmemb); return size * nmemb; -} \ No newline at end of file +} diff --git a/external/3rd/library/webAPI/webAPI.h b/external/3rd/library/webAPI/webAPI.h index be5b64ef..87331f41 100644 --- a/external/3rd/library/webAPI/webAPI.h +++ b/external/3rd/library/webAPI/webAPI.h @@ -28,7 +28,7 @@ namespace webAPI { using namespace std; - string simplePost(const string &endpoint, const string &data, const string &slotName); + string simplePost(const string &endpoint, const string &data, const string &slotName, const string &messageSlot); nlohmann::json request(const string &endpoint, const string &data, const int &reqType); // 1 for post, 0 for get size_t writeCallback(void *contents, size_t size, size_t nmemb, void *userp); };