Added additional JSON web api for metrics, added support for old auth and json-based auth via new useJsonWebApi cfg setting.

This commit is contained in:
seefo
2019-03-15 14:16:23 -04:00
parent beb15fce41
commit 8df64ba5a3
11 changed files with 572 additions and 358 deletions
@@ -124,7 +124,7 @@
#include "sharedFoundation/CrcConstexpr.hpp"
#include "webAPI.h"
#include "jsonWebAPI.h"
namespace CentralServerNamespace
{
@@ -128,7 +128,7 @@ void ConfigCentralServer::install(void)
KEY_BOOL (requestDbSaveOnPlanetServerCrash, true);
KEY_INT (maxTimeToWaitForPlanetServerStartSeconds, 5*60); // seconds
KEY_STRING (metricsDataURL, "");
KEY_INT (webUpdateIntervalSeconds, 0);
KEY_INT (webUpdateIntervalSeconds, 5);
int index = 0;
char const * result = 0;
@@ -22,6 +22,7 @@
#include "sharedFoundation/CrcConstexpr.hpp"
#include "webAPI.h"
#include "jsonWebAPI.h"
//-----------------------------------------------------------------------
@@ -150,64 +151,91 @@ 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
const std::string trimmedId = trim(id);
const std::string trimmedKey = trim(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
const std::string trimmedId = trim(id);
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());
// 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);
std::transform(trimmedId.begin(),trimmedId.end(),lcaseId.begin(),::tolower);
// make sure username isn't too long
if (lcaseId.length() > MAX_ACCOUNT_NAME_LENGTH) {
ErrorMessage err("Login Failed", "Account name is too long!");
this->send(err, true);
return;
}
StationId suid = atoi(lcaseId.c_str());
int authOK = 0;
// hash username into station id
StationId suid = atoi(lcaseId.c_str());
if (suid == 0)
{
std::hash<std::string> h;
suid = h(lcaseId.c_str()); //lint !e603 // Symbol 'h' not initialized (it's a functor)
}
if (suid == 0)
{
std::hash<std::string> 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()));
LOG("LoginClientConnection", ("validateClient() for stationId (%lu) at IP (%s), id (%s)", m_stationId, getRemoteAddress().c_str(), lcaseId.c_str()));
std::string authURL(ConfigLoginServer::getExternalAuthUrl());
int authOK = 0;
std::string authURL(ConfigLoginServer::getExternalAuthUrl());
if (!authURL.empty())
{
if(ConfigLoginServer::getUseJsonWebApi())
{
StellaBellum::webAPI api(authURL);
if (!authURL.empty())
{
StellaBellum::webAPI api(authURL);
api.addJsonData<std::string>("user_name", trimmedId);
api.addJsonData<std::string>("user_password", trimmedKey);
api.addJsonData<std::string>("ip", getRemoteAddress());
if (api.submit()) {
std::string msg(api.getString("message"));
if(msg == "success") {
authOK = 1;
} else {
ErrorMessage err("Login Message", msg);
this->send(err, true);
}
}
else {
ErrorMessage err("Login Failed", "request failed");
this->send(err, true);
}
}
else
{
authOK = 1;
}
api.addJsonData<std::string>("user_name", trimmedId);
api.addJsonData<std::string>("user_password", trimmedKey);
api.addJsonData<long>("stationID", suid);
api.addJsonData<std::string>("ip", getRemoteAddress());
if (authOK)
{
LoginServer::getInstance().onValidateClient(suid, lcaseId, this, true, NULL, 0xFFFFFFFF, 0xFFFFFFFF);
}
// else this case will never be reached, noop
if (api.submit())
{
std::string msg(api.getString("message"));
if(msg == "success") {
authOK = 1;
}
else
{
ErrorMessage err("Login Message", msg);
this->send(err, true);
}
}
else
{
ErrorMessage err("Login Failed", "request failed");
this->send(err, true);
}
}
else
{
std::ostringstream postBuf;
postBuf << "user_name=" << trimmedId << "&user_password=" << trimmedKey << "&stationID=" << suid << "&ip=" << getRemoteAddress();
std::string response = webAPI::simplePost(authURL, std::string(postBuf.str()), "");
if (response == "success") {
authOK = 1;
}
else
{
ErrorMessage err("Login Failed", response);
this->send(err, true);
}
}
}
else
{
authOK = 1;
}
if (authOK)
{
LoginServer::getInstance().onValidateClient(suid, lcaseId, this, true, NULL, 0xFFFFFFFF, 0xFFFFFFFF);
}
}
@@ -115,6 +115,7 @@ void ConfigLoginServer::install(void)
KEY_INT (populationLightThresholdPercent, 8);
KEY_INT (csToolPort, 0); // use 10666 if you want to turn the tool on
KEY_BOOL(requireSecureLoginForCsTool, true);
KEY_BOOL(useJsonWebApi, false);
KEY_BOOL(useExternalAuth, false);
KEY_STRING(externalAuthURL, "");
KEY_BOOL(useOldSuidGenerator, false);
@@ -66,11 +66,11 @@ class ConfigLoginServer
int csToolPort;
bool requireSecureLoginForCsTool;
bool useExternalAuth;
bool useOldSuidGenerator;
bool useExternalAuth;
bool useJsonWebApi;
const char * externalAuthURL;
bool useOldSuidGenerator;
};
static const uint16 getCentralServicePort();
@@ -135,8 +135,9 @@ class ConfigLoginServer
static int getPopulationLightThresholdPercent();
static bool getUseExternalAuth();
static bool getUseJsonWebApi();
static const char * getExternalAuthUrl();
static bool getUseOldSuidGenerator();
static bool getUseOldSuidGenerator();
// has character creation for this cluster been disabled through config option
static bool isCharacterCreationDisabled(std::string const & cluster);
@@ -486,6 +487,11 @@ inline const int ConfigLoginServer::getCSToolPort()
return data->csToolPort;
}
inline bool ConfigLoginServer::getUseJsonWebApi()
{
return data->useJsonWebApi;
}
inline bool ConfigLoginServer::getUseExternalAuth()
{
return data->useExternalAuth;