diff options
Diffstat (limited to 'src/web_service')
-rw-r--r-- | src/web_service/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/web_service/announce_room_json.cpp | 145 | ||||
-rw-r--r-- | src/web_service/announce_room_json.h | 41 | ||||
-rw-r--r-- | src/web_service/telemetry_json.cpp | 5 | ||||
-rw-r--r-- | src/web_service/telemetry_json.h | 5 | ||||
-rw-r--r-- | src/web_service/verify_login.cpp | 5 | ||||
-rw-r--r-- | src/web_service/verify_login.h | 5 | ||||
-rw-r--r-- | src/web_service/verify_user_jwt.cpp | 69 | ||||
-rw-r--r-- | src/web_service/verify_user_jwt.h | 26 | ||||
-rw-r--r-- | src/web_service/web_backend.cpp | 5 | ||||
-rw-r--r-- | src/web_service/web_backend.h | 5 |
11 files changed, 301 insertions, 19 deletions
diff --git a/src/web_service/CMakeLists.txt b/src/web_service/CMakeLists.txt index ae85a72ea..3f75d97d1 100644 --- a/src/web_service/CMakeLists.txt +++ b/src/web_service/CMakeLists.txt @@ -1,12 +1,19 @@ +# 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 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 nlohmann_json::nlohmann_json httplib) +target_link_libraries(web_service PRIVATE common network nlohmann_json::nlohmann_json httplib cpp-jwt) diff --git a/src/web_service/announce_room_json.cpp b/src/web_service/announce_room_json.cpp new file mode 100644 index 000000000..4c3195efd --- /dev/null +++ b/src/web_service/announce_room_json.cpp @@ -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 diff --git a/src/web_service/announce_room_json.h b/src/web_service/announce_room_json.h new file mode 100644 index 000000000..32c08858d --- /dev/null +++ b/src/web_service/announce_room_json.h @@ -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 diff --git a/src/web_service/telemetry_json.cpp b/src/web_service/telemetry_json.cpp index 46faddb61..51c792004 100644 --- a/src/web_service/telemetry_json.cpp +++ b/src/web_service/telemetry_json.cpp @@ -1,6 +1,5 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. +// SPDX-FileCopyrightText: 2017 Citra Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later #include <nlohmann/json.hpp> #include "common/detached_tasks.h" diff --git a/src/web_service/telemetry_json.h b/src/web_service/telemetry_json.h index df51e00f8..504002c04 100644 --- a/src/web_service/telemetry_json.h +++ b/src/web_service/telemetry_json.h @@ -1,6 +1,5 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. +// SPDX-FileCopyrightText: 2017 Citra Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later #pragma once diff --git a/src/web_service/verify_login.cpp b/src/web_service/verify_login.cpp index ceb55ca6b..050080278 100644 --- a/src/web_service/verify_login.cpp +++ b/src/web_service/verify_login.cpp @@ -1,6 +1,5 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. +// SPDX-FileCopyrightText: 2017 Citra Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later #include <nlohmann/json.hpp> #include "web_service/verify_login.h" diff --git a/src/web_service/verify_login.h b/src/web_service/verify_login.h index 821b345d7..8d0adce74 100644 --- a/src/web_service/verify_login.h +++ b/src/web_service/verify_login.h @@ -1,6 +1,5 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. +// SPDX-FileCopyrightText: 2017 Citra Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later #pragma once diff --git a/src/web_service/verify_user_jwt.cpp b/src/web_service/verify_user_jwt.cpp new file mode 100644 index 000000000..129eb1968 --- /dev/null +++ b/src/web_service/verify_user_jwt.cpp @@ -0,0 +1,69 @@ +// 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" +#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 diff --git a/src/web_service/verify_user_jwt.h b/src/web_service/verify_user_jwt.h new file mode 100644 index 000000000..27b0a100c --- /dev/null +++ b/src/web_service/verify_user_jwt.h @@ -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 diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index dce9772fe..378804c08 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp @@ -1,6 +1,5 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. +// SPDX-FileCopyrightText: 2017 Citra Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later #include <array> #include <mutex> diff --git a/src/web_service/web_backend.h b/src/web_service/web_backend.h index 81f58583c..11b5f558c 100644 --- a/src/web_service/web_backend.h +++ b/src/web_service/web_backend.h @@ -1,6 +1,5 @@ -// Copyright 2017 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. +// SPDX-FileCopyrightText: 2017 Citra Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later #pragma once |