yuzu/src/web_service/web_backend.cpp

204 lines
7 KiB
C++
Raw Normal View History

chore: make yuzu REUSE compliant [REUSE] is a specification that aims at making file copyright information consistent, so that it can be both human and machine readable. It basically requires that all files have a header containing copyright and licensing information. When this isn't possible, like when dealing with binary assets, generated files or embedded third-party dependencies, it is permitted to insert copyright information in the `.reuse/dep5` file. Oh, and it also requires that all the licenses used in the project are present in the `LICENSES` folder, that's why the diff is so huge. This can be done automatically with `reuse download --all`. The `reuse` tool also contains a handy subcommand that analyzes the project and tells whether or not the project is (still) compliant, `reuse lint`. Following REUSE has a few advantages over the current approach: - Copyright information is easy to access for users / downstream - Files like `dist/license.md` do not need to exist anymore, as `.reuse/dep5` is used instead - `reuse lint` makes it easy to ensure that copyright information of files like binary assets / images is always accurate and up to date To add copyright information of files that didn't have it I looked up who committed what and when, for each file. As yuzu contributors do not have to sign a CLA or similar I couldn't assume that copyright ownership was of the "yuzu Emulator Project", so I used the name and/or email of the commit author instead. [REUSE]: https://reuse.software Follow-up to 01cf05bc75b1e47beb08937439f3ed9339e7b254
2022-05-14 20:06:02 -04:00
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2018-09-16 14:05:51 -04:00
#include <array>
#include <mutex>
2018-09-16 14:05:51 -04:00
#include <string>
#include <fmt/format.h>
#ifdef __GNUC__
#pragma GCC diagnostic push
#ifndef __clang__
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
#endif
#include <httplib.h>
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
2018-09-16 14:05:51 -04:00
#include "common/logging/log.h"
#include "web_service/web_backend.h"
#include "web_service/web_result.h"
2018-09-16 14:05:51 -04:00
namespace WebService {
2018-09-28 20:51:28 -04:00
constexpr std::array<const char, 1> API_VERSION{'1'};
2018-09-16 14:05:51 -04:00
constexpr std::size_t TIMEOUT_SECONDS = 30;
2018-09-16 14:05:51 -04: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_)} {
std::scoped_lock lock{jwt_cache.mutex};
if (username == jwt_cache.username && token == jwt_cache.token) {
jwt = jwt_cache.jwt;
}
}
/// A generic function handles POST, GET and DELETE request together
WebResult GenericRequest(const std::string& method, const std::string& path,
const std::string& data, bool allow_anonymous,
const std::string& accept) {
if (jwt.empty()) {
UpdateJWT();
}
if (jwt.empty() && !allow_anonymous) {
LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
return WebResult{WebResult::Code::CredentialsMissing, "Credentials needed", ""};
}
auto result = GenericRequest(method, path, data, accept, jwt);
if (result.result_string == "401") {
// Try again with new JWT
UpdateJWT();
result = GenericRequest(method, path, data, accept, jwt);
}
2018-09-16 14:05:51 -04:00
return result;
2018-09-16 14:05:51 -04:00
}
/**
* A generic function with explicit authentication method specified
* JWT is used if the jwt parameter is not empty
* 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
*/
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_ = "") {
if (cli == nullptr) {
cli = std::make_unique<httplib::Client>(host);
2018-09-16 14:05:51 -04:00
}
if (!cli->is_valid()) {
LOG_ERROR(WebService, "Client is invalid, skipping request!");
return {};
}
cli->set_connection_timeout(TIMEOUT_SECONDS);
cli->set_read_timeout(TIMEOUT_SECONDS);
cli->set_write_timeout(TIMEOUT_SECONDS);
2018-09-16 14:05:51 -04: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"));
}
2018-09-16 14:05:51 -04:00
httplib::Request request;
request.method = method;
request.path = path;
request.headers = params;
request.body = data;
2018-09-16 14:05:51 -04:00
httplib::Response response;
httplib::Error error;
2018-09-16 14:05:51 -04:00
if (!cli->send(request, response, error)) {
LOG_ERROR(WebService, "{} to {} returned null (httplib Error: {})", method, host + path,
httplib::to_string(error));
return WebResult{WebResult::Code::LibError, "Null response", ""};
}
2018-09-16 14:05:51 -04:00
if (response.status >= 400) {
LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path,
response.status);
return WebResult{WebResult::Code::HttpError, std::to_string(response.status), ""};
}
2018-09-16 14:05:51 -04:00
auto content_type = response.headers.find("content-type");
2018-09-16 14:05:51 -04:00
if (content_type == response.headers.end()) {
LOG_ERROR(WebService, "{} to {} returned no content", method, host + path);
return WebResult{WebResult::Code::WrongContent, "", ""};
}
2018-09-16 14:05:51 -04:00
if (content_type->second.find(accept) == std::string::npos) {
LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path,
content_type->second);
return WebResult{WebResult::Code::WrongContent, "Wrong content", ""};
}
return WebResult{WebResult::Code::Success, "", response.body};
2018-09-16 14:05:51 -04:00
}
// Retrieve a new JWT from given username and token
void UpdateJWT() {
if (username.empty() || token.empty()) {
return;
}
2018-09-16 14:05:51 -04:00
auto result = GenericRequest("POST", "/jwt/internal", "", "text/html", "", username, token);
if (result.result_code != WebResult::Code::Success) {
2018-09-16 14:05:51 -04:00
LOG_ERROR(WebService, "UpdateJWT failed");
} else {
std::scoped_lock lock{jwt_cache.mutex};
2018-09-16 14:05:51 -04:00
jwt_cache.username = username;
jwt_cache.token = token;
jwt_cache.jwt = jwt = result.returned_data;
}
}
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 14:05:51 -04: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 14:05:51 -04:00
Client::~Client() = default;
WebResult Client::PostJson(const std::string& path, const std::string& data, bool allow_anonymous) {
return impl->GenericRequest("POST", path, data, allow_anonymous, "application/json");
}
WebResult Client::GetJson(const std::string& path, bool allow_anonymous) {
return impl->GenericRequest("GET", path, "", allow_anonymous, "application/json");
}
2018-09-16 14:05:51 -04:00
WebResult Client::DeleteJson(const std::string& path, const std::string& data,
bool allow_anonymous) {
return impl->GenericRequest("DELETE", path, data, allow_anonymous, "application/json");
}
WebResult Client::GetPlain(const std::string& path, bool allow_anonymous) {
return impl->GenericRequest("GET", path, "", allow_anonymous, "text/plain");
}
WebResult Client::GetImage(const std::string& path, bool allow_anonymous) {
return impl->GenericRequest("GET", path, "", allow_anonymous, "image/png");
}
WebResult Client::GetExternalJWT(const std::string& audience) {
return impl->GenericRequest("POST", fmt::format("/jwt/external/{}", audience), "", false,
"text/html");
2018-09-16 14:05:51 -04:00
}
} // namespace WebService