diff --git a/src/server/sparkd/http/ApiGateway.cpp b/src/server/sparkd/http/ApiGateway.cpp index b6bbfca76..eeb55b67b 100644 --- a/src/server/sparkd/http/ApiGateway.cpp +++ b/src/server/sparkd/http/ApiGateway.cpp @@ -4,6 +4,7 @@ #include "lib/api/public/ApiUtils.h" #include "lib/api/core/ApiClientInterop.h" #include "lib/api/services/UserService.client.pb.h" +#include "lib/api/services/AdminService.client.pb.h" #include "lib/utils/bts_assert.h" #include #include @@ -123,7 +124,7 @@ ApiGatewaySession::~ApiGatewaySession(){} void ApiGatewaySession::handle(const api::gateway::GatewayRequest& request, const Responder& reply) { GatewayResponse response; - LOG(Api,Info, "Gateway Request: {}", api::toJSON(request)); + LOG(Api,Diag, "Gateway Request: {}", api::toJSON(request)); switch(request.request_case()) { case GatewayRequest::kLogin: _me->handle_login(request.login(), reply); @@ -138,4 +139,32 @@ void ApiGatewaySession::handle(const api::gateway::GatewayRequest& request, cons } } +std::string get_server_info(const api::ServiceContext& ctx) { + using namespace api::admin; + using std::endl; + api::VoidMessage vod; + AdminService admin(ctx.locate()); + auto response = admin.get_admin_data(vod, 100); + + std::stringstream ss; + ss << "{" << endl; + + if (response.success()) { + ss << " \"users\": [" << endl; + for (const auto& user : response.result()->users()) { + ss << " "; + if (&user != &response.result()->users()[0]) ss << ","; + ss << "{ "; + ss << "\"name\": \"" << user.name() << "\", "; + ss << "\"operator_id\": \"" << user.user_data().exchange_user_data().cme_data().operator_id() << "\""; + ss << "}" << endl; + } + ss << " ]" << endl; + } + + ss << "}" << endl; + + return ss.str(); +} + }} \ No newline at end of file diff --git a/src/server/sparkd/http/ApiGateway.h b/src/server/sparkd/http/ApiGateway.h index f710e00eb..3d8fb3f8f 100644 --- a/src/server/sparkd/http/ApiGateway.h +++ b/src/server/sparkd/http/ApiGateway.h @@ -20,6 +20,10 @@ class ApiGatewaySession { ~ApiGatewaySession(); void handle(const api::gateway::GatewayRequest& request, const Responder&); + }; +std::string get_server_info(const api::ServiceContext&); + + }} \ No newline at end of file diff --git a/src/server/sparkd/http/WebServer.cpp b/src/server/sparkd/http/WebServer.cpp index 6a6a09327..b4d9d3ec1 100644 --- a/src/server/sparkd/http/WebServer.cpp +++ b/src/server/sparkd/http/WebServer.cpp @@ -156,6 +156,34 @@ namespace http { _mtimer->observe((utils::TimeStamp::now() - start).microseconds() / 1000.0); } + bool require_authentication(connection_ptr& con, const std::string& username, const std::string& password) { + auto auth = con->get_request_header("Authorization"); + if (!auth.empty() && auth.size() >= 7 && boost::iequals(auth.substr(0, 6), "Basic ")) { + auto b64 = auth.substr(6); + auto decoded = websocketpp::base64_decode(b64); + auto colon = decoded.find(':'); + if (colon != std::string::npos) { + auto user = decoded.substr(0, colon); + auto pass = decoded.substr(colon+1); + if (user == username && pass == password) return true; + } + } + + // Failed + con->append_header("WWW-Authenticate", "Basic realm=\"BTS\""); + con->set_status(websocketpp::http::status_code::unauthorized); + con->set_body("Not Authorized"); + return false; + } + + void check_auth_and_return_info(connection_ptr& con) { + if (require_authentication(con, "bts", "bbb835af-b1ea-42d3-8be1-fd9d5555530d")) { + auto horrible_json = get_server_info(_ctx); + con->set_body(horrible_json); + con->set_status(websocketpp::http::status_code::ok); + } + } + void directory_index(connection_ptr& con, const std::string& resource, const boost::filesystem::path& path) { // directory listing std::string body = "\n"; @@ -256,6 +284,9 @@ namespace http { else if (boost::iequals(resource, "/metrics")) { return_metrics(con); } + else if (boost::iequals(resource, "/server_info")) { + check_auth_and_return_info(con); + } else if (boost::iequals(resource, "/bts_installer.exe") && boost::filesystem::is_regular_file(_www+"/bts_installer.exe")) { return_bts_installer(con); }