diff --git a/engine/server/application/LoginServer/src/shared/ClientConnection.cpp b/engine/server/application/LoginServer/src/shared/ClientConnection.cpp index f57947f1..911116b1 100755 --- a/engine/server/application/LoginServer/src/shared/ClientConnection.cpp +++ b/engine/server/application/LoginServer/src/shared/ClientConnection.cpp @@ -172,23 +172,27 @@ 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) { - StationId suid = atoi(id.c_str()); + // 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 + const std::string trimmedId = trim(id); + + StationId suid = atoi(trimmedId.c_str()); int authOK = 0; if (suid == 0) { std::hash h; - suid = h(id); //lint !e603 // Symbol 'h' not initialized (it's a functor) + suid = h(trimmedId); //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(), id.c_str())); + LOG("LoginClientConnection", ("validateClient() for stationId (%lu) at IP (%s), id (%s)", m_stationId, getRemoteAddress().c_str(), trimmedId.c_str())); std::string authURL(ConfigLoginServer::getExternalAuthUrl()); if (!authURL.empty()) { std::ostringstream postBuf; - postBuf << "user_name=" << id << "&user_password=" << key << "&stationID=" << suid << "&ip=" << getRemoteAddress(); + postBuf << "user_name=" << trimmedId << "&user_password=" << key << "&stationID=" << suid << "&ip=" << getRemoteAddress(); std::string response = webAPI::simplePost(authURL, std::string(postBuf.str()), ""); @@ -210,7 +214,7 @@ void ClientConnection::validateClient(const std::string & id, const std::string if (authOK) { - LoginServer::getInstance().onValidateClient(suid, id, this, true, NULL, 0xFFFFFFFF, 0xFFFFFFFF); + LoginServer::getInstance().onValidateClient(suid, trimmedId, this, true, NULL, 0xFFFFFFFF, 0xFFFFFFFF); } // else this case will never be reached, noop } diff --git a/engine/server/application/LoginServer/src/shared/ClientConnection.h b/engine/server/application/LoginServer/src/shared/ClientConnection.h index 432e875f..8f92f68f 100755 --- a/engine/server/application/LoginServer/src/shared/ClientConnection.h +++ b/engine/server/application/LoginServer/src/shared/ClientConnection.h @@ -10,6 +10,10 @@ #include "serverUtility/ServerConnection.h" #include "sharedFoundation/StationId.h" +#include +#include +#include + class ClientCommandChannel; class NetworkId; @@ -68,6 +72,20 @@ private: }; //lint !e1712 // default constructor not defined +//----------------------------------------------------------------------- + +// stolen from http://www.codeproject.com/Articles/10880/A-trim-implementation-for-std-string +// i'm rusty and haven't gotten to lambdas yet +inline const std::string trim(std::string str) +{ + + str.erase(str.begin(), std::find_if(str.begin(), str.end(), + [](char& ch)->bool { return !isspace(ch); })); + str.erase(std::find_if(str.rbegin(), str.rend(), + [](char& ch)->bool { return !isspace(ch); }).base(), str.end()); + return str; +} + //----------------------------------------------------------------------- inline const StationId ClientConnection::getStationId() const diff --git a/external/3rd/library/webAPI/webAPI.cpp b/external/3rd/library/webAPI/webAPI.cpp index 847010f6..b9637b03 100644 --- a/external/3rd/library/webAPI/webAPI.cpp +++ b/external/3rd/library/webAPI/webAPI.cpp @@ -15,13 +15,15 @@ License: what's a license? we're a bunch of dirty pirates! #include "webAPI.h" +using namespace std; + // if status == success, returns "success", or slotName's contents if specified... // otherwise returns the "message" if no success -std::string webAPI::simplePost(std::string endpoint, std::string data, std::string slotName) +string webAPI::simplePost(string endpoint, string data, string slotName) { // declare our output and go ahead and attempt to get data from remote nlohmann::json response = request(endpoint, data, 1); - std::string output; + string output; // if we got data back... if (response.count("status") && response["status"].get() == "success") @@ -53,7 +55,7 @@ std::string webAPI::simplePost(std::string endpoint, std::string data, std::stri // this can be broken out to separate the json bits later if we need raw or other http type requests // all it does is fetch via get or post, and if the status is 200 returns the json, else error json -nlohmann::json webAPI::request(std::string endpoint, std::string data, int reqType) +nlohmann::json webAPI::request(string endpoint, string data, int reqType) { nlohmann::json response; @@ -63,7 +65,7 @@ nlohmann::json webAPI::request(std::string endpoint, std::string data, int reqTy if (curl) { - std::string readBuffer; // container for the remote response + string readBuffer; // container for the remote response long http_code = 0; // we get this after performing the get or post curl_easy_setopt(curl, CURLOPT_URL, endpoint.c_str()); // endpoint is always specified by caller @@ -83,7 +85,7 @@ nlohmann::json webAPI::request(std::string endpoint, std::string data, int reqTy { try { response = nlohmann::json::parse(readBuffer); - } catch (std::string e) { + } catch (string e) { response["message"] = e; response["status"] = "failure"; } catch (...) { @@ -116,7 +118,6 @@ nlohmann::json webAPI::request(std::string endpoint, std::string data, int reqTy // This is used by curl to grab the response and put it into a var size_t webAPI::writeCallback(void *contents, size_t size, size_t nmemb, void *userp) { - ((std::string*)userp)->append((char*)contents, size * nmemb); + ((string*)userp)->append((char*)contents, size * nmemb); return size * nmemb; } - diff --git a/external/3rd/library/webAPI/webAPI.h b/external/3rd/library/webAPI/webAPI.h index 763c69e7..51e60a2c 100644 --- a/external/3rd/library/webAPI/webAPI.h +++ b/external/3rd/library/webAPI/webAPI.h @@ -21,12 +21,14 @@ License: what's a license? we're a bunch of dirty pirates! namespace webAPI { - std::string simplePost(std::string endpoint, std::string data, std::string slotName); + using namespace std; + + string simplePost(string endpoint, string data, string slotName); //std::string simpleGet(char* endpoint, char* data); //nlohmann::json post(char* endpoint, char* data); //nlohmann::json get(char* endpoint, char* data); - nlohmann::json request(std::string endpoint, std::string data, int reqType); // 1 for post, 0 for get + nlohmann::json request(string endpoint, string data, int reqType); // 1 for post, 0 for get size_t writeCallback(void *contents, size_t size, size_t nmemb, void *userp); };