diff options
Diffstat (limited to 'src/web_service')
| -rw-r--r-- | src/web_service/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/web_service/verify_login.cpp | 28 | ||||
| -rw-r--r-- | src/web_service/verify_login.h | 24 | ||||
| -rw-r--r-- | src/web_service/web_backend.cpp | 101 | ||||
| -rw-r--r-- | src/web_service/web_backend.h | 16 |
5 files changed, 159 insertions, 12 deletions
diff --git a/src/web_service/CMakeLists.txt b/src/web_service/CMakeLists.txt index 334d82a8a..c93811892 100644 --- a/src/web_service/CMakeLists.txt +++ b/src/web_service/CMakeLists.txt | |||
| @@ -1,10 +1,12 @@ | |||
| 1 | set(SRCS | 1 | set(SRCS |
| 2 | telemetry_json.cpp | 2 | telemetry_json.cpp |
| 3 | verify_login.cpp | ||
| 3 | web_backend.cpp | 4 | web_backend.cpp |
| 4 | ) | 5 | ) |
| 5 | 6 | ||
| 6 | set(HEADERS | 7 | set(HEADERS |
| 7 | telemetry_json.h | 8 | telemetry_json.h |
| 9 | verify_login.h | ||
| 8 | web_backend.h | 10 | web_backend.h |
| 9 | ) | 11 | ) |
| 10 | 12 | ||
diff --git a/src/web_service/verify_login.cpp b/src/web_service/verify_login.cpp new file mode 100644 index 000000000..1bc3b5afe --- /dev/null +++ b/src/web_service/verify_login.cpp | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <json.hpp> | ||
| 6 | #include "web_service/verify_login.h" | ||
| 7 | #include "web_service/web_backend.h" | ||
| 8 | |||
| 9 | namespace WebService { | ||
| 10 | |||
| 11 | std::future<bool> VerifyLogin(std::string& username, std::string& token, | ||
| 12 | const std::string& endpoint_url, std::function<void()> func) { | ||
| 13 | auto get_func = [func, username](const std::string& reply) -> bool { | ||
| 14 | func(); | ||
| 15 | if (reply.empty()) | ||
| 16 | return false; | ||
| 17 | nlohmann::json json = nlohmann::json::parse(reply); | ||
| 18 | std::string result; | ||
| 19 | try { | ||
| 20 | result = json["username"]; | ||
| 21 | } catch (const nlohmann::detail::out_of_range&) { | ||
| 22 | } | ||
| 23 | return result == username; | ||
| 24 | }; | ||
| 25 | return GetJson<bool>(get_func, endpoint_url, false, username, token); | ||
| 26 | } | ||
| 27 | |||
| 28 | } // namespace WebService | ||
diff --git a/src/web_service/verify_login.h b/src/web_service/verify_login.h new file mode 100644 index 000000000..303f5dbbc --- /dev/null +++ b/src/web_service/verify_login.h | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <functional> | ||
| 8 | #include <future> | ||
| 9 | #include <string> | ||
| 10 | |||
| 11 | namespace WebService { | ||
| 12 | |||
| 13 | /** | ||
| 14 | * Checks if username and token is valid | ||
| 15 | * @param username Citra username to use for authentication. | ||
| 16 | * @param token Citra token to use for authentication. | ||
| 17 | * @param endpoint_url URL of the services.citra-emu.org endpoint. | ||
| 18 | * @param func A function that gets exectued when the verification is finished | ||
| 19 | * @returns Future with bool indicating whether the verification succeeded | ||
| 20 | */ | ||
| 21 | std::future<bool> VerifyLogin(std::string& username, std::string& token, | ||
| 22 | const std::string& endpoint_url, std::function<void()> func); | ||
| 23 | |||
| 24 | } // namespace WebService | ||
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index d28a3f757..b17d82f9c 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp | |||
| @@ -18,6 +18,19 @@ static constexpr char API_VERSION[]{"1"}; | |||
| 18 | 18 | ||
| 19 | static std::unique_ptr<cpr::Session> g_session; | 19 | static std::unique_ptr<cpr::Session> g_session; |
| 20 | 20 | ||
| 21 | void Win32WSAStartup() { | ||
| 22 | #ifdef _WIN32 | ||
| 23 | // On Windows, CPR/libcurl does not properly initialize Winsock. The below code is used to | ||
| 24 | // initialize Winsock globally, which fixes this problem. Without this, only the first CPR | ||
| 25 | // session will properly be created, and subsequent ones will fail. | ||
| 26 | WSADATA wsa_data; | ||
| 27 | const int wsa_result{WSAStartup(MAKEWORD(2, 2), &wsa_data)}; | ||
| 28 | if (wsa_result) { | ||
| 29 | LOG_CRITICAL(WebService, "WSAStartup failed: %d", wsa_result); | ||
| 30 | } | ||
| 31 | #endif | ||
| 32 | } | ||
| 33 | |||
| 21 | void PostJson(const std::string& url, const std::string& data, bool allow_anonymous, | 34 | void PostJson(const std::string& url, const std::string& data, bool allow_anonymous, |
| 22 | const std::string& username, const std::string& token) { | 35 | const std::string& username, const std::string& token) { |
| 23 | if (url.empty()) { | 36 | if (url.empty()) { |
| @@ -31,16 +44,7 @@ void PostJson(const std::string& url, const std::string& data, bool allow_anonym | |||
| 31 | return; | 44 | return; |
| 32 | } | 45 | } |
| 33 | 46 | ||
| 34 | #ifdef _WIN32 | 47 | Win32WSAStartup(); |
| 35 | // On Windows, CPR/libcurl does not properly initialize Winsock. The below code is used to | ||
| 36 | // initialize Winsock globally, which fixes this problem. Without this, only the first CPR | ||
| 37 | // session will properly be created, and subsequent ones will fail. | ||
| 38 | WSADATA wsa_data; | ||
| 39 | const int wsa_result{WSAStartup(MAKEWORD(2, 2), &wsa_data)}; | ||
| 40 | if (wsa_result) { | ||
| 41 | LOG_CRITICAL(WebService, "WSAStartup failed: %d", wsa_result); | ||
| 42 | } | ||
| 43 | #endif | ||
| 44 | 48 | ||
| 45 | // Built request header | 49 | // Built request header |
| 46 | cpr::Header header; | 50 | cpr::Header header; |
| @@ -56,8 +60,81 @@ void PostJson(const std::string& url, const std::string& data, bool allow_anonym | |||
| 56 | } | 60 | } |
| 57 | 61 | ||
| 58 | // Post JSON asynchronously | 62 | // Post JSON asynchronously |
| 59 | static cpr::AsyncResponse future; | 63 | static std::future<void> future; |
| 60 | future = cpr::PostAsync(cpr::Url{url.c_str()}, cpr::Body{data.c_str()}, header); | 64 | future = cpr::PostCallback( |
| 65 | [](cpr::Response r) { | ||
| 66 | if (r.error) { | ||
| 67 | LOG_ERROR(WebService, "POST returned cpr error: %u:%s", | ||
| 68 | static_cast<u32>(r.error.code), r.error.message.c_str()); | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | if (r.status_code >= 400) { | ||
| 72 | LOG_ERROR(WebService, "POST returned error status code: %u", r.status_code); | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | if (r.header["content-type"].find("application/json") == std::string::npos) { | ||
| 76 | LOG_ERROR(WebService, "POST returned wrong content: %s", | ||
| 77 | r.header["content-type"].c_str()); | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | }, | ||
| 81 | cpr::Url{url}, cpr::Body{data}, header); | ||
| 82 | } | ||
| 83 | |||
| 84 | template <typename T> | ||
| 85 | std::future<T> GetJson(std::function<T(const std::string&)> func, const std::string& url, | ||
| 86 | bool allow_anonymous, const std::string& username, | ||
| 87 | const std::string& token) { | ||
| 88 | if (url.empty()) { | ||
| 89 | LOG_ERROR(WebService, "URL is invalid"); | ||
| 90 | return std::async(std::launch::async, [func{std::move(func)}]() { return func(""); }); | ||
| 91 | } | ||
| 92 | |||
| 93 | const bool are_credentials_provided{!token.empty() && !username.empty()}; | ||
| 94 | if (!allow_anonymous && !are_credentials_provided) { | ||
| 95 | LOG_ERROR(WebService, "Credentials must be provided for authenticated requests"); | ||
| 96 | return std::async(std::launch::async, [func{std::move(func)}]() { return func(""); }); | ||
| 97 | } | ||
| 98 | |||
| 99 | Win32WSAStartup(); | ||
| 100 | |||
| 101 | // Built request header | ||
| 102 | cpr::Header header; | ||
| 103 | if (are_credentials_provided) { | ||
| 104 | // Authenticated request if credentials are provided | ||
| 105 | header = {{"Content-Type", "application/json"}, | ||
| 106 | {"x-username", username.c_str()}, | ||
| 107 | {"x-token", token.c_str()}, | ||
| 108 | {"api-version", API_VERSION}}; | ||
| 109 | } else { | ||
| 110 | // Otherwise, anonymous request | ||
| 111 | header = cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}}; | ||
| 112 | } | ||
| 113 | |||
| 114 | // Get JSON asynchronously | ||
| 115 | return cpr::GetCallback( | ||
| 116 | [func{std::move(func)}](cpr::Response r) { | ||
| 117 | if (r.error) { | ||
| 118 | LOG_ERROR(WebService, "GET returned cpr error: %u:%s", | ||
| 119 | static_cast<u32>(r.error.code), r.error.message.c_str()); | ||
| 120 | return func(""); | ||
| 121 | } | ||
| 122 | if (r.status_code >= 400) { | ||
| 123 | LOG_ERROR(WebService, "GET returned error code: %u", r.status_code); | ||
| 124 | return func(""); | ||
| 125 | } | ||
| 126 | if (r.header["content-type"].find("application/json") == std::string::npos) { | ||
| 127 | LOG_ERROR(WebService, "GET returned wrong content: %s", | ||
| 128 | r.header["content-type"].c_str()); | ||
| 129 | return func(""); | ||
| 130 | } | ||
| 131 | return func(r.text); | ||
| 132 | }, | ||
| 133 | cpr::Url{url}, header); | ||
| 61 | } | 134 | } |
| 62 | 135 | ||
| 136 | template std::future<bool> GetJson(std::function<bool(const std::string&)> func, | ||
| 137 | const std::string& url, bool allow_anonymous, | ||
| 138 | const std::string& username, const std::string& token); | ||
| 139 | |||
| 63 | } // namespace WebService | 140 | } // namespace WebService |
diff --git a/src/web_service/web_backend.h b/src/web_service/web_backend.h index d17100398..a63c75d13 100644 --- a/src/web_service/web_backend.h +++ b/src/web_service/web_backend.h | |||
| @@ -4,6 +4,8 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <functional> | ||
| 8 | #include <future> | ||
| 7 | #include <string> | 9 | #include <string> |
| 8 | #include "common/common_types.h" | 10 | #include "common/common_types.h" |
| 9 | 11 | ||
| @@ -20,4 +22,18 @@ namespace WebService { | |||
| 20 | void PostJson(const std::string& url, const std::string& data, bool allow_anonymous, | 22 | void PostJson(const std::string& url, const std::string& data, bool allow_anonymous, |
| 21 | const std::string& username = {}, const std::string& token = {}); | 23 | const std::string& username = {}, const std::string& token = {}); |
| 22 | 24 | ||
| 25 | /** | ||
| 26 | * Gets JSON from services.citra-emu.org. | ||
| 27 | * @param func A function that gets exectued when the json as a string is received | ||
| 28 | * @param url URL of the services.citra-emu.org endpoint to post data to. | ||
| 29 | * @param allow_anonymous If true, allow anonymous unauthenticated requests. | ||
| 30 | * @param username Citra username to use for authentication. | ||
| 31 | * @param token Citra token to use for authentication. | ||
| 32 | * @return future that holds the return value T of the func | ||
| 33 | */ | ||
| 34 | template <typename T> | ||
| 35 | std::future<T> GetJson(std::function<T(const std::string&)> func, const std::string& url, | ||
| 36 | bool allow_anonymous, const std::string& username = {}, | ||
| 37 | const std::string& token = {}); | ||
| 38 | |||
| 23 | } // namespace WebService | 39 | } // namespace WebService |