summaryrefslogtreecommitdiff
path: root/src/web_service
diff options
context:
space:
mode:
Diffstat (limited to 'src/web_service')
-rw-r--r--src/web_service/CMakeLists.txt6
-rw-r--r--src/web_service/announce_room_json.cpp145
-rw-r--r--src/web_service/announce_room_json.h41
-rw-r--r--src/web_service/verify_user_jwt.cpp67
-rw-r--r--src/web_service/verify_user_jwt.h26
5 files changed, 284 insertions, 1 deletions
diff --git a/src/web_service/CMakeLists.txt b/src/web_service/CMakeLists.txt
index ae85a72ea..753fb6e7a 100644
--- a/src/web_service/CMakeLists.txt
+++ b/src/web_service/CMakeLists.txt
@@ -1,12 +1,16 @@
1add_library(web_service STATIC 1add_library(web_service STATIC
2 announce_room_json.cpp
3 announce_room_json.h
2 telemetry_json.cpp 4 telemetry_json.cpp
3 telemetry_json.h 5 telemetry_json.h
4 verify_login.cpp 6 verify_login.cpp
5 verify_login.h 7 verify_login.h
8 verify_user_jwt.cpp
9 verify_user_jwt.h
6 web_backend.cpp 10 web_backend.cpp
7 web_backend.h 11 web_backend.h
8 web_result.h 12 web_result.h
9) 13)
10 14
11create_target_directory_groups(web_service) 15create_target_directory_groups(web_service)
12target_link_libraries(web_service PRIVATE common nlohmann_json::nlohmann_json httplib) 16target_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 @@
1// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#include <future>
5#include <nlohmann/json.hpp>
6#include "common/detached_tasks.h"
7#include "common/logging/log.h"
8#include "web_service/announce_room_json.h"
9#include "web_service/web_backend.h"
10
11namespace AnnounceMultiplayerRoom {
12
13static void to_json(nlohmann::json& json, const Member& member) {
14 if (!member.username.empty()) {
15 json["username"] = member.username;
16 }
17 json["nickname"] = member.nickname;
18 if (!member.avatar_url.empty()) {
19 json["avatarUrl"] = member.avatar_url;
20 }
21 json["gameName"] = member.game.name;
22 json["gameId"] = member.game.id;
23}
24
25static void from_json(const nlohmann::json& json, Member& member) {
26 member.nickname = json.at("nickname").get<std::string>();
27 member.game.name = json.at("gameName").get<std::string>();
28 member.game.id = json.at("gameId").get<u64>();
29 try {
30 member.username = json.at("username").get<std::string>();
31 member.avatar_url = json.at("avatarUrl").get<std::string>();
32 } catch (const nlohmann::detail::out_of_range&) {
33 member.username = member.avatar_url = "";
34 LOG_DEBUG(Network, "Member \'{}\' isn't authenticated", member.nickname);
35 }
36}
37
38static void to_json(nlohmann::json& json, const Room& room) {
39 json["port"] = room.information.port;
40 json["name"] = room.information.name;
41 if (!room.information.description.empty()) {
42 json["description"] = room.information.description;
43 }
44 json["preferredGameName"] = room.information.preferred_game.name;
45 json["preferredGameId"] = room.information.preferred_game.id;
46 json["maxPlayers"] = room.information.member_slots;
47 json["netVersion"] = room.net_version;
48 json["hasPassword"] = room.has_password;
49 if (room.members.size() > 0) {
50 nlohmann::json member_json = room.members;
51 json["players"] = member_json;
52 }
53}
54
55static void from_json(const nlohmann::json& json, Room& room) {
56 room.verify_uid = json.at("externalGuid").get<std::string>();
57 room.ip = json.at("address").get<std::string>();
58 room.information.name = json.at("name").get<std::string>();
59 try {
60 room.information.description = json.at("description").get<std::string>();
61 } catch (const nlohmann::detail::out_of_range&) {
62 room.information.description = "";
63 LOG_DEBUG(Network, "Room \'{}\' doesn't contain a description", room.information.name);
64 }
65 room.information.host_username = json.at("owner").get<std::string>();
66 room.information.port = json.at("port").get<u16>();
67 room.information.preferred_game.name = json.at("preferredGameName").get<std::string>();
68 room.information.preferred_game.id = json.at("preferredGameId").get<u64>();
69 room.information.member_slots = json.at("maxPlayers").get<u32>();
70 room.net_version = json.at("netVersion").get<u32>();
71 room.has_password = json.at("hasPassword").get<bool>();
72 try {
73 room.members = json.at("players").get<std::vector<Member>>();
74 } catch (const nlohmann::detail::out_of_range& e) {
75 LOG_DEBUG(Network, "Out of range {}", e.what());
76 }
77}
78
79} // namespace AnnounceMultiplayerRoom
80
81namespace WebService {
82
83void RoomJson::SetRoomInformation(const std::string& name, const std::string& description,
84 const u16 port, const u32 max_player, const u32 net_version,
85 const bool has_password,
86 const AnnounceMultiplayerRoom::GameInfo& preferred_game) {
87 room.information.name = name;
88 room.information.description = description;
89 room.information.port = port;
90 room.information.member_slots = max_player;
91 room.net_version = net_version;
92 room.has_password = has_password;
93 room.information.preferred_game = preferred_game;
94}
95void RoomJson::AddPlayer(const AnnounceMultiplayerRoom::Member& member) {
96 room.members.push_back(member);
97}
98
99WebService::WebResult RoomJson::Update() {
100 if (room_id.empty()) {
101 LOG_ERROR(WebService, "Room must be registered to be updated");
102 return WebService::WebResult{WebService::WebResult::Code::LibError,
103 "Room is not registered", ""};
104 }
105 nlohmann::json json{{"players", room.members}};
106 return client.PostJson(fmt::format("/lobby/{}", room_id), json.dump(), false);
107}
108
109WebService::WebResult RoomJson::Register() {
110 nlohmann::json json = room;
111 auto result = client.PostJson("/lobby", json.dump(), false);
112 if (result.result_code != WebService::WebResult::Code::Success) {
113 return result;
114 }
115 auto reply_json = nlohmann::json::parse(result.returned_data);
116 room = reply_json.get<AnnounceMultiplayerRoom::Room>();
117 room_id = reply_json.at("id").get<std::string>();
118 return WebService::WebResult{WebService::WebResult::Code::Success, "", room.verify_uid};
119}
120
121void RoomJson::ClearPlayers() {
122 room.members.clear();
123}
124
125AnnounceMultiplayerRoom::RoomList RoomJson::GetRoomList() {
126 auto reply = client.GetJson("/lobby", true).returned_data;
127 if (reply.empty()) {
128 return {};
129 }
130 return nlohmann::json::parse(reply).at("rooms").get<AnnounceMultiplayerRoom::RoomList>();
131}
132
133void RoomJson::Delete() {
134 if (room_id.empty()) {
135 LOG_ERROR(WebService, "Room must be registered to be deleted");
136 return;
137 }
138 Common::DetachedTasks::AddTask(
139 [host{this->host}, username{this->username}, token{this->token}, room_id{this->room_id}]() {
140 // create a new client here because the this->client might be destroyed.
141 Client{host, username, token}.DeleteJson(fmt::format("/lobby/{}", room_id), "", false);
142 });
143}
144
145} // 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 @@
1// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6#include <functional>
7#include <string>
8#include "common/announce_multiplayer_room.h"
9#include "web_service/web_backend.h"
10
11namespace WebService {
12
13/**
14 * Implementation of AnnounceMultiplayerRoom::Backend that (de)serializes room information into/from
15 * JSON, and submits/gets it to/from the yuzu web service
16 */
17class RoomJson : public AnnounceMultiplayerRoom::Backend {
18public:
19 RoomJson(const std::string& host_, const std::string& username_, const std::string& token_)
20 : client(host_, username_, token_), host(host_), username(username_), token(token_) {}
21 ~RoomJson() = default;
22 void SetRoomInformation(const std::string& name, const std::string& description, const u16 port,
23 const u32 max_player, const u32 net_version, const bool has_password,
24 const AnnounceMultiplayerRoom::GameInfo& preferred_game) override;
25 void AddPlayer(const AnnounceMultiplayerRoom::Member& member) override;
26 WebResult Update() override;
27 WebResult Register() override;
28 void ClearPlayers() override;
29 AnnounceMultiplayerRoom::RoomList GetRoomList() override;
30 void Delete() override;
31
32private:
33 AnnounceMultiplayerRoom::Room room;
34 Client client;
35 std::string host;
36 std::string username;
37 std::string token;
38 std::string room_id;
39};
40
41} // namespace WebService
diff --git a/src/web_service/verify_user_jwt.cpp b/src/web_service/verify_user_jwt.cpp
new file mode 100644
index 000000000..3bff46f0a
--- /dev/null
+++ b/src/web_service/verify_user_jwt.cpp
@@ -0,0 +1,67 @@
1// SPDX-FileCopyrightText: Copyright 2018 Citra Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#if defined(__GNUC__) || defined(__clang__)
5#pragma GCC diagnostic push
6#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
7#endif
8#include <jwt/jwt.hpp>
9#if defined(__GNUC__) || defined(__clang__)
10#pragma GCC diagnostic pop
11#endif
12
13#include <system_error>
14#include "common/logging/log.h"
15#include "web_service/verify_user_jwt.h"
16#include "web_service/web_backend.h"
17#include "web_service/web_result.h"
18
19namespace WebService {
20
21static std::string public_key;
22std::string GetPublicKey(const std::string& host) {
23 if (public_key.empty()) {
24 Client client(host, "", ""); // no need for credentials here
25 public_key = client.GetPlain("/jwt/external/key.pem", true).returned_data;
26 if (public_key.empty()) {
27 LOG_ERROR(WebService, "Could not fetch external JWT public key, verification may fail");
28 } else {
29 LOG_INFO(WebService, "Fetched external JWT public key (size={})", public_key.size());
30 }
31 }
32 return public_key;
33}
34
35VerifyUserJWT::VerifyUserJWT(const std::string& host) : pub_key(GetPublicKey(host)) {}
36
37Network::VerifyUser::UserData VerifyUserJWT::LoadUserData(const std::string& verify_uid,
38 const std::string& token) {
39 const std::string audience = fmt::format("external-{}", verify_uid);
40 using namespace jwt::params;
41 std::error_code error;
42 auto decoded =
43 jwt::decode(token, algorithms({"rs256"}), error, secret(pub_key), issuer("yuzu-core"),
44 aud(audience), validate_iat(true), validate_jti(true));
45 if (error) {
46 LOG_INFO(WebService, "Verification failed: category={}, code={}, message={}",
47 error.category().name(), error.value(), error.message());
48 return {};
49 }
50 Network::VerifyUser::UserData user_data{};
51 if (decoded.payload().has_claim("username")) {
52 user_data.username = decoded.payload().get_claim_value<std::string>("username");
53 }
54 if (decoded.payload().has_claim("displayName")) {
55 user_data.display_name = decoded.payload().get_claim_value<std::string>("displayName");
56 }
57 if (decoded.payload().has_claim("avatarUrl")) {
58 user_data.avatar_url = decoded.payload().get_claim_value<std::string>("avatarUrl");
59 }
60 if (decoded.payload().has_claim("roles")) {
61 auto roles = decoded.payload().get_claim_value<std::vector<std::string>>("roles");
62 user_data.moderator = std::find(roles.begin(), roles.end(), "moderator") != roles.end();
63 }
64 return user_data;
65}
66
67} // 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 @@
1// SPDX-FileCopyrightText: Copyright 2018 Citra Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6#include <fmt/format.h>
7#include "network/verify_user.h"
8#include "web_service/web_backend.h"
9
10namespace WebService {
11
12std::string GetPublicKey(const std::string& host);
13
14class VerifyUserJWT final : public Network::VerifyUser::Backend {
15public:
16 VerifyUserJWT(const std::string& host);
17 ~VerifyUserJWT() = default;
18
19 Network::VerifyUser::UserData LoadUserData(const std::string& verify_uid,
20 const std::string& token) override;
21
22private:
23 std::string pub_key;
24};
25
26} // namespace WebService