From 4d139943f2407144d5f8e3dc5a673f24850d43d0 Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Sun, 16 Sep 2018 20:05:51 +0200 Subject: Port web_service from Citra --- src/web_service/web_backend.cpp | 147 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 src/web_service/web_backend.cpp (limited to 'src/web_service/web_backend.cpp') diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp new file mode 100644 index 000000000..a726fb8eb --- /dev/null +++ b/src/web_service/web_backend.cpp @@ -0,0 +1,147 @@ +// Copyright 2017 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include +#include +#include +#include "common/logging/log.h" +#include "common/web_result.h" +#include "core/settings.h" +#include "web_service/web_backend.h" + +namespace WebService { + +static constexpr char API_VERSION[]{"1"}; + +constexpr int HTTP_PORT = 80; +constexpr int HTTPS_PORT = 443; + +constexpr int TIMEOUT_SECONDS = 30; + +Client::JWTCache Client::jwt_cache{}; + +Client::Client(const std::string& host, const std::string& username, const std::string& token) + : host(host), username(username), token(token) { + if (username == jwt_cache.username && token == jwt_cache.token) { + jwt = jwt_cache.jwt; + } +} + +Common::WebResult Client::GenericJson(const std::string& method, const std::string& path, + const std::string& data, const std::string& jwt, + const std::string& username, const std::string& token) { + if (cli == nullptr) { + auto parsedUrl = LUrlParser::clParseURL::ParseURL(host); + int port; + if (parsedUrl.m_Scheme == "http") { + if (!parsedUrl.GetPort(&port)) { + port = HTTP_PORT; + } + cli = + std::make_unique(parsedUrl.m_Host.c_str(), port, TIMEOUT_SECONDS); + } else if (parsedUrl.m_Scheme == "https") { + if (!parsedUrl.GetPort(&port)) { + port = HTTPS_PORT; + } + cli = std::make_unique(parsedUrl.m_Host.c_str(), port, + TIMEOUT_SECONDS); + } else { + LOG_ERROR(WebService, "Bad URL scheme {}", parsedUrl.m_Scheme); + return Common::WebResult{Common::WebResult::Code::InvalidURL, "Bad URL scheme"}; + } + } + if (cli == nullptr) { + LOG_ERROR(WebService, "Invalid URL {}", host + path); + return Common::WebResult{Common::WebResult::Code::InvalidURL, "Invalid URL"}; + } + + httplib::Headers params; + if (!jwt.empty()) { + params = { + {std::string("Authorization"), fmt::format("Bearer {}", jwt)}, + }; + } else if (!username.empty()) { + params = { + {std::string("x-username"), username}, + {std::string("x-token"), token}, + }; + } + + params.emplace(std::string("api-version"), std::string(API_VERSION)); + if (method != "GET") { + params.emplace(std::string("Content-Type"), std::string("application/json")); + }; + + httplib::Request request; + request.method = method; + request.path = path; + request.headers = params; + request.body = data; + + httplib::Response response; + + if (!cli->send(request, response)) { + LOG_ERROR(WebService, "{} to {} returned null", method, host + path); + return Common::WebResult{Common::WebResult::Code::LibError, "Null response"}; + } + + if (response.status >= 400) { + LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path, + response.status); + return Common::WebResult{Common::WebResult::Code::HttpError, + std::to_string(response.status)}; + } + + auto content_type = response.headers.find("content-type"); + + if (content_type == response.headers.end()) { + LOG_ERROR(WebService, "{} to {} returned no content", method, host + path); + return Common::WebResult{Common::WebResult::Code::WrongContent, ""}; + } + + if (content_type->second.find("application/json") == std::string::npos && + content_type->second.find("text/html; charset=utf-8") == std::string::npos) { + LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path, + content_type->second); + return Common::WebResult{Common::WebResult::Code::WrongContent, "Wrong content"}; + } + return Common::WebResult{Common::WebResult::Code::Success, "", response.body}; +} + +void Client::UpdateJWT() { + if (!username.empty() && !token.empty()) { + auto result = GenericJson("POST", "/jwt/internal", "", "", username, token); + if (result.result_code != Common::WebResult::Code::Success) { + LOG_ERROR(WebService, "UpdateJWT failed"); + } else { + jwt_cache.username = username; + jwt_cache.token = token; + jwt_cache.jwt = jwt = result.returned_data; + } + } +} + +Common::WebResult Client::GenericJson(const std::string& method, const std::string& path, + const std::string& data, bool allow_anonymous) { + if (jwt.empty()) { + UpdateJWT(); + } + + if (jwt.empty() && !allow_anonymous) { + LOG_ERROR(WebService, "Credentials must be provided for authenticated requests"); + return Common::WebResult{Common::WebResult::Code::CredentialsMissing, "Credentials needed"}; + } + + auto result = GenericJson(method, path, data, jwt); + if (result.result_string == "401") { + // Try again with new JWT + UpdateJWT(); + result = GenericJson(method, path, data, jwt); + } + + return result; +} + +} // namespace WebService -- cgit v1.2.3 From b4ace6ec6f86079b3bd297f95dfe133240b53e15 Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Mon, 17 Sep 2018 17:16:01 +0200 Subject: Address a bunch of review comments --- src/web_service/web_backend.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/web_service/web_backend.cpp') diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index a726fb8eb..3a3f44dc2 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp @@ -13,12 +13,12 @@ namespace WebService { -static constexpr char API_VERSION[]{"1"}; +constexpr char API_VERSION[]{"1"}; -constexpr int HTTP_PORT = 80; -constexpr int HTTPS_PORT = 443; +constexpr u32 HTTP_PORT = 80; +constexpr u32 HTTPS_PORT = 443; -constexpr int TIMEOUT_SECONDS = 30; +constexpr u32 TIMEOUT_SECONDS = 30; Client::JWTCache Client::jwt_cache{}; -- cgit v1.2.3 From 62f9409ba3e114b40b6923808290c02bf5af3d2c Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Mon, 17 Sep 2018 14:28:58 -0400 Subject: web_backend: protect jwt cache with a mutex --- src/web_service/web_backend.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/web_service/web_backend.cpp') diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index 3a3f44dc2..5df4df5eb 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp @@ -24,6 +24,7 @@ Client::JWTCache Client::jwt_cache{}; Client::Client(const std::string& host, const std::string& username, const std::string& token) : host(host), username(username), token(token) { + std::lock_guard lock(jwt_cache.mutex); if (username == jwt_cache.username && token == jwt_cache.token) { jwt = jwt_cache.jwt; } @@ -116,6 +117,7 @@ void Client::UpdateJWT() { if (result.result_code != Common::WebResult::Code::Success) { LOG_ERROR(WebService, "UpdateJWT failed"); } else { + std::lock_guard lock(jwt_cache.mutex); jwt_cache.username = username; jwt_cache.token = token; jwt_cache.jwt = jwt = result.returned_data; -- cgit v1.2.3 From ac06105dfe9c7e5306e1f4cac0ee12dc5f72f14e Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Sat, 29 Sep 2018 02:51:28 +0200 Subject: Review comments -part 4 --- src/web_service/web_backend.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/web_service/web_backend.cpp') diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index 5df4df5eb..787b0fbcb 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp @@ -13,7 +13,7 @@ namespace WebService { -constexpr char API_VERSION[]{"1"}; +constexpr std::array API_VERSION{'1'}; constexpr u32 HTTP_PORT = 80; constexpr u32 HTTPS_PORT = 443; @@ -70,7 +70,7 @@ Common::WebResult Client::GenericJson(const std::string& method, const std::stri }; } - params.emplace(std::string("api-version"), std::string(API_VERSION)); + params.emplace(std::string("api-version"), std::string(API_VERSION.begin(), API_VERSION.end())); if (method != "GET") { params.emplace(std::string("Content-Type"), std::string("application/json")); }; -- cgit v1.2.3