Initial Commit
This commit is contained in:
24
src/web_service/CMakeLists.txt
Normal file
24
src/web_service/CMakeLists.txt
Normal file
@ -0,0 +1,24 @@
|
||||
# SPDX-FileCopyrightText: 2018 yuzu Emulator Project
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
add_library(web_service STATIC
|
||||
announce_room_json.cpp
|
||||
announce_room_json.h
|
||||
precompiled_headers.h
|
||||
telemetry_json.cpp
|
||||
telemetry_json.h
|
||||
verify_login.cpp
|
||||
verify_login.h
|
||||
verify_user_jwt.cpp
|
||||
verify_user_jwt.h
|
||||
web_backend.cpp
|
||||
web_backend.h
|
||||
web_result.h
|
||||
)
|
||||
|
||||
create_target_directory_groups(web_service)
|
||||
target_link_libraries(web_service PRIVATE common network nlohmann_json::nlohmann_json httplib::httplib cpp-jwt::cpp-jwt)
|
||||
|
||||
if (YUZU_USE_PRECOMPILED_HEADERS)
|
||||
target_precompile_headers(web_service PRIVATE precompiled_headers.h)
|
||||
endif()
|
145
src/web_service/announce_room_json.cpp
Normal file
145
src/web_service/announce_room_json.cpp
Normal file
@ -0,0 +1,145 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <future>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "common/detached_tasks.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "web_service/announce_room_json.h"
|
||||
#include "web_service/web_backend.h"
|
||||
|
||||
namespace AnnounceMultiplayerRoom {
|
||||
|
||||
static void to_json(nlohmann::json& json, const Member& member) {
|
||||
if (!member.username.empty()) {
|
||||
json["username"] = member.username;
|
||||
}
|
||||
json["nickname"] = member.nickname;
|
||||
if (!member.avatar_url.empty()) {
|
||||
json["avatarUrl"] = member.avatar_url;
|
||||
}
|
||||
json["gameName"] = member.game.name;
|
||||
json["gameId"] = member.game.id;
|
||||
}
|
||||
|
||||
static void from_json(const nlohmann::json& json, Member& member) {
|
||||
member.nickname = json.at("nickname").get<std::string>();
|
||||
member.game.name = json.at("gameName").get<std::string>();
|
||||
member.game.id = json.at("gameId").get<u64>();
|
||||
try {
|
||||
member.username = json.at("username").get<std::string>();
|
||||
member.avatar_url = json.at("avatarUrl").get<std::string>();
|
||||
} catch (const nlohmann::detail::out_of_range&) {
|
||||
member.username = member.avatar_url = "";
|
||||
LOG_DEBUG(Network, "Member \'{}\' isn't authenticated", member.nickname);
|
||||
}
|
||||
}
|
||||
|
||||
static void to_json(nlohmann::json& json, const Room& room) {
|
||||
json["port"] = room.information.port;
|
||||
json["name"] = room.information.name;
|
||||
if (!room.information.description.empty()) {
|
||||
json["description"] = room.information.description;
|
||||
}
|
||||
json["preferredGameName"] = room.information.preferred_game.name;
|
||||
json["preferredGameId"] = room.information.preferred_game.id;
|
||||
json["maxPlayers"] = room.information.member_slots;
|
||||
json["netVersion"] = room.net_version;
|
||||
json["hasPassword"] = room.has_password;
|
||||
if (room.members.size() > 0) {
|
||||
nlohmann::json member_json = room.members;
|
||||
json["players"] = member_json;
|
||||
}
|
||||
}
|
||||
|
||||
static void from_json(const nlohmann::json& json, Room& room) {
|
||||
room.verify_uid = json.at("externalGuid").get<std::string>();
|
||||
room.ip = json.at("address").get<std::string>();
|
||||
room.information.name = json.at("name").get<std::string>();
|
||||
try {
|
||||
room.information.description = json.at("description").get<std::string>();
|
||||
} catch (const nlohmann::detail::out_of_range&) {
|
||||
room.information.description = "";
|
||||
LOG_DEBUG(Network, "Room \'{}\' doesn't contain a description", room.information.name);
|
||||
}
|
||||
room.information.host_username = json.at("owner").get<std::string>();
|
||||
room.information.port = json.at("port").get<u16>();
|
||||
room.information.preferred_game.name = json.at("preferredGameName").get<std::string>();
|
||||
room.information.preferred_game.id = json.at("preferredGameId").get<u64>();
|
||||
room.information.member_slots = json.at("maxPlayers").get<u32>();
|
||||
room.net_version = json.at("netVersion").get<u32>();
|
||||
room.has_password = json.at("hasPassword").get<bool>();
|
||||
try {
|
||||
room.members = json.at("players").get<std::vector<Member>>();
|
||||
} catch (const nlohmann::detail::out_of_range& e) {
|
||||
LOG_DEBUG(Network, "Out of range {}", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace AnnounceMultiplayerRoom
|
||||
|
||||
namespace WebService {
|
||||
|
||||
void RoomJson::SetRoomInformation(const std::string& name, const std::string& description,
|
||||
const u16 port, const u32 max_player, const u32 net_version,
|
||||
const bool has_password,
|
||||
const AnnounceMultiplayerRoom::GameInfo& preferred_game) {
|
||||
room.information.name = name;
|
||||
room.information.description = description;
|
||||
room.information.port = port;
|
||||
room.information.member_slots = max_player;
|
||||
room.net_version = net_version;
|
||||
room.has_password = has_password;
|
||||
room.information.preferred_game = preferred_game;
|
||||
}
|
||||
void RoomJson::AddPlayer(const AnnounceMultiplayerRoom::Member& member) {
|
||||
room.members.push_back(member);
|
||||
}
|
||||
|
||||
WebService::WebResult RoomJson::Update() {
|
||||
if (room_id.empty()) {
|
||||
LOG_ERROR(WebService, "Room must be registered to be updated");
|
||||
return WebService::WebResult{WebService::WebResult::Code::LibError,
|
||||
"Room is not registered", ""};
|
||||
}
|
||||
nlohmann::json json{{"players", room.members}};
|
||||
return client.PostJson(fmt::format("/lobby/{}", room_id), json.dump(), false);
|
||||
}
|
||||
|
||||
WebService::WebResult RoomJson::Register() {
|
||||
nlohmann::json json = room;
|
||||
auto result = client.PostJson("/lobby", json.dump(), false);
|
||||
if (result.result_code != WebService::WebResult::Code::Success) {
|
||||
return result;
|
||||
}
|
||||
auto reply_json = nlohmann::json::parse(result.returned_data);
|
||||
room = reply_json.get<AnnounceMultiplayerRoom::Room>();
|
||||
room_id = reply_json.at("id").get<std::string>();
|
||||
return WebService::WebResult{WebService::WebResult::Code::Success, "", room.verify_uid};
|
||||
}
|
||||
|
||||
void RoomJson::ClearPlayers() {
|
||||
room.members.clear();
|
||||
}
|
||||
|
||||
AnnounceMultiplayerRoom::RoomList RoomJson::GetRoomList() {
|
||||
auto reply = client.GetJson("/lobby", true).returned_data;
|
||||
if (reply.empty()) {
|
||||
return {};
|
||||
}
|
||||
return nlohmann::json::parse(reply).at("rooms").get<AnnounceMultiplayerRoom::RoomList>();
|
||||
}
|
||||
|
||||
void RoomJson::Delete() {
|
||||
if (room_id.empty()) {
|
||||
LOG_ERROR(WebService, "Room must be registered to be deleted");
|
||||
return;
|
||||
}
|
||||
Common::DetachedTasks::AddTask([host_{this->host}, username_{this->username},
|
||||
token_{this->token}, room_id_{this->room_id}]() {
|
||||
// create a new client here because the this->client might be destroyed.
|
||||
Client{host_, username_, token_}.DeleteJson(fmt::format("/lobby/{}", room_id_), "", false);
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace WebService
|
41
src/web_service/announce_room_json.h
Normal file
41
src/web_service/announce_room_json.h
Normal file
@ -0,0 +1,41 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include "common/announce_multiplayer_room.h"
|
||||
#include "web_service/web_backend.h"
|
||||
|
||||
namespace WebService {
|
||||
|
||||
/**
|
||||
* Implementation of AnnounceMultiplayerRoom::Backend that (de)serializes room information into/from
|
||||
* JSON, and submits/gets it to/from the yuzu web service
|
||||
*/
|
||||
class RoomJson : public AnnounceMultiplayerRoom::Backend {
|
||||
public:
|
||||
RoomJson(const std::string& host_, const std::string& username_, const std::string& token_)
|
||||
: client(host_, username_, token_), host(host_), username(username_), token(token_) {}
|
||||
~RoomJson() = default;
|
||||
void SetRoomInformation(const std::string& name, const std::string& description, const u16 port,
|
||||
const u32 max_player, const u32 net_version, const bool has_password,
|
||||
const AnnounceMultiplayerRoom::GameInfo& preferred_game) override;
|
||||
void AddPlayer(const AnnounceMultiplayerRoom::Member& member) override;
|
||||
WebResult Update() override;
|
||||
WebResult Register() override;
|
||||
void ClearPlayers() override;
|
||||
AnnounceMultiplayerRoom::RoomList GetRoomList() override;
|
||||
void Delete() override;
|
||||
|
||||
private:
|
||||
AnnounceMultiplayerRoom::Room room;
|
||||
Client client;
|
||||
std::string host;
|
||||
std::string username;
|
||||
std::string token;
|
||||
std::string room_id;
|
||||
};
|
||||
|
||||
} // namespace WebService
|
6
src/web_service/precompiled_headers.h
Normal file
6
src/web_service/precompiled_headers.h
Normal file
@ -0,0 +1,6 @@
|
||||
// SPDX-FileCopyrightText: 2022 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common/common_precompiled_headers.h"
|
130
src/web_service/telemetry_json.cpp
Normal file
130
src/web_service/telemetry_json.cpp
Normal file
@ -0,0 +1,130 @@
|
||||
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "common/detached_tasks.h"
|
||||
#include "web_service/telemetry_json.h"
|
||||
#include "web_service/web_backend.h"
|
||||
#include "web_service/web_result.h"
|
||||
|
||||
namespace WebService {
|
||||
|
||||
namespace Telemetry = Common::Telemetry;
|
||||
|
||||
struct TelemetryJson::Impl {
|
||||
Impl(std::string host_, std::string username_, std::string token_)
|
||||
: host{std::move(host_)}, username{std::move(username_)}, token{std::move(token_)} {}
|
||||
|
||||
nlohmann::json& TopSection() {
|
||||
return sections[static_cast<u8>(Telemetry::FieldType::None)];
|
||||
}
|
||||
|
||||
const nlohmann::json& TopSection() const {
|
||||
return sections[static_cast<u8>(Telemetry::FieldType::None)];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Serialize(Telemetry::FieldType type, const std::string& name, T value) {
|
||||
sections[static_cast<u8>(type)][name] = value;
|
||||
}
|
||||
|
||||
void SerializeSection(Telemetry::FieldType type, const std::string& name) {
|
||||
TopSection()[name] = sections[static_cast<unsigned>(type)];
|
||||
}
|
||||
|
||||
nlohmann::json output;
|
||||
std::array<nlohmann::json, 7> sections;
|
||||
std::string host;
|
||||
std::string username;
|
||||
std::string token;
|
||||
};
|
||||
|
||||
TelemetryJson::TelemetryJson(std::string host, std::string username, std::string token)
|
||||
: impl{std::make_unique<Impl>(std::move(host), std::move(username), std::move(token))} {}
|
||||
TelemetryJson::~TelemetryJson() = default;
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<bool>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<double>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<float>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<u8>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<u16>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<u32>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<u64>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<s8>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<s16>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<s32>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<s64>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<std::string>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<const char*>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), std::string(field.GetValue()));
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<std::chrono::microseconds>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue().count());
|
||||
}
|
||||
|
||||
void TelemetryJson::Complete() {
|
||||
impl->SerializeSection(Telemetry::FieldType::App, "App");
|
||||
impl->SerializeSection(Telemetry::FieldType::Session, "Session");
|
||||
impl->SerializeSection(Telemetry::FieldType::Performance, "Performance");
|
||||
impl->SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig");
|
||||
impl->SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem");
|
||||
|
||||
auto content = impl->TopSection().dump();
|
||||
// Send the telemetry async but don't handle the errors since they were written to the log
|
||||
Common::DetachedTasks::AddTask([host{impl->host}, content]() {
|
||||
Client{host, "", ""}.PostJson("/telemetry", content, true);
|
||||
});
|
||||
}
|
||||
|
||||
bool TelemetryJson::SubmitTestcase() {
|
||||
impl->SerializeSection(Telemetry::FieldType::App, "App");
|
||||
impl->SerializeSection(Telemetry::FieldType::Session, "Session");
|
||||
impl->SerializeSection(Telemetry::FieldType::UserFeedback, "UserFeedback");
|
||||
impl->SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem");
|
||||
impl->SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig");
|
||||
|
||||
auto content = impl->TopSection().dump();
|
||||
Client client(impl->host, impl->username, impl->token);
|
||||
auto value = client.PostJson("/gamedb/testcase", content, false);
|
||||
|
||||
return value.result_code == WebResult::Code::Success;
|
||||
}
|
||||
|
||||
} // namespace WebService
|
44
src/web_service/telemetry_json.h
Normal file
44
src/web_service/telemetry_json.h
Normal file
@ -0,0 +1,44 @@
|
||||
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include "common/telemetry.h"
|
||||
|
||||
namespace WebService {
|
||||
|
||||
/**
|
||||
* Implementation of VisitorInterface that serialized telemetry into JSON, and submits it to the
|
||||
* yuzu web service
|
||||
*/
|
||||
class TelemetryJson : public Common::Telemetry::VisitorInterface {
|
||||
public:
|
||||
TelemetryJson(std::string host, std::string username, std::string token);
|
||||
~TelemetryJson() override;
|
||||
|
||||
void Visit(const Common::Telemetry::Field<bool>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<double>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<float>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<u8>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<u16>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<u32>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<u64>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<s8>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<s16>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<s32>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<s64>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<std::string>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<const char*>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<std::chrono::microseconds>& field) override;
|
||||
|
||||
void Complete() override;
|
||||
bool SubmitTestcase() override;
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl;
|
||||
};
|
||||
|
||||
} // namespace WebService
|
27
src/web_service/verify_login.cpp
Normal file
27
src/web_service/verify_login.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "web_service/verify_login.h"
|
||||
#include "web_service/web_backend.h"
|
||||
#include "web_service/web_result.h"
|
||||
|
||||
namespace WebService {
|
||||
|
||||
bool VerifyLogin(const std::string& host, const std::string& username, const std::string& token) {
|
||||
Client client(host, username, token);
|
||||
auto reply = client.GetJson("/profile", false).returned_data;
|
||||
if (reply.empty()) {
|
||||
return false;
|
||||
}
|
||||
nlohmann::json json = nlohmann::json::parse(reply);
|
||||
const auto iter = json.find("username");
|
||||
|
||||
if (iter == json.end()) {
|
||||
return username.empty();
|
||||
}
|
||||
|
||||
return *iter == username;
|
||||
}
|
||||
|
||||
} // namespace WebService
|
19
src/web_service/verify_login.h
Normal file
19
src/web_service/verify_login.h
Normal file
@ -0,0 +1,19 @@
|
||||
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace WebService {
|
||||
|
||||
/**
|
||||
* Checks if username and token is valid
|
||||
* @param host the web API URL
|
||||
* @param username yuzu username to use for authentication.
|
||||
* @param token yuzu token to use for authentication.
|
||||
* @returns a bool indicating whether the verification succeeded
|
||||
*/
|
||||
bool VerifyLogin(const std::string& host, const std::string& username, const std::string& token);
|
||||
|
||||
} // namespace WebService
|
70
src/web_service/verify_user_jwt.cpp
Normal file
70
src/web_service/verify_user_jwt.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2018 Citra Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" // for deprecated OpenSSL functions
|
||||
#endif
|
||||
#include <jwt/jwt.hpp>
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
#include <system_error>
|
||||
#include "common/logging/log.h"
|
||||
#include "web_service/verify_user_jwt.h"
|
||||
#include "web_service/web_backend.h"
|
||||
#include "web_service/web_result.h"
|
||||
|
||||
namespace WebService {
|
||||
|
||||
static std::string public_key;
|
||||
std::string GetPublicKey(const std::string& host) {
|
||||
if (public_key.empty()) {
|
||||
Client client(host, "", ""); // no need for credentials here
|
||||
public_key = client.GetPlain("/jwt/external/key.pem", true).returned_data;
|
||||
if (public_key.empty()) {
|
||||
LOG_ERROR(WebService, "Could not fetch external JWT public key, verification may fail");
|
||||
} else {
|
||||
LOG_INFO(WebService, "Fetched external JWT public key (size={})", public_key.size());
|
||||
}
|
||||
}
|
||||
return public_key;
|
||||
}
|
||||
|
||||
VerifyUserJWT::VerifyUserJWT(const std::string& host) : pub_key(GetPublicKey(host)) {}
|
||||
|
||||
Network::VerifyUser::UserData VerifyUserJWT::LoadUserData(const std::string& verify_uid,
|
||||
const std::string& token) {
|
||||
const std::string audience = fmt::format("external-{}", verify_uid);
|
||||
using namespace jwt::params;
|
||||
std::error_code error;
|
||||
|
||||
// We use the Citra backend so the issuer is citra-core
|
||||
auto decoded =
|
||||
jwt::decode(token, algorithms({"rs256"}), error, secret(pub_key), issuer("citra-core"),
|
||||
aud(audience), validate_iat(true), validate_jti(true));
|
||||
if (error) {
|
||||
LOG_INFO(WebService, "Verification failed: category={}, code={}, message={}",
|
||||
error.category().name(), error.value(), error.message());
|
||||
return {};
|
||||
}
|
||||
Network::VerifyUser::UserData user_data{};
|
||||
if (decoded.payload().has_claim("username")) {
|
||||
user_data.username = decoded.payload().get_claim_value<std::string>("username");
|
||||
}
|
||||
if (decoded.payload().has_claim("displayName")) {
|
||||
user_data.display_name = decoded.payload().get_claim_value<std::string>("displayName");
|
||||
}
|
||||
if (decoded.payload().has_claim("avatarUrl")) {
|
||||
user_data.avatar_url = decoded.payload().get_claim_value<std::string>("avatarUrl");
|
||||
}
|
||||
if (decoded.payload().has_claim("roles")) {
|
||||
auto roles = decoded.payload().get_claim_value<std::vector<std::string>>("roles");
|
||||
user_data.moderator = std::find(roles.begin(), roles.end(), "moderator") != roles.end();
|
||||
}
|
||||
return user_data;
|
||||
}
|
||||
|
||||
} // namespace WebService
|
26
src/web_service/verify_user_jwt.h
Normal file
26
src/web_service/verify_user_jwt.h
Normal file
@ -0,0 +1,26 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2018 Citra Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include "network/verify_user.h"
|
||||
#include "web_service/web_backend.h"
|
||||
|
||||
namespace WebService {
|
||||
|
||||
std::string GetPublicKey(const std::string& host);
|
||||
|
||||
class VerifyUserJWT final : public Network::VerifyUser::Backend {
|
||||
public:
|
||||
VerifyUserJWT(const std::string& host);
|
||||
~VerifyUserJWT() = default;
|
||||
|
||||
Network::VerifyUser::UserData LoadUserData(const std::string& verify_uid,
|
||||
const std::string& token) override;
|
||||
|
||||
private:
|
||||
std::string pub_key;
|
||||
};
|
||||
|
||||
} // namespace WebService
|
206
src/web_service/web_backend.cpp
Normal file
206
src/web_service/web_backend.cpp
Normal file
@ -0,0 +1,206 @@
|
||||
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <array>
|
||||
#include <mutex>
|
||||
#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
|
||||
|
||||
#include "common/logging/log.h"
|
||||
#include "web_service/web_backend.h"
|
||||
#include "web_service/web_result.h"
|
||||
|
||||
namespace WebService {
|
||||
|
||||
constexpr std::array<const char, 1> API_VERSION{'1'};
|
||||
|
||||
constexpr std::size_t TIMEOUT_SECONDS = 30;
|
||||
|
||||
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 (this->username == jwt_cache.username && this->token == jwt_cache.token) {
|
||||
jwt = jwt_cache.jwt;
|
||||
}
|
||||
|
||||
// Normalize host expression
|
||||
if (!this->host.empty() && this->host.back() == '/') {
|
||||
static_cast<void>(this->host.pop_back());
|
||||
}
|
||||
}
|
||||
|
||||
/// 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);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.c_str());
|
||||
cli->set_connection_timeout(TIMEOUT_SECONDS);
|
||||
cli->set_read_timeout(TIMEOUT_SECONDS);
|
||||
cli->set_write_timeout(TIMEOUT_SECONDS);
|
||||
}
|
||||
if (!cli->is_valid()) {
|
||||
LOG_ERROR(WebService, "Invalid URL {}", host + path);
|
||||
return WebResult{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.begin(), API_VERSION.end()));
|
||||
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::Result result = cli->send(request);
|
||||
|
||||
if (!result) {
|
||||
LOG_ERROR(WebService, "{} to {} returned null", method, host + path);
|
||||
return WebResult{WebResult::Code::LibError, "Null response", ""};
|
||||
}
|
||||
|
||||
httplib::Response response = result.value();
|
||||
|
||||
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), ""};
|
||||
}
|
||||
|
||||
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 WebResult{WebResult::Code::WrongContent, "", ""};
|
||||
}
|
||||
|
||||
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};
|
||||
}
|
||||
|
||||
// Retrieve a new JWT from given username and token
|
||||
void UpdateJWT() {
|
||||
if (username.empty() || token.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto result = GenericRequest("POST", "/jwt/internal", "", "text/html", "", username, token);
|
||||
if (result.result_code != WebResult::Code::Success) {
|
||||
LOG_ERROR(WebService, "UpdateJWT failed");
|
||||
} else {
|
||||
std::scoped_lock lock{jwt_cache.mutex};
|
||||
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;
|
||||
};
|
||||
|
||||
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))} {}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
} // namespace WebService
|
72
src/web_service/web_backend.h
Normal file
72
src/web_service/web_backend.h
Normal file
@ -0,0 +1,72 @@
|
||||
// SPDX-FileCopyrightText: 2017 Citra Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace WebService {
|
||||
|
||||
struct WebResult;
|
||||
|
||||
class Client {
|
||||
public:
|
||||
Client(std::string host, std::string username, std::string token);
|
||||
~Client();
|
||||
|
||||
/**
|
||||
* Posts JSON to the specified path.
|
||||
* @param path the URL segment after the host address.
|
||||
* @param data String of JSON data to use for the body of the POST request.
|
||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||
* @return the result of the request.
|
||||
*/
|
||||
WebResult PostJson(const std::string& path, const std::string& data, bool allow_anonymous);
|
||||
|
||||
/**
|
||||
* Gets JSON from the specified path.
|
||||
* @param path the URL segment after the host address.
|
||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||
* @return the result of the request.
|
||||
*/
|
||||
WebResult GetJson(const std::string& path, bool allow_anonymous);
|
||||
|
||||
/**
|
||||
* Deletes JSON to the specified path.
|
||||
* @param path the URL segment after the host address.
|
||||
* @param data String of JSON data to use for the body of the DELETE request.
|
||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||
* @return the result of the request.
|
||||
*/
|
||||
WebResult DeleteJson(const std::string& path, const std::string& data, bool allow_anonymous);
|
||||
|
||||
/**
|
||||
* Gets a plain string from the specified path.
|
||||
* @param path the URL segment after the host address.
|
||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||
* @return the result of the request.
|
||||
*/
|
||||
WebResult GetPlain(const std::string& path, bool allow_anonymous);
|
||||
|
||||
/**
|
||||
* Gets an PNG image from the specified path.
|
||||
* @param path the URL segment after the host address.
|
||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||
* @return the result of the request.
|
||||
*/
|
||||
WebResult GetImage(const std::string& path, bool allow_anonymous);
|
||||
|
||||
/**
|
||||
* Requests an external JWT for the specific audience provided.
|
||||
* @param audience the audience of the JWT requested.
|
||||
* @return the result of the request.
|
||||
*/
|
||||
WebResult GetExternalJWT(const std::string& audience);
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl;
|
||||
};
|
||||
|
||||
} // namespace WebService
|
24
src/web_service/web_result.h
Normal file
24
src/web_service/web_result.h
Normal file
@ -0,0 +1,24 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "common/common_types.h"
|
||||
|
||||
namespace WebService {
|
||||
struct WebResult {
|
||||
enum class Code : u32 {
|
||||
Success,
|
||||
InvalidURL,
|
||||
CredentialsMissing,
|
||||
LibError,
|
||||
HttpError,
|
||||
WrongContent,
|
||||
NoWebservice,
|
||||
};
|
||||
Code result_code;
|
||||
std::string result_string;
|
||||
std::string returned_data;
|
||||
};
|
||||
} // namespace WebService
|
Reference in New Issue
Block a user