diff options
| author | 2017-09-19 03:18:26 +0200 | |
|---|---|---|
| committer | 2017-09-18 21:18:26 -0400 | |
| commit | 28c726f20545744a3052a3e8a0a3bf5ff95a5042 (patch) | |
| tree | 496b81a9d2cefbdb3ac2ed12c6525ecd72610c14 /src/web_service/web_backend.cpp | |
| parent | Merge pull request #2906 from Subv/ns_new_framework (diff) | |
| download | yuzu-28c726f20545744a3052a3e8a0a3bf5ff95a5042.tar.gz yuzu-28c726f20545744a3052a3e8a0a3bf5ff95a5042.tar.xz yuzu-28c726f20545744a3052a3e8a0a3bf5ff95a5042.zip | |
WebService: Verify username and token (#2930)
* WebService: Verify username and token; Log errors in PostJson
* Fixup: added docstrings to the functions
* Webservice: Added Icons to the verification, imrpved error detection in cpr, fixup nits
* fixup: fmt warning
Diffstat (limited to 'src/web_service/web_backend.cpp')
| -rw-r--r-- | src/web_service/web_backend.cpp | 101 |
1 files changed, 89 insertions, 12 deletions
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 |