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