2018-09-16 21:05:51 +03:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2019-11-09 11:09:39 +03:00
|
|
|
#include <array>
|
2018-10-11 04:23:41 +03:00
|
|
|
#include <mutex>
|
2018-09-16 21:05:51 +03:00
|
|
|
#include <string>
|
2020-08-23 02:23:22 +03:00
|
|
|
|
2019-11-09 11:09:39 +03:00
|
|
|
#include <fmt/format.h>
|
2021-07-12 09:18:33 +03:00
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#ifndef __clang__
|
|
|
|
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
|
|
|
#endif
|
|
|
|
#endif
|
2018-10-11 04:23:41 +03:00
|
|
|
#include <httplib.h>
|
2021-07-12 09:18:33 +03:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
2020-08-23 02:23:22 +03:00
|
|
|
|
2018-09-16 21:05:51 +03:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "web_service/web_backend.h"
|
2020-08-23 02:23:22 +03:00
|
|
|
#include "web_service/web_result.h"
|
2018-09-16 21:05:51 +03:00
|
|
|
|
|
|
|
namespace WebService {
|
|
|
|
|
2018-09-29 03:51:28 +03:00
|
|
|
constexpr std::array<const char, 1> API_VERSION{'1'};
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2019-11-09 11:09:39 +03:00
|
|
|
constexpr std::size_t TIMEOUT_SECONDS = 30;
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2018-10-11 04:23:41 +03:00
|
|
|
struct Client::Impl {
|
|
|
|
Impl(std::string host, std::string username, std::string token)
|
|
|
|
: host{std::move(host)}, username{std::move(username)}, token{std::move(token)} {
|
2022-04-07 21:32:47 +03:00
|
|
|
std::scoped_lock lock{jwt_cache.mutex};
|
2018-10-11 04:23:41 +03:00
|
|
|
if (this->username == jwt_cache.username && this->token == jwt_cache.token) {
|
|
|
|
jwt = jwt_cache.jwt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A generic function handles POST, GET and DELETE request together
|
2020-08-23 02:23:22 +03:00
|
|
|
WebResult GenericRequest(const std::string& method, const std::string& path,
|
|
|
|
const std::string& data, bool allow_anonymous,
|
|
|
|
const std::string& accept) {
|
2018-10-11 04:23:41 +03:00
|
|
|
if (jwt.empty()) {
|
|
|
|
UpdateJWT();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jwt.empty() && !allow_anonymous) {
|
|
|
|
LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
|
2020-08-23 02:23:22 +03:00
|
|
|
return WebResult{WebResult::Code::CredentialsMissing, "Credentials needed", ""};
|
2018-10-11 04:23:41 +03:00
|
|
|
}
|
|
|
|
|
2019-11-09 11:09:39 +03:00
|
|
|
auto result = GenericRequest(method, path, data, accept, jwt);
|
2018-10-11 04:23:41 +03:00
|
|
|
if (result.result_string == "401") {
|
|
|
|
// Try again with new JWT
|
|
|
|
UpdateJWT();
|
2019-11-09 11:09:39 +03:00
|
|
|
result = GenericRequest(method, path, data, accept, jwt);
|
2018-10-11 04:23:41 +03:00
|
|
|
}
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2018-10-11 04:23:41 +03:00
|
|
|
return result;
|
2018-09-16 21:05:51 +03:00
|
|
|
}
|
|
|
|
|
2018-10-11 04:23:41 +03:00
|
|
|
/**
|
|
|
|
* A generic function with explicit authentication method specified
|
|
|
|
* JWT is used if the jwt parameter is not empty
|
2019-11-09 11:09:39 +03:00
|
|
|
* username + token is used if jwt is empty but username and token are
|
|
|
|
* not empty anonymous if all of jwt, username and token are empty
|
2018-10-11 04:23:41 +03:00
|
|
|
*/
|
2020-08-23 02:23:22 +03:00
|
|
|
WebResult GenericRequest(const std::string& method, const std::string& path,
|
|
|
|
const std::string& data, const std::string& accept,
|
|
|
|
const std::string& jwt = "", const std::string& username = "",
|
|
|
|
const std::string& token = "") {
|
2018-10-11 04:23:41 +03:00
|
|
|
if (cli == nullptr) {
|
2020-10-20 03:14:41 +03:00
|
|
|
cli = std::make_unique<httplib::Client>(host.c_str());
|
2018-09-16 21:05:51 +03:00
|
|
|
}
|
2020-10-29 03:18:33 +03:00
|
|
|
|
|
|
|
if (!cli->is_valid()) {
|
|
|
|
LOG_ERROR(WebService, "Client is invalid, skipping request!");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-10-13 15:10:50 +03:00
|
|
|
cli->set_connection_timeout(TIMEOUT_SECONDS);
|
|
|
|
cli->set_read_timeout(TIMEOUT_SECONDS);
|
|
|
|
cli->set_write_timeout(TIMEOUT_SECONDS);
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2018-10-11 04:23:41 +03:00
|
|
|
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.begin(), API_VERSION.end()));
|
|
|
|
if (method != "GET") {
|
|
|
|
params.emplace(std::string("Content-Type"), std::string("application/json"));
|
2020-08-23 02:23:22 +03:00
|
|
|
}
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2018-10-11 04:23:41 +03:00
|
|
|
httplib::Request request;
|
|
|
|
request.method = method;
|
|
|
|
request.path = path;
|
|
|
|
request.headers = params;
|
|
|
|
request.body = data;
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2018-10-11 04:23:41 +03:00
|
|
|
httplib::Response response;
|
2021-06-18 21:48:53 +03:00
|
|
|
httplib::Error error;
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2021-06-18 21:48:53 +03:00
|
|
|
if (!cli->send(request, response, error)) {
|
2018-10-11 04:23:41 +03:00
|
|
|
LOG_ERROR(WebService, "{} to {} returned null", method, host + path);
|
2020-08-23 02:23:22 +03:00
|
|
|
return WebResult{WebResult::Code::LibError, "Null response", ""};
|
2018-10-11 04:23:41 +03:00
|
|
|
}
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2018-10-11 04:23:41 +03:00
|
|
|
if (response.status >= 400) {
|
|
|
|
LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path,
|
|
|
|
response.status);
|
2020-08-23 02:23:22 +03:00
|
|
|
return WebResult{WebResult::Code::HttpError, std::to_string(response.status), ""};
|
2018-10-11 04:23:41 +03:00
|
|
|
}
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2018-10-11 04:23:41 +03:00
|
|
|
auto content_type = response.headers.find("content-type");
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2018-10-11 04:23:41 +03:00
|
|
|
if (content_type == response.headers.end()) {
|
|
|
|
LOG_ERROR(WebService, "{} to {} returned no content", method, host + path);
|
2020-08-23 02:23:22 +03:00
|
|
|
return WebResult{WebResult::Code::WrongContent, "", ""};
|
2018-10-11 04:23:41 +03:00
|
|
|
}
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2019-11-09 11:09:39 +03:00
|
|
|
if (content_type->second.find(accept) == std::string::npos) {
|
2018-10-11 04:23:41 +03:00
|
|
|
LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path,
|
|
|
|
content_type->second);
|
2020-08-23 02:23:22 +03:00
|
|
|
return WebResult{WebResult::Code::WrongContent, "Wrong content", ""};
|
2018-10-11 04:23:41 +03:00
|
|
|
}
|
2020-08-23 02:23:22 +03:00
|
|
|
return WebResult{WebResult::Code::Success, "", response.body};
|
2018-09-16 21:05:51 +03:00
|
|
|
}
|
|
|
|
|
2018-10-11 04:23:41 +03:00
|
|
|
// Retrieve a new JWT from given username and token
|
|
|
|
void UpdateJWT() {
|
|
|
|
if (username.empty() || token.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2019-11-09 11:09:39 +03:00
|
|
|
auto result = GenericRequest("POST", "/jwt/internal", "", "text/html", "", username, token);
|
2020-08-23 02:23:22 +03:00
|
|
|
if (result.result_code != WebResult::Code::Success) {
|
2018-09-16 21:05:51 +03:00
|
|
|
LOG_ERROR(WebService, "UpdateJWT failed");
|
|
|
|
} else {
|
2022-04-07 21:32:47 +03:00
|
|
|
std::scoped_lock lock{jwt_cache.mutex};
|
2018-09-16 21:05:51 +03:00
|
|
|
jwt_cache.username = username;
|
|
|
|
jwt_cache.token = token;
|
|
|
|
jwt_cache.jwt = jwt = result.returned_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 04:23:41 +03:00
|
|
|
std::string host;
|
|
|
|
std::string username;
|
|
|
|
std::string token;
|
|
|
|
std::string jwt;
|
|
|
|
std::unique_ptr<httplib::Client> cli;
|
|
|
|
|
|
|
|
struct JWTCache {
|
|
|
|
std::mutex mutex;
|
|
|
|
std::string username;
|
|
|
|
std::string token;
|
|
|
|
std::string jwt;
|
|
|
|
};
|
|
|
|
static inline JWTCache jwt_cache;
|
|
|
|
};
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2018-10-11 04:23:41 +03:00
|
|
|
Client::Client(std::string host, std::string username, std::string token)
|
|
|
|
: impl{std::make_unique<Impl>(std::move(host), std::move(username), std::move(token))} {}
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2018-10-11 04:23:41 +03:00
|
|
|
Client::~Client() = default;
|
|
|
|
|
2020-08-23 02:23:22 +03:00
|
|
|
WebResult Client::PostJson(const std::string& path, const std::string& data, bool allow_anonymous) {
|
2019-11-09 11:09:39 +03:00
|
|
|
return impl->GenericRequest("POST", path, data, allow_anonymous, "application/json");
|
2018-10-11 04:23:41 +03:00
|
|
|
}
|
|
|
|
|
2020-08-23 02:23:22 +03:00
|
|
|
WebResult Client::GetJson(const std::string& path, bool allow_anonymous) {
|
2019-11-09 11:09:39 +03:00
|
|
|
return impl->GenericRequest("GET", path, "", allow_anonymous, "application/json");
|
2018-10-11 04:23:41 +03:00
|
|
|
}
|
2018-09-16 21:05:51 +03:00
|
|
|
|
2020-08-23 02:23:22 +03:00
|
|
|
WebResult Client::DeleteJson(const std::string& path, const std::string& data,
|
|
|
|
bool allow_anonymous) {
|
2019-11-09 11:09:39 +03:00
|
|
|
return impl->GenericRequest("DELETE", path, data, allow_anonymous, "application/json");
|
|
|
|
}
|
|
|
|
|
2020-08-23 02:23:22 +03:00
|
|
|
WebResult Client::GetPlain(const std::string& path, bool allow_anonymous) {
|
2019-11-09 11:09:39 +03:00
|
|
|
return impl->GenericRequest("GET", path, "", allow_anonymous, "text/plain");
|
|
|
|
}
|
|
|
|
|
2020-08-23 02:23:22 +03:00
|
|
|
WebResult Client::GetImage(const std::string& path, bool allow_anonymous) {
|
2019-11-09 11:09:39 +03:00
|
|
|
return impl->GenericRequest("GET", path, "", allow_anonymous, "image/png");
|
|
|
|
}
|
|
|
|
|
2020-08-23 02:23:22 +03:00
|
|
|
WebResult Client::GetExternalJWT(const std::string& audience) {
|
2019-11-09 11:09:39 +03:00
|
|
|
return impl->GenericRequest("POST", fmt::format("/jwt/external/{}", audience), "", false,
|
|
|
|
"text/html");
|
2018-09-16 21:05:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace WebService
|